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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils.xml;
038:
039: import java.io.BufferedInputStream;
040: import java.io.BufferedOutputStream;
041: import java.io.ByteArrayInputStream;
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.FileOutputStream;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.InputStreamReader;
048: import java.io.OutputStream;
049: import java.io.OutputStreamWriter;
050: import java.io.PrintWriter;
051: import java.io.Reader;
052: import java.io.UnsupportedEncodingException;
053: import java.io.Writer;
054: import java.nio.charset.Charset;
055: import javax.xml.parsers.DocumentBuilder;
056: import javax.xml.parsers.DocumentBuilderFactory;
057: import javax.xml.parsers.ParserConfigurationException;
058: import javax.xml.transform.Result;
059: import javax.xml.transform.Source;
060: import javax.xml.transform.Transformer;
061: import javax.xml.transform.TransformerConfigurationException;
062: import javax.xml.transform.TransformerException;
063: import javax.xml.transform.TransformerFactory;
064: import javax.xml.transform.dom.DOMSource;
065: import javax.xml.transform.stream.StreamResult;
066: import org.netbeans.installer.utils.StringUtils;
067: import org.netbeans.installer.utils.exceptions.ParseException;
068: import org.w3c.dom.Document;
069: import org.w3c.dom.Element;
070: import org.xml.sax.InputSource;
071: import org.xml.sax.SAXException;
072:
073: /**
074: *
075: * @author Danila_Dugurov
076: */
077: public class DomUtil {
078:
079: private static final DocumentBuilderFactory BUILDER_FACTORY;
080: private static final TransformerFactory TRANSFORMER_FACTORY;
081:
082: static {
083: BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
084: TRANSFORMER_FACTORY = TransformerFactory.newInstance();
085: }
086:
087: public static Document parseXmlFile(File xmlFile)
088: throws IOException, ParseException {
089: return parseXmlFile(xmlFile, null);
090: }
091:
092: public static Document parseXmlFile(File xmlFile, Charset charset)
093: throws IOException, ParseException {
094: final InputStream in = new BufferedInputStream(
095: new FileInputStream(xmlFile));
096: try {
097: return parseXmlFile(in, charset);
098: } finally {
099: try {
100: in.close();
101: } catch (IOException ignord) {//skip
102: }
103: }
104: }
105:
106: public static Document parseXmlFile(CharSequence xmlFile)
107: throws ParseException {
108: try {
109: final InputStream in = new ByteArrayInputStream(xmlFile
110: .toString().getBytes(StringUtils.ENCODING_UTF8));
111: return parseXmlFile(in);
112: } catch (UnsupportedEncodingException worntHappend) {
113: throw new ParseException("utf-8 not supported!",
114: worntHappend);
115: } catch (IOException worntHappend) {
116: throw new ParseException(
117: "fatal error: I/O mustn't happen here.",
118: worntHappend);
119: }
120: }
121:
122: public static Document parseXmlFile(InputStream xmlStream,
123: Charset charset) throws IOException, ParseException {
124: try {
125: final DocumentBuilder builder = BUILDER_FACTORY
126: .newDocumentBuilder();
127: InputSource inputSource;
128: if (charset != null) {
129: final Reader reader = new InputStreamReader(xmlStream,
130: charset);
131: inputSource = new InputSource(reader);
132: } else {
133: inputSource = new InputSource(xmlStream);
134: }
135: return builder.parse(inputSource);
136: } catch (ParserConfigurationException worntHappend) {
137: throw new ParseException("parse configuration error.",
138: worntHappend);
139: } catch (SAXException ex) {
140: throw new ParseException("parsing error occuers!", ex);
141: }
142: }
143:
144: public static Document parseXmlFile(InputStream xmlStream)
145: throws IOException, ParseException {
146: return parseXmlFile(xmlStream, null);
147: }
148:
149: public static void writeXmlFile(Document document,
150: OutputStream outputStream, Charset charset)
151: throws IOException {
152: try {
153: final Source domSource = new DOMSource(document);
154: Result output;
155: if (charset != null) {
156: final Writer writer = new PrintWriter(
157: new OutputStreamWriter(outputStream, charset));
158: output = new StreamResult(writer);
159: } else {
160: output = new StreamResult(outputStream);
161: }
162: final Transformer transformer = TRANSFORMER_FACTORY
163: .newTransformer();
164: transformer.transform(domSource, output);
165: //think :check. is it really flushed here or some action should be done
166: } catch (TransformerConfigurationException worntHappend) {
167: throw new IOException(worntHappend.getMessage());
168: } catch (TransformerException ex) {
169: throw new IOException(ex.getMessage());
170: }
171: }
172:
173: public static void writeXmlFile(Document document,
174: OutputStream outputStream) throws IOException {
175: writeXmlFile(document, outputStream, null);
176: }
177:
178: public static void writeXmlFile(Document document, File file)
179: throws IOException {
180: writeXmlFile(document, file, null);
181: }
182:
183: public static void writeXmlFile(Document document, File file,
184: Charset charset) throws IOException {
185: OutputStream out = new BufferedOutputStream(
186: new FileOutputStream(file));
187: try {
188: writeXmlFile(document, out, charset);
189: } finally {
190: try {
191: out.close();
192: } catch (IOException ignord) {//skip
193: }
194: }
195: }
196:
197: public static <T extends DomExternalizable> void addChild(
198: Element parent, T object) {
199: parent.appendChild(object.writeXML(parent.getOwnerDocument()));
200: }
201:
202: public static void addElement(Element parent, String elemName,
203: String elemText) {
204: final Element child = parent.getOwnerDocument().createElement(
205: elemName);
206: child.setTextContent(elemText);
207: parent.appendChild(child);
208: }
209: }
|