/** Transform a SAX Source to a TinyTree. */ public static DocumentInfo readTinyTree( Configuration configuration, Source source, boolean handleXInclude) { final TinyBuilder treeBuilder = new TinyBuilder(); try { if (handleXInclude) { // Insert XIncludeContentHandler final TransformerXMLReceiver identityHandler = getIdentityTransformerHandler(configuration); identityHandler.setResult(treeBuilder); final XMLReceiver receiver = new XIncludeProcessor.XIncludeXMLReceiver( null, identityHandler, null, new TransformerURIResolver(XMLUtils.ParserConfiguration.PLAIN)); TransformerUtils.sourceToSAX(source, receiver); } else { final Transformer identity = getIdentityTransformer(configuration); identity.transform(source, treeBuilder); } } catch (TransformerException e) { throw new OXFException(e); } return (DocumentInfo) treeBuilder.getCurrentRoot(); }
public static org.dom4j.Document createDocument(DebugXML debugXML) { final TransformerXMLReceiver identity = TransformerUtils.getIdentityTransformerHandler(); final LocationDocumentResult result = new LocationDocumentResult(); identity.setResult(result); final ContentHandlerHelper helper = new ContentHandlerHelper( new ForwardingXMLReceiver(identity) { @Override public void startDocument() {} @Override public void endDocument() {} }); try { identity.startDocument(); debugXML.toXML(helper); identity.endDocument(); } catch (SAXException e) { throw new OXFException(e); } return result.getDocument(); }
/** Transform an InputStream to a TinyTree. */ public static DocumentInfo readTinyTree( Configuration configuration, InputStream inputStream, String systemId, boolean handleXInclude, boolean handleLexical) { final TinyBuilder treeBuilder = new TinyBuilder(); { final TransformerXMLReceiver identityHandler = getIdentityTransformerHandler(configuration); identityHandler.setResult(treeBuilder); final XMLReceiver xmlReceiver; if (handleXInclude) { // Insert XIncludeContentHandler xmlReceiver = new XIncludeProcessor.XIncludeXMLReceiver( null, identityHandler, null, new TransformerURIResolver(XMLUtils.ParserConfiguration.PLAIN)); } else { xmlReceiver = identityHandler; } XMLUtils.inputStreamToSAX( inputStream, systemId, xmlReceiver, XMLUtils.ParserConfiguration.PLAIN, handleLexical); } return (DocumentInfo) treeBuilder.getCurrentRoot(); }
/** * Transform a SAXStore into a TinyTree document * * @param saxStore input SAXStore * @return DocumentInfo */ public static DocumentInfo saxStoreToTinyTree(Configuration configuration, SAXStore saxStore) { final TinyBuilder treeBuilder = new TinyBuilder(); try { final TransformerXMLReceiver identity = getIdentityTransformerHandler(configuration); identity.setResult(treeBuilder); saxStore.replay(identity); } catch (SAXException e) { throw new OXFException(e); } return (DocumentInfo) treeBuilder.getCurrentRoot(); }
/** * Transform a SAXStore into a dom4j document * * @param saxStore input SAXStore * @return dom4j document */ public static Document saxStoreToDom4jDocument(SAXStore saxStore) { final TransformerXMLReceiver identity = getIdentityTransformerHandler(); final LocationDocumentResult documentResult = new LocationDocumentResult(); identity.setResult(documentResult); try { saxStore.replay(identity); } catch (SAXException e) { throw new OXFException(e); } return documentResult.getDocument(); }
private XMLReceiver getDebugReceiver(final IndentedLogger indentedLogger) { final TransformerXMLReceiver identity = TransformerUtils.getIdentityTransformerHandler(); final StringBuilderWriter writer = new StringBuilderWriter(); identity.setResult(new StreamResult(writer)); return new ForwardingXMLReceiver(identity) { @Override public void endDocument() throws SAXException { super.endDocument(); // Log out at end of document indentedLogger.logDebug("", "static state input", "input", writer.toString()); } }; }
public void serialize(PipelineContext pipelineContext, URL url) { try { if (OXFHandler.PROTOCOL.equals(url.getProtocol())) { // NOTE: This is probably done as an optimization. Is this really necessary? final OutputStream os = ResourceManagerWrapper.instance().getOutputStream(url.getFile()); try { final TransformerXMLReceiver identity = TransformerUtils.getIdentityTransformerHandler(); identity.setResult(new StreamResult(os)); readInputAsSAX(pipelineContext, INPUT_DATA, identity); } finally { // Clean up if (os != null) os.close(); } } else { // Open the URL final URLConnection conn = url.openConnection(); try { conn.setDoOutput(true); conn.connect(); final OutputStream os = conn.getOutputStream(); try { // Create an identity transformer and start the transformation final TransformerXMLReceiver identity = TransformerUtils.getIdentityTransformerHandler(); identity.setResult(new StreamResult(os)); readInputAsSAX(pipelineContext, INPUT_DATA, identity); } finally { // Clean up if (os != null) os.close(); } } finally { // Clean up if (conn instanceof HttpURLConnection) ((HttpURLConnection) conn).disconnect(); } } } catch (Exception e) { throw new OXFException(e); } }
public StaticStateBits( PipelineContext pipelineContext, ExternalContext externalContext, IndentedLogger indentedLogger, String existingStaticStateDigest) { final boolean computeDigest = isLogStaticStateInput || existingStaticStateDigest == null; indentedLogger.startHandleOperation( "", "reading input", "existing digest", existingStaticStateDigest); final TransformerXMLReceiver documentReceiver = TransformerUtils.getIdentityTransformerHandler(); final LocationDocumentResult documentResult = new LocationDocumentResult(); documentReceiver.setResult(documentResult); final XMLUtils.DigestContentHandler digestReceiver = computeDigest ? new XMLUtils.DigestContentHandler("MD5") : null; final XMLReceiver extractorOutput; if (isLogStaticStateInput) { extractorOutput = computeDigest ? new TeeXMLReceiver( documentReceiver, digestReceiver, getDebugReceiver(indentedLogger)) : new TeeXMLReceiver(documentReceiver, getDebugReceiver(indentedLogger)); } else { extractorOutput = computeDigest ? new TeeXMLReceiver(documentReceiver, digestReceiver) : documentReceiver; } // Read the input through the annotator and gather namespace mappings // // Output of annotator is: // // o annotated page template (TODO: this should not include model elements) // o extractor // // Output of extractor is: // // o static state document // o optionally: digest // o optionally: debug output // readInputAsSAX( pipelineContext, INPUT_ANNOTATED_DOCUMENT, new XFormsAnnotatorContentHandler( annotatedTemplate, new XFormsExtractorContentHandler(extractorOutput, metadata), metadata)); this.staticStateDocument = documentResult.getDocument(); this.staticStateDigest = computeDigest ? NumberUtils.toHexString(digestReceiver.getResult()) : null; assert !isLogStaticStateInput || existingStaticStateDigest == null || this.staticStateDigest.equals(existingStaticStateDigest); indentedLogger.endHandleOperation("computed digest", this.staticStateDigest); }