/** * Test newTransformerHandler with a Template Handler. * * @throws Exception If any errors occur. */ public void testcase08() throws Exception { String outputFile = USER_DIR + "saxtf008.out"; String goldFile = GOLDEN_DIR + "saxtf008GF.out"; try (FileOutputStream fos = new FileOutputStream(outputFile)) { XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); reader.setContentHandler(thandler); reader.parse(XSLT_FILE); TransformerHandler tfhandler = saxTFactory.newTransformerHandler(thandler.getTemplates()); Result result = new StreamResult(fos); tfhandler.setResult(result); reader.setContentHandler(tfhandler); reader.parse(XML_FILE); } assertTrue(compareWithGold(goldFile, outputFile)); }
/** * Test newTransformerHandler with a Template Handler along with a relative * URI in the style-sheet file. * * @throws Exception If any errors occur. */ @Test public void testcase09() throws Exception { String outputFile = USER_DIR + "saxtf009.out"; String goldFile = GOLDEN_DIR + "saxtf009GF.out"; try (FileOutputStream fos = new FileOutputStream(outputFile)) { XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); thandler.setSystemId("file:///" + XML_DIR); reader.setContentHandler(thandler); reader.parse(XSLT_INCL_FILE); TransformerHandler tfhandler= saxTFactory.newTransformerHandler(thandler.getTemplates()); Result result = new StreamResult(fos); tfhandler.setResult(result); reader.setContentHandler(tfhandler); reader.parse(XML_FILE); } assertTrue(compareWithGold(goldFile, outputFile)); }
/** * Unit test for TemplatesHandler setter/getter. * * @throws Exception If any errors occur. */ @Test public void testcase13() throws Exception { String outputFile = USER_DIR + "saxtf013.out"; String goldFile = GOLDEN_DIR + "saxtf013GF.out"; try(FileInputStream fis = new FileInputStream(XML_FILE)) { // The transformer will use a SAX parser as it's reader. XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); // I have put this as it was complaining about systemid thandler.setSystemId("file:///" + USER_DIR); reader.setContentHandler(thandler); reader.parse(XSLT_FILE); XMLFilter filter = saxTFactory.newXMLFilter(thandler.getTemplates()); filter.setParent(reader); filter.setContentHandler(new MyContentHandler(outputFile)); filter.parse(new InputSource(fis)); } assertTrue(compareWithGold(goldFile, outputFile)); }
@Override protected TransformerHandler getTransformerHandler(String xslFileName) throws SAXException, ParserConfigurationException, TransformerConfigurationException, IOException { SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance(); factory.setURIResolver(uriResolver); TemplatesHandler templatesHandler = factory.newTemplatesHandler(); SAXParserFactory pFactory = SAXParserFactory.newInstance(); pFactory.setNamespaceAware(true); XMLReader xmlreader = pFactory.newSAXParser().getXMLReader(); // create the stylesheet input source InputSource xslSrc = new InputSource(xslFileName); xslSrc.setSystemId(filenameToURL(xslFileName)); // hook up the templates handler as the xsl content handler xmlreader.setContentHandler(templatesHandler); // call parse on the xsl input source xmlreader.parse(xslSrc); // extract the Templates object created from the xsl input source return factory.newTransformerHandler(templatesHandler.getTemplates()); }
/** * Set the xslt resource. * * @param xsltresource * an Input Source to the XSLT * @throws Exception */ public void setXslt(InputSource xsltresource) throws Exception { if ( saxParserFactory == null) { saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); } TemplatesHandler th = factory.newTemplatesHandler(); String systemId = xsltresource.getSystemId(); th.setSystemId(systemId); SAXParser parser = saxParserFactory.newSAXParser(); XMLReader xr = parser.getXMLReader(); xr.setContentHandler(th); xr.parse(xsltresource); templates = th.getTemplates(); }
/** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Get a TemplatesHandler object that can process SAX ContentHandler * events into a Templates object. * * @return A TemplatesHandler object that can handle SAX events * @throws TransformerConfigurationException */ @Override public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException { final TemplatesHandlerImpl handler = new TemplatesHandlerImpl(_indentNumber, this); if (_uriResolver != null) { handler.setURIResolver(_uriResolver); } return handler; }
/** * Get a TemplatesHandler object that can process SAX ContentHandler * events into a Templates object. Uses the * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory. */ public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException { if (_xsltcFactory == null) { createXSLTCTransformerFactory(); } if (_errorlistener != null) { _xsltcFactory.setErrorListener(_errorlistener); } if (_uriresolver != null) { _xsltcFactory.setURIResolver(_uriresolver); } return _xsltcFactory.newTemplatesHandler(); }
/** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Get a TemplatesHandler object that can process SAX ContentHandler * events into a Templates object. * * @return A TemplatesHandler object that can handle SAX events * @throws TransformerConfigurationException */ @Override public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException { // create CatalogFeatures that is accessible by the Handler // through the factory instance buildCatalogFeatures(); final TemplatesHandlerImpl handler = new TemplatesHandlerImpl(_indentNumber, this); if (_uriResolver != null) { handler.setURIResolver(_uriResolver); } return handler; }
/** * Gets a <code>TemplatesHandler</code> object that can process SAX * ContentHandler events into a <code>Templates</code> object. Implementation * of the {@link SAXTransformerFactory} * * @see SAXTransformerFactory * @return {@link TemplatesHandler} ready to parse a stylesheet. * @throws TransformerConfigurationException */ @Override public TemplatesHandler newTemplatesHandler () throws TransformerConfigurationException { synchronized (m_aReentryGuard) { if (CSTX.DEBUG) log.debug ("create a TemplatesHandler-instance"); final TemplatesHandlerImpl thandler = new TemplatesHandlerImpl (this); return thandler; } }
/** * Show the Transformer using TemplatesHandler */ public static boolean exampleSAXTransformerFactory (final String sourceID, final String stxID) throws TransformerException, TransformerConfigurationException, SAXException, IOException { final TransformerFactory tfactory = TransformerFactory.newInstance (); // Does this factory support SAX features? if (tfactory.getFeature (SAXSource.FEATURE)) { // If so, we can safely cast. final SAXTransformerFactory stfactory = ((SAXTransformerFactory) tfactory); final TemplatesHandler handler = stfactory.newTemplatesHandler (); handler.setSystemId (_getSystemId (stxID)); final XMLReader reader = XMLReaderFactory.createXMLReader (); reader.setContentHandler (handler); reader.parse (_getInputSource (stxID)); final Templates templates = handler.getTemplates (); // transform final Transformer transformer = templates.newTransformer (); transformer.transform (_getSource (sourceID), new StreamResult (System.out)); } return true; }
public void parse(InputSource input) throws IOException, SAXException { if (logCat.isDebugEnabled()) logCat.debug("starting parse(" + input.getSystemId() + ")"); Reader cs = input.getCharacterStream(); Lexer lex = new Lexer(new BufferedReader(cs), input.getSystemId()); try { Statement stat = StatementFactory.getInstance().getStatement(lex); ContentHandler handler = getContentHandler(); if (handler instanceof TemplatesHandler) { if (logCat.isDebugEnabled()) logCat.debug("SystemId is " + ((TemplatesHandler)handler).getSystemId()); } handler.setDocumentLocator(new MyLocator(lex, input.getPublicId(), input.getSystemId())); handler.startDocument(); stat.outputXML(handler, new NamespaceTracker()); handler.endDocument(); } catch (SyntaxException se) { throw new SAXException(se); } if (logCat.isDebugEnabled()) logCat.debug("ending parse(" + input.getSystemId() + ")"); }
public void parse(InputSource input) throws IOException, SAXException { if (logCat.isDebugEnabled()) logCat.debug("starting parse(" + input.getSystemId() + ")"); Reader cs = input.getCharacterStream(); if (cs == null) cs = new InputStreamReader(input.getByteStream()); Lexer lex = new Lexer(new BufferedReader(cs), input.getSystemId()); try { ContentHandler handler = getContentHandler(); if (handler instanceof TemplatesHandler) { if (logCat.isDebugEnabled()) logCat.debug("SystemId is " + ((TemplatesHandler)handler).getSystemId()); } handler.setDocumentLocator(new MyLocator(lex, input.getPublicId(), input.getSystemId())); handler.startDocument(); startStatement(lex); handler.endDocument(); } catch (SyntaxException se) { throw new SAXException(se); } if (logCat.isDebugEnabled()) logCat.debug("ending parse(" + input.getSystemId() + ")"); }
/** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Get a TemplatesHandler object that can process SAX ContentHandler * events into a Templates object. * * @return A TemplatesHandler object that can handle SAX events * @throws TransformerConfigurationException */ public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException { final TemplatesHandlerImpl handler = new TemplatesHandlerImpl(_indentNumber, this); if (_uriResolver != null) { handler.setURIResolver(_uriResolver); } return handler; }
public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException { return new SAXTemplatesHandler(this); }
@Override public ProcessorOutput createOutput(String name) { ProcessorOutput output = new CacheableTransformerOutputImpl(TraxTransformer.this, name) { public void readImpl(org.orbeon.oxf.pipeline.api.PipelineContext context, XMLReceiver xmlReceiver) { try { // Inputs final ProcessorInput configInput = getInputByName(INPUT_CONFIG); final ProcessorInput typeInput = getInputByName(INPUT_TRANSFORMER_CONFIG); final ProcessorInput dataInput = getInputByName(INPUT_DATA); // Create combined key for (transformer type, config) final CacheKey typeCacheKey = transformerFactory == null ? (CacheKey) getInputKey(context, typeInput) : (CacheKey) new InternalCacheKey(TraxTransformer.this, "transformerType", transformerFactory.getClass().getName()); final CacheKey configCacheKey = getInputKey(context, configInput); final InternalCacheKey combinedInputCacheKey = typeCacheKey == null || configCacheKey == null ? null : new InternalCacheKey(TraxTransformer.this, Arrays.asList(typeCacheKey, configCacheKey)); // Create combined validity for (transformer type, config) final Object typeValidity = transformerFactory == null ? getInputValidity(context, typeInput) : new Long(0); final Object configValidity = getInputValidity(context, configInput); final Object combinedInputValidity = typeValidity == null || configValidity == null ? null : Arrays.asList(typeValidity, configValidity); // Get templates from cache, or create it Templates templates = (Templates) ObjectCache.instance().findValid (combinedInputCacheKey, combinedInputValidity); if (templates == null) { // Create template handler if (transformerFactory == null) { final Node config = readCacheInputAsDOM4J(context, INPUT_TRANSFORMER_CONFIG); final String transformerClass = XPathUtils.selectStringValueNormalize(config, "/config/class"); transformerFactory = TransformerUtils.getFactory(transformerClass); } // TODO: If we were to use setURIResolver(), be careful to null it afterwards so that no ref to TransformerURIResolver occurs final TemplatesHandler templatesHandler = transformerFactory.newTemplatesHandler(); // Create template readInputAsSAX(context, configInput, new ForwardingXMLReceiver(templatesHandler)); templates = templatesHandler.getTemplates(); // Save template in cache ObjectCache.instance().add(combinedInputCacheKey, combinedInputValidity, templates); } // Perform transformation final Transformer transformer = templates.newTransformer(); transformer.setURIResolver(new TransformerURIResolver(TraxTransformer.this, context, INPUT_DATA, XMLUtils.ParserConfiguration.PLAIN)); final SAXResult saxResult = new SAXResult(xmlReceiver); saxResult.setLexicalHandler(xmlReceiver); transformer.transform(new SAXSource(new ProcessorOutputXMLReader(context, dataInput.getOutput()), new InputSource()), saxResult); } catch (Exception e) { throw new OXFException(e); } } }; addOutput(name, output); return output; }
/** * Create a new Transformer object that performs a copy * of the source to the result. * * @return A Transformer object that may be used to perform a transformation * in a single thread, never null. * * @throws TransformerConfigurationException May throw this during * the parse when it is constructing the * Templates object and fails. */ public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException { return new StylesheetHandler(this); }