You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mm...@locus.apache.org on 2000/06/27 16:24:17 UTC

cvs commit: xml-xalan/src/org/apache/xalan/xslt Process.java

mmidy       00/06/27 07:24:16

  Modified:    src/org/apache/xalan/xslt Process.java
  Log:
  Attempt to fix problem with xerces1.02 and URLs
  
  Revision  Changes    Path
  1.20      +302 -8    xml-xalan/src/org/apache/xalan/xslt/Process.java
  
  Index: Process.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/src/org/apache/xalan/xslt/Process.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- Process.java	2000/03/29 03:51:57	1.19
  +++ Process.java	2000/06/27 14:24:15	1.20
  @@ -202,7 +202,8 @@
         boolean formatOutput = false;
   
         // QName mode = null;
  -      String inFileName = null;
  +      //String 
  +      URL  inFileName = null;
         String outFileName = null;
         String dumpFileName = null;
         String xslFileName = null;
  @@ -295,10 +296,16 @@
           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_NOT_SUCCESSFUL, null)); 
  +            }  
             else
               System.err.println(XSLMessages.createMessage(XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, new Object[] {"-IN"})); //"Missing argument for);
  -
  +          System.out.println("in " + inFileName.getFile()); 
           }
           else if ("-MEDIA".equalsIgnoreCase(argv[i]))
           {
  @@ -503,21 +510,21 @@
           {
             if(null != stylesheet)
             {
  -            Node sourceTree = processor.getSourceTreeFromInput(new XSLTInputSource(inFileName));
  +            Node sourceTree = processor.getSourceTreeFromInput(new XSLTInputSource((inFileName.toExternalForm())));
               stylesheet.process(processor, sourceTree, new XSLTResultTarget(outputStream));
             }
             else if(null != media)
             {
  -            StylesheetSpec spec = processor.getAssociatedStylesheet(new XSLTInputSource(inFileName),
  +            StylesheetSpec spec = processor.getAssociatedStylesheet(new XSLTInputSource((inFileName.toExternalForm())),
                                                                       media, null);
               if(null != spec)
               {
                 if(spec.getSystemId() != null)
                 {
  -                URL url = processor.getXMLProcessorLiaison().getURLFromString(spec.getSystemId(), inFileName);
  +                URL url = processor.getXMLProcessorLiaison().getURLFromString(spec.getSystemId(), (inFileName.toExternalForm()));
                   spec.setSystemId(url.toExternalForm());
                 }
  -              processor.process(new XSLTInputSource(inFileName),
  +              processor.process(new XSLTInputSource(inFileName.toExternalForm()),
                                   spec,
                                   new XSLTResultTarget(outputStream));
               }
  @@ -528,7 +535,7 @@
             }
             else
             {
  -            processor.process(new XSLTInputSource(inFileName),
  +            processor.process(new XSLTInputSource((inFileName.toExternalForm())),
                                 (XSLTInputSource)null,
                                 new XSLTResultTarget(outputStream));
             }
  @@ -708,5 +715,292 @@
         else
           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;
     }
   }