KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > util > FileUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.axi.util;
20
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.net.URL JavaDoc;
24 import javax.xml.parsers.SAXParser JavaDoc;
25 import javax.xml.parsers.SAXParserFactory JavaDoc;
26 import org.netbeans.modules.xml.axi.AXIComponent;
27 import org.netbeans.modules.xml.axi.AXIComponentFactory;
28 import org.netbeans.modules.xml.axi.AXIDocument;
29 import org.netbeans.modules.xml.axi.AXIModel;
30 import org.netbeans.modules.xml.axi.Attribute;
31 import org.netbeans.modules.xml.axi.Compositor;
32 import org.netbeans.modules.xml.axi.Element;
33 import org.xml.sax.Attributes JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.helpers.DefaultHandler JavaDoc;
36
37 /**
38  *
39  * @author Samaresh
40  */

41 public class FileUtil {
42     
43     private FileReader JavaDoc fileReader;
44     
45     /**
46      * Creates a new instance of FileUtil
47      */

48     private FileUtil() {
49     }
50     
51     public static FileUtil getInstance() {
52         return new FileUtil();
53     }
54     
55     InputSource JavaDoc openFile(URL JavaDoc url) throws Exception JavaDoc {
56         File JavaDoc file = new File JavaDoc(url.toURI());
57         if(file == null || !file.exists()) {
58             return null;
59         }
60         fileReader = new FileReader JavaDoc(file);
61         InputSource JavaDoc inputSource = new InputSource JavaDoc(fileReader);
62         if(inputSource == null) {
63             return null;
64         }
65         
66         return inputSource;
67     }
68     
69     void closeFile() {
70         try {
71             if(fileReader != null)
72             fileReader.close();
73         } catch(Exception JavaDoc ex) {
74             ex.printStackTrace();
75         }
76     }
77
78     /**
79      * Uses SAX parser to parse the input XML file and
80      * creates an AXI tree.
81      */

82     public static void parseXMLAndPopulateAXIModel(URL JavaDoc url, AXIModel model) {
83         FileUtil util = FileUtil.getInstance();
84         MyContentHandler handler = new MyContentHandler(model);
85         try {
86             model.startTransaction();
87             InputSource JavaDoc inputSource = util.openFile(url);
88             SAXParser JavaDoc saxParser = SAXParserFactory.newInstance().newSAXParser();
89             saxParser.parse(inputSource, handler);
90         } catch (Exception JavaDoc ex) {
91             ex.printStackTrace();
92         } finally {
93             try {
94                 model.endTransaction();
95                 util.closeFile();
96             } catch (Exception JavaDoc ex) {
97                 //TODO: model.endTransaction() throws an NPE
98
}
99         }
100     }
101
102     public static class MyContentHandler extends DefaultHandler JavaDoc {
103         private AXIComponent root;
104         private java.util.Stack JavaDoc<AXIComponent> stack;
105         private AXIModel model;
106         private AXIComponentFactory factory;
107         
108         MyContentHandler(AXIModel model) {
109             this.model = model;
110             this.factory = model.getComponentFactory();
111             this.stack = new java.util.Stack JavaDoc<AXIComponent>();
112         }
113                 
114         public void startElement(String JavaDoc uri, String JavaDoc localName,
115                 String JavaDoc qName, Attributes JavaDoc atts) {
116             
117             AXIComponent newChild = createCompositor(qName);
118             if(newChild == null) {
119                 Element element = factory.createElement();
120                 element.setName(qName);
121                 for(int i=0; i<atts.getLength(); i++) {
122                     Attribute attr = factory.createAttribute();
123                     attr.setName(atts.getQName(i));
124                     element.addAttribute(attr);
125                 }
126                 newChild = element;
127             }
128             addChild(newChild);
129         }
130         
131         public void endElement(String JavaDoc uri, String JavaDoc localName,
132                 String JavaDoc qName) {
133             AXIComponent component = stack.peek();
134             if(component instanceof Compositor) {
135                 Compositor c = (Compositor)component;
136                 if(c.toString().equals(qName)) {
137                     stack.pop();
138                 }
139             }
140             if(component instanceof Element) {
141                 Element e = (Element)component;
142                 if(e.getName().equals(qName)) {
143                     stack.pop();
144                 }
145             }
146         }
147         
148         private Compositor createCompositor(String JavaDoc qName) {
149             if(qName.equals("Sequence")) {
150                 return factory.createSequence();
151             }
152             if(qName.equals("Choice")) {
153                 return factory.createChoice();
154             }
155             if(qName.equals("All")) {
156                 return factory.createAll();
157             }
158             
159             return null;
160         }
161         
162         private void addChild(AXIComponent child) {
163             AXIComponent parent = null;
164             if(stack.empty()) {
165                 parent = model.getRoot();
166                 addChild(parent, child);
167                 stack.push(parent);
168             } else {
169                 parent = stack.peek();
170                 addChild(parent, child);
171             }
172             stack.push(child);
173         }
174
175         private void addChild(AXIComponent parent, AXIComponent child) {
176             if(parent instanceof AXIDocument) {
177                 AXIDocument doc = (AXIDocument)parent;
178                 if(child instanceof Element) doc.addElement((Element)child);
179             }
180             
181             if(parent instanceof Element) {
182                 Element element = (Element)parent;
183                 if(child instanceof Attribute) element.addAttribute((Attribute)child);
184                 if(child instanceof Compositor) element.addCompositor((Compositor)child);
185                 if(child instanceof Element) element.addElement((Element)child);
186             }
187
188             if(parent instanceof Compositor) {
189                 Compositor compositor = (Compositor)parent;
190                 if(child instanceof Compositor) compositor.addCompositor((Compositor)child);
191                 if(child instanceof Element) compositor.addElement((Element)child);
192             }
193         }
194         
195     }
196             
197 }
198
Popular Tags