List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer(Source source) throws TransformerConfigurationException;
From source file:Main.java
static public Document createDocumentFromXMLContent(String docContent) throws SAXException, ParserConfigurationException, IOException, TransformerConfigurationException, TransformerException { // create builder DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); // create document Document doc = docBuilder.parse(new ByteArrayInputStream(docContent.getBytes())); // create transformer to remove spaces TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory .newTransformer(new StreamSource("src/test/resources/strip-spaces.xls")); // load doc/* w w w . j a v a 2 s . c o m*/ DOMSource source = new DOMSource(doc); OutputStream os = new ByteArrayOutputStream(); StreamResult result = new StreamResult(os); // remove spaces from doc transformer.transform(source, result); // re-create doc return docBuilder.parse(new ByteArrayInputStream(os.toString().getBytes())); }
From source file:Main.java
public static synchronized void deserialize(Source source, Result result, InputStream xsltSource) throws TransformerConfigurationException, JAXBException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer;//from ww w .j a v a 2 s . c o m transformer = factory.newTransformer(new StreamSource(xsltSource)); transformer.transform(source, result); }
From source file:Main.java
/** * General method for transformation to a DOM Document. Transform a Source * document using a Source XSL document 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 XML document to transform. * @param xsl the XSL transformation program. * @param params the array of transformation parameters. * @return the transformed text./* w w w.j ava 2 s . c om*/ */ public static Document getTransformedDocument(Source doc, Source xsl, Object[] params) throws Exception { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xsl); if ((params != null) && (params.length > 1)) { for (int i = 0; i < params.length; i = i + 2) { transformer.setParameter((String) params[i], params[i + 1]); } } DOMResult domResult = new DOMResult(); transformer.transform(doc, domResult); return (Document) domResult.getNode(); }
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 {/*w w w .j a v a 2 s . c o 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
/** * <p>Use this to grab things like the Plays title, or the characters in the play.</p> * <p>You will have to specify the XSLT file to do this</p> * * @param xmlFile the name and path of the xml file * @param xslFile the name and path of the xslt file *@param delim the delimiter you wish to split with. * @return A string delimited of the transformed document * @see MimeConstants// w ww . j a v a 2 s. c om */ public static String simpleTransformToStr(String xmlFile, String xslFile) { // -------------------------------------------------------------------- // http://www.oreillynet.com/pub/a/oreilly/java/news/javaxslt_0801.html // -------------------------------------------------------------------- String returnValue = ""; try { // JAXP reads data using the Source interface Source xmlSource = new StreamSource(xmlFile); Source xsltSource = new StreamSource(xslFile); // the factory pattern supports different XSLT processors TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans; trans = transFact.newTransformer(xsltSource); // ---------------------------------------------------------- // http://forum.java.sun.com/thread.jspa?threadID=636335&messageID=3709470 // Paul, you had alot of trouble figuring out how to pass the output of the transformation, the url above is hwere you got the following chunk // ---------------------------------------------------------- StringWriter output = new StringWriter(); trans.transform(xmlSource, new StreamResult(output)); return output.toString(); } catch (TransformerException ex) { ex.printStackTrace(); } return null; }
From source file:Main.java
/** * * @param source//ww w . j a v a2 s .c o m * @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
public static synchronized Object deserialize(Source source, InputStream xsltSource, Class cls) throws TransformerConfigurationException, JAXBException, TransformerException { Object obj = null;//from w ww .j a va2 s . c o m JAXBContext jc = JAXBContext.newInstance(cls); if (xsltSource != null) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); JAXBResult result = new JAXBResult(jc); transformer.transform(source, result); obj = result.getResult(); } else { obj = jc.createUnmarshaller().unmarshal(source); } return obj; }
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
/** * * @param source/*from w w w. j a v a 2s. co m*/ * @param xsltSource * @return * @throws TransformerConfigurationException * @throws JAXBException * @throws TransformerException * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static synchronized Document transform(Element source, InputStream xsltSource) throws TransformerConfigurationException, JAXBException, TransformerException, ParserConfigurationException, SAXException, IOException { Document obj = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); if (xsltSource != null) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); obj = dbf.newDocumentBuilder().newDocument(); Result result = new DOMResult(obj); transformer.transform(new DOMSource(source), result); } return obj; }
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 *//*from w w w .j a va 2s . c om*/ 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; } }