cziegeler    01/05/28 02:22:51

  Modified:    src/org/apache/cocoon/transformation TraxTransformer.java
  Log:
  Fixed the URIResolver, the xsl:include is now evaluated relative to the current 
stylesheet
  
  Revision  Changes    Path
  1.8       +54 -2     
xml-cocoon2/src/org/apache/cocoon/transformation/TraxTransformer.java
  
  Index: TraxTransformer.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/org/apache/cocoon/transformation/TraxTransformer.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TraxTransformer.java      2001/05/25 16:13:42     1.7
  +++ TraxTransformer.java      2001/05/28 09:22:51     1.8
  @@ -24,6 +24,7 @@
   import javax.xml.transform.sax.SAXSource;
   import javax.xml.transform.sax.SAXTransformerFactory;
   import javax.xml.transform.sax.TransformerHandler;
  +import javax.xml.transform.URIResolver;
   import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.component.Component;
   import org.apache.avalon.framework.component.ComponentManager;
  @@ -99,10 +100,10 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Davanum Srinivas</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Giacomo Pati</a>
  - * @version CVS $Id: TraxTransformer.java,v 1.7 2001/05/25 16:13:42 giacomo Exp $
  + * @version CVS $Id: TraxTransformer.java,v 1.8 2001/05/28 09:22:51 cziegeler Exp $
    */
   public class TraxTransformer extends ContentHandlerWrapper
  -implements Transformer, Composable, Recyclable, Configurable, Cacheable, Disposable 
{
  +implements Transformer, Composable, Recyclable, Configurable, Cacheable, 
Disposable, URIResolver {
   
       private static final String FILE = "file:";
   
  @@ -215,12 +216,63 @@
       }
   
       /**
  +     * Called by the processor when it encounters
  +     * an xsl:include, xsl:import, or document() function.
  +     *
  +     * @param href An href attribute, which may be relative or absolute.
  +     * @param base The base URI in effect when the href attribute
  +     * was encountered.
  +     *
  +     * @return A Source object, or null if the href cannot be resolved,
  +     * and the processor should try to resolve the URI itself.
  +     *
  +     * @throws TransformerException if an error occurs when trying to
  +     * resolve the URI.
  +     */
  +    public javax.xml.transform.Source resolve(String href, String base)
  +    throws javax.xml.transform.TransformerException {
  +        try {
  +            Source xslSource;
  +            if (href.indexOf(":") > 1) {
  +                xslSource = this.resolver.resolve(href);
  +            } else {
  +                // is the base a file or a real url
  +                if (base.startsWith("file:") == false) {
  +                    java.net.URL url = new java.net.URL(base);
  +                    String parentPath = url.getPath();
  +                    int lastPathElementPos = parentPath.indexOf('/');
  +                    if (lastPathElementPos == -1) {
  +                        xslSource = this.resolver.resolve(url.getProtocol()+ "://"
  +                                                       + url.getHost()+ ":"
  +                                                       + url.getPort()+ "/" + href);
  +                    } else {
  +                        xslSource = this.resolver.resolve(url.getProtocol()+ "://"
  +                                                       + url.getHost()+ ":"
  +                                                       + url.getPort()+ "/"
  +                                                       + parentPath.substring(0, 
lastPathElementPos)
  +                                                       + "/" + href);
  +                    }
  +                } else {
  +                    File parent = new File(base.substring(5));
  +                    xslSource = this.resolver.resolve("file:/"
  +                               + new File(parent.getParentFile(), 
href).getAbsolutePath());
  +                }
  +            }
  +            return new 
javax.xml.transform.stream.StreamSource(xslSource.getReader());
  +       } catch (IOException e) {
  +            throw new javax.xml.transform.TransformerException(e);
  +       } catch (SAXException e) {
  +           throw new javax.xml.transform.TransformerException(e);
  +        }
  +    }
  +    /**
        * Helper for TransformerFactory.
        */
       private synchronized SAXTransformerFactory getTransformerFactory() {
           if(tfactory == null)  {
               tfactory = (SAXTransformerFactory) TransformerFactory.newInstance();
               tfactory.setErrorListener(new TraxErrorHandler(getLogger()));
  +            tfactory.setURIResolver(this);
           }
           return tfactory;
       }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to