public String toString(final Reflections reflections) { Document document = createDocument(reflections); try { StringWriter writer = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint()); xmlWriter.write(document); xmlWriter.close(); return writer.toString(); } catch (IOException e) { throw new RuntimeException(); } }
public void close() { try { writer.endDocument(); writer.flush(); } catch (SAXException e) { throw new StreamException(e); } catch (IOException e) { throw new StreamException(e); } }
public void endNode() { try { if (!children) { Element element = new DefaultElement((String)elementStack.pop()); for (int i = 0; i < attributes.getLength(); ++i) { element.addAttribute(attributes.getQName(i), attributes.getValue(i)); } writer.write(element); attributes.clear(); children = true; // node just closed is child of node on top of stack started = true; } else { startElement(); writer.endElement("", "", (String)elementStack.pop()); } } catch (SAXException e) { throw new StreamException(e); } catch (IOException e) { throw new StreamException(e); } }
private void writeXML(Writer out, Project project) throws IOException { Document document = endDocument(project); XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); writer.write(document); }
private static void dump(Document document) { if ( !log.isTraceEnabled() ) { return; } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Writer w = new PrintWriter( baos ); try { final XMLWriter xw = new XMLWriter( w, new OutputFormat( " ", true ) ); xw.write( document ); w.flush(); } catch (IOException e1) { throw new RuntimeException( "Error dumping enhanced class", e1 ); } log.tracef( "Envers-generate entity mapping -----------------------------\n%s", baos.toString() ); log.trace( "------------------------------------------------------------" ); } }
public void testPenguin() throws IOException { // U+1F427 PENGUIN final String penguin = "\ud83d\udc27"; Document document = DocumentHelper.createDocument(); document.addElement("doc").setText(penguin); OutputFormat outputFormat = OutputFormat.createCompactFormat(); outputFormat.setSuppressDeclaration(true); StringWriter stringWriter = new StringWriter(); XMLWriter writer = new XMLWriter(stringWriter, outputFormat); writer.write(document); writer.close(); Assert.assertEquals(stringWriter.toString(), "<doc>"+penguin+"</doc>"); }
public String asXML() { try { StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, new OutputFormat()); writer.write(this); writer.flush(); return out.toString(); } catch (IOException e) { throw new RuntimeException("IOException while generating " + "textual representation: " + e.getMessage()); } }
public void testRussian() throws Exception { Document doc = getDocument("/xml/russArticle.xml"); assertEquals("encoding not correct", "koi8-r", doc.getXMLEncoding()); Element el = doc.getRootElement(); StringWriter writer = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(writer); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("koi8-r"); xmlWriter.write(doc); log(writer.toString()); }
@Override public String buildXml(Report report) { try { Document document = DocumentFactory.getInstance().createDocument(); Element root = document.addElement("report"); root.addAttribute("name", report.getName()); writeTemplates(report, root); writeInputParameters(report, root); writeValueFormats(report, root); writeRootBand(report, root); StringWriter stringWriter = new StringWriter(); new XMLWriter(stringWriter, OutputFormat.createPrettyPrint()).write(document); return stringWriter.toString(); } catch (IOException e) { throw new ReportingException(e); } }
public String prettyPrint(final String xml){ if (StringUtils.isBlank(xml)) { throw new RuntimeException("xml was null or blank in prettyPrint()"); } final StringWriter sw; try { final OutputFormat format = OutputFormat.createPrettyPrint(); final org.dom4j.Document document = DocumentHelper.parseText(xml); sw = new StringWriter(); final XMLWriter writer = new XMLWriter(sw, format); writer.write(document); } catch (Exception e) { throw new RuntimeException("Error pretty printing xml:\n" + xml, e); } return sw.toString(); }
StringWriter sw = new StringWriter(); Object result; try { Document dom = new SAXReader().read(new StringReader(sw.toString())); XPath xExclude = dom.createXPath(exclude); xExclude.setFunctionContext(functionContext); List<org.dom4j.Node> list = (List<org.dom4j.Node>)xExclude.selectNodes(dom); Element parent = n.getParent(); if(parent!=null) parent.remove(n); result = dom; } else { XPath comp = dom.createXPath(xpath); comp.setFunctionContext(functionContext); List list = comp.selectNodes(dom); for (Object o : list) { if (o instanceof String) { root.addText(o.toString()); } else { root.add(((org.dom4j.Node)o).detach()); new XMLWriter(o).write(result);
public void testNamespaceBug() throws Exception { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("root", "ns1"); Element child1 = root.addElement("joe", "ns2"); child1.addElement("zot", "ns1"); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); writer.write(doc); String text = out.toString(); // System.out.println( "Generated:" + text ); Document doc2 = DocumentHelper.parseText(text); root = doc2.getRootElement(); assertEquals("root has incorrect namespace", "ns1", root .getNamespaceURI()); Element joe = (Element) root.elementIterator().next(); assertEquals("joe has correct namespace", "ns2", joe.getNamespaceURI()); Element zot = (Element) joe.elementIterator().next(); assertEquals("zot has correct namespace", "ns1", zot.getNamespaceURI()); }
public void testXmlWriter() throws Exception { Element elem = DocumentHelper.createElement("elem"); Document doc = DocumentHelper.createDocument(elem); elem.addCDATA(EXPECTED_TEXT); StringWriter sw = new StringWriter(); XMLWriter xWriter = new XMLWriter(sw, OutputFormat.createPrettyPrint()); xWriter.write(doc); xWriter.close(); String xmlString = sw.toString(); doc = DocumentHelper.parseText(xmlString); elem = doc.getRootElement(); assertEquals(EXPECTED_TEXT, elem.getText()); } }
/** * This test was provided by Manfred Lotz * * @throws Exception * DOCUMENT ME! */ public void testWhitespaceBug() throws Exception { String notes = "<notes> This is a multiline\n\rentry</notes>"; Document doc = DocumentHelper.parseText(notes); OutputFormat format = new OutputFormat(); format.setEncoding("UTF-8"); format.setIndentSize(4); format.setNewlines(true); format.setTrimText(true); format.setExpandEmptyElements(true); StringWriter buffer = new StringWriter(); XMLWriter writer = new XMLWriter(buffer, format); writer.write(doc); String xml = buffer.toString(); log(xml); Document doc2 = DocumentHelper.parseText(xml); String text = doc2.valueOf("/notes"); String expected = "This is a multiline entry"; assertEquals("valueOf() returns the correct text padding", expected, text); assertEquals("getText() returns the correct text padding", expected, doc2.getRootElement().getText()); }
public void testBug1119733WithSAXEvents() throws Exception { StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); writer.startDocument(); writer.startElement(null, "root", "root", new AttributesImpl()); writer.startElement(null, "code", "code", new AttributesImpl()); writer.characters(new char[] {'f', 'o', 'o'}, 0, 3); writer.endElement(null, "code", "code"); writer.characters(new char[] {' ', 'b', 'a', 'r'}, 0, 4); writer.endElement(null, "root", "root"); writer.endDocument(); writer.close(); String xml = out.toString(); System.out.println(xml); assertEquals("whitespace problem", -1, xml.indexOf("</code>bar")); }
public void testWritingEmptyElement() throws Exception { Document doc = DocumentFactory.getInstance().createDocument(); Element grandFather = doc.addElement("grandfather"); Element parent1 = grandFather.addElement("parent"); Element child1 = parent1.addElement("child1"); Element child2 = parent1.addElement("child2"); child2.setText("test"); Element parent2 = grandFather.addElement("parent"); Element child3 = parent2.addElement("child3"); child3.setText("test"); StringWriter buffer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(buffer, format); writer.write(doc); String xml = buffer.toString(); System.out.println(xml); assertTrue("child2 not present", xml.indexOf("<child2>test</child2>") != -1); }
private String renderSchema(Schema schema) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Schema.class); Marshaller marshaller = jaxbContext.createMarshaller(); NamespaceFilter outFilter = new NamespaceFilter(CORE_PREFIX, CORE_NAMESPACE, true); OutputFormat format = new OutputFormat(); format.setIndent(true); format.setNewlines(true); StringWriter sw = new StringWriter(); XMLWriter writer = new XMLWriter(sw, format); outFilter.setContentHandler(writer); marshaller.marshal(schema, outFilter); return sw.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } } }
public void testAttributeQuotes() throws Exception { Document doc = DocumentFactory.getInstance().createDocument(); doc.addElement("root").addAttribute("test", "text with ' in it"); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, OutputFormat .createCompactFormat()); writer.write(doc); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<root test=\"text with ' in it\"/>"; assertEquals(expected, out.toString()); }
public void testEscapeChars() throws Exception { Document document = DocumentFactory.getInstance().createDocument(); Element root = document.addElement("root"); root.setText("blahblah " + '\u008f'); XMLWriter writer = new XMLWriter(); StringWriter strWriter = new StringWriter(); writer.setWriter(strWriter); writer.setMaximumAllowedCharacter(127); writer.write(document); String xml = strWriter.toString(); }
/** * This test harness was supplied by Lari Hotari * * @throws Exception * DOCUMENT ME! */ public void testContentHandler() throws Exception { StringWriter out = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("iso-8859-1"); XMLWriter writer = new XMLWriter(out, format); generateXML(writer); writer.close(); String text = out.toString(); if (VERBOSE) { log("Created XML"); log(text); } // now lets parse the output and test it with XPath Document doc = DocumentHelper.parseText(text); String value = doc.valueOf("/processes[@name='arvojoo']"); assertEquals("Document contains the correct text", "jeejee", value); }