List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
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 text.//from w w w . j a v a 2 s . c om */ public static String getTransformedText(File doc, File xsl, Object[] params) throws Exception { return getTransformedText(new StreamSource(doc), new StreamSource(xsl), params); }
From source file:Main.java
public static void saveDomSource(final DOMSource source, final File target, final File xslt) throws TransformerException, IOException { TransformerFactory transFact = TransformerFactory.newInstance(); transFact.setAttribute("indent-number", 2); Transformer trans;//from w w w.j a v a2s .c o m if (xslt == null) { trans = transFact.newTransformer(); } else { trans = transFact.newTransformer(new StreamSource(xslt)); } trans.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream fos = new FileOutputStream(target); trans.transform(source, new StreamResult(new OutputStreamWriter(fos, "UTF-8"))); fos.close(); }
From source file:Main.java
/** * This method will apply an XSLT transformation * @param source the source reader// ww w . j a v a 2s . c om * @param result the target writter * @param style the stylesheet to be applied * @throws TransformerException */ static void transform(Reader source, Writer result, String style) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(new StringReader(style))); transformer.transform(new StreamSource(source), new StreamResult(result)); }
From source file:Main.java
/** * Unmarshal XML data from XML DOM document using XSD string and return the resulting JAXB content tree * * @param dummyJAXBObject//from w w w. jav a2 s. c o m * Dummy contect object for creating related JAXB context * @param doc * XML DOM document * @param strXSD * XSD * @return resulting JAXB content tree * @throws Exception * in error case */ public static Object doUnmarshallingFromDOMDocument(Object dummyJAXBObject, Document doc, String strXSD) throws Exception { if (dummyJAXBObject == null) { throw new RuntimeException("No dummy context objekt (null)!"); } if (doc == null) { throw new RuntimeException("No XML DOM document (null)!"); } if (strXSD == null) { throw new RuntimeException("No XSD document (null)!"); } Object unmarshalledObject = null; StringReader reader = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(dummyJAXBObject.getClass().getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); reader = new StringReader(strXSD); javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(reader)); unmarshaller.setSchema(schema); unmarshalledObject = unmarshaller.unmarshal(doc); } catch (Exception e) { // Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (reader != null) { reader.close(); reader = null; } } return unmarshalledObject; }
From source file:com.github.woozoo73.ht.XmlUtils.java
public static boolean validate(String xsd, String xml) { try {//from w ww .j a v a 2 s . co m SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = factory.newSchema(new StreamSource(new ByteArrayInputStream(xsd.getBytes("UTF-8")))); Validator validator = schema.newValidator(); boolean valid = false; try { validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8")))); valid = true; } catch (Exception e) { logger.warn(e.getMessage(), e); } return valid; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static StringBuffer transformToString(String xmlFileName, String xslFileName) { return transformToString(new StreamSource(new File(xmlFileName)), new StreamSource(new File(xslFileName))); }
From source file:Main.java
/** * Unmarshal XML data from XML string using XSD string and return the resulting JAXB content tree * * @param dummyCtxObject// w w w. j a v a 2 s. c o m * Dummy contect object for creating related JAXB context * @param strXML * XML * @param strXSD * XSD * @return resulting JAXB content tree * @throws Exception * in error case */ public static Object doUnmarshalling(Object dummyCtxObject, String strXML, String strXSD) throws Exception { if (dummyCtxObject == null) { throw new RuntimeException("No dummy context objekt (null)!"); } if (strXML == null) { throw new RuntimeException("No XML document (null)!"); } if (strXSD == null) { throw new RuntimeException("No XSD document (null)!"); } Object unmarshalledObject = null; StringReader readerXSD = null; StringReader readerXML = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); readerXSD = new StringReader(strXSD); readerXML = new StringReader(strXML); javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource(readerXSD)); unmarshaller.setSchema(schema); unmarshalledObject = unmarshaller.unmarshal(new StreamSource(readerXML)); } catch (Exception e) { // Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (readerXSD != null) { readerXSD.close(); readerXSD = null; } if (readerXML != null) { readerXML.close(); readerXML = null; } } return unmarshalledObject; }
From source file:gov.nih.cadsr.transform.FilesTransformation.java
public static String transformCdeToCSV(String xmlFile) { StringBuffer sb = null;/*from w w w.j av a2 s. c om*/ try { File tf = new File("/local/content/cadsrapi/transform/xslt/", "cdebrowser.xslt"); // template file String path = "/local/content/cadsrapi/transform/data/"; String ext = "txt"; File dir = new File(path); String name = String.format("%s.%s", RandomStringUtils.randomAlphanumeric(8), ext); File rf = new File(dir, name); if (tf == null || !tf.exists() || !tf.canRead()) { System.out.println("File path incorrect"); return ""; } long startTime = System.currentTimeMillis(); //Obtain a new instance of a TransformerFactory. TransformerFactory f = TransformerFactory.newInstance(); // Process the Source into a Transformer Object...Construct a StreamSource from a File. Transformer t = f.newTransformer(new StreamSource(tf)); /* Source s = new StreamSource(xmlFile); Result r = new StreamResult(rf); //Transform the XML Source to a Result. t.transform(s,r); System.out.println("Tranformation completed ..."); */ //Construct a StreamSource from input and output Source s; try { s = new StreamSource((new ByteArrayInputStream(xmlFile.getBytes("utf-8")))); Result r = new StreamResult(rf); //Transform the XML Source to a Result. t.transform(s, r); System.out.println("Tranformation completed ..."); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block System.out.println(e1.toString()); } try { BufferedReader bf = new BufferedReader(new FileReader(rf)); sb = new StringBuffer(); try { String currentLine; while ((currentLine = bf.readLine()) != null) { sb.append(currentLine).append("\n"); //System.out.println(bf.readLine()); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); System.out.println("Transformation took " + (endTime - startTime) + " milliseconds"); System.out.println("Transformation took " + (endTime - startTime) / 1000 + " seconds"); } catch (TransformerConfigurationException e) { System.out.println(e.toString()); e.printStackTrace(); } catch (TransformerException e) { System.out.println(e.toString()); } return sb.toString(); }
From source file:Main.java
/** * Transforms an xml-file (xmlFilename) using an xsl-file (xslFilename) and * writes the output into file (outputFilename). * /*from w ww.j a va 2s. c om*/ * @param xmlFile * File: the xml-source-file to be transformed * @param xslFile * File: the xsl-file with the transformation rules * @param outputFilename * String: the name of the file the result will be written to */ public static void applyXSL(File xmlFile, File xslFile, String outputFilename) { try { // DocumentBuilderFactory docBFactory = DocumentBuilderFactory // .newInstance(); // DocumentBuilder docBuilder = docBFactory.newDocumentBuilder(); StreamSource xslStream = new StreamSource(xslFile); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(xslStream); StreamSource xmlStream = new StreamSource(xmlFile); StreamResult result = new StreamResult(new BufferedWriter(new FileWriter(outputFilename))); transformer.transform(xmlStream, result); result.getWriter().close(); } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:Main.java
public static String indent(String xml) throws TransformerException { xml = removeBOM(xml);//w w w .ja v a2s . co m 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), true); return checkResult(result, true); }