xalan-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From minc...@apache.org
Subject cvs commit: xml-xalan/java/src/org/apache/xalan/processor ProcessorInclude.java StylesheetHandler.java
Date Thu, 29 Sep 2005 16:12:55 GMT
minchau     2005/09/29 09:12:55

  Modified:    java/src/org/apache/xalan/processor ProcessorInclude.java
                        StylesheetHandler.java
  Log:
  Applying fix for XALANJ-2205, which gets Xalan out of the business of parsing base URIs
of stylesheets and include/import href values provided that:
  
  1) The user has provided a URIResolver
  2) The Source returned by a URIResolver has its system ID set.
  
  If the system ID is not set, or there is no URI resolver the behavior is as before, namely
that Xalan-J will call its SystemIDResolver class to figure out how to get a base URI for
the included stylesheet. This does fine for most users, but has issues if the URIs are not
valid. One way to be invalid is to have non-ASCII characters in the URI. For example, suppose
one is running on an operating system that allows non-ASCII characters in its directories
or file names (e.g. Chinese characters). To use URIs such as:
    file:///dir1/dirXX2/part.xsl
  where XX are some Chinese characters, then the user must provide 1) and 2) above.
  
  Revision  Changes    Path
  1.31      +76 -12    xml-xalan/java/src/org/apache/xalan/processor/ProcessorInclude.java
  
  Index: ProcessorInclude.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/processor/ProcessorInclude.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- ProcessorInclude.java	7 Jun 2005 15:35:52 -0000	1.30
  +++ ProcessorInclude.java	29 Sep 2005 16:12:55 -0000	1.31
  @@ -128,8 +128,11 @@
   
       try
       {
  -      String hrefUrl = SystemIDResolver.getAbsoluteURI(getHref(),
  -                           handler.getBaseIdentifier());    
  +
  +      // Get the Source from the user's URIResolver (if any).
  +      Source sourceFromURIResolver = getSourceFromUriResolver(handler);
  +      // Get the system ID of the included/imported stylesheet module
  +      String hrefUrl = getBaseURIOfIncludedStylesheet(handler, sourceFromURIResolver);
   
         if (handler.importStackContains(hrefUrl))
         {
  @@ -138,7 +141,10 @@
             getStylesheetInclErr(), new Object[]{ hrefUrl }));  //"(StylesheetHandler) "+hrefUrl+"
