lunedì 3 dicembre 2012

org.w3c.dom.Document convertion Utils

How to convert an InputStream to a Document?
How to contert a Document to an InputStream? The snippet of code posted is an utility Class written in

Java
for org.w3c.dom.Document convertions, it supports:

  • Document to String
  • String to Document
  • Document to InputStream
  • InputStream to Document

package org.gma.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class DocumentUtils {

 /**
  * Convert a document to an Inputstream
  * @param document the document to convert
  * @param prettyPrint prettyPrinted if true
  * @return An input stream of the document
  * @throws IOException
  */
 public static InputStream document2InputStream(Document document, boolean prettyPrint) 
throws IOException {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  OutputFormat outputFormat = new OutputFormat(document);
  if (prettyPrint) {
   outputFormat.setIndenting(true);
   outputFormat.setIndent(2);
   outputFormat.setLineWidth(65);
   outputFormat.setPreserveSpace(false);
  }
  XMLSerializer serializer = new XMLSerializer(outputStream, outputFormat);
  serializer.serialize(document);
  return new ByteArrayInputStream(outputStream.toByteArray());
 }

 /**
  * 
  * Convert a string to a Document Object
  * 
  * @param xml The xml to convert
  * @return A document Object
  * @throws IOException
  * @throws SAXException
  * @throws ParserConfigurationException
  */
 public static Document string2Document(String xml)
 throws IOException, SAXException, ParserConfigurationException {

  if (xml == null)
   return null;

  return inputStream2Document(new ByteArrayInputStream(xml.getBytes()));

 }

 /**
  * Convert an inputStream to a Document Object
  * @param inputStream The inputstream to convert
  * @return a Document Object
  * @throws IOException
  * @throws SAXException
  * @throws ParserConfigurationException
  */
 public static Document inputStream2Document(InputStream inputStream) throws IOException, 
SAXException, 
ParserConfigurationException {
  DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
  newInstance.setNamespaceAware(true);
  Document parse = newInstance.newDocumentBuilder().parse(inputStream);
  return parse;

 }

 /**
  * Convert a Document object to a string
  * @param document The document to Convert
  * @param prettyPrint prettyPrinted if true
  * @return A string rapresentation of the document
  * @throws IOException
  */
 public static String documentToString(Document document, boolean prettyPrint) 
throws IOException {
  Writer writer = new StringWriter();
  char[] buffer = new char[1024];
  InputStream is = null;
  try {
   is = document2InputStream(document, prettyPrint);
   Reader reader = new BufferedReader(new InputStreamReader(is));
   int n;
   while ((n = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, n);
   }
  } finally {
   if (is != null) {
    is.close();
   }
  }
  return writer.toString();
 }

}
SVN Repository link
<!-- Maven Configuration -->
<repositories>
 <repository>
  <id>org.gma</id>
  <name>gma repository</name>
  <url>https://repository-giordanomaestro.forge.cloudbees.com/release/</url>
 </repository>
</repositories>

<dependencies>
 <dependency>
  <groupId>org.gma.util</groupId>
  <artifactId>DocumentConversionUtils</artifactId>
  <version>1.0</version>
 </dependency>
</dependencies>

0 commenti:

Posta un commento