/** Check for required facilities. If not available, an exception will be thrown. */ public TemplatesPool(boolean templateCaching) throws Exception { final TransformerFactory tFactory = TransformerFactory.newInstance(); final String processorClass = tFactory.getClass().getName(); /* * Only report XSLT processor class once. */ if (!reportedProcessors.contains(processorClass)) { logger.info("XSLT transformer factory: " + processorClass); reportedProcessors.add(processorClass); } if (!tFactory.getFeature(SAXSource.FEATURE) || !tFactory.getFeature(SAXResult.FEATURE)) { throw new Exception("Required source types not supported by the transformer factory."); } if (!tFactory.getFeature(SAXResult.FEATURE) || !tFactory.getFeature(StreamResult.FEATURE)) { throw new Exception("Required result types not supported by the transformer factory."); } if (!(tFactory instanceof SAXTransformerFactory)) { throw new Exception( "TransformerFactory not an instance of SAXTransformerFactory: " + tFactory.getClass().getName()); } this.tFactory = ((SAXTransformerFactory) tFactory); this.tFactory.setErrorListener(new StylesheetErrorListener()); this.templateCaching = templateCaching; }
public AssemblingParser(EntityResolver er, InputSource in) throws ConfigException { this.er = er; this.main = in; factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); TransformerFactory tf = TransformerFactory.newInstance(); if (!tf.getFeature(SAXSource.FEATURE) || !tf.getFeature(SAXResult.FEATURE)) throw new ConfigException("XSLT not supported"); transfactory = (SAXTransformerFactory) tf; }
public static void main(String[] args) throws TransformerException, TransformerConfigurationException, SAXException, IOException { // Instantiate a TransformerFactory. TransformerFactory tFactory = TransformerFactory.newInstance(); // Determine whether the TransformerFactory supports The use uf SAXSource // and SAXResult if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) { // Cast the TransformerFactory to SAXTransformerFactory. SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory); // Create an XMLFilter for each stylesheet. XMLFilter xmlFilter1 = saxTFactory.newXMLFilter(new StreamSource("foo1.xsl")); XMLFilter xmlFilter2 = saxTFactory.newXMLFilter(new StreamSource("foo2.xsl")); XMLFilter xmlFilter3 = saxTFactory.newXMLFilter(new StreamSource("foo3.xsl")); // Create an XMLReader. XMLReader reader = XMLReaderFactory.createXMLReader(); // xmlFilter1 uses the XMLReader as its reader. xmlFilter1.setParent(reader); // xmlFilter2 uses xmlFilter1 as its reader. xmlFilter2.setParent(xmlFilter1); // xmlFilter3 uses xmlFilter2 as its reader. xmlFilter3.setParent(xmlFilter2); // xmlFilter3 outputs SAX events to the serializer. java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml"); xmlProps.setProperty("indent", "yes"); xmlProps.setProperty("standalone", "no"); Serializer serializer = SerializerFactory.getSerializer(xmlProps); serializer.setOutputStream(System.out); xmlFilter3.setContentHandler(serializer.asContentHandler()); // Perform the series of transformations as follows: // - transformer3 gets its parent (transformer2) as the XMLReader/XMLFilter // and calls transformer2.parse(new InputSource("foo.xml")). // - transformer2 gets its parent (transformer1) as the XMLReader/XMLFilter // and calls transformer1.parse(new InputSource("foo.xml")). // - transformer1 gets its parent (reader, a SAXParser) as the XMLReader // and calls reader.parse(new InputSource("foo.xml")). // - reader parses the XML document and sends the SAX parse events to transformer1, // which performs transformation 1 and sends the output to transformer2. // - transformer2 parses the transformation 1 output, performs transformation 2, and // sends the output to transformer3. // - transformer3 parses the transformation 2 output, performs transformation 3, // and sends the output to the serializer. xmlFilter3.parse(new InputSource("foo.xml")); } }
protected void setHandler(Result result, Source stylesheet) throws MarcException { try { TransformerFactory factory = TransformerFactory.newInstance(); if (!factory.getFeature(SAXTransformerFactory.FEATURE)) throw new UnsupportedOperationException("SAXTransformerFactory is not supported"); SAXTransformerFactory saxFactory = (SAXTransformerFactory) factory; if (stylesheet == null) handler = saxFactory.newTransformerHandler(); else handler = saxFactory.newTransformerHandler(stylesheet); handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml"); handler.setResult(result); } catch (Exception e) { throw new MarcException(e.getMessage(), e); } }
@Override public int doEndTag() throws JspException { try { // set up our DocumentBuilder if (dbf == null) { dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); } db = dbf.newDocumentBuilder(); // if we've gotten a filter, set up a transformer to support it if (filter != null) { if (tf == null) { tf = TransformerFactory.newInstance(); } if (!tf.getFeature(SAXTransformerFactory.FEATURE)) { throw new JspTagException(Resources.getMessage("PARSE_NO_SAXTRANSFORMER")); } SAXTransformerFactory stf = (SAXTransformerFactory) tf; th = stf.newTransformerHandler(); } // produce a Document by parsing whatever the attributes tell us to use Document d; Object xmlText = this.xml; if (xmlText == null) { // if the attribute was specified, use the body as 'xml' if (bodyContent != null && bodyContent.getString() != null) { xmlText = bodyContent.getString().trim(); } else { xmlText = ""; } } if (xmlText instanceof String) { d = parseStringWithFilter((String) xmlText, filter); } else if (xmlText instanceof Reader) { d = parseReaderWithFilter((Reader) xmlText, filter); } else { throw new JspTagException(Resources.getMessage("PARSE_INVALID_SOURCE")); } // we've got a Document object; store it out as appropriate // (let any exclusivity or other constraints be enforced by TEI/TLV) if (var != null) { pageContext.setAttribute(var, d, scope); } if (varDom != null) { pageContext.setAttribute(varDom, d, scopeDom); } return EVAL_PAGE; } catch (SAXException ex) { throw new JspException(ex); } catch (IOException ex) { throw new JspException(ex); } catch (ParserConfigurationException ex) { throw new JspException(ex); } catch (TransformerConfigurationException ex) { throw new JspException(ex); } }
public int process() throws TransformerException, IOException, SAXException { ZipInputStream zis = new ZipInputStream(input); final ZipOutputStream zos = new ZipOutputStream(output); final OutputStreamWriter osw = new OutputStreamWriter(zos); Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); TransformerFactory tf = TransformerFactory.newInstance(); if (!tf.getFeature(SAXSource.FEATURE) || !tf.getFeature(SAXResult.FEATURE)) { return 0; } SAXTransformerFactory saxtf = (SAXTransformerFactory) tf; Templates templates = null; if (xslt != null) { templates = saxtf.newTemplates(xslt); } // configuring outHandlerFactory // /////////////////////////////////////////////////////// EntryElement entryElement = getEntryElement(zos); ContentHandler outDocHandler = null; switch (outRepresentation) { case BYTECODE: outDocHandler = new OutputSlicingHandler(new ASMContentHandlerFactory(zos), entryElement, false); break; case MULTI_XML: outDocHandler = new OutputSlicingHandler(new SAXWriterFactory(osw, true), entryElement, true); break; case SINGLE_XML: ZipEntry outputEntry = new ZipEntry(SINGLE_XML_NAME); zos.putNextEntry(outputEntry); outDocHandler = new SAXWriter(osw, false); break; } // configuring inputDocHandlerFactory // ///////////////////////////////////////////////// ContentHandler inDocHandler; if (templates == null) { inDocHandler = outDocHandler; } else { inDocHandler = new InputSlicingHandler( "class", outDocHandler, new TransformerHandlerFactory(saxtf, templates, outDocHandler)); } ContentHandlerFactory inDocHandlerFactory = new SubdocumentHandlerFactory(inDocHandler); if (inDocHandler != null && inRepresentation != SINGLE_XML) { inDocHandler.startDocument(); inDocHandler.startElement("", "classes", "classes", new AttributesImpl()); } int i = 0; ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { update(ze.getName(), n++); if (isClassEntry(ze)) { processEntry(zis, ze, inDocHandlerFactory); } else { OutputStream os = entryElement.openEntry(getName(ze)); copyEntry(zis, os); entryElement.closeEntry(); } i++; } if (inDocHandler != null && inRepresentation != SINGLE_XML) { inDocHandler.endElement("", "classes", "classes"); inDocHandler.endDocument(); } if (outRepresentation == SINGLE_XML) { zos.closeEntry(); } zos.flush(); zos.close(); return i; }