You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by "Timm, Sean" <ST...@mailgo.com> on 2000/07/20 23:04:01 UTC

[PATCH] Make Xalan 2.0 command-line accept OS paths (like current version of Xalan does)

I just snarfed the necessary changes from the current version of
Process.java (in Xalan 1) with a few minor differences in this
implementation.

- Sean T.

Index: Process.java
===================================================================
RCS file:
/home/cvspublic/xml-xalan/java/src/org/apache/xalan/xslt/Process.java,v
retrieving revision 1.4
diff -u -r1.4 Process.java
--- Process.java	2000/07/18 01:38:20	1.4
+++ Process.java	2000/07/20 20:57:20
@@ -56,6 +56,7 @@
  */
 package org.apache.xalan.xslt;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
@@ -71,10 +72,14 @@
 
 import java.lang.reflect.Constructor;
 
+import java.net.URL;
+import java.net.MalformedURLException;
+
 import java.util.TooManyListenersException;
 import java.util.Vector;
 import java.util.Properties;
 import java.util.Enumeration;
+import java.util.StringTokenizer;
 
 // Needed Xalan classes
 import org.apache.xalan.res.XSLMessages;
@@ -238,10 +243,10 @@
         System.exit(-1);
       }
       boolean formatOutput = false;
-      String inFileName = null;
+      URL inFileName = null;
       String outFileName = null;
       String dumpFileName = null;
-      String xslFileName = null;
+      URL xslFileName = null;
       String compiledStylesheetFileNameOut = null;
       String compiledStylesheetFileNameIn = null;
       String treedumpFileName = null;
@@ -298,7 +303,13 @@
         else if ("-IN".equalsIgnoreCase(argv[i]))
         {
           if ( i+1 < argv.length)
-            inFileName = argv[++i];
+            try{
+              inFileName = getURLFromString(argv[++i], null);
+            }
+            catch (SAXException sxe)
+            {  
+
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ER_NO
T_SUCCESSFUL, null)); 
+            }  
           else
 
System.err.println(XSLMessages.createMessage(XSLTErrorResources.ER_MISSING_A
RG_FOR_OPTION, new Object[] {"-IN"})); //"Missing argument for);
 
@@ -323,7 +334,13 @@
         else if ("-XSL".equalsIgnoreCase(argv[i]))
         {
           if ( i+1 < argv.length)
-            xslFileName = argv[++i];
+            try{
+              xslFileName = getURLFromString(argv[++i], null);
+            }
+            catch (SAXException sxe)
+            {  
+
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ER_NO
T_SUCCESSFUL, null)); 
+            }  
           else
 
System.err.println(XSLMessages.createMessage(XSLTErrorResources.ER_MISSING_A
RG_FOR_OPTION, new Object[] {"-XSL"})); //"Missing argument for);
 
@@ -463,7 +480,7 @@
         }
         else if(null != xslFileName)
         {
-          stylesheet = processor.process(new InputSource(xslFileName));
+          stylesheet = processor.process(new
InputSource(xslFileName.toExternalForm()));
         }
 
         PrintWriter resultWriter;
@@ -476,7 +493,7 @@
         // document?
         if(null == stylesheet)
         {
-          InputSource[] sources = processor.getAssociatedStylesheets(new
InputSource(inFileName),
+          InputSource[] sources = processor.getAssociatedStylesheets(new
InputSource(inFileName.toExternalForm()),
                                                                      media,
null, null);
           if(null != sources)
             stylesheet = processor.processMultiple(sources);
@@ -525,7 +542,7 @@
           
           if(null != inFileName)
           {
-            transformer.transform(new InputSource(inFileName), new
Result(outputStream));
+            transformer.transform(new
InputSource(inFileName.toExternalForm()), new Result(outputStream));
           }
           else
           {
@@ -677,4 +694,293 @@
         diagnosticsWriter.println(""); //"Xalan: done");
     }
   }
+
+  
+  /**
+   * Take a user string and try and parse XML, and also return 
+   * the url.
+   * @exception XSLProcessorException thrown if the active ProblemListener
and XPathContext decide 
+   * the error condition is severe enough to halt processing.
+   */
+  public static URL getURLFromString(String urlString, String base)
+    throws SAXException 
+  {
+    String origURLString = urlString;
+    String origBase = base;
+    
+    // System.out.println("getURLFromString - urlString: "+urlString+",
base: "+base);
+    Object doc;
+    URL url = null;
+    int fileStartType = 0;
+    try
+    {
+      
+      if(null != base)
+      {
+        if(base.toLowerCase().startsWith("file:/"))
+        {
+          fileStartType = 1;
+        }
+        else if(base.toLowerCase().startsWith("file:"))
+        {
+          fileStartType = 2;
+        }
+      }
+      
+      boolean isAbsoluteURL;
+      
+      // From http://www.ics.uci.edu/pub/ietf/uri/rfc1630.txt
+      // A partial form can be distinguished from an absolute form in that
the
+      // latter must have a colon and that colon must occur before any
slash
+      // characters. Systems not requiring partial forms should not use any
+      // unencoded slashes in their naming schemes.  If they do, absolute
URIs
+      // will still work, but confusion may result.
+      int indexOfColon = urlString.indexOf(':');
+      int indexOfSlash = urlString.indexOf('/');
+      if((indexOfColon != -1) && (indexOfSlash != -1) && (indexOfColon <
indexOfSlash))
+      {
+        // The url (or filename, for that matter) is absolute.
+        isAbsoluteURL = true;
+      }
+      else
+      {
+        isAbsoluteURL = false;
+      }
+      
+      if(isAbsoluteURL || (null == base) || (base.length() == 0))
+      {
+        try 
+        {
+          url = new URL(urlString);
+        }
+        catch (MalformedURLException e) {}
+      }
+      // The Java URL handling doesn't seem to handle relative file names.
+      else if(!((urlString.charAt(0) == '.') || (fileStartType > 0)))
+      {
+        try 
+        {
+          URL baseUrl = new URL(base);
+          url = new URL(baseUrl, urlString);
+        }
+        catch (MalformedURLException e) 
+        {
+        }
+      }
+      
+      if(null == url)
+      {
+        // Then we're going to try and make a file URL below, so strip 
+        // off the protocol header.
+        if(urlString.toLowerCase().startsWith("file:/"))
+        {
+          urlString = urlString.substring(6);
+        }
+        else if(urlString.toLowerCase().startsWith("file:"))
+        {
+          urlString = urlString.substring(5);
+        }
+      }
+      
+      if((null == url) && ((null == base) || (fileStartType > 0)))
+      {
+        if(1 == fileStartType)
+        {
+          if(null != base)
+            base = base.substring(6);
+          fileStartType = 1;
+        }
+        else if(2 == fileStartType)
+        {
+          if(null != base)
+            base = base.substring(5);
+          fileStartType = 2;
+        }
+        
+        File f = new File(urlString);
+        
+        if(!f.isAbsolute() && (null != base))
+        {
+          // String dir = f.isDirectory() ? f.getAbsolutePath() :
f.getParent();
+          // System.out.println("prebuiltUrlString (1): "+base);
+          StringTokenizer tokenizer = new StringTokenizer(base, "\\/");
+          String fixedBase = null;
+          while(tokenizer.hasMoreTokens())
+          {
+            String token = tokenizer.nextToken();
+            if (null == fixedBase) 
+            {
+              // Thanks to Rick Maddy for the bug fix for UNIX here.
+              if (base.charAt(0) == '\\' || base.charAt(0) == '/') 
+              {
+                fixedBase = File.separator + token;
+              }
+              else 
+              {
+                fixedBase = token;
+              }
+            }
+            else 
+            {
+              fixedBase+= File.separator + token;
+            }
+          }
+          // System.out.println("rebuiltUrlString (1): "+fixedBase);
+          f = new File(fixedBase);
+          String dir = f.isDirectory() ? f.getAbsolutePath() :
f.getParent();
+          // System.out.println("dir: "+dir);
+          // System.out.println("urlString: "+urlString);
+          // f = new File(dir, urlString);
+          // System.out.println("f (1): "+f.toString());
+          // urlString = f.getAbsolutePath();
+          f = new File(urlString); 
+          boolean isAbsolute =  f.isAbsolute() 
+                                || (urlString.charAt( 0 ) == '\\')
+                                || (urlString.charAt( 0 ) == '/');
+          if(!isAbsolute)
+          {
+            // Getting more and more ugly...
+            if(dir.charAt( dir.length()-1 ) != File.separator.charAt(0) && 
+               urlString.charAt( 0 ) != File.separator.charAt(0))
+            {
+              urlString = dir + File.separator + urlString;
+            }
+            else
+            {
+              urlString = dir + urlString;
+            }
+
+            // System.out.println("prebuiltUrlString (2): "+urlString);
+            tokenizer = new StringTokenizer(urlString, "\\/");
+            String rebuiltUrlString = null;
+            while(tokenizer.hasMoreTokens())
+            {
+              String token = tokenizer.nextToken();
+              if (null == rebuiltUrlString) 
+              {
+                // Thanks to Rick Maddy for the bug fix for UNIX here.
+                if (urlString.charAt(0) == '\\' || urlString.charAt(0) ==
'/') 
+                {
+                  rebuiltUrlString = File.separator + token;
+                }
+                else 
+                {
+                  rebuiltUrlString = token;
+                }
+              }
+              else 
+              {
+                rebuiltUrlString+= File.separator + token;
+              }
+            }
+            // System.out.println("rebuiltUrlString (2):
"+rebuiltUrlString);
+            if(null != rebuiltUrlString)
+              urlString = rebuiltUrlString;
+          }
+          // System.out.println("fileStartType: "+fileStartType);
+          if(1 == fileStartType)
+          {
+            if (urlString.charAt(0) == '/') 
+            {
+              urlString = "file://"+urlString;
+            }
+            else
+            {
+              urlString = "file:/"+urlString;
+            }
+          }
+          else if(2 == fileStartType)
+          {
+            urlString = "file:"+urlString;
+          }
+          try 
+          {
+            // System.out.println("Final before try: "+urlString);
+            url = new URL(urlString);
+          }
+          catch (MalformedURLException e) 
+          {
+            // System.out.println("Error trying to make URL from
"+urlString);
+          }
+        }
+      }
+      if(null == url)
+      {
+        // The sun java VM doesn't do this correctly, but I'll 
+        // try it here as a second-to-last resort.
+        if((null != origBase) && (origBase.length() > 0))
+        {
+          try 
+          {
+            URL baseURL = new URL(origBase);
+            // System.out.println("Trying to make URL from "+origBase+" and
"+origURLString);
+            url = new URL(baseURL, origURLString);
+            // System.out.println("Success! New URL is: "+url.toString());
+          }
+          catch (MalformedURLException e) 
+          {
+            // System.out.println("Error trying to make URL from
"+origBase+" and "+origURLString);
+          }
+        }
+        
+        if(null == url)
+        {
+          try 
+          {
+            String lastPart;
+            if(null != origBase)
+            {
+              File baseFile = new File(origBase);
+              if(baseFile.isDirectory())
+              {
+                lastPart = new File(baseFile, urlString).getAbsolutePath
();
+              }
+              else
+              {
+                String parentDir = baseFile.getParent();
+                lastPart = new File(parentDir, urlString).getAbsolutePath
();
+              }
+            }
+            else
+            {
+              lastPart = new File (urlString).getAbsolutePath ();
+            }
+            // Hack
+            // if((lastPart.charAt(0) == '/') && (lastPart.charAt(2) ==
':'))
+            //   lastPart = lastPart.substring(1, lastPart.length() - 1);
+            
+            String fullpath;
+            if (lastPart.charAt(0) == '\\' || lastPart.charAt(0) == '/') 
+            {
+              fullpath = "file://" + lastPart;
+            }
+            else
+            {
+              fullpath = "file:" + lastPart;
+            }
+            url = new URL(fullpath);
+          }
+          catch (MalformedURLException e2)
+          {
+            throw new SAXException("Cannot create url for: " + urlString,
e2 ); 
+
//XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CANNOT_CREATE_URL,
new Object[]{urlString}),e2); //"Cannot create url for: " + urlString, e2 );
+          }
+        }
+      }
+    }
+    catch(SecurityException se)
+    {
+      try
+      {
+        url = new
URL("http://xml.apache.org/xslt/"+java.lang.Math.random()); // dummy
+      }
+      catch (MalformedURLException e2)
+      {
+        // I give up
+      }
+    }
+    // System.out.println("url: "+url.toString());
+    return url;
+     }
+
 }