/** * Converts a .docx document into HTML markup. This code * is based on <a href="http://stackoverflow.com/a/9053258/313554">this StackOverflow</a> answer. * * @param wordDocument The converted .docx document. * @return */ public ConvertedDocumentDTO convertWordDocumentIntoHtml(MultipartFile wordDocument) { LOGGER.info("Converting word document: {} into HTML", wordDocument.getOriginalFilename()); try { InputStream input = wordDocument.getInputStream(); Parser parser = new OOXMLParser(); StringWriter sw = new StringWriter(); SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler handler = factory.newTransformerHandler(); handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "utf-8"); handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html"); handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes"); handler.setResult(new StreamResult(sw)); Metadata metadata = new Metadata(); metadata.add(Metadata.CONTENT_TYPE, "text/html;charset=utf-8"); parser.parse(input, handler, metadata, new ParseContext()); return new ConvertedDocumentDTO(wordDocument.getOriginalFilename(), sw.toString()); } catch (IOException | SAXException | TransformerException | TikaException ex) { LOGGER.error("Conversion failed because an exception was thrown", ex); throw new DocumentConversionException(ex.getMessage(), ex); } }
public void xsltprocess(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { // 1. Instantiate a TransformerFactory. SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); // 2. Use the TransformerFactory to process the stylesheet Source and // generate a Transformer. InputStream is = getClass().getResourceAsStream("xmg2pol.xsl"); Transformer transformer = tFactory.newTransformer (new StreamSource(is)); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "polarities.dtd,xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); // 3. Use the Transformer to transform an XML Source and send the // output to a Result object. try { String input = args[0]; String output= args[1]; SAXSource saxs = new SAXSource(new InputSource(input)); XMLReader saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); saxReader.setEntityResolver(new MyEntityResolver()); saxs.setXMLReader(saxReader); transformer.transform(saxs, new StreamResult(new OutputStreamWriter(new FileOutputStream(output), "utf-8"))); } catch (Exception e) { e.printStackTrace(); } }
/** * Saves the xml, contained by the specified input with the custom indentation. * If the input is the result of jaxb marshalling, make sure to set * Marshaller.JAXB_FORMATTED_OUTPUT to false in order for this method to work * properly. * * @param input * @param fos * @param indentation */ public static void saveWithCustomIndetation(ByteArrayInputStream input, FileOutputStream fos, int indentation) { try { Transformer transformer = SAXTransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentation)); Source xmlSource = new SAXSource(new org.xml.sax.InputSource(input)); StreamResult res = new StreamResult(fos); transformer.transform(xmlSource, res); fos.flush(); fos.close(); } catch (TransformerFactoryConfigurationError | TransformerException | IOException e) { log.log(Level.SEVERE, e.getMessage(), e); } }
public static TransformerHandler createTransformerHandler(final ErrorListener errorListener, final boolean indentOutput) throws TransformerConfigurationException { final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactoryFactory.newInstance(); if (errorListener != null) { stf.setErrorListener(errorListener); } final TransformerHandler th = stf.newTransformerHandler(); final Transformer transformer = th.getTransformer(); setCommonOutputProperties(transformer, indentOutput); if (errorListener != null) { transformer.setErrorListener(errorListener); } return th; }
@Override public TransformerHandler getSerializer() { try { SAXTransformerFactory transformerFactory = buildTransformerFactory(); configure(transformerFactory); TransformerHandler transformer = buildTransformer(transformerFactory); configure(transformer.getTransformer()); return transformer; } catch (TransformerConfigurationException ex) { throw new FluentXmlConfigurationException(ex); } }
private Result buildSingleTransformerPipeline(Result result) { try { SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler(); if (result != null) { transformerHandler.setResult(result); } return new SAXResult(transformerHandler); } catch (TransformerConfigurationException ex) { throw new FluentXmlConfigurationException(ex); } }
private SAXResult toSAXResult(Result result) { if (result instanceof SAXResult) { return (SAXResult) result; } try { SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler transformerHandler = transformerFactory.newTransformerHandler(); transformerHandler.setResult(result); return new SAXResult(transformerHandler); } catch (TransformerConfigurationException ex) { throw new FluentXmlConfigurationException(ex); } }
public SchemaToXML(Schema schema, StreamResult streamResult) { final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance(); try { xmlOut = stf.newTransformerHandler(); } catch (TransformerConfigurationException error) { throw new RuntimeException("Unable to create TransformerHandler.", error); } final Transformer t = xmlOut.getTransformer(); try { t.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); } catch (final IllegalArgumentException e) { // It was worth a try } t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.ENCODING, SchemaComparator.CHAR_SET); xmlOut.setResult(streamResult); this.schema = schema; }
@Override public String format(String response) { try { Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(response.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()).trim(); } catch (Exception e) { e.printStackTrace(); Logger.e(TAG, e.getMessage(), e); return response; } }
/** * Performs identity transformation. */ public static <T extends Result> T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException { if (src instanceof StreamSource) { // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing // is not turned on by default StreamSource ssrc = (StreamSource) src; TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler(); th.setResult(result); XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader(); reader.setContentHandler(th); reader.setProperty(LEXICAL_HANDLER_PROPERTY, th); reader.parse(toInputSource(ssrc)); } else { newTransformer().transform(src, result); } return result; }
/** * Performs identity transformation. * @param <T> * @param src * @param result * @return * @throws javax.xml.transform.TransformerException * @throws java.io.IOException * @throws org.xml.sax.SAXException * @throws javax.xml.parsers.ParserConfigurationException */ public static <T extends Result> T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException { if (src instanceof StreamSource) { // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing // is not turned on by default StreamSource ssrc = (StreamSource) src; TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler(); th.setResult(result); XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader(); reader.setContentHandler(th); reader.setProperty(LEXICAL_HANDLER_PROPERTY, th); reader.parse(toInputSource(ssrc)); } else { newTransformer().transform(src, result); } return result; }
@Test public void test3() throws Exception { SAXTransformerFactory sf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); Transformer t = sf.newTransformer(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder parser = dbf.newDocumentBuilder(); Document dom = parser.parse(Bug5072946.class.getResourceAsStream("Bug5072946.xml")); DOMResult r = new DOMResult(); t.transform(new DOMSource(dom), r); Assert.assertNotNull(r.getNode()); Node n = r.getNode().getFirstChild(); r.setNode(n); t.transform(new DOMSource(dom), r); Assert.assertNotNull(r.getNode()); Assert.assertSame(r.getNode(), n); r.setNextSibling(r.getNode().getFirstChild()); t.transform(new DOMSource(dom), r); Assert.assertNotNull(r.getNode()); Assert.assertSame(r.getNode(), n); }
@Test public void test03() { String xsl = "<?xml version='1.0'?>\n" + "<xsl:stylesheet" + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" + " version='1.0'>\n" + " <xsl:template match='/'>Hello World!</xsl:template>\n" + "</xsl:stylesheet>\n"; ReaderStub.used = false; setSystemProperty("org.xml.sax.driver", ReaderStub.class.getName()); try { TransformerFactory transFactory = TransformerFactory.newInstance(); if (transFactory.getFeature(SAXTransformerFactory.FEATURE) == false) { System.out.println("SAXTransformerFactory not supported"); } InputSource in = new InputSource(new StringReader(xsl)); SAXSource source = new SAXSource(in); transFactory.newTransformer(source); Assert.assertTrue(printWasReaderStubCreated()); } catch (TransformerException e) { Assert.fail(e.getMessage()); } }
@Test public final void testXMLStackOverflowBug() throws TransformerConfigurationException, IOException, SAXException { try { SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler ser = stf.newTransformerHandler(); ser.setResult(new StreamResult(System.out)); StringBuilder sb = new StringBuilder(4096); for (int x = 4096; x > 0; x--) { sb.append((char) x); } ser.characters(sb.toString().toCharArray(), 0, sb.toString().toCharArray().length); ser.endDocument(); } catch (StackOverflowError se) { se.printStackTrace(); Assert.fail("StackOverflow"); } }
@Test public void test() throws Exception { TransformerHandler th = ((SAXTransformerFactory) TransformerFactory.newInstance()).newTransformerHandler(); DOMResult result = new DOMResult(); th.setResult(result); th.startDocument(); th.startElement("", "root", "root", new AttributesImpl()); th.characters(new char[0], 0, 0); th.endElement("", "root", "root"); th.endDocument(); // there's no point in having empty text --- we should remove it Assert.assertEquals(0, ((Document) result.getNode()).getDocumentElement().getChildNodes().getLength()); }
/** * SAXTFactory.newTransformerhandler() method which takes SAXSource as * argument can be set to XMLReader. SAXSource has input XML file as its * input source. XMLReader has a transformer handler which write out the * result to output file. Test verifies output file is same as golden file. * * @throws Exception If any errors occur. */ @Test public void testcase01() throws Exception { String outputFile = USER_DIR + "saxtf001.out"; String goldFile = GOLDEN_DIR + "saxtf001GF.out"; try (FileOutputStream fos = new FileOutputStream(outputFile)) { XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler handler = saxTFactory.newTransformerHandler(new StreamSource(XSLT_FILE)); Result result = new StreamResult(fos); handler.setResult(result); reader.setContentHandler(handler); reader.parse(XML_FILE); } assertTrue(compareWithGold(goldFile, outputFile)); }
/** * SAXTFactory.newTransformerhandler() method which takes SAXSource as * argument can be set to XMLReader. SAXSource has input XML file as its * input source. XMLReader has a content handler which write out the result * to output file. Test verifies output file is same as golden file. * * @throws Exception If any errors occur. */ @Test public void testcase02() throws Exception { String outputFile = USER_DIR + "saxtf002.out"; String goldFile = GOLDEN_DIR + "saxtf002GF.out"; try (FileOutputStream fos = new FileOutputStream(outputFile); FileInputStream fis = new FileInputStream(XSLT_FILE)) { XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); SAXSource ss = new SAXSource(); ss.setInputSource(new InputSource(fis)); TransformerHandler handler = saxTFactory.newTransformerHandler(ss); Result result = new StreamResult(fos); handler.setResult(result); reader.setContentHandler(handler); reader.parse(XML_FILE); } assertTrue(compareWithGold(goldFile, outputFile)); }
/** * Unit test for newTransformerhandler(Source). DcoumentBuilderFactory is * namespace awareness, DocumentBuilder parse xslt file as DOMSource. * * @throws Exception If any errors occur. */ @Test public void testcase03() throws Exception { String outputFile = USER_DIR + "saxtf003.out"; String goldFile = GOLDEN_DIR + "saxtf003GF.out"; try (FileOutputStream fos = new FileOutputStream(outputFile)) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document document = docBuilder.parse(new File(XSLT_FILE)); Node node = (Node)document; DOMSource domSource= new DOMSource(node); XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); TransformerHandler handler = saxTFactory.newTransformerHandler(domSource); Result result = new StreamResult(fos); handler.setResult(result); reader.setContentHandler(handler); reader.parse(XML_FILE); } assertTrue(compareWithGold(goldFile, outputFile)); }
/** * Test newTransformerHandler with a Template Handler. * * @throws Exception If any errors occur. */ public void testcase08() throws Exception { String outputFile = USER_DIR + "saxtf008.out"; String goldFile = GOLDEN_DIR + "saxtf008GF.out"; try (FileOutputStream fos = new FileOutputStream(outputFile)) { XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); reader.setContentHandler(thandler); reader.parse(XSLT_FILE); TransformerHandler tfhandler = saxTFactory.newTransformerHandler(thandler.getTemplates()); Result result = new StreamResult(fos); tfhandler.setResult(result); reader.setContentHandler(tfhandler); reader.parse(XML_FILE); } assertTrue(compareWithGold(goldFile, outputFile)); }
/** * Test newTransformerHandler with a Template Handler along with a relative * URI in the style-sheet file. * * @throws Exception If any errors occur. */ @Test public void testcase09() throws Exception { String outputFile = USER_DIR + "saxtf009.out"; String goldFile = GOLDEN_DIR + "saxtf009GF.out"; try (FileOutputStream fos = new FileOutputStream(outputFile)) { XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); thandler.setSystemId("file:///" + XML_DIR); reader.setContentHandler(thandler); reader.parse(XSLT_INCL_FILE); TransformerHandler tfhandler= saxTFactory.newTransformerHandler(thandler.getTemplates()); Result result = new StreamResult(fos); tfhandler.setResult(result); reader.setContentHandler(tfhandler); reader.parse(XML_FILE); } assertTrue(compareWithGold(goldFile, outputFile)); }
/** * Unit test for contentHandler setter/getter along reader as handler's * parent. * * @throws Exception If any errors occur. */ @Test public void testcase10() throws Exception { String outputFile = USER_DIR + "saxtf010.out"; String goldFile = GOLDEN_DIR + "saxtf010GF.out"; // The transformer will use a SAX parser as it's reader. XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); XMLFilter filter = saxTFactory.newXMLFilter(new StreamSource(XSLT_FILE)); filter.setParent(reader); filter.setContentHandler(new MyContentHandler(outputFile)); // Now, when you call transformer.parse, it will set itself as // the content handler for the parser object (it's "parent"), and // will then call the parse method on the parser. filter.parse(new InputSource(XML_FILE)); assertTrue(compareWithGold(goldFile, outputFile)); }
/** * Unit test for contentHandler setter/getter. * * @throws Exception If any errors occur. */ @Test public void testcase12() throws Exception { String outputFile = USER_DIR + "saxtf012.out"; String goldFile = GOLDEN_DIR + "saxtf012GF.out"; // The transformer will use a SAX parser as it's reader. XMLReader reader = XMLReaderFactory.createXMLReader(); InputSource is = new InputSource(new FileInputStream(XSLT_FILE)); SAXSource saxSource = new SAXSource(); saxSource.setInputSource(is); SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); XMLFilter filter = saxTFactory.newXMLFilter(saxSource); filter.setParent(reader); filter.setContentHandler(new MyContentHandler(outputFile)); // Now, when you call transformer.parse, it will set itself as // the content handler for the parser object (it's "parent"), and // will then call the parse method on the parser. filter.parse(new InputSource(XML_FILE)); assertTrue(compareWithGold(goldFile, outputFile)); }
/** * Unit test for TemplatesHandler setter/getter. * * @throws Exception If any errors occur. */ @Test public void testcase13() throws Exception { String outputFile = USER_DIR + "saxtf013.out"; String goldFile = GOLDEN_DIR + "saxtf013GF.out"; try(FileInputStream fis = new FileInputStream(XML_FILE)) { // The transformer will use a SAX parser as it's reader. XMLReader reader = XMLReaderFactory.createXMLReader(); SAXTransformerFactory saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); TemplatesHandler thandler = saxTFactory.newTemplatesHandler(); // I have put this as it was complaining about systemid thandler.setSystemId("file:///" + USER_DIR); reader.setContentHandler(thandler); reader.parse(XSLT_FILE); XMLFilter filter = saxTFactory.newXMLFilter(thandler.getTemplates()); filter.setParent(reader); filter.setContentHandler(new MyContentHandler(outputFile)); filter.parse(new InputSource(fis)); } assertTrue(compareWithGold(goldFile, outputFile)); }
@Test public void testSAXTransformerFactory() throws TransformerConfigurationException { final String xsl = "<?xml version='1.0'?>\n" + "<xsl:stylesheet" + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" + " version='1.0'>\n" + " <xsl:template match='/'>Hello World!</xsl:template>\n" + "</xsl:stylesheet>\n"; ReaderStub.used = false; TransformerFactory transFactory = TransformerFactory.newInstance(); assertTrue(transFactory.getFeature(SAXTransformerFactory.FEATURE)); InputSource in = new InputSource(new StringReader(xsl)); SAXSource source = new SAXSource(in); transFactory.newTransformer(source); assertTrue(ReaderStub.used); }
@Override protected TransformerHandler getTransformerHandler(String xslFileName) throws SAXException, ParserConfigurationException, TransformerConfigurationException, IOException { SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance(); factory.setURIResolver(uriResolver); TemplatesHandler templatesHandler = factory.newTemplatesHandler(); SAXParserFactory pFactory = SAXParserFactory.newInstance(); pFactory.setNamespaceAware(true); XMLReader xmlreader = pFactory.newSAXParser().getXMLReader(); // create the stylesheet input source InputSource xslSrc = new InputSource(xslFileName); xslSrc.setSystemId(filenameToURL(xslFileName)); // hook up the templates handler as the xsl content handler xmlreader.setContentHandler(templatesHandler); // call parse on the xsl input source xmlreader.parse(xslSrc); // extract the Templates object created from the xsl input source return factory.newTransformerHandler(templatesHandler.getTemplates()); }
@Test public void test03() { String xsl = "<?xml version='1.0'?>\n" + "<xsl:stylesheet" + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" + " version='1.0'>\n" + " <xsl:template match='/'>Hello World!</xsl:template>\n" + "</xsl:stylesheet>\n"; ReaderStub.used = false; System.setProperty("org.xml.sax.driver", ReaderStub.class.getName()); try { TransformerFactory transFactory = TransformerFactory.newInstance(); if (transFactory.getFeature(SAXTransformerFactory.FEATURE) == false) { System.out.println("SAXTransformerFactory not supported"); } InputSource in = new InputSource(new StringReader(xsl)); SAXSource source = new SAXSource(in); transFactory.newTransformer(source); Assert.assertTrue(printWasReaderStubCreated()); } catch (TransformerException e) { Assert.fail(e.getMessage()); } }