/** StreamResult object is re-used and the values are set appropriately. */
 StreamResult toStreamResult(OutputStream os, Writer writer, String systemId) {
   StreamResult sr = new StreamResult();
   sr.setOutputStream(os);
   sr.setWriter(writer);
   sr.setSystemId(systemId);
   return sr;
 }
  public static String formatMetadata(String input, int indent) {
    input = input.replace("> <", "><");
    try {
      Source xmlInput = new StreamSource(new StringReader(input));
      StringWriter stringWriter = new StringWriter();
      StreamResult xmlOutput = new StreamResult(stringWriter);
      StringWriter sw = new StringWriter();
      xmlOutput.setWriter(sw);
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      transformerFactory.setAttribute("indent-number", indent);
      Transformer transformer = transformerFactory.newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + indent);
      transformer.transform(xmlInput, xmlOutput);

      return xmlOutput.getWriter().toString();
    } catch (IllegalArgumentException | TransformerException e) {
      return input;
    }
  }
示例#3
0
  private String transform(StringBuffer XMLdata, StringBuffer XSLdata, Hashtable paramHash) {
    StringReader XML = new StringReader(XMLdata.toString());
    StringReader XSL = new StringReader(XSLdata.toString());

    StreamSource XMLsource = new StreamSource(XML);
    StreamSource XSLsource = new StreamSource(XSL);

    TransformerFactory tFact = TransformerFactory.newInstance();
    ByteArrayOutputStream out1 = new ByteArrayOutputStream();
    StreamResult out = new StreamResult(out1);
    Writer out2 = new StringWriter();
    out.setWriter(out2);

    try {
      Transformer transformer = tFact.newTransformer(XSLsource);
      setTransformParameters(transformer, paramHash);
      transformer.transform(XMLsource, out);

    } catch (Exception e) {
      log.debug(this.working_for + " : " + e.getCause());
      return (e.toString());
    }
    return (out2.toString());
  }