List of usage examples for javax.xml.transform TransformerFactory setURIResolver
public abstract void setURIResolver(URIResolver resolver);
From source file:edu.mayo.xsltserver.Transformer.java
/** * Transform./* w w w . ja v a 2 s . co m*/ * * @param xmlInputStream the xml input stream * @param xsltInputStream the xslt input stream * @param outputStream the output stream * @param parameters the parameters */ public void transform(InputStream xmlInputStream, InputStream xsltInputStream, OutputStream outputStream, Map<String, String> parameters) { try { // Source XML File StreamSource xmlFile = new StreamSource(xmlInputStream); // Source XSLT Stylesheet StreamSource xsltFile = new StreamSource(xsltInputStream); TransformerFactory xsltFactory = TransformerFactory.newInstance(); final URIResolver decoratedResolver = xsltFactory.getURIResolver(); xsltFactory.setURIResolver(new URIResolver() { @Override public Source resolve(String href, String base) throws TransformerException { Source source = decoratedResolver.resolve(href, fileService.getStorageDirectory() + File.separator); return source; } }); javax.xml.transform.Transformer transformer = xsltFactory.newTransformer(xsltFile); if (parameters != null) { for (Entry<String, String> entry : parameters.entrySet()) { transformer.setParameter(entry.getKey(), entry.getValue()); } } // Send transformed output to the console StreamResult resultStream = new StreamResult(outputStream); // Apply the transformation transformer.transform(xmlFile, resultStream); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:jeeves.utils.Xml.java
/** * Transforms an xml tree putting the result to a stream with optional parameters. * * @param xml/* w ww .ja v a 2 s . c om*/ * @param styleSheetPath * @param result * @param params * @throws Exception */ public static void transform(Element xml, String styleSheetPath, Result result, Map<String, String> params) throws Exception { File styleSheet = new File(styleSheetPath); Source srcXml = new JDOMSource(new Document((Element) xml.detach())); Source srcSheet = new StreamSource(styleSheet); // Dear old saxon likes to yell loudly about each and every XSLT 1.0 // stylesheet so switch it off but trap any exceptions because this // code is run on transformers other than saxon TransformerFactory transFact = TransformerFactoryFactory.getTransformerFactory(); transFact.setURIResolver(new JeevesURIResolver()); try { transFact.setAttribute(FeatureKeys.VERSION_WARNING, false); transFact.setAttribute(FeatureKeys.LINE_NUMBERING, true); transFact.setAttribute(FeatureKeys.PRE_EVALUATE_DOC_FUNCTION, false); transFact.setAttribute(FeatureKeys.RECOVERY_POLICY, Configuration.RECOVER_SILENTLY); // Add the following to get timing info on xslt transformations //transFact.setAttribute(FeatureKeys.TIMING,true); } catch (IllegalArgumentException e) { Log.warning(Log.ENGINE, "WARNING: transformerfactory doesnt like saxon attributes!"); //e.printStackTrace(); } finally { Transformer t = transFact.newTransformer(srcSheet); if (params != null) { for (Map.Entry<String, String> param : params.entrySet()) { t.setParameter(param.getKey(), param.getValue()); } } t.transform(srcXml, result); } }
From source file:mx.bigdata.sat.cfd.CFDv2.java
public void setTransformerFactory(TransformerFactory tf) { this.tf = tf; tf.setURIResolver(new URIResolverImpl()); }
From source file:jeeves.utils.Xml.java
/** * Transform an xml tree to PDF using XSL-FOP * putting the result to a stream (uses a stylesheet * on disk)//from w w w . j ava2 s . co m */ public static String transformFOP(String uploadDir, Element xml, String styleSheetPath) throws Exception { String file = uploadDir + UUID.randomUUID().toString() + ".pdf"; // Step 1: Construct a FopFactory // (reuse if you plan to render multiple documents!) FopFactory fopFactory = FopFactory.newInstance(); // Step 2: Set up output stream. // Note: Using BufferedOutputStream for performance reasons (helpful // with FileOutputStreams). OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file))); try { // Step 3: Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); // Step 4: Setup JAXP using identity transformer TransformerFactory factory = TransformerFactoryFactory.getTransformerFactory(); factory.setURIResolver(new JeevesURIResolver()); Source xslt = new StreamSource(new File(styleSheetPath)); try { factory.setAttribute(FeatureKeys.VERSION_WARNING, false); factory.setAttribute(FeatureKeys.LINE_NUMBERING, true); factory.setAttribute(FeatureKeys.RECOVERY_POLICY, Configuration.RECOVER_SILENTLY); } catch (IllegalArgumentException e) { Log.warning(Log.ENGINE, "WARNING: transformerfactory doesnt like saxon attributes!"); //e.printStackTrace(); } finally { Transformer transformer = factory.newTransformer(xslt); // Step 5: Setup input and output for XSLT transformation // Setup input stream Source src = new JDOMSource(new Document((Element) xml.detach())); // Resulting SAX events (the generated FO) must be piped through to // FOP Result res = new SAXResult(fop.getDefaultHandler()); // Step 6: Start XSLT transformation and FOP processing transformer.transform(src, res); } } finally { // Clean-up out.close(); } return file; }
From source file:dk.defxws.fedoragsearch.server.GTransformer.java
public Transformer getTransformer(String xsltPath, URIResolver uriResolver, boolean checkInHome) throws Exception { if (cache.containsKey(xsltPath)) { return cache.get(xsltPath); }/* w w w . jav a2 s .c om*/ Transformer transformer = null; String xsltPathName = xsltPath + ".xslt"; try { InputStream stylesheet; try { if (checkInHome) { String dirname = Constants.WORKING_DIR + File.separator + "xsl"; File textFile = new File(dirname, xsltPathName); if ((textFile.exists()) && (textFile.canRead())) { stylesheet = new FileInputStream(textFile); } else { stylesheet = this.getClass() .getResourceAsStream("/cz/incad/kramerius/indexer/res/" + xsltPathName); } } else { stylesheet = new FileInputStream(new File(xsltPath)); } } catch (Exception ex) { throw new Exception(xsltPath + " not found"); } TransformerFactory tfactory = TransformerFactory.newInstance(); if (uriResolver != null) { tfactory.setURIResolver(uriResolver); } StreamSource xslt = new StreamSource(stylesheet); transformer = tfactory.newTransformer(xslt); if (uriResolver != null) { transformer.setURIResolver(uriResolver); } } catch (TransformerConfigurationException e) { throw new Exception("getTransformer " + xsltPath + ":\n", e); } catch (TransformerFactoryConfigurationError e) { throw new Exception("getTransformerFactory " + xsltPath + ":\n", e); } if (transformer != null) { cache.put(xsltPath, transformer); } return transformer; }
From source file:com.google.code.docbook4j.renderer.BaseRenderer.java
protected Transformer createTransformer(FileObject xmlSource, FileObject xslStylesheet) throws TransformerConfigurationException, IOException { TransformerFactory transformerFactory = createTransformerFactory(); if (xslStylesheet != null) { transformerFactory.setURIResolver(new XslURIResolver()); }// www .j a v a 2 s . co m FileObject xsl = xslStylesheet != null ? xslStylesheet : getDefaultXslStylesheet(); Source source = new StreamSource(xsl.getContent().getInputStream(), xsl.getURL().toExternalForm()); Transformer transformer = transformerFactory.newTransformer(source); transformer.setParameter("use.extensions", "1"); transformer.setParameter("callout.graphics", "0"); transformer.setParameter("callout.unicode", "1"); transformer.setParameter("callouts.extension", "1"); transformer.setParameter("base.dir", xmlSource.getParent().getURL().toExternalForm()); for (Map.Entry<String, String> entry : this.params.entrySet()) { transformer.setParameter(entry.getKey(), entry.getValue()); } return transformer; }
From source file:mx.bigdata.sat.cfd.CFDv2.java
byte[] getOriginalBytes() throws Exception { JAXBSource in = new JAXBSource(context, document); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Result out = new StreamResult(baos); TransformerFactory factory = tf; if (factory == null) { factory = TransformerFactory.newInstance(); factory.setURIResolver(new URIResolverImpl()); }//from w w w . ja va2s .c om Transformer transformer = factory.newTransformer(new StreamSource(getClass().getResourceAsStream(XSLT))); transformer.transform(in, out); return baos.toByteArray(); }
From source file:mx.bigdata.sat.cfdi.CFDv33.java
@Override public void setTransformerFactory(TransformerFactory tf) { this.tf = tf; tf.setURIResolver(new URIResolverImpl()); }
From source file:edu.unc.lib.dl.schematron.SchematronValidator.java
/** * Use this to initialize the configured schemas. Generate stylesheet * implementations of ISO Schematron files and preload them into Transformer * Templates for quick use./* w ww. j av a 2s . co m*/ */ public void loadSchemas() { templates = new HashMap<String, Templates>(); // Load up a transformer and the ISO Schematron to XSL templates. Templates isoSVRLTemplates = null; ClassPathResource svrlRes = new ClassPathResource("/edu/unc/lib/dl/schematron/iso_svrl.xsl", SchematronValidator.class); Source svrlrc; try { svrlrc = new StreamSource(svrlRes.getInputStream()); } catch (IOException e1) { throw new Error("Cannot load iso_svrl.xsl", e1); } TransformerFactory factory = null; try { factory = new TransformerFactoryImpl(); // enable relative classpath-based URIs factory.setURIResolver(new URIResolver() { public Source resolve(String href, String base) throws TransformerException { ClassPathResource svrlRes = new ClassPathResource(href, SchematronValidator.class); Source result; try { result = new StreamSource(svrlRes.getInputStream()); } catch (IOException e1) { throw new TransformerException("Cannot resolve " + href, e1); } return result; } }); isoSVRLTemplates = factory.newTemplates(svrlrc); } catch (TransformerFactoryConfigurationError e) { log.error("Error setting up transformer factory.", e); throw new Error("Error setting up transformer factory", e); } catch (TransformerConfigurationException e) { log.error("Error setting up transformer.", e); throw new Error("Error setting up transformer", e); } // Get a transformer Transformer t = null; try { t = isoSVRLTemplates.newTransformer(); } catch (TransformerConfigurationException e) { throw new Error("There was a problem configuring the transformer.", e); } for (String schema : schemas.keySet()) { // make XSLT out of Schematron for each schema Resource resource = schemas.get(schema); Source schematron = null; try { schematron = new StreamSource(resource.getInputStream()); } catch (IOException e) { throw new Error("Cannot load resource for schema \"" + schema + "\" at " + resource.getDescription() + resource.toString()); } JDOMResult res = new JDOMResult(); try { t.transform(schematron, res); } catch (TransformerException e) { throw new Error("Schematron issue: There were problems transforming Schematron to XSL.", e); } // compile templates object for each profile try { Templates schemaTemplates = factory.newTemplates(new JDOMSource(res.getDocument())); templates.put(schema, schemaTemplates); } catch (TransformerConfigurationException e) { throw new Error("There was a problem configuring the transformer.", e); } } }
From source file:mx.bigdata.sat.cfdi.CFDv33.java
byte[] getOriginalBytes(InputStream in) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Source source = new StreamSource(in); Result out = new StreamResult(baos); TransformerFactory factory = tf; if (factory == null) { factory = TransformerFactory.newInstance(); factory.setURIResolver(new URIResolverImpl()); }/* w w w . ja v a 2s . c o m*/ Transformer transformer = factory.newTransformer(new StreamSource(getClass().getResourceAsStream(XSLT))); transformer.transform(source, out); in.close(); return baos.toByteArray(); }