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