List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:Main.java
public static boolean validate(URL schemaFile, String xmlString) { boolean success = false; try {//from w w w. j av a2 s . co m SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(schemaFile); Validator validator = schema.newValidator(); Source xmlSource = null; try { xmlSource = new StreamSource(new java.io.StringReader(xmlString)); validator.validate(xmlSource); System.out.println("Congratulations, the document is valid"); success = true; } catch (SAXException e) { e.printStackTrace(); System.out.println(xmlSource.getSystemId() + " is NOT valid"); System.out.println("Reason: " + e.getLocalizedMessage()); } } catch (Exception e) { e.printStackTrace(); } return success; }
From source file:Main.java
protected static Result internalTransformWithParams(Reader doc, Templates templates, Result r, boolean trace, String[] params) {/* w w w .jav a2s .co m*/ StringWriter sw = new StringWriter(); try { Transformer transformer = templates.newTransformer(); if (params != null && params.length > 0) { for (int i = 0; i < params.length; i++) transformer.setParameter("param_" + i, params[i]); } transformer.transform(new StreamSource(doc), r); sw.close(); return r; } catch (Throwable th) { th.printStackTrace(); return r; } }
From source file:Main.java
public static String getXmlPage(String xmlString, int page, String xslPath) { String styledResponse = ""; StringReader rd = new StringReader(xmlString); StringWriter wrt = new StringWriter(); TransformerFactory tFac = TransformerFactory.newInstance(); try {/*from w w w .j a v a 2 s. co m*/ File xsl = new File(xslPath); Transformer transformer = tFac.newTransformer(new StreamSource(xsl)); transformer.setParameter("Page", String.format("%s", page)); transformer.transform(new StreamSource(rd), new StreamResult(wrt)); styledResponse = wrt.toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return styledResponse; }
From source file:Main.java
public static String tranform(String xslSource, String original) { StringReader sr = new StringReader(xslSource); StringReader sro = new StringReader(original); StringWriter result = new StringWriter(); doTransform(new StreamSource(sr), new StreamSource(sro), new StreamResult(result)); return result.toString(); }
From source file:Main.java
public static String prettyPrintXML(String source) { try {/*from w ww. j a va2 s. c o m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); TransformerFactory tff = TransformerFactory.newInstance(); Transformer tf; tf = tff.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(baos); tf.transform(new StreamSource(new StringReader(source)), result); return new String(baos.toByteArray()); } catch (Exception e) { return source; } }
From source file:Main.java
/** * Pretty format a given XML document//from www. j a va2 s. c o m * * @param strInput * Valid XML document (No validity check yet!) * @param nIndent * Indent * @return Formatted XML document * @throws Exception * in error case */ public static String prettyFormat(String strInput, int nIndent) throws Exception { try { Source xmlInput = new StreamSource(new StringReader(strInput)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", nIndent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(nIndent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { // Logger.XMLEval.logState("Pretty formatting: " + e.getMessage(), LogLevel.Error); throw e; } }
From source file:Main.java
public static String transform(InputStream xsltStream, InputStream xmlStream, String charset) throws TransformerException { return transform(new StreamSource(xsltStream), new StreamSource(xmlStream), charset); }
From source file:Main.java
public static <T> T converyToJavaBean(JAXBContext context, String xmlStr, Class<T> c) { JAXBElement<T> t = null; try {/*from www . j a v a2 s .c o m*/ Unmarshaller unmarshaller = context.createUnmarshaller(); t = (JAXBElement<T>) unmarshaller.unmarshal((new StreamSource(new StringReader(xmlStr))), c); } catch (Exception e) { e.printStackTrace(); } return t.getValue(); }
From source file:Main.java
public static Document xslt(InputStream stylesheet, Document input) throws FileNotFoundException, TransformerException, ParserConfigurationException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet)); Document result = newDocument(); DOMResult domResult = new DOMResult(result); transformer.transform(new DOMSource(input), domResult); return result; }
From source file:Main.java
public static boolean xmlStringValidate(String xmlStr, String xsdPath) { boolean flag = false; try {/*from w ww. j a va 2s. c o m*/ SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG); File schemaLocation = new File(xsdPath); Schema schema = factory.newSchema(schemaLocation); Validator validator = schema.newValidator(); InputStream is = new ByteArrayInputStream(xmlStr.getBytes()); Source source = new StreamSource(is); try { validator.validate(source); flag = true; } catch (SAXException ex) { System.out.println(ex.getMessage()); } } catch (Exception e) { e.printStackTrace(); } return flag; }