List of usage examples for org.dom4j.io DocumentResult DocumentResult
public DocumentResult()
From source file:net.contextfw.web.application.internal.Transformers.java
License:Apache License
public Document transform(Document document) { if (initialized) { DocumentSource source = new DocumentSource(document); DocumentResult result = new DocumentResult(); Transformer tr = transformers.get(); String lang = document.getRootElement().attributeValue("xml:lang"); if (lang != null) { tr.setParameter("xml:lang", lang); }/*w w w . j ava 2 s . com*/ try { tr.transform(source, result); } catch (TransformerException e) { throw new WebApplicationException(e); } return result.getDocument(); } else { throw new WebApplicationException("Transformers are not initialized"); } }
From source file:net.sf.ginp.util.GinpUtil.java
License:Open Source License
/** * * @param stylePath the classpath resource name of the transforming xsl doc * @param visitDoc the doc to transform//from w w w . j ava 2 s.com * @return the transformed document * @throws TransformerException */ public static Document transform(final String stylePath, final Document visitDoc) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory .newTransformer(new StreamSource(GinpUtil.class.getResourceAsStream(stylePath))); DocumentResult result = new DocumentResult(); transformer.transform(new DocumentSource(visitDoc), result); return result.getDocument(); }
From source file:nl.knaw.dans.dccd.application.services.DccdSearchService.java
License:Apache License
/** Transform the given foxml to a Solr indexing document * * @param foxml The xml of the fodora object (indexed) * @return The xml document (for updating the Solr index) * @throws SearchServiceException//from w w w. jav a2s .c o m */ private Document transformFoxml(String foxml) throws SearchServiceException { if (foxml == null) throw new IllegalArgumentException(); // get the xslt to transform with, specific for Solr final String DCCD_TO_SOLR_XSLT_FILENAME = "dccdToSolr.xslt"; ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL xsltUrl = loader.getResource(DCCD_TO_SOLR_XSLT_FILENAME); logger.info("Transform foxml to indexing document using " + DCCD_TO_SOLR_XSLT_FILENAME); // transform foxml with xslt Document document = null; Transformer transformer = null; Document transformedDoc = null; TransformerFactory transformerFactory = TransformerFactory.newInstance(); try { document = DocumentHelper.parseText(foxml); //transformer = transformerFactory.newTransformer( new StreamSource(xsltStr ) ); transformer = transformerFactory.newTransformer(new StreamSource(xsltUrl.getFile())); DocumentSource source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); transformedDoc = result.getDocument(); } catch (TransformerConfigurationException e) { // ? should not happen ? throw new SearchServiceException(e); } catch (DocumentException e) { throw new SearchServiceException(e); } catch (TransformerException e) { throw new SearchServiceException(e); } // print transformedDoc /* try { OutputFormat format = OutputFormat.createPrettyPrint(); System.out.println("xml:\n"); XMLWriter writer = new XMLWriter( System.out, format ); writer.write( transformedDoc ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } */ return transformedDoc; }
From source file:org.apache.commons.jelly.tags.xml.TransformTag.java
License:Apache License
/** * Process this tag instance/*w w w . j av a2s . co m*/ * * @param output The pipeline for xml events * @throws Exception - when required attributes are missing */ public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { if (null == this.getXslt()) { throw new MissingAttributeException("The xslt attribute cannot be null"); } // set a resolver to locate uri this.tf.setURIResolver(createURIResolver()); try { this.transformerHandler = this.tf.newTransformerHandler(this.getObjAsSAXSource(this.getXslt())); } catch (TransformerConfigurationException e) { throw new JellyTagException(e); } // run any nested param tags this.doNestedParamTag(output); try { // get a reader to provide SAX events to transformer XMLReader xmlReader = this.createXMLReader(); xmlReader.setContentHandler(this.transformerHandler); xmlReader.setProperty(LEXICAL_HANDLER_PROPERTY, this.transformerHandler); // handle result differently, depending on if var is specified String varName = this.getVar(); if (null == varName) { // pass the result of the transform out as SAX events this.transformerHandler.setResult(this.createSAXResult(output)); xmlReader.parse(this.getXMLInputSource()); } else { // pass the result of the transform out as a document DocumentResult result = new DocumentResult(); this.transformerHandler.setResult(result); xmlReader.parse(this.getXMLInputSource()); // output the result as a variable Document transformedDoc = result.getDocument(); this.context.setVariable(varName, transformedDoc); } } catch (SAXException e) { throw new JellyTagException(e); } catch (IOException e) { throw new JellyTagException(e); } }
From source file:org.danann.cernunnos.xml.XslTransformTask.java
License:Apache License
public void perform(TaskRequest req, TaskResponse res) { final String contextLocation = (String) context.evaluate(req, res); final String stylesheetLocation = (String) stylesheet.evaluate(req, res); final Tuple<String, String> transformerKey = new Tuple<String, String>(contextLocation, stylesheetLocation); final Templates templates = this.transformerCache.getCachedObject(req, res, transformerKey, this.transformerFactory); Element srcElement = null;//from www. j a v a 2 s . com Node nodeReagentEvaluated = node != null ? (Node) node.evaluate(req, res) : null; if (nodeReagentEvaluated != null) { // Reading from the NODE reagent is preferred... srcElement = (Element) nodeReagentEvaluated; } else { // But read from LOCATION if NODE isn't set... final String locationStr = (String) location.evaluate(req, res); final URL loc; try { final URL ctx; try { ctx = new URL(contextLocation); } catch (MalformedURLException mue) { throw new RuntimeException("Failed to parse context '" + contextLocation + "' into URL", mue); } loc = new URL(ctx, locationStr); } catch (MalformedURLException mue) { throw new RuntimeException("Failed to parse location '" + locationStr + "' with context '" + contextLocation + "' into URL", mue); } // Use an EntityResolver if provided... SAXReader rdr = new SAXReader(); EntityResolver resolver = (EntityResolver) entityResolver.evaluate(req, res); if (resolver != null) { rdr.setEntityResolver(resolver); } final Document document; try { document = rdr.read(loc); } catch (DocumentException de) { throw new RuntimeException("Failed to read XML Document for XSLT from " + loc.toExternalForm(), de); } srcElement = document.getRootElement(); } DocumentFactory dfac = new DocumentFactory(); Document ddoc = dfac.createDocument((Element) srcElement.clone()); DOMWriter dwriter = new DOMWriter(); DocumentResult rslt = new DocumentResult(); final Transformer trans; try { trans = templates.newTransformer(); } catch (TransformerConfigurationException tce) { throw new RuntimeException("Failed to retrieve Transformer for XSLT", tce); } try { trans.transform(new DOMSource(dwriter.write(ddoc)), rslt); } catch (TransformerException te) { throw new RuntimeException("Failed to perform XSL transformation", te); } catch (DocumentException de) { throw new RuntimeException("Failed to translate JDOM Document to W3C Document", de); } final Element rootElement = rslt.getDocument().getRootElement(); if (to_file != null) { File f = new File((String) to_file.evaluate(req, res)); if (f.getParentFile() != null) { // Make sure the necessary directories are in place... f.getParentFile().mkdirs(); } final XMLWriter writer; try { writer = new XMLWriter(new FileOutputStream(f), new OutputFormat(" ", true)); } catch (UnsupportedEncodingException uee) { throw new RuntimeException("Failed to create XML writer", uee); } catch (FileNotFoundException fnfe) { throw new RuntimeException("Could not create file for XML output: " + f, fnfe); } try { writer.write(rootElement); } catch (IOException ioe) { throw new RuntimeException("Failed to write transformed XML document to: " + f, ioe); } } else { // default behavior... res.setAttribute(Attributes.NODE, rootElement); } super.performSubtasks(req, res); }
From source file:org.dcm4chex.archive.hl7.HL7ServerService.java
License:LGPL
private Document preprocessHL7(Document msg, InetSocketAddress inetAddr) { MSH msh = new MSH(msg); String[] subdirs = getXsltSearchDirectories(inetAddr, msh); String[] variations = new String[] { "_" + msh.messageType + "^" + msh.triggerEvent, "_" + msh.messageType, "" }; Templates xslt = templates.findTemplates(subdirs, PREPROCESS_XSL, variations, XSL_EXT); if (xslt != null) { log.info("Preprocess HL7 message with stylesheet!"); try {// w ww . j a va 2s . c o m Transformer t; t = xslt.newTransformer(); DocumentResult result = new DocumentResult(); t.transform(new DocumentSource(msg), result); return result.getDocument(); } catch (Exception x) { log.error("Can not apply preprocess stylesheet!", x); } } return null; }
From source file:org.dom4j.samples.jaxp.RoundTripDemo.java
License:Open Source License
protected Document parseDocument(Reader in) { try {//from w w w . j a va 2s . c om TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DocumentResult result = new DocumentResult(); StreamSource source = new StreamSource(in); transformer.transform(source, result); return result.getDocument(); } catch (Exception ex) { ex.printStackTrace(); return null; } }
From source file:org.dom4j.samples.XSLTDemo.java
License:Open Source License
/** Perform XSLT on the stylesheet */ protected void process(Document document) throws Exception { // load the transformer TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xsl)); // now lets create the TrAX source and result // objects and do the transformation Source source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); // output the transformed document Document transformedDoc = result.getDocument(); writer.write(transformedDoc);/*w ww .ja v a 2 s .c om*/ }
From source file:org.eclipse.jubula.client.core.businessprocess.HtmlResultReportWriter.java
License:Open Source License
/** * /*from w w w. j a v a 2 s .com*/ * {@inheritDoc} */ public void write(Document document) { // write html, transformed by XSLT OutputFormat htmlFormat = OutputFormat.createCompactFormat(); htmlFormat.setEncoding(m_writer.getEncoding()); TransformerFactory factory = TransformerFactory.newInstance(); try { TestResultBP trbp = TestResultBP.getInstance(); final Transformer transformer = factory .newTransformer(new StreamSource(trbp.getXslFileURL().openStream())); DocumentSource source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); Document transformedDoc = result.getDocument(); XMLWriter htmlWriter = new XMLWriter(m_writer, htmlFormat); htmlWriter.write(transformedDoc); } catch (TransformerConfigurationException e1) { LOG.error(Messages.ErrorFileWriting + StringConstants.DOT, e1); } catch (TransformerException e) { LOG.error(Messages.ErrorFileWriting + StringConstants.DOT, e); } catch (IOException e) { LOG.error(Messages.ErrorFileWriting + StringConstants.DOT, e); } }
From source file:org.jbpm.jpdl.convert.Converter.java
License:Open Source License
public Document convert(Document document) throws Exception { // load the transformer using JAXP TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory .newTransformer(new StreamSource(this.getClass().getResourceAsStream(STYLESHEET_NAME))); // apply the conversion stylesheet to the incoming process definition DocumentSource source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); // return the transformed document return result.getDocument(); }