001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.debugger.ui.util;
021:
022: import java.io.IOException;
023: import java.io.StringReader;
024: import java.io.StringWriter;
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.parsers.ParserConfigurationException;
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerConfigurationException;
030: import javax.xml.transform.TransformerException;
031: import javax.xml.transform.TransformerFactory;
032: import javax.xml.transform.TransformerFactoryConfigurationError;
033: import javax.xml.transform.dom.DOMSource;
034: import javax.xml.transform.stream.StreamResult;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039: import org.xml.sax.InputSource;
040: import org.xml.sax.SAXException;
041:
042: /**
043: *
044: * @author Alexander Zgursky
045: */
046: public final class XmlUtil {
047:
048: private static DocumentBuilder cDocumentBuilder;
049:
050: /** Creates a new instance of Util */
051: private XmlUtil() {
052: }
053:
054: public static String toString(final Node node) {
055: StringWriter writer = new StringWriter();
056: DOMSource source = new DOMSource(node);
057: StreamResult result = new StreamResult(writer);
058: TransformerFactory factory = null;
059: factory = TransformerFactory.newInstance();
060:
061: Transformer transformer = null;
062: try {
063: transformer = factory.newTransformer();
064: } catch (TransformerConfigurationException ex) {
065: ex.printStackTrace();
066: return "";
067: } catch (TransformerFactoryConfigurationError ex) {
068: ex.printStackTrace();
069: return "";
070: }
071:
072: try {
073: transformer.transform(source, result);
074: } catch (TransformerException ex) {
075: ex.printStackTrace();
076: return "";
077: }
078:
079: return writer.toString();
080: }
081:
082: public static boolean isTextOnlyNode(final Node node) {
083: if (node.getNodeType() == Node.ELEMENT_NODE) {
084: final NodeList children = node.getChildNodes();
085:
086: if ((children.getLength() == 1)
087: && (children.item(0).getNodeType() == Node.TEXT_NODE)) {
088: return true;
089: }
090: }
091:
092: return false;
093: }
094:
095: public static Element parseXmlElement(final String xml) {
096:
097: Document doc = null;
098:
099: if (xml != null && xml.length() > 0) {
100: final InputSource is = new InputSource(
101: new StringReader(xml));
102:
103: try {
104: doc = getDocumentBuilder().parse(is);
105: } catch (ParserConfigurationException e) {
106: //TODO:handle this properly
107: //e.printStackTrace();
108: } catch (IOException e) {
109: //TODO:handle this properly
110: //e.printStackTrace();
111: } catch (SAXException e) {
112: //TODO:handle this properly
113: //e.printStackTrace();
114: }
115: }
116:
117: if (doc != null) {
118: return doc.getDocumentElement();
119: } else {
120: return null;
121: }
122: }
123:
124: private static DocumentBuilder getDocumentBuilder()
125: throws ParserConfigurationException {
126:
127: if (cDocumentBuilder == null) {
128: final DocumentBuilderFactory factory = DocumentBuilderFactory
129: .newInstance();
130:
131: factory.setIgnoringComments(false);
132: factory.setNamespaceAware(true);
133: factory.setIgnoringElementContentWhitespace(false);
134:
135: cDocumentBuilder = factory.newDocumentBuilder();
136: }
137:
138: return cDocumentBuilder;
139: }
140: }
|