Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: df2e1207a2e5f8a3bae9c9d446d6b498259fc636 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.ws.internal.explorer.platform.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public final class XMLUtils
{
  /**
   * Serialize an XML Element into a String.
   * @param e Element to be serialized.
   * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
   * @return String representation of the XML document fragment.
   */
  public static final String serialize(Element e,boolean omitXMLDeclaration)
  {
    if (e != null)
    {
      try
      {
		DOMSource domSource = new DOMSource(e);
		Transformer serializer = TransformerFactory.newInstance().newTransformer();
		serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ((omitXMLDeclaration) ? "yes" : "no"));
		serializer.setOutputProperty(OutputKeys.INDENT, "yes");
		serializer.setOutputProperty(OutputKeys.ENCODING, HTMLUtils.UTF8_ENCODING);
		serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		serializer.transform(domSource, new	StreamResult(baos));
		baos.close();
		return new String(baos.toByteArray(), HTMLUtils.UTF8_ENCODING);
      }
      catch (Throwable t)
      {
      }
    }
    return null;
  }
    
  /**
   * Serialize an XML Element into a String.
   * @param df DocumentFragment to be serialized.
   * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
   * @return String representation of the XML document fragment.
   */  
  public static final String serialize(DocumentFragment df,boolean omitXMLDeclaration)
  {
    if (df != null)
    {
      try
      {
		DOMSource domSource = new DOMSource(df);
		Transformer serializer = TransformerFactory.newInstance().newTransformer();
		serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ((omitXMLDeclaration) ? "yes" : "no"));
		serializer.setOutputProperty(OutputKeys.INDENT, "yes");
		serializer.setOutputProperty(OutputKeys.ENCODING, HTMLUtils.UTF8_ENCODING);
		serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		serializer.transform(domSource, new	StreamResult(baos));
		baos.close();
		return new String(baos.toByteArray(), HTMLUtils.UTF8_ENCODING);
      }
      catch (Throwable t)
      {
      }
    }
    return null;
  }
  
  /**
   * Create a new XML Document.
   * @param docBuilder DocumentBuilder. Setting this to null will create a new DocumentBuilder.
   * @return Document
   */
  public static final Document createNewDocument(DocumentBuilder docBuilder) throws ParserConfigurationException
  {
    if (docBuilder == null)
    {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(false);
      factory.setValidating(false);
      docBuilder = factory.newDocumentBuilder();
    }
    Document doc = docBuilder.newDocument();
    return doc;
  }

  /**
   * Convert the String representation of an Element into an Element
   * @param String representation of an Element.
   * @return Element
   */
  public static Element stringToElement(String s) throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException {
    return stringToElement(s, false);
  }

  /**
   * Convert the String representation of an Element into an Element
   * @param String representation of an Element.
   * @param boolean set whether the return Element should be namespace aware.
   * @return Element
   */
  public static Element stringToElement(String s, boolean namespaceAware) throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException
  {
    return byteArrayToElement(s.getBytes(HTMLUtils.UTF8_ENCODING), namespaceAware);
  }

  /**
   * Convert the byte array representation of an Element into an Element
   * @param byte[] representation of an Element.
   * @param boolean set whether the return Element should be namespace aware.
   * @return Element
   */
  public static Element byteArrayToElement(byte[] b, boolean namespaceAware) throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException
  {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(namespaceAware);
    docBuilderFactory.setValidating(false);
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new ByteArrayInputStream(b));
    return doc.getDocumentElement();
  }
}

Back to the top