001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.lib.collab.xmpp.httpbind;
042:
043: import java.io.ByteArrayOutputStream;
044: import java.io.IOException;
045: import java.io.StringReader;
046: import java.io.UnsupportedEncodingException;
047: import javax.xml.parsers.DocumentBuilder;
048: import javax.xml.parsers.DocumentBuilderFactory;
049: import javax.xml.parsers.ParserConfigurationException;
050: import javax.xml.transform.OutputKeys;
051: import javax.xml.transform.Transformer;
052: import javax.xml.transform.TransformerConfigurationException;
053: import javax.xml.transform.TransformerException;
054: import javax.xml.transform.TransformerFactory;
055: import javax.xml.transform.dom.DOMSource;
056: import javax.xml.transform.stream.StreamResult;
057: import org.w3c.dom.Node;
058: import org.xml.sax.InputSource;
059: import org.xml.sax.SAXException;
060:
061: /**
062: *
063: * @author Mridul Muralidharan
064: */
065: public class XMLParserHelper {
066:
067: private static DocumentBuilderFactory _domFactory;
068: private static final String UTF_8 = "UTF-8"; // NOI18N
069: private static final String BODY_TAG = "body"; // NOI18N
070:
071: private static final String SID_ATTRIBUTE = "sid"; // NOI18N
072: private static final String TO_ATTRIBUTE = "to"; // NOI18N
073: private static final String RID_ATTRIBUTE = "rid"; // NOI18N
074:
075: private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; // NOI18N
076:
077: private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; // NOI18N
078:
079: static {
080: _domFactory = DocumentBuilderFactory.newInstance();
081: _domFactory.setValidating(false);
082: _domFactory.setNamespaceAware(false);
083: _domFactory.setIgnoringElementContentWhitespace(true);
084: _domFactory.setIgnoringComments(true);
085: _domFactory.setCoalescing(true);
086: }
087:
088: public static org.w3c.dom.Element parseXMLInput(String input) {
089:
090: org.w3c.dom.Element xmlDoc = null;
091:
092: try {
093: StringReader reader = new StringReader(input);
094: InputSource inputSource = new InputSource(reader);
095:
096: DocumentBuilder builder = _domFactory.newDocumentBuilder();
097: org.w3c.dom.Document doc = builder.parse(inputSource);
098:
099: xmlDoc = doc.getDocumentElement();
100:
101: // Kick this out when we move to using the schema
102: if (!xmlDoc.getTagName().equals(BODY_TAG)) {
103: xmlDoc = null;
104: }
105: } catch (ParserConfigurationException pcEx) {
106: xmlDoc = null;
107: } catch (UnsupportedEncodingException ueEx) {
108: xmlDoc = null;
109: } catch (SAXException saxEx) {
110: xmlDoc = null;
111: } catch (IOException ioEx) {
112: xmlDoc = null;
113: }
114:
115: return xmlDoc;
116: }
117:
118: public static int getIntAttribute(org.w3c.dom.Element bodyElement,
119: String attr) {
120: String attrib = bodyElement.getAttribute(attr);
121: int retval = -1;
122:
123: if (null != attrib && 0 != attrib.trim().length()) {
124: try {
125: retval = Integer.parseInt(attrib);
126: } catch (NumberFormatException nfEx) {
127: retval = -1;
128: }
129: }
130:
131: return retval;
132: }
133:
134: public static long getLongAttribute(
135: org.w3c.dom.Element bodyElement, String attr) {
136: String attrib = bodyElement.getAttribute(attr);
137: long retval = -1L;
138:
139: if (null != attrib && 0 != attrib.trim().length()) {
140: try {
141: retval = Long.parseLong(attrib);
142: } catch (NumberFormatException nfEx) {
143: retval = -1L;
144: }
145: }
146:
147: return retval;
148: }
149:
150: public static String getStringAttribute(
151: org.w3c.dom.Element bodyElement, String attr) {
152: return bodyElement.getAttribute(attr);
153: }
154:
155: private static Transformer xslTransformer = null;
156:
157: protected static Transformer initialiseTransformer()
158: throws TransformerConfigurationException {
159:
160: if (null == xslTransformer) {
161: xslTransformer = TransformerFactory.newInstance()
162: .newTransformer();
163: xslTransformer.setOutputProperty(
164: OutputKeys.OMIT_XML_DECLARATION, "yes"); // NOI18N
165: }
166: return xslTransformer;
167: }
168:
169: public static String nodeToString(Node node) {
170:
171: try {
172: Transformer transformer = initialiseTransformer();
173: DOMSource src = new DOMSource(node);
174: ByteArrayOutputStream baos = new ByteArrayOutputStream();
175: StreamResult res = new StreamResult(baos);
176: transformer.transform(src, res);
177: return new String(baos.toByteArray());
178: }
179: // Remove the debug statements below
180: catch (TransformerConfigurationException tcEx) {
181: tcEx.printStackTrace();
182: } catch (TransformerException tEx) {
183: tEx.printStackTrace();
184: } catch (Exception ex) {
185: ex.printStackTrace();
186: System.err.println("node : " + node); // NOI18N
187: System.err.println("nodeToString for NodeName : "
188: + node.getNodeName()); // NOI18N
189: }
190:
191: return null;
192: }
193: }
|