Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

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

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:edu.unc.lib.dl.xml.ModsXmlHelper.java

public static Document transform(Element mods) throws TransformerException {
    Source modsSrc = new JDOMSource(mods);
    JDOMResult dcResult = new JDOMResult();
    Transformer t = null;
    try {//from   www. j  a v  a2s .  co  m
        t = mods2dc.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new Error("There was a problem configuring the transformer.", e);
    }
    t.transform(modsSrc, dcResult);
    return dcResult.getDocument();
}

From source file:Main.java

public static byte[] transform(Transformer transformer, Document useDocument) {
    byte[] returnData = new byte[0];
    try {/*  w w w. j  a v a  2 s  . com*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(useDocument.getDocumentElement());
        transformer.transform(domSource, new StreamResult(baos));

        returnData = baos.toByteArray();

        baos.close();
    } catch (TransformerException te) {
        String err = new String(te.toString());
        returnData = err.getBytes();
        te.printStackTrace();
    } catch (IOException ie) {
        String err = new String(ie.toString());
        returnData = err.getBytes();
        ie.printStackTrace();
    }
    return returnData;
}

From source file:Main.java

private static String docToString(Document doc) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(source, result);

    return result.getWriter().toString();
}

From source file:Main.java

/**
 * write this as an XML document in a stream
 * @param out the stream//w  w  w .  j a  v  a2 s  .  c o  m
 * @param omitdecl should we omit the declaration
 */
static public void print(Node node, Writer out, boolean omitdecl) {
    try {
        if (node == null)
            return;

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", (omitdecl ? "yes" : "no"));
        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException("Cannot print Node", e);
    }

}

From source file:Main.java

public static void writeXml(Document doc, File file) {
    try {/*  w w w .j av  a  2 s .  c  o m*/
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();

        try {
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        } catch (IllegalArgumentException e) { // ignore
        }

        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        throw new IllegalStateException(e);
    } catch (TransformerException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static void serialize(Document doc, OutputStream out) throws Exception {

    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    try {/*from  ww  w  . j  a va2 s  .  com*/
        serializer = tfactory.newTransformer();
        //Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(new DOMSource(doc), new StreamResult(out));
    } catch (TransformerException e) {
        // this is fatal, just dump the stack and throw a runtime exception
        e.printStackTrace();

        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Writes  XML Document into an xml file.
 * //from w  w  w .  j ava2 s  .c  o  m
 * @param fileName  the target file with the full path
 * @param document   the source document
 * @return   boolean true if the file saved
 * @throws Exception
 */
public static boolean writeXmlFile(String fileName, Document document) throws Exception {

    // creating and writing to xml file  

    File file = new File(fileName);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // to prevent XML External Entities attack

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.transform(new DOMSource(document), new StreamResult(file));

    return true;

}

From source file:Main.java

/**
 * Store xml Signature represented by dom.Document into xml file
 * @param doc - XML Signature//from   w  w w.  j a  v  a  2  s  .  c o  m
 * @param fileName - name of output file
 */
public static boolean storeSignatureToXMLFile(Document doc, String fileName) {
    boolean stored = false;
    OutputStream os;
    File absolute = new File(fileName);

    String outputPath = new String();
    if (absolute.getParentFile().isAbsolute()) {
        outputPath = fileName; //path is absolute
    } else {
        outputPath = baseDir + File.separator + fileName; //relative, store it into %project_dir%/dist/signatures
    }

    File file = new File(outputPath);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }

    try {
        os = new FileOutputStream(outputPath);

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

        trans.transform(new DOMSource(doc), new StreamResult(os));
        stored = true;
    } catch (FileNotFoundException e) {
        handleError(e);
    } catch (TransformerConfigurationException e) {
        handleError(e);
    } catch (TransformerException e) {
        handleError(e);
    }
    return stored;
}

From source file:Main.java

/**
 * Convert XML Node to String/*from w w w .  java 2s .  c om*/
 *
 * @param node
 *            the node to convert
 * @return the String equivalent of the node
 * @throws TransformerException
 */
public static String nodeToString(final Node node) throws TransformerException {
    final Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    final Writer out = new StringWriter();
    tf.transform(new DOMSource(node), new StreamResult(out));
    return out.toString();
}