You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Marc <Ma...@free.fr> on 2006/10/15 19:36:09 UTC

EntityResolver in a document() function to solve OASIS Catalog

Hello,

I try to use the OASIS Catalog to solve the PUBLIC DTD.
All is OK but I have a problem with the XML file charged by the document().
I use the CatalogResolver with the setURIResolver for the Factory, and the setEntityResolver for the XMLReader, but I don't find how the set the Entityresolver for the XMLReader used by the transformer.
Here is my code :
            SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
            
            // on met en place la gestion du catalog
            CatalogResolver ct = new CatalogResolver();
            tFactory.setURIResolver(ct);
            javax.xml.transform.sax.TemplatesHandler templatesHandler =
                    tFactory.newTemplatesHandler();
            
            // Create an XMLReader and set its ContentHandler.
            org.xml.sax.XMLReader reader =
                    org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
            reader.setContentHandler(templatesHandler);
            reader.setEntityResolver(new CatalogResolver());
            // Parse the stylesheet.
            reader.parse(fichierXSLT);
            // Get the Templates object (generated during the parsing of the stylesheet)
            // from the TemplatesHandler.
            javax.xml.transform.Templates templates =
                    templatesHandler.getTemplates();
            // Create a Transformer ContentHandler to handle parsing of
            // the XML Source.
            javax.xml.transform.sax.TransformerHandler transformerHandler
                    = tFactory.newTransformerHandler(templates);
            // Reset the XMLReader's ContentHandler to the TransformerHandler.
            reader.setContentHandler(transformerHandler);
            // Set the ContentHandler to also function as a LexicalHandler, which
            // can process "lexical" events (such as comments and CDATA).
            reader.setProperty("http://xml.org/sax/properties/lexical-handler",
                    transformerHandler);
            
            // Set up a Serializer to serialize the Result to a file.
            org.apache.xml.serializer.Serializer serializer =
                    org.apache.xml.serializer.SerializerFactory.getSerializer
                    (org.apache.xml.serializer.OutputPropertiesFactory.getDefaultMethodProperties
                    ("xml"));
            serializer.setOutputStream(new java.io.FileOutputStream(fichierSortie));
            // The Serializer functions as a SAX ContentHandler.
            javax.xml.transform.Result result =
                    new javax.xml.transform.sax.SAXResult(serializer.asContentHandler());
            transformerHandler.setResult(result);
            
            // Parse the XML input document.
            reader.parse(fichierEntree);
I try something more simple with a transformer without contentHandler but it's the same:
            SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
            
            // on met en place la gestion du catalog
            CatalogResolver ct = new CatalogResolver();
            tFactory.setURIResolver(ct);
            
            // Use the TransformerFactory to instantiate a Transformer that will work with
            // the stylesheet you specify. This method call also processes the stylesheet
            // into a compiled Templates object.
            InputSource inputXSLT = new InputSource(new FileReader(new File(VerifFichierServeurXSL(fichierXSLT))));
            Source sourceXSLT = new StreamSource(inputXSLT.getCharacterStream(), inputXSLT.getSystemId());
            Transformer transformer = tFactory.newTransformer(sourceXSLT);
            //   Transformer transformer = tFactory.newTransformer(new StreamSource(fichierXSLT));
            XMLReader XMLr;
            //get a SAXParserFactory instance
            SAXParserFactory SAXpf=SAXParserFactory.newInstance();
            
            //enabling the namespaces processing
            if(SAXpf.isNamespaceAware()==false)
                SAXpf.setNamespaceAware(true);
            
            //get a SAXParser object
            SAXParser SAXparser=SAXpf.newSAXParser();
            
            //get the XMLReader
            XMLr=SAXparser.getXMLReader();
            XMLr.setEntityResolver(new CatalogResolver());
            
            //creating the SAXSource
            SAXSource sXML=new SAXSource(XMLr, new InputSource
                    (fichierEntree));
            sXML.setXMLReader(XMLr);
            transformer.transform(sXML, fichierSortie);

If someone as an idea?

Thank's

Marc


Re: EntityResolver in a document() function to solve OASIS Catalog

Posted by Marc <Ma...@free.fr>.
I send the answer to my question, the probleme is that the CatalogResolver class don't set the EntityResolver of the XMLReader of the returned source.
Then I create my own URIResolver to solve this problem :
class MyURIResolver implements URIResolver
{
    CatalogResolver catalogResolver;
   MyURIResolver()
   {
       catalogResolver = new CatalogResolver();
   }
   
   public Source resolve(String href,String base) throws javax.xml.transform.TransformerException{
       Source source = catalogResolver.resolve(href,base);
       if (source == null) return(null);
       try {
            XMLReader XMLr;
            //get a SAXParserFactory instance
            SAXParserFactory SAXpf=SAXParserFactory.newInstance();
            
            //enabling the namespaces processing
            if(SAXpf.isNamespaceAware()==false)
                SAXpf.setNamespaceAware(true);
            
            //get a SAXParser object
            SAXParser SAXparser=SAXpf.newSAXParser();
            
            //get the XMLReader
            XMLr=SAXparser.getXMLReader();
            XMLr.setEntityResolver(catalogResolver);
            
            //creating the SAXSource
            SAXSource sXML=new SAXSource(XMLr,new InputSource(source.getSystemId()));
            sXML.setXMLReader(XMLr);
       return(sXML);
       }
       catch (javax.xml.parsers.ParserConfigurationException e) {
           return(null);
       }
       catch (org.xml.sax.SAXException e) {
           return(null);
       }
}
}

-----------------------------------------
Le dimanche 15 octobre 2006 à 19:36:09, vous écriviez :

M> Hello,

M> I try to use the OASIS Catalog to solve the PUBLIC DTD.
M> All is OK but I have a problem with the XML file charged by the document().
M> I use the CatalogResolver with the setURIResolver for the Factory, and the setEntityResolver for the XMLReader, but I don't find how the set the Entityresolver for the XMLReader used by the
M> transformer.
M> Here is my code :
M>             SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
            
M>             // on met en place la gestion du catalog
M>             CatalogResolver ct = new CatalogResolver();
M>             tFactory.setURIResolver(ct);
M>             javax.xml.transform.sax.TemplatesHandler templatesHandler =
M>                     tFactory.newTemplatesHandler();
            
M>             // Create an XMLReader and set its ContentHandler.
M>             org.xml.sax.XMLReader reader =
M>                     org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
M>             reader.setContentHandler(templatesHandler);
M>             reader.setEntityResolver(new CatalogResolver());
M>             // Parse the stylesheet.
M>             reader.parse(fichierXSLT);
M>             // Get the Templates object (generated during the parsing of the stylesheet)
M>             // from the TemplatesHandler.
M>             javax.xml.transform.Templates templates =
M>                     templatesHandler.getTemplates();
M>             // Create a Transformer ContentHandler to handle parsing of
M>             // the XML Source.
M>             javax.xml.transform.sax.TransformerHandler transformerHandler
M>                     = tFactory.newTransformerHandler(templates);
M>             // Reset the XMLReader's ContentHandler to the TransformerHandler.
M>             reader.setContentHandler(transformerHandler);
M>             // Set the ContentHandler to also function as a LexicalHandler, which
M>             // can process "lexical" events (such as comments and CDATA).
M>             reader.setProperty("http://xml.org/sax/properties/lexical-handler",
M>                     transformerHandler);
            
M>             // Set up a Serializer to serialize the Result to a file.
M>             org.apache.xml.serializer.Serializer serializer =
M>                     org.apache.xml.serializer.SerializerFactory.getSerializer
M>                     (org.apache.xml.serializer.OutputPropertiesFactory.getDefaultMethodProperties
M>                     ("xml"));
M>             serializer.setOutputStream(new java.io.FileOutputStream(fichierSortie));
M>             // The Serializer functions as a SAX ContentHandler.
M>             javax.xml.transform.Result result =
M>                     new javax.xml.transform.sax.SAXResult(serializer.asContentHandler());
M>             transformerHandler.setResult(result);
            
M>             // Parse the XML input document.
M>             reader.parse(fichierEntree);
M> I try something more simple with a transformer without contentHandler but it's the same:
M>             SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
            
M>             // on met en place la gestion du catalog
M>             CatalogResolver ct = new CatalogResolver();
M>             tFactory.setURIResolver(ct);
            
M>             // Use the TransformerFactory to instantiate a Transformer that will work with
M>             // the stylesheet you specify. This method call also processes the stylesheet
M>             // into a compiled Templates object.
M>             InputSource inputXSLT = new InputSource(new FileReader(new File(VerifFichierServeurXSL(fichierXSLT))));
M>             Source sourceXSLT = new StreamSource(inputXSLT.getCharacterStream(), inputXSLT.getSystemId());
M>             Transformer transformer = tFactory.newTransformer(sourceXSLT);
M>             //   Transformer transformer = tFactory.newTransformer(new StreamSource(fichierXSLT));
M>             XMLReader XMLr;
M>             //get a SAXParserFactory instance
M>             SAXParserFactory SAXpf=SAXParserFactory.newInstance();
            
M>             //enabling the namespaces processing
M>             if(SAXpf.isNamespaceAware()==false)
M>                 SAXpf.setNamespaceAware(true);
            
M>             //get a SAXParser object
M>             SAXParser SAXparser=SAXpf.newSAXParser();
            
M>             //get the XMLReader
M>             XMLr=SAXparser.getXMLReader();
M>             XMLr.setEntityResolver(new CatalogResolver());
            
M>             //creating the SAXSource
M>             SAXSource sXML=new SAXSource(XMLr, new InputSource
M>                     (fichierEntree));
M>             sXML.setXMLReader(XMLr);
M>             transformer.transform(sXML, fichierSortie);

M> If someone as an idea?

M> Thank's

M> Marc