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

public static String XMLToString(Document doc, Boolean singleLine) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*  w w w . j  a  v a  2s . c om*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String string = writer.getBuffer().toString();
    if (!singleLine) {
        return string;
    }
    return string.replaceAll("\n|\r", "");

}

From source file:Main.java

/**
 * Creates a string representation of a {@link Node} instance. This method
 * does not introduce any character to the string representation of the
 * {@link Node} (eg. \n or \r characters)
 *
 * @param node A {@link Node} instance//from w  w w.  j  av  a  2 s  . c  o m
 * @return A string representation of the node instance
 * @throws RequestSecurityTokenException
 */
public static String xmlToString(Node node) throws Exception {
    try {
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();

    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        throw new Exception("Cannot build a string representation of the assertion.");
    } catch (TransformerException e) {
        e.printStackTrace();
        throw new Exception("Cannot build a string representation of the assertion.");
    }
}

From source file:Main.java

public static byte[] doc2bytes(Document doc, boolean formated) {
    try {/*from  w ww.  j a  va  2 s  .  co  m*/
        Source source = new DOMSource(doc);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Result result = new StreamResult(out);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        if (formated) {
            // linefeed formatting
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        } else {
            // remove xml header
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        transformer.transform(source, result);
        return out.toByteArray();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String prettifyXmlString(String uglyXml) {
    Transformer transformer = null;
    try {/*w  w  w  .jav  a2 s .  c o m*/
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = null;
    try {
        source = new DOMSource(string2Document(uglyXml));
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String xmlString = result.getWriter().toString();
    return xmlString;
}

From source file:Main.java

public static String documentToString(Document d) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {//from w w  w  . jav  a  2s  . c o  m
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(d), new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}

From source file:Main.java

public static String getString(Node doc) {
    StringWriter sw = new StringWriter();

    try {/*from   w ww  .  j  a  v  a2s  .  c om*/
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        // create string from xml tree
        trans.transform(new DOMSource(doc), new StreamResult(sw));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static void transformDocument(Document doc, String xsl, String targetFile) {
    try {//from   w  ww . j a v a  2 s .  c  o m
        Source source = new DOMSource(doc);

        // Creation of the output file
        File file = new File(targetFile);
        Result result = new StreamResult(file);

        // configuration of the transformer
        TransformerFactory factoryT = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void printNode(Node node, String fn) {
    try {//from  w  w  w .ja va 2s. c om
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(node);
        PrintStream out = System.out;
        if (fn != null)
            out = new PrintStream(new FileOutputStream(fn));
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
        out.close();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {

        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String docToString(Document doc, boolean formated) {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/* w  w w. ja v  a2 s .  co  m*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    }

    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    if (formated) {
        // linefeed formatting
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    } else {
        // remove xml header
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);

    DOMSource domSource = new DOMSource(doc);
    try {
        transformer.transform(domSource, sr);
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
    return sw.toString();
}

From source file:Main.java

/**
 * @param xsltFile The XSLT stylesheet file
 * @param parms XSL parameters to pass to the stylesheet.
 * @return the configured XSLT transformer
 * @throws TransformerException if there is a problem configuring the transformer
 */// w w w . jav a2s. c  om
static Transformer createTransformer(final File xsltFile, final Map<String, String> parms)
        throws TransformerException {
    try {
        Source xslSource = new StreamSource(xsltFile);
        TransformerFactory transFact = TransformerFactory.newInstance();
        transFact.setAttribute("indent-number", 2);
        Transformer transformer = transFact.newTransformer(xslSource);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        for (String parm : parms.keySet()) {
            transformer.setParameter(parm, parms.get(parm));
        }
        return transformer;
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        throw e;
    }
}