List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:Main.java
/** * Try to cache parsed Templates, but check for changes on disk * @param src/*from w w w .j a va2 s. com*/ * @return */ public static Templates getTemplates(File src) throws TransformerException { String key = src.getAbsolutePath(); if (XSL_MODIFIED.containsKey(key)) { // check to see if it has changed if (src.lastModified() <= XSL_MODIFIED.get(key).longValue()) { return TEMPLATES_CACHE.get(key); } } Templates template = getTransformerFactory().newTemplates(new StreamSource(src)); TEMPLATES_CACHE.put(key, template); XSL_MODIFIED.put(key, src.lastModified()); return template; }
From source file:Main.java
/** * Unmarshals an XML ({@link File}) to a bean. * //from w w w .ja v a2 s . c o m * @param file {@link File} * @param clazz {@link Class} * @return bean * @throws JAXBException * @throws IOException */ public static <T> T unmarshal(File file, Class<T> clazz) throws JAXBException, IOException { return unmarshal(new StreamSource(file), clazz); }
From source file:Main.java
public static Templates TrAXPath(String xpath) throws TransformerConfigurationException { StringBuffer sb = new StringBuffer(); // Create stylesheet that copies matching nodes sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); sb.append("<xsl:stylesheet version=\"1.0\" "); sb.append(" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"); sb.append("<xsl:output method=\"xml\" indent=\"yes\"/>"); sb.append("<xsl:template match=\"" + xpath + "\">"); sb.append("<xsl:copy-of select=\".\"/>"); sb.append("</xsl:template>"); sb.append("<xsl:template match=\"*|@*|text()\">"); sb.append("<xsl:apply-templates />"); sb.append("</xsl:template>"); sb.append("</xsl:stylesheet>"); // Construct the stylesheet Templates object. TransformerFactory tf = TransformerFactory.newInstance(); String stylesheet = sb.toString(); java.io.Reader r = new java.io.StringReader(stylesheet); StreamSource ssrc = new StreamSource(r); // Create templates object return tf.newTemplates(ssrc); }
From source file:Main.java
/** * @param sourceXML the XML file to transform * @param xslt the XSL stylesheet to use for transformation * @param targetXML the result of XSL transformation * @throws TransformerException if an error occurs configuraing or applying the transformation *//* w ww . j a va 2 s. co m*/ public static void transform(final File sourceXML, final File xslt, final File targetXML) throws TransformerException { // logger.debug("sourceXML = " + sourceXML); // logger.debug("xslt = " + xslt); // logger.debug("targetXML = " + targetXML); Transformer transformer = createTransformer(xslt, Collections.<String, String>emptyMap()); Source source = new StreamSource(sourceXML); Result result = new StreamResult(targetXML); transformer.transform(source, result); }
From source file:Main.java
/** * Transform an XML file using an XSL file and an array of parameters. * The parameter array consists of a sequence of pairs of (String parametername) * followed by (Object parametervalue) in an Object[]. * @param doc the document to transform. * @param xsl the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed DOM Document. *///w w w .ja v a 2 s . c o m public static Document getTransformedDocument(File doc, File xsl, Object[] params) throws Exception { return getTransformedDocument(new StreamSource(doc), new StreamSource(xsl), params); }
From source file:Main.java
public static void writeTransformedXml(Document doc, OutputStream output, InputStream style) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); StreamSource stylesource = new StreamSource(style); Transformer transformer = transformerFactory.newTransformer(stylesource); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(output); transformer.transform(source, result); }
From source file:Main.java
public static String toHTML(String xml) throws TransformerException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(baos); xmlToHTMLTransformer.transform(new StreamSource(new ByteArrayInputStream(xml.getBytes())), result); return baos.toString(); }
From source file:Main.java
public static void prettyFormatXml(final InputStream xml, final OutputStream os, final int indent) { try {/* w w w. j av a2s. co m*/ final Source xmlInput = new StreamSource(xml); final StreamResult xmlOutput = new StreamResult(os); final TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); final Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static XMLStreamReader createSafeReader(InputStream inputStream) throws XMLStreamException { if (inputStream == null) { throw new IllegalArgumentException("The provided input stream cannot be null"); }//from www . j a va 2 s . c om return createSafeReader(new StreamSource(inputStream)); }
From source file:Main.java
/** * This method validate XML by input XML as String and XSD File. * * @param xml input XML as String/*w w w.j av a2 s .c o m*/ * @param xsd input XSD File * * @return true or false, valid or not */ public static boolean validateXMLByXSD(String xml, File xsd) { try { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsd).newValidator() .validate(new StreamSource(new StringReader(xml))); } catch (Exception e) { e.printStackTrace(); return false; } return true; }