List of usage examples for org.dom4j.io XMLWriter XMLWriter
public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException
From source file:com.haulmont.cuba.restapi.XMLConverter2.java
License:Apache License
protected String documentToString(Document document) { try {//from w ww .ja v a 2 s. c om OutputFormat format = OutputFormat.createPrettyPrint(); StringWriter sw = new StringWriter(); XMLWriter writer = new XMLWriter(sw, format); writer.write(document); return sw.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java
License:Apache License
/** * XML?? XML???/*from w w w .j av a 2 s . c o m*/ * * @param document * ? * @param file * ?XML */ public void writeXml(Document document, File file) { try { //? OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter output = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), format); output.write(document); output.flush(); output.close(); } catch (IOException e) { log.info(e.getMessage()); } }
From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java
License:Apache License
/** * XML?? ?//from w w w. ja v a 2 s. co m * * @param document * ? * @param file * ?XML */ public void writeXml(Document document) { try { //? OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter output = new XMLWriter(System.out, format); //XMLWriter output = new XMLWriter(new FileWriter(file)); output.write(document); output.flush(); output.close(); } catch (IOException e) { log.info(e.getMessage()); } }
From source file:com.iisigroup.cap.utils.CapXmlUtil.java
License:Open Source License
/** * xml document ? string/*from w w w. j ava2 s. c o m*/ * * @param doc * document * @param format * format * @return String */ public static String convertDocumentToString(Document doc, boolean format) { StringWriter out = new StringWriter(); try { new XMLWriter(out, new OutputFormat(Constants.EMPTY_STRING, format, CharEncoding.UTF_8)).write(doc); return out.toString(); } catch (IOException e) { throw new CapMessageException(e, CapXmlUtil.class); } }
From source file:com.intuit.tank.script.ScriptEditor.java
License:Open Source License
public String getPrettyString(String s, String mimetype) { if (StringUtils.isNotBlank(s)) { if (StringUtils.containsIgnoreCase(mimetype, "json")) { try { JsonParser parser = new JsonParser(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonElement el = parser.parse(s); s = gson.toJson(el); // done } catch (JsonSyntaxException e) { LOG.warn("Cannot format json string: " + e); }/*from w w w . j av a 2 s .co m*/ } else if (StringUtils.containsIgnoreCase(mimetype, "xml")) { try { StringWriter sw; final OutputFormat format = OutputFormat.createPrettyPrint(); final org.dom4j.Document document = DocumentHelper.parseText(s); sw = new StringWriter(); final XMLWriter writer = new XMLWriter(sw, format); writer.write(document); s = sw.toString(); } catch (Exception e) { LOG.warn("Cannot format xml string: " + e); } } s = s.trim(); } return s; }
From source file:com.iterzp.momo.utils.SettingUtils.java
License:Open Source License
/** * //from w ww . j av a 2s .c o m * * @param setting * */ public static void set(Setting setting) { try { File shopxxXmlFile = new ClassPathResource(CommonAttributes.MOMO_XML_PATH).getFile(); Document document = new SAXReader().read(shopxxXmlFile); List<Element> elements = document.selectNodes("/momo/setting"); for (Element element : elements) { try { String name = element.attributeValue("name"); String value = beanUtils.getProperty(setting, name); Attribute attribute = element.attribute("value"); attribute.setValue(value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } FileOutputStream fileOutputStream = null; XMLWriter xmlWriter = null; try { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); outputFormat.setIndent(true); outputFormat.setIndent(" "); outputFormat.setNewlines(true); fileOutputStream = new FileOutputStream(shopxxXmlFile); xmlWriter = new XMLWriter(fileOutputStream, outputFormat); xmlWriter.write(document); } catch (Exception e) { e.printStackTrace(); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (IOException e) { } } IOUtils.closeQuietly(fileOutputStream); } Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME); cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting)); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.itextpdf.rups.model.XfaFile.java
License:Open Source License
/** * Writes a formatted XML file to the OutputStream. * @see com.itextpdf.rups.io.OutputStreamResource#writeTo(java.io.OutputStream) *//*from ww w . ja v a 2 s. com*/ public void writeTo(OutputStream os) throws IOException { if (xfaDocument == null) return; OutputFormat format = new OutputFormat(" ", true); XMLWriter writer = new XMLWriter(os, format); writer.write(xfaDocument); }
From source file:com.jaspersoft.jasperserver.export.ExporterImpl.java
License:Open Source License
protected void writeIndexDocument(Document indexDocument) { OutputStream indexOut = getIndexOutput(); boolean closeIndexOut = true; try {/*from www .jav a 2s . c o m*/ OutputFormat format = new OutputFormat(); format.setEncoding(getCharacterEncoding()); XMLWriter writer = new XMLWriter(indexOut, format); writer.write(indexDocument); closeIndexOut = false; indexOut.close(); } catch (IOException e) { log.error(e); throw new JSExceptionWrapper(e); } finally { if (closeIndexOut) { try { indexOut.close(); } catch (IOException e) { log.error("Error while closing index output", e); } } } }
From source file:com.jboss.transaction.txinterop.test.XMLResultsServlet.java
License:LGPL
public void doStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); HttpSession session = request.getSession(); final FullTestResult testResult = (FullTestResult) session .getAttribute(TestConstants.ATTRIBUTE_TEST_RESULT); DOMDocument report = new DOMDocument(); DOMElement testsuite = new DOMElement("testsuite"); report.setRootElement(testsuite);/* w ww. j a va 2s . c o m*/ if (testResult == null) { // No JUnit test results generated. } else { List passedTests = testResult.getPassedTests(); List failedTests = testResult.getFailedTests(); List errorTests = testResult.getErrorTests(); final int runCount = testResult.runCount(); final int errorCount = testResult.errorCount(); final int failureCount = testResult.failureCount(); testsuite.addAttribute("name", "com.jboss.transaction.txinterop.interop.InteropTestSuite"); testsuite.addAttribute("errors", Integer.toString(errorCount)); testsuite.addAttribute("failures", Integer.toString(failureCount)); testsuite.addAttribute("hostname", request.getServerName()); testsuite.addAttribute("tests", Integer.toString(runCount)); testsuite.addAttribute("timestamp", new Date().toString()); DOMElement properties = new DOMElement("properties"); testsuite.add(properties); DOMElement status = newPropertyDOMElement("status"); properties.add(status); status.addAttribute("value", "finished"); long totalDuration = 0; if (!passedTests.isEmpty()) { Iterator passedTestsIterator = passedTests.iterator(); while (passedTestsIterator.hasNext()) { FullTestResult.PassedTest passedTest = (FullTestResult.PassedTest) passedTestsIterator.next(); totalDuration += passedTest.duration; final String name = passedTest.test.toString(); final String description = (String) TestConstants.DESCRIPTIONS.get(name); testsuite.add(newTestcase(passedTest.test.getClass().getName(), name + ": " + description, passedTest.duration)); } } if (!failedTests.isEmpty()) { Iterator failedTestsIterator = failedTests.iterator(); while (failedTestsIterator.hasNext()) { FullTestResult.FailedTest failedTest = (FullTestResult.FailedTest) failedTestsIterator.next(); totalDuration += failedTest.duration; final String name = failedTest.test.toString(); final String description = (String) TestConstants.DESCRIPTIONS.get(name); CharArrayWriter charArrayWriter = new CharArrayWriter(); PrintWriter printWriter = new PrintWriter(charArrayWriter, true); failedTest.assertionFailedError.printStackTrace(printWriter); printWriter.close(); charArrayWriter.close(); testsuite.add(newFailedTestcase(failedTest.test.getClass().getName(), name + ": " + description, failedTest.duration, failedTest.assertionFailedError.getMessage(), charArrayWriter.toString())); } } if (!errorTests.isEmpty()) { Iterator errorTestsIterator = errorTests.iterator(); while (errorTestsIterator.hasNext()) { FullTestResult.ErrorTest errorTest = (FullTestResult.ErrorTest) errorTestsIterator.next(); totalDuration += errorTest.duration; final String name = errorTest.test.toString(); final String description = (String) TestConstants.DESCRIPTIONS.get(name); CharArrayWriter charArrayWriter = new CharArrayWriter(); PrintWriter printWriter = new PrintWriter(charArrayWriter, true); errorTest.throwable.printStackTrace(printWriter); printWriter.close(); charArrayWriter.close(); System.out.println("charArrayWriter.toString()=" + charArrayWriter.toString()); testsuite.add(newErrorTestcase(errorTest.test.getClass().getName(), name + ": " + description, errorTest.duration, errorTest.throwable.getMessage(), charArrayWriter.toString())); } } // total time of all tests testsuite.addAttribute("time", Float.toString(totalDuration / 1000f)); } String logContent = null; final String logName = (String) session.getAttribute(TestConstants.ATTRIBUTE_LOG_NAME); if (logName != null) { try { logContent = TestLogController.readLog(logName); } catch (final Throwable th) { log("Error reading log file", th); } } testsuite.add(new DOMElement("system-out").addCDATA((logContent != null) ? logContent : "")); testsuite.add(new DOMElement("system-err").addCDATA("")); XMLWriter outputter = new XMLWriter(response.getWriter(), OutputFormat.createPrettyPrint()); try { outputter.write(testsuite); outputter.close(); } catch (IOException e) { throw new ServletException(e); } }
From source file:com.jboss.transaction.wstf.test.XMLResultsServlet.java
License:LGPL
public void doStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); HttpSession session = request.getSession(); final FullTestResult testResult = (FullTestResult) session .getAttribute(TestConstants.ATTRIBUTE_TEST_RESULT); DOMDocument report = new DOMDocument(); DOMElement testsuite = new DOMElement("testsuite"); report.setRootElement(testsuite);//from w w w. j a v a 2s. c o m if (testResult == null) { // No JUnit test results generated. } else { List passedTests = testResult.getPassedTests(); List failedTests = testResult.getFailedTests(); List errorTests = testResult.getErrorTests(); final int runCount = testResult.runCount(); final int errorCount = testResult.errorCount(); final int failureCount = testResult.failureCount(); testsuite.addAttribute("name", "com.jboss.transaction.wstf.interop.InteropTestSuite"); testsuite.addAttribute("errors", Integer.toString(errorCount)); testsuite.addAttribute("failures", Integer.toString(failureCount)); testsuite.addAttribute("hostname", request.getServerName()); testsuite.addAttribute("tests", Integer.toString(runCount)); testsuite.addAttribute("timestamp", new Date().toString()); DOMElement properties = new DOMElement("properties"); testsuite.add(properties); DOMElement status = newPropertyDOMElement("status"); properties.add(status); status.addAttribute("value", "finished"); long totalDuration = 0; if (!passedTests.isEmpty()) { Iterator passedTestsIterator = passedTests.iterator(); while (passedTestsIterator.hasNext()) { FullTestResult.PassedTest passedTest = (FullTestResult.PassedTest) passedTestsIterator.next(); totalDuration += passedTest.duration; final String name = passedTest.test.toString(); final String description = (String) TestConstants.DESCRIPTIONS.get(name); testsuite.add(newTestcase(passedTest.test.getClass().getName(), name + ": " + description, passedTest.duration)); } } if (!failedTests.isEmpty()) { Iterator failedTestsIterator = failedTests.iterator(); while (failedTestsIterator.hasNext()) { FullTestResult.FailedTest failedTest = (FullTestResult.FailedTest) failedTestsIterator.next(); totalDuration += failedTest.duration; final String name = failedTest.test.toString(); final String description = (String) TestConstants.DESCRIPTIONS.get(name); CharArrayWriter charArrayWriter = new CharArrayWriter(); PrintWriter printWriter = new PrintWriter(charArrayWriter, true); failedTest.assertionFailedError.printStackTrace(printWriter); printWriter.close(); charArrayWriter.close(); testsuite.add(newFailedTestcase(failedTest.test.getClass().getName(), name + ": " + description, failedTest.duration, failedTest.assertionFailedError.getMessage(), charArrayWriter.toString())); } } if (!errorTests.isEmpty()) { Iterator errorTestsIterator = errorTests.iterator(); while (errorTestsIterator.hasNext()) { FullTestResult.ErrorTest errorTest = (FullTestResult.ErrorTest) errorTestsIterator.next(); totalDuration += errorTest.duration; final String name = errorTest.test.toString(); final String description = (String) TestConstants.DESCRIPTIONS.get(name); CharArrayWriter charArrayWriter = new CharArrayWriter(); PrintWriter printWriter = new PrintWriter(charArrayWriter, true); errorTest.throwable.printStackTrace(printWriter); printWriter.close(); charArrayWriter.close(); System.out.println("charArrayWriter.toString()=" + charArrayWriter.toString()); testsuite.add(newErrorTestcase(errorTest.test.getClass().getName(), name + ": " + description, errorTest.duration, errorTest.throwable.getMessage(), charArrayWriter.toString())); } } // total time of all tests testsuite.addAttribute("time", Float.toString(totalDuration / 1000f)); } String logContent = null; final String logName = (String) session.getAttribute(TestConstants.ATTRIBUTE_LOG_NAME); if (logName != null) { try { logContent = TestLogController.readLog(logName); } catch (final Throwable th) { log("Error reading log file", th); } } testsuite.add(new DOMElement("system-out").addCDATA((logContent != null) ? logContent : "")); testsuite.add(new DOMElement("system-err").addCDATA("")); XMLWriter outputter = new XMLWriter(response.getWriter(), OutputFormat.createPrettyPrint()); try { outputter.write(testsuite); outputter.close(); } catch (IOException e) { throw new ServletException(e); } }