is directly or indirectly importing itself!");
         }
   
  +      // Push the system ID and corresponding Source
  +      // on some stacks for later retrieval during parse() time.
         handler.pushImportURL(hrefUrl);
  +      handler.pushImportSource(sourceFromURIResolver);
   
         int savedStylesheetType = handler.getStylesheetType();
   
  @@ -153,6 +159,7 @@
         {
           handler.setStylesheetType(savedStylesheetType);
           handler.popImportURL();
  +        handler.popImportSource();
           handler.popNamespaceSupport();
         }
       }
  @@ -190,20 +197,23 @@
   
         if (null != uriresolver)
         {
  -        source = uriresolver.resolve(getHref(),
  -                                     handler.getBaseIdentifier());
  +        // There is a user provided URI resolver.
  +        // At the startElement() call we would
  +        // have tried to obtain a Source from it
  +        // which we now retrieve
  +        source = handler.peekSourceFromURIResolver();
   
           if (null != source && source instanceof DOMSource)
           {
             Node node = ((DOMSource)source).getNode();
             
  -          String systemId = source.getSystemId();
  -          if (systemId == null)
  -          {
  -            systemId = SystemIDResolver.getAbsoluteURI(getHref(),
  -                         handler.getBaseIdentifier());
  -            
  -          }
  +          // There is a user provided URI resolver.
  +          // At the startElement() call we would
  +          // have already pushed the system ID, obtained
  +          // from either the source.getSystemId(), if non-null
  +          // or from SystemIDResolver.getAbsoluteURI() as a backup
  +          // which we now retrieve.
  +          String systemId = handler.peekImportURL();
             
             TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(),
systemId);
   
  @@ -311,4 +321,58 @@
     {
         return source;
     }
  +  
  +  /**
  +   * Get the Source object for the included or imported stylesheet module
  +   * obtained from the user's URIResolver, if there is no user provided 
  +   * URIResolver null is returned.
  +   */
  +  private Source getSourceFromUriResolver(StylesheetHandler handler)
  +            throws TransformerException {
  +        Source s = null;
  +            TransformerFactoryImpl processor = handler.getStylesheetProcessor();
  +            URIResolver uriresolver = processor.getURIResolver();
  +            if (uriresolver != null) {
  +                String href = getHref();
  +                String base = handler.getBaseIdentifier();
  +                s = uriresolver.resolve(href,base);
  +            }
  +
  +        return s;
  +    }
  +
  +    /**
  +     * Get the base URI of the included or imported stylesheet,
  +     * if the user provided a URIResolver, then get the Source
  +     * object for the stylsheet from it, and get the systemId 
  +     * from that Source object, otherwise try to recover by
  +     * using the SysteIDResolver to figure out the base URI.
  +     * @param handler The handler that processes the stylesheet as SAX events,
  +     * and maintains state
  +     * @param s The Source object from a URIResolver, for the included stylesheet module,
  +     * so this will be null if there is no URIResolver set.
  +     */
  +    private String getBaseURIOfIncludedStylesheet(StylesheetHandler handler, Source s)
  +            throws TransformerException {
  +        
  +
  +        
  +        String baseURI;
  +        String idFromUriResolverSource;
  +        if (s != null && (idFromUriResolverSource = s.getSystemId()) != null) {
  +            // We have a Source obtained from a users's URIResolver,
  +            // and the system ID is set on it, so return that as the base URI
  +            baseURI = idFromUriResolverSource;
  +        } else {
  +            // The user did not provide a URIResolver, or it did not 
  +            // return a Source for the included stylesheet module, or
  +            // the Source has no system ID set, so we fall back to using
  +            // the system ID Resolver to take the href and base
  +            // to generate the baseURI of the included stylesheet.
  +            baseURI = SystemIDResolver.getAbsoluteURI(getHref(), handler
  +                    .getBaseIdentifier());
  +        }
  +
  +        return baseURI;
  +    }
   }
  
  
  
  1.71      +38 -1     xml-xalan/java/src/org/apache/xalan/processor/StylesheetHandler.java
  
  Index: StylesheetHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/processor/StylesheetHandler.java,v
  retrieving revision 1.70
  retrieving revision 1.71
  diff -u -r1.70 -r1.71
  --- StylesheetHandler.java	22 Jun 2005 19:07:10 -0000	1.70
  +++ StylesheetHandler.java	29 Sep 2005 16:12:55 -0000	1.71
  @@ -21,6 +21,7 @@
   import java.util.Stack;
   
   import javax.xml.transform.ErrorListener;
  +import javax.xml.transform.Source;
   import javax.xml.transform.SourceLocator;
   import javax.xml.transform.Templates;
   import javax.xml.transform.TransformerConfigurationException;
  @@ -1461,6 +1462,13 @@
      * used to diagnose circular imports.
      */
     private Stack m_importStack = new Stack();
  +  
  +  /**
  +   * A stack of Source objects obtained from a URIResolver,
  +   * for each element in this stack there is a 1-1 correspondence
  +   * with an element in the m_importStack.
  +   */
  +  private Stack m_importSourceStack = new Stack();
   
     /**
      * Push an import href onto the stylesheet stack.
  @@ -1472,6 +1480,16 @@
     {
       m_importStack.push(hrefUrl);
     }
  +  
  +  /**
  +   * Push the Source of an import href onto the stylesheet stack,
  +   * obtained from a URIResolver, null if there is no URIResolver,
  +   * or if that resolver returned null.
  +   */
  +  void pushImportSource(Source sourceFromURIResolver)
  +  {
  +    m_importSourceStack.push(sourceFromURIResolver);
  +  }
   
     /**
      * See if the imported stylesheet stack already contains
  @@ -1495,6 +1513,25 @@
     {
       return (String) m_importStack.pop();
     }
  +  
  +  String peekImportURL()
  +  {
  +    return (String) m_importStack.peek();
  +  }
  +  
  +  Source peekSourceFromURIResolver()
  +  {
  +    return (Source) m_importSourceStack.peek();
  +  }
  +  
  +  /**
  +   * Pop a Source from a user provided URIResolver, corresponding
  +   * to the URL popped from the m_importStack.
  +   */
  +  Source popImportSource()
  +  {
  +    return (Source) m_importSourceStack.pop();
  +  }
   
     /**
      * If this is set to true, we've already warned about using the
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org


Mime
View raw message