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.modules.xml.axi.util;
042:
043: import java.io.File;
044: import java.io.FileReader;
045: import java.net.URL;
046: import javax.xml.parsers.SAXParser;
047: import javax.xml.parsers.SAXParserFactory;
048: import org.netbeans.modules.xml.axi.AXIComponent;
049: import org.netbeans.modules.xml.axi.AXIComponentFactory;
050: import org.netbeans.modules.xml.axi.AXIDocument;
051: import org.netbeans.modules.xml.axi.AXIModel;
052: import org.netbeans.modules.xml.axi.Attribute;
053: import org.netbeans.modules.xml.axi.Compositor;
054: import org.netbeans.modules.xml.axi.Element;
055: import org.xml.sax.Attributes;
056: import org.xml.sax.InputSource;
057: import org.xml.sax.helpers.DefaultHandler;
058:
059: /**
060: *
061: * @author Samaresh
062: */
063: public class FileUtil {
064:
065: private FileReader fileReader;
066:
067: /**
068: * Creates a new instance of FileUtil
069: */
070: private FileUtil() {
071: }
072:
073: public static FileUtil getInstance() {
074: return new FileUtil();
075: }
076:
077: InputSource openFile(URL url) throws Exception {
078: File file = new File(url.toURI());
079: if (file == null || !file.exists()) {
080: return null;
081: }
082: fileReader = new FileReader(file);
083: InputSource inputSource = new InputSource(fileReader);
084: if (inputSource == null) {
085: return null;
086: }
087:
088: return inputSource;
089: }
090:
091: void closeFile() {
092: try {
093: if (fileReader != null)
094: fileReader.close();
095: } catch (Exception ex) {
096: ex.printStackTrace();
097: }
098: }
099:
100: /**
101: * Uses SAX parser to parse the input XML file and
102: * creates an AXI tree.
103: */
104: public static void parseXMLAndPopulateAXIModel(URL url,
105: AXIModel model) {
106: FileUtil util = FileUtil.getInstance();
107: MyContentHandler handler = new MyContentHandler(model);
108: try {
109: model.startTransaction();
110: InputSource inputSource = util.openFile(url);
111: SAXParser saxParser = SAXParserFactory.newInstance()
112: .newSAXParser();
113: saxParser.parse(inputSource, handler);
114: } catch (Exception ex) {
115: ex.printStackTrace();
116: } finally {
117: try {
118: model.endTransaction();
119: util.closeFile();
120: } catch (Exception ex) {
121: //TODO: model.endTransaction() throws an NPE
122: }
123: }
124: }
125:
126: public static class MyContentHandler extends DefaultHandler {
127: private AXIComponent root;
128: private java.util.Stack<AXIComponent> stack;
129: private AXIModel model;
130: private AXIComponentFactory factory;
131:
132: MyContentHandler(AXIModel model) {
133: this .model = model;
134: this .factory = model.getComponentFactory();
135: this .stack = new java.util.Stack<AXIComponent>();
136: }
137:
138: public void startElement(String uri, String localName,
139: String qName, Attributes atts) {
140:
141: AXIComponent newChild = createCompositor(qName);
142: if (newChild == null) {
143: Element element = factory.createElement();
144: element.setName(qName);
145: for (int i = 0; i < atts.getLength(); i++) {
146: Attribute attr = factory.createAttribute();
147: attr.setName(atts.getQName(i));
148: element.addAttribute(attr);
149: }
150: newChild = element;
151: }
152: addChild(newChild);
153: }
154:
155: public void endElement(String uri, String localName,
156: String qName) {
157: AXIComponent component = stack.peek();
158: if (component instanceof Compositor) {
159: Compositor c = (Compositor) component;
160: if (c.toString().equals(qName)) {
161: stack.pop();
162: }
163: }
164: if (component instanceof Element) {
165: Element e = (Element) component;
166: if (e.getName().equals(qName)) {
167: stack.pop();
168: }
169: }
170: }
171:
172: private Compositor createCompositor(String qName) {
173: if (qName.equals("Sequence")) {
174: return factory.createSequence();
175: }
176: if (qName.equals("Choice")) {
177: return factory.createChoice();
178: }
179: if (qName.equals("All")) {
180: return factory.createAll();
181: }
182:
183: return null;
184: }
185:
186: private void addChild(AXIComponent child) {
187: AXIComponent parent = null;
188: if (stack.empty()) {
189: parent = model.getRoot();
190: addChild(parent, child);
191: stack.push(parent);
192: } else {
193: parent = stack.peek();
194: addChild(parent, child);
195: }
196: stack.push(child);
197: }
198:
199: private void addChild(AXIComponent parent, AXIComponent child) {
200: if (parent instanceof AXIDocument) {
201: AXIDocument doc = (AXIDocument) parent;
202: if (child instanceof Element)
203: doc.addElement((Element) child);
204: }
205:
206: if (parent instanceof Element) {
207: Element element = (Element) parent;
208: if (child instanceof Attribute)
209: element.addAttribute((Attribute) child);
210: if (child instanceof Compositor)
211: element.addCompositor((Compositor) child);
212: if (child instanceof Element)
213: element.addElement((Element) child);
214: }
215:
216: if (parent instanceof Compositor) {
217: Compositor compositor = (Compositor) parent;
218: if (child instanceof Compositor)
219: compositor.addCompositor((Compositor) child);
220: if (child instanceof Element)
221: compositor.addElement((Element) child);
222: }
223: }
224:
225: }
226:
227: }
|