Example usage for javax.xml.transform OutputKeys METHOD

List of usage examples for javax.xml.transform OutputKeys METHOD

Introduction

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

Prototype

String METHOD

To view the source code for javax.xml.transform OutputKeys METHOD.

Click Source Link

Document

method = "xml" | "html" | "text" | expanded name.

Usage

From source file:com.jaeksoft.searchlib.util.XmlWriter.java

public XmlWriter(PrintWriter out, String encoding) throws TransformerConfigurationException, SAXException {
    StreamResult streamResult = new StreamResult(out);
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    transformerHandler = tf.newTransformerHandler();
    Transformer serializer = transformerHandler.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, encoding);
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformerHandler.setResult(streamResult);
    startedElementStack = new Stack<String>();
    transformerHandler.startDocument();// ww  w .j  a v a 2 s . c o  m
    elementAttributes = new AttributesImpl();
    Pattern p = Pattern.compile("\\p{Cntrl}");
    controlMatcher = p.matcher("");

}

From source file:Main.java

private static void writeTo(Result result, Document document) throws TransformerConfigurationException,
        TransformerException, FileNotFoundException, UnsupportedEncodingException {

    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", new Integer(4));

    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(document);

    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N

    // indent the output to make it more legible... 
    try {//from   ww  w  . j  av a  2 s  .c  o m
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
    } catch (IllegalArgumentException e) {
        // the JAXP implementation doesn't support indentation, no big deal
        //e.printStackTrace();
    }

    transformer.transform(source, result);
}

From source file:net.sf.joost.emitter.StreamEmitter.java

/**
 * Creates an emitter using a given <code>Writer</code>, an output
 * encoding and a set of output properties. The value of the
 * <code>OutputKeys.METHOD</code> property determines the returned
 * emitter object.//ww w . j  ava2 s .co m
 * @param writer A <code>Writer</code> for receiving the output.
 * @param encoding the encoding used in the writer resp. that should
 *        be used for the encoding declaration
 * @param outputProperties The set of output properties to be used.
 * @return a proper stream emitter object
 */
public static StreamEmitter newEmitter(Writer writer, String encoding, Properties outputProperties) {
    StreamEmitter emitter = null;
    if (outputProperties != null) {
        String outputMethod = outputProperties.getProperty(OutputKeys.METHOD);
        if (outputMethod == null || outputMethod.equals("xml"))
            emitter = new XmlEmitter(writer, encoding, outputProperties);
        else if (outputMethod.equals("text"))
            emitter = new TextEmitter(writer, encoding);
        else if (outputMethod.equals(HTML_METHOD))
            emitter = new HtmlEmitter(writer, encoding);
        else {
            String msg = "Unsupported output method '" + outputMethod + "', use default 'xml' method instead";
            if (log != null)
                log.warn(msg);
            else
                System.err.println("Warning: " + msg);
        }
        if (emitter != null) {
            String val = outputProperties.getProperty(TrAXConstants.OUTPUT_KEY_SUPPORT_DISABLE_OUTPUT_ESCAPING);
            if (val != null)
                emitter.setSupportDisableOutputEscaping(val.equals("yes"));
        }
    }
    if (emitter == null) {
        // either outputProperties==null or unknown output method
        emitter = new XmlEmitter(writer, encoding, outputProperties);
    }
    return emitter;
}

From source file:com.hangum.tadpole.engine.sql.util.export.XMLExporter.java

/**
 * make content/*from   www  . ja v  a 2  s  .c o m*/
 * 
 * @param tableName
 * @param rsDAO
 * @return
 */
public static String makeContent(String tableName, QueryExecuteResultDTO rsDAO, int intLimitCnt)
        throws Exception {
    final StringWriter stWriter = new StringWriter();
    final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.newDocument();
    final Element results = doc.createElement(tableName);
    doc.appendChild(results);

    Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
    for (int i = 0; i < dataList.size(); i++) {
        Map<Integer, Object> mapColumns = dataList.get(i);
        Element row = doc.createElement("Row");
        results.appendChild(row);

        for (int j = 1; j < mapColumns.size(); j++) {
            String columnName = mapLabelName.get(j);
            String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j);

            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(strValue));
            row.appendChild(node);
        }

        if (i == intLimitCnt)
            break;
    }

    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setAttribute("indent-number", 4);

    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//"ISO-8859-1");
    StreamResult sr = new StreamResult(stWriter);
    transformer.transform(domSource, sr);

    return stWriter.toString();
}

From source file:Main.java

/**
 * DOCUMENT ME!//from ww w .  j  a  va  2s .c om
 *
 * @return DOCUMENT ME!
 *
 * @throws TransformerConfigurationException DOCUMENT ME!
 * @throws TransformerException DOCUMENT ME!
 * @throws Exception DOCUMENT ME!
 */
public static byte[] writeToBytes(Document document)
        throws TransformerConfigurationException, TransformerException, Exception {
    byte[] ret = null;

    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", new Integer(4));

    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(document);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bos);
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N

    // indent the output to make it more legible...
    try {
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
    } catch (IllegalArgumentException e) {
        // the JAXP implementation doesn't support indentation, no big deal
        //e.printStackTrace();
    }

    try {
        transformer.transform(source, result);
        ret = bos.toByteArray();
    } finally {
        if (bos != null) {
            bos.flush();
            bos.close();
        }
    }

    return ret;
}

From source file:com.mindquarry.search.serializer.IndexPostSerializer.java

/**
 * Set the configurations for this serializer.
 *//*from w w w .j  av a 2  s.co m*/
public void configure(Configuration conf) throws ConfigurationException {
    super.configure(conf);
    this.format.put(OutputKeys.METHOD, "xml");
}

From source file:com.beligum.core.utils.Toolkit.java

public static String xmlToString(Document document, boolean indent) throws Exception {
    if (document == null) {
        return null;
    } else {//from w ww  .j av  a 2  s  .  c o m
        try {
            StringWriter sw = new StringWriter();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

            transformer.transform(new DOMSource(document), new StreamResult(sw));
            return sw.toString();
        } catch (Exception e) {
            throw new Exception("Error converting XML to String", e);
        }
    }
}

From source file:com.aurel.track.exchange.docx.exporter.CustomXML.java

/**
 * @param items//from ww  w. j  ava2  s  .co m
 * @return
 */
public static void convertToXml(OutputStream outputStream, Document dom) {
    Transformer transformer = null;
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    } catch (TransformerConfigurationException e) {
        LOGGER.error(
                "Creating the transformer failed with TransformerConfigurationException: " + e.getMessage());
        return;
    }
    try {
        transformer.transform(new DOMSource(dom), new StreamResult(outputStream));
    } catch (TransformerException e) {
        LOGGER.error("Transform failed with TransformerException: " + e.getMessage());
    }
}

From source file:com.mockey.storage.xml.MockeyXmlFactory.java

private String getDocumentAsString(Document document) throws IOException, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, HTTP.UTF_8);
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
    return result.getWriter().toString();
}