List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:Main.java
/** * Unmarshals an XML ({@link InputStream}) to a bean. * /*from w ww . j av a 2 s .com*/ * @param is {@link InputStream} * @param clazz {@link Class} * @return bean * @throws JAXBException */ public static <T> T unmarshal(InputStream is, Class<T> clazz) throws JAXBException { return unmarshal(new StreamSource(is), clazz); }
From source file:Main.java
/** * Executes an identity transform./*from w ww. j av a 2 s . c om*/ * @param xml the xml * @throws TransformerException if an exception occurs */ public static String identity(String xml) throws TransformerException { xml = removeBOM(xml); if (xml != null) xml = xml.trim(); if ((xml == null) || (xml.length() == 0)) { throw new TransformerException("Empty XML."); } StringWriter result = new StringWriter(); transform(new StreamSource(new StringReader(xml)), new StreamResult(result), false); return checkResult(result, false); }
From source file:Main.java
public static StringBuffer transformToString(Document document, String xslFileName) { return transformToString(new DOMSource(document), new StreamSource(new File(xslFileName))); }
From source file:Main.java
public static void saveDocument(Document doc, File transform, File file) throws IOException { try {//from w w w . j av a 2 s . c om TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(2)); Transformer transformer = tFactory.newTransformer(new StreamSource(transform)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream fos = new FileOutputStream(file.getPath()); DOMSource source = new DOMSource(doc); CharsetEncoder utf8Encoder = Charset.forName("UTF-8").newEncoder(); StreamResult result = new StreamResult(new OutputStreamWriter(fos, utf8Encoder)); transformer.transform(source, result); fos.close(); } catch (TransformerException e) { throw new IOException(e); } }
From source file:JAXPTransletMultipleTransformations.java
static void doTransform(Templates translet, String xmlInURI, String htmlOutURI) throws TransformerException, FileNotFoundException { // For each transformation, instantiate a new Transformer, and perform // the transformation from a StreamSource to a StreamResult; Transformer transformer = translet.newTransformer(); transformer.transform(new StreamSource(xmlInURI), new StreamResult(new FileOutputStream(htmlOutURI))); }
From source file:jlib.xml.XMLUtil.java
public static Document LoadXML(File f) { Source file = new StreamSource(f); return LoadXML(file); }
From source file:Main.java
public static Templates TrAXPath(String xpath) throws TransformerConfigurationException { StringBuffer sb = new StringBuffer(); 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>"); TransformerFactory tf = TransformerFactory.newInstance(); String stylesheet = sb.toString(); Reader r = new StringReader(stylesheet); StreamSource ssrc = new StreamSource(r); return tf.newTemplates(ssrc); }
From source file:Main.java
/** * @param xsltFile The XSLT stylesheet file * @param parms XSL parameters to pass to the stylesheet. * @return the configured XSLT transformer * @throws TransformerException if there is a problem configuring the transformer *//* w ww . ja v a 2 s.c o m*/ static Transformer createTransformer(final File xsltFile, final Map<String, String> parms) throws TransformerException { try { Source xslSource = new StreamSource(xsltFile); TransformerFactory transFact = TransformerFactory.newInstance(); transFact.setAttribute("indent-number", 2); Transformer transformer = transFact.newTransformer(xslSource); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); for (String parm : parms.keySet()) { transformer.setParameter(parm, parms.get(parm)); } return transformer; } catch (TransformerConfigurationException e) { e.printStackTrace(); throw e; } }
From source file:Main.java
/** * * @param source//from w w w . ja va 2 s . c om * @param xsltSource * @return * @throws TransformerConfigurationException * @throws JAXBException * @throws TransformerException */ public static synchronized String getElementValue(InputStream source, InputStream xsltSource) throws TransformerConfigurationException, JAXBException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(new StreamSource(source), result); return sw.toString(); }
From source file:Main.java
/** * Check a xml file against a schema.//ww w. ja va 2s . c o m * * @param fileToCheck * @param schemURL * @throws Exception */ static public void schemaCheck(File fileToCheck, URL schemURL) throws Exception { final String W3C_SCHEMA_SPEC = "http://www.w3.org/2001/XMLSchema"; HashMap<String, Schema> MetsSchema = null; if (MetsSchema == null) { MetsSchema = new HashMap(); } if (MetsSchema.get(schemURL.toString()) == null) { // 1. Lookup a factory for the W3C XML Schema language SchemaFactory factory = SchemaFactory.newInstance(W3C_SCHEMA_SPEC); // 2. Compile the schema. MetsSchema.put(schemURL.toString(), factory.newSchema(schemURL)); } // 3. Get a validator from the schema. Validator validator = MetsSchema.get(schemURL.toString()).newValidator(); // 4. Parse the document you want to check. Source source = new StreamSource(fileToCheck); // 5. Check the document validator.validate(source); }