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: */package org.netbeans.modules.vmd.midp.converter.wizard;
041:
042: import org.netbeans.modules.vmd.api.model.Debug;
043: import org.openide.filesystems.FileLock;
044: import org.openide.filesystems.FileObject;
045: import org.openide.filesystems.FileSystem;
046: import org.w3c.dom.*;
047: import org.xml.sax.ErrorHandler;
048: import org.xml.sax.InputSource;
049: import org.xml.sax.SAXException;
050: import org.xml.sax.SAXParseException;
051:
052: import java.io.IOException;
053: import java.io.InputStream;
054: import java.util.ArrayList;
055: import java.util.List;
056:
057: /**
058: * @author David Kaspar
059: */
060: public class XMLUtil {
061:
062: static Node getRootNode(final FileObject fileObject)
063: throws IOException {
064: final Node[] node = new Node[1];
065: fileObject.getFileSystem().runAtomicAction(
066: new FileSystem.AtomicAction() {
067: public void run() throws IOException {
068: Document document = null;
069: if (fileObject != null) {
070: FileLock lock = null;
071: try {
072: lock = fileObject.lock();
073: document = getXMLDocument(fileObject
074: .getInputStream());
075: } finally {
076: if (lock != null)
077: lock.releaseLock();
078: }
079: }
080: node[0] = document != null ? document
081: .getFirstChild() : null;
082: }
083: });
084: return node[0];
085: }
086:
087: private static Document getXMLDocument(InputStream is)
088: throws IOException {
089: Document doc = null;
090: try {
091: doc = org.openide.xml.XMLUtil.parse(new InputSource(is),
092: false, false, new ErrorHandler() {
093: public void error(SAXParseException e)
094: throws SAXException {
095: throw new SAXException(e);
096: }
097:
098: public void fatalError(SAXParseException e)
099: throws SAXException {
100: throw new SAXException(e);
101: }
102:
103: public void warning(SAXParseException e) {
104: Debug.warning(e);
105: }
106: }, null);
107: } catch (SAXException e) {
108: throw Debug.error(e);
109: } finally {
110: try {
111: is.close();
112: } catch (IOException e) {
113: throw Debug.error(e);
114: }
115: }
116: return doc;
117: }
118:
119: static Node[] getChildren(Node node) {
120: NodeList childNodes = node.getChildNodes();
121: Node[] nodes = new Node[childNodes != null ? childNodes
122: .getLength() : 0];
123: for (int i = 0; i < nodes.length; i++)
124: nodes[i] = childNodes.item(i);
125: return nodes;
126: }
127:
128: static List<Node> getChildren(Node node, String nodeName) {
129: ArrayList<Node> children = new ArrayList<Node>();
130: NodeList nodes = node.getChildNodes();
131: for (int i = 0; i < nodes.getLength(); i++) {
132: Node child = nodes.item(i);
133: if (nodeName.equals(child.getNodeName()))
134: children.add(child);
135: }
136: return children;
137: }
138:
139: static Node getChild(Node node, String nodeName) {
140: NodeList nodes = node.getChildNodes();
141: for (int i = 0; i < nodes.getLength(); i++) {
142: Node child = nodes.item(i);
143: if (nodeName.equals(child.getNodeName()))
144: return child;
145: }
146: return null;
147: }
148:
149: static String getAttributeValue(Node node, String attr) {
150: try {
151: if (node != null) {
152: NamedNodeMap map = node.getAttributes();
153: if (map != null) {
154: node = map.getNamedItem(attr);
155: if (node != null)
156: return node.getNodeValue();
157: }
158: }
159: } catch (DOMException e) {
160: Debug.warning(e);
161: }
162: return null;
163: }
164:
165: }
|