Java XML String Transform getPropertyAsString(final Properties prop, final String comment)

Here you can find the source of getPropertyAsString(final Properties prop, final String comment)

Description

get Property As String

License

Open Source License

Declaration

public static byte[] getPropertyAsString(final Properties prop, final String comment) throws IOException 

Method Source Code

//package com.java2s;
/**/* w  w  w  .  j  a v  a  2  s.c  o m*/
 * Copyright (C) 2015 BonitaSoft S.A.
 * BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 **/

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.util.Properties;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    public static byte[] getPropertyAsString(final Properties prop, final String comment) throws IOException {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        prop.store(out, comment);
        return out.toByteArray();
    }

    public static byte[] toByteArray(final Document document) throws IOException, TransformerException {
        if (document == null) {
            throw new IllegalArgumentException("Document should not be null.");
        }
        final Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        ByteArrayOutputStream out = null;
        try {
            out = new ByteArrayOutputStream();
            tf.transform(new DOMSource(document), new StreamResult(out));
            return out.toByteArray();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}

Related

  1. getIntegerAttribute(XMLStreamReader in, String attribute)
  2. getNode(String text)
  3. getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)
  4. getOutputMimeType(String xslName)
  5. getOutputSQLPageXML(String inStatement, Map map)
  6. getSchema(String schemaString)
  7. getSchemaFromResource(String... files)
  8. getSourceAsString(Source source)
  9. getStreamResultForFile(String filename)