List of usage examples for javax.xml.transform Transformer setURIResolver
public abstract void setURIResolver(URIResolver resolver);
From source file:Main.java
private static String getNodeValue(NodeList nodeList) throws TransformerFactoryConfigurationError, TransformerException { final Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setURIResolver(null); final StringBuilder buf = new StringBuilder(); for (int i = 0; i < nodeList.getLength(); i++) { final StringWriter sw = new StringWriter(); serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw)); String xml = sw.toString(); final Matcher matcher = HEADER_PATTERN.matcher(xml); if (matcher.matches()) { xml = matcher.group(1);//w w w. j a v a 2 s . c o m } buf.append(xml); buf.append("\n"); } return buf.toString(); }
From source file:Main.java
/** * @param xmlSource// www . j a v a2s. com * @param xslSource * @param resolver * @param output * @throws TransformerFactoryConfigurationError * @throws TransformerConfigurationException * @throws TransformerException */ public static void formatXML(Source xmlSource, Source xslSource, URIResolver resolver, Result result) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Templates t = factory.newTemplates(xslSource); Transformer transformer = t.newTransformer(); if (resolver != null) { transformer.setURIResolver(resolver); } transformer.transform(xmlSource, result); }
From source file:Main.java
public static void formatXML(Document xml, URIResolver resolver, Document xsl, Writer output) throws Exception { StreamResult result = new StreamResult(output); DOMSource xslSource = new DOMSource(xsl); TransformerFactory factory = TransformerFactory.newInstance(); DOMSource xmlSource = new DOMSource(xml); Templates t = factory.newTemplates(xslSource); Transformer transformer = t.newTransformer(); transformer.setURIResolver(resolver); transformer.transform(xmlSource, result); }
From source file:Main.java
public static Document format(Source xslSource, Document xml, URIResolver resolver) throws TransformerFactoryConfigurationError, TransformerConfigurationException, Exception, TransformerException {//from w ww . j av a 2 s .co m TransformerFactory factory = TransformerFactory.newInstance(); DOMSource xmlSource = new DOMSource(xml); Templates t = factory.newTemplates(xslSource); Transformer transformer = t.newTransformer(); transformer.setURIResolver(resolver); Document resultDoc = newDocument(); DOMResult result = new DOMResult(resultDoc); transformer.transform(xmlSource, result); return resultDoc; }
From source file:Main.java
public static Document formatXML(Document xml, URIResolver resolver, String... xsls) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); for (String xsl : xsls) { DOMSource xmlSource = new DOMSource(xml); Source xslSource = resolver.resolve(xsl, ""); Templates t = factory.newTemplates(xslSource); Transformer transformer = t.newTransformer(); transformer.setURIResolver(resolver); Document resultDoc = newDocument(); DOMResult result = new DOMResult(resultDoc); transformer.transform(xmlSource, result); xml = resultDoc;/*from w w w .ja va2 s . c o m*/ } return xml; }
From source file:it.cnr.icar.eric.server.cms.CanonicalXMLFilteringService.java
/** * Runs XSLT based upon specified inputs and returns the outputfile. * * TODO: Need some refactoring to make this reusable throughout OMAR * particularly in CanonicalXMLCatalogingService. *//*from ww w . j ava 2 s . c o m*/ protected static File runXSLT(StreamSource input, StreamSource xslt, URIResolver resolver, HashMap<String, String> params) throws RegistryException { File outputFile = null; try { //dumpStream(xslt); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = initTransformer(tFactory, xslt); // Use FilteringService URIResolver to resolve RIs submitted in the // ServiceInput object transformer.setURIResolver(resolver); //Set respository item as parameter //Create the output file with the filtered RegistryObject Metadata outputFile = File.createTempFile("CanonicalXMLFilteringService_Output", ".xml"); outputFile.deleteOnExit(); log.debug("outputFile= " + outputFile.getAbsolutePath()); Iterator<String> iter = params.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); Object value = params.get(key); transformer.setParameter(key, value); } StreamResult sr = new StreamResult(outputFile); transformer.transform(input, sr); } catch (Exception e) { throw new RegistryException(e); } return outputFile; }
From source file:eu.semaine.util.XMLTool.java
/** * Merge two XML files using XSLT//from w w w. java 2s . c o m * @param xmlFileContent1, first XML file content * @param xmlFileContent2, second XML file content * @param mergingStylesheet, the XSLT style sheet merging xml2 into xml1. * @param refCodeName, code name used in xsl sheet to refer xmlFile2 (example: semaine.mary.intonation ) * @return output of merged xml file * @throws Exception * @throws FileNotFoundException */ public static Document mergeTwoXMLFiles(Document xmlFileContent1, final Document xmlFileContent2, Templates mergingStylesheet, final String refCodeName) throws Exception, FileNotFoundException { DOMSource xml1Source = new DOMSource(xmlFileContent1); DOMResult mergedDR = new DOMResult(); // Transformer is not guaranteed to be thread-safe -- therefore, we // need one per thread. Transformer mergingTransformer = mergingStylesheet.newTransformer(); mergingTransformer.setURIResolver(new URIResolver() { public Source resolve(String href, String base) { if (href == null) { return null; } else if (href.equals(refCodeName)) { return (new DOMSource(xmlFileContent2)); } else { return null; } } }); mergingTransformer.transform(xml1Source, mergedDR); return (Document) mergedDR.getNode(); }
From source file:eu.semaine.util.XMLTool.java
/** * Merge two XML files using XSLT//from w ww. ja v a2s.c om * @param xmlFileContent1, first XML file content * @param xmlFileContent2, second XML file content * @param xmlStyleSheet, XSL style sheet as a inputstream * @param refCodeName, code name used in xsl sheet to refer xmlFile2 (example: semaine.mary.intonation ) * @return output of merged xml file * @throws Exception * @throws FileNotFoundException */ public static String mergeTwoXMLFiles(String xmlFileContent1, final String xmlFileContent2, InputStream xmlStyleSheet, final String refCodeName) throws Exception, FileNotFoundException { Templates mergeXML2IntoStylesheet; TransformerFactory tFactory = TransformerFactory.newInstance(); //StreamSource stylesheetStream = new StreamSource(new FileReader(xmlStyleSheet)); StreamSource stylesheetStream = new StreamSource(xmlStyleSheet); mergeXML2IntoStylesheet = tFactory.newTemplates(stylesheetStream); StreamSource xml1Source = new StreamSource(new StringReader(xmlFileContent1)); StringWriter mergedWriter = new StringWriter(); StreamResult mergedResult = new StreamResult(mergedWriter); // Transformer is not guaranteed to be thread-safe -- therefore, we // need one per thread. Transformer mergingTransformer = mergeXML2IntoStylesheet.newTransformer(); mergingTransformer.setURIResolver(new URIResolver() { public Source resolve(String href, String base) { if (href == null) { return null; } else if (href.equals(refCodeName)) { return (new StreamSource(new StringReader(xmlFileContent2))); } else { return null; } } }); mergingTransformer.transform(xml1Source, mergedResult); return mergedWriter.toString(); }
From source file:laboratorios.CreatePDF.java
/** * Renders an XML file into a PDF file by applying a stylesheet * that converts the XML to XSL-FO. The PDF is written to a byte array * that is returned as the method's result. * @param xml the XML file/*w w w . j av a 2s . c o m*/ * @param xslt the XSLT file * @param response HTTP response object * @throws FOPException If an error occurs during the rendering of the * XSL-FO * @throws TransformerException If an error occurs during XSL * transformation * @throws IOException In case of an I/O problem */ public void renderXML(String xml, String xsl, HttpServletResponse response, ServletContext context) throws FOPException, TransformerException, IOException { URIResolver uriResolver = new ServletContextURIResolver(context); //Setup sources Source xmlSrc = new StreamSource(new StringReader(xml)); String realpath = context.getRealPath(xsl); Source xsltSrc = new StreamSource(new File(realpath)); //Le damos el path real de la aplicacion para que encuentre los ficheros realpath = context.getRealPath("."); //this.transFactory.setURIResolver(uriResolver); Transformer transformer = this.transFactory.newTransformer(xsltSrc); transformer.setURIResolver(uriResolver); //Start transformation and rendering process render(xmlSrc, transformer, response, realpath); }
From source file:fedora.localservices.fop.FOPServlet.java
/** * Renders an XSL-FO file into a PDF file. The PDF is written to a byte * array that is returned as the method's result. * @param fo the XSL-FO file// www. jav a 2 s . co m * @param response HTTP response object * @throws FOPException If an error occurs during the rendering of the * XSL-FO * @throws TransformerException If an error occurs while parsing the input * file * @throws IOException In case of an I/O problem */ protected void renderFO(String fo, HttpServletResponse response) throws FOPException, TransformerException, IOException { //Setup source Source foSrc = convertString2Source(fo); //Setup the identity transformation Transformer transformer = this.transFactory.newTransformer(); transformer.setURIResolver(this.uriResolver); //Start transformation and rendering process render(foSrc, transformer, response); }