Example usage for javax.xml.transform TransformerConfigurationException printStackTrace

List of usage examples for javax.xml.transform TransformerConfigurationException printStackTrace

Introduction

In this page you can find the example usage for javax.xml.transform TransformerConfigurationException printStackTrace.

Prototype

@Override
public void printStackTrace() 

Source Link

Document

Print the the trace of methods from where the error originated.

Usage

From source file:Main.java

/**
 * @param xml//from   w  ww .j a v a  2  s  .c  om
 */
public static String prettyPrintToString(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    return formattedxml;

}

From source file:Main.java

/**
 * @param xml/*  ww w.  ja v a2  s .com*/
 */
public static void prettyPrint(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    System.out.println(formattedxml);

}

From source file:Main.java

public static Node xsltTransform(InputStream styleSheet, Document response, Map<String, String> params) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*from  w ww . j a v  a2 s .  c  o  m*/
        javax.xml.transform.stream.StreamSource streamSource = new javax.xml.transform.stream.StreamSource(
                styleSheet);
        transformer = tFactory.newTransformer(streamSource);
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for (Iterator<String> iterator = params.keySet().iterator(); iterator.hasNext();) {
        String key = iterator.next();
        String value = params.get(key);
        transformer.setParameter(key, value);
    }

    Source xmlSource = new DOMSource(response);
    DOMResult result = new DOMResult();
    try {
        transformer.transform(xmlSource, result);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result.getNode();
}

From source file:Main.java

public static ByteArrayOutputStream printDOMDocumentToOutputStream(Document doc) {
    ByteArrayOutputStream os;//from w w  w.j  a  v  a2s .co  m
    os = new ByteArrayOutputStream();

    try {
        // TODO remove, nonsence here XMLSignatureHelper.storeSignatureToXMLFile(doc, "signature.xml");

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        //trans.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        //trans.setOutputProperty(OutputKeys.INDENT, "yes");
        //trans.setOutputProperty(OutputKeys.METHOD, "xml");

        trans.transform(new DOMSource(doc), new StreamResult(os));

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return os;

}

From source file:Main.java

public static String constructXMLString(Document dom) throws IOException {
    String retXMLStr = null;//w  w  w  .  j a  v  a  2s .c  om
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = factory.newTransformer();
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        DOMSource source = new DOMSource(dom);
        transformer.transform(source, result);
        writer.close();
        retXMLStr = writer.toString();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /*
    // Make a string out of the DOM object
    System.out.println(dom.toString());
    OutputFormat format = new OutputFormat(dom);
    format.setIndenting(true);
    ByteArrayOutputStream byteoutStream = new ByteArrayOutputStream();
    XMLSerializer serializer = new XMLSerializer(byteoutStream, format);
    serializer.serialize(dom);
    String retXMLStr = new String(byteoutStream.toByteArray());
    byteoutStream.close();
     */
    return retXMLStr;

}

From source file:Main.java

public static String GetStringFromDoc(Document d) {
    /*//  w  w w  .ja  v  a  2  s .  c o m
     DOMImplementationLS domImplementation = (DOMImplementationLS) d.getImplementation();
     LSSerializer lsSerializer = domImplementation.createLSSerializer();
            
     return lsSerializer.writeToString(d);
     */
    StringWriter output = new StringWriter();

    try {
        //Transformer transformer = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null).newTransformer();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(d.getDocumentElement()), new StreamResult(output));
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output.toString();
}

From source file:com.pentaho.repository.importexport.PDIImportUtil.java

public static String asXml(Document document) {
    try {/*  w  ww . ja  v a  2 s.c om*/
        Source source = new DOMSource(document.getParentNode());
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 *  write the dom content into xml file/*w w  w  .j  a  va2 s  .  com*/
 * @param indent
 * @param n
 */
public static void writeDOM2XMLFile(Document domDoc, String fileName) {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // no effect ?
    //      transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    //      transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    //      transformer.setOutputProperty(OutputKeys.STANDALONE , "yes");
    //      transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(domDoc);
    StreamResult result = new StreamResult(new File(fileName));

    // Output to console for testing
    // StreamResult result = new StreamResult(System.out);

    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.err.println("Info: dom document saved in file, " + fileName + " !");
}

From source file:Main.java

/**
 * Parses {@code org.w3c.dom.Document} and returns its xml serialization as {@code String}
 * @param dom//w ww. j  a  v  a 2  s . c  o m
 * @return
 */
public static String DocumentToString(Document dom) {
    String xmlString = null;
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //         initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(dom);
        transformer.transform(source, result);

        xmlString = result.getWriter().toString();

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return xmlString;
}

From source file:Main.java

private static void saveFile(String file_path, Document doc) {
    try {//from  w w w  .  j  a  va 2 s.c  o  m
        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(file_path));

        // Output to console for testing
        //StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}