KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdrxml > FillRepositoryAction


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 Forte for Java, Community Edition. The Initial
16  * Developer of the Original Software is Sun Microsystems, Inc. Portions
17  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.mdrxml;
21
22 import org.xml.sax.*;
23
24 import java.util.*;
25 import java.io.*;
26
27 import org.openide.*;
28 import org.openide.filesystems.*;
29 import org.openide.loaders.*;
30 import org.openide.xml.*;
31 import org.openide.util.*;
32
33 //import org.netbeans.modules.xml.core.tree.ModuleEntityResolver;
34

35 import org.netbeans.api.mdr.*;
36 import xmlmodel.*;
37
38 import javax.jmi.reflect.RefPackage;
39 import javax.jmi.model.*;
40
41 /**
42  * Simple MDR filler. Waring: It does not work for mixed content XML files.
43  *
44  * @author Petr Kuzel
45  */

46 public class FillRepositoryAction extends org.netbeans.modules.mdrxml.util.XMLToolsAction {
47     
48     private static final boolean DEBUG = false;
49     
50     // shortcuts
51
xmlmodel.Contains contains;
52     xmlmodel.TextNodeClass textNodeClass;
53     xmlmodel.RootNodeClass rootNodeClass;
54     xmlmodel.AttributeNodeClass attributeClass;
55     xmlmodel.ElementNodeClass nodeClass;
56     
57     /** Creates a new instance of FillRepositoryAction */
58     public FillRepositoryAction() {
59     }
60     
61     public org.openide.util.HelpCtx getHelpCtx() {
62         return null;
63     }
64     
65     public String JavaDoc getName() {
66         return "Fill MDRepository";
67     }
68     
69     protected void performXMLAction(FileObject fo) {
70         
71 // EntityResolver resolver = (ModuleEntityResolver) Lookup.getDefault().lookup(ModuleEntityResolver.class);
72

73         // create model
74

75         // find repository implementation using MDRManager
76
MDRepository repository = MDRManager.getDefault().getDefaultRepository();
77         TopManager tm = TopManager.getDefault();
78         
79         // get package, fill classes
80
XmlmodelPackage extent = (XmlmodelPackage) repository.getExtent(MDRXMLModule.XML_MODEL);
81         contains = extent.getContains();
82         textNodeClass = extent.getTextNode();
83         rootNodeClass = extent.getRootNode();
84         attributeClass = extent.getAttributeNode();
85         nodeClass = extent.getElementNode();
86         
87         // put it into repository, how to specify where? an instance?
88
try {
89             repository.beginTrans(true);
90             tm.setStatusText ("XML parsing started.");
91             
92             String JavaDoc id = fo.getURL().toExternalForm();
93             if (DEBUG) System.err.println("URL:" + id);
94             ParserImpl impl = new ParserImpl(id);
95             XMLReader reader = XMLUtil.createXMLReader();
96 // reader.setEntityResolver(resolver);
97

98             reader.setErrorHandler( new ErrorHandler() {
99                 public void error(SAXParseException ex) throws SAXException {
100                     throw ex;
101                 }
102                 public void warning(SAXParseException ex) throws SAXException {
103                     throw ex;
104                 }
105                 
106                 public void fatalError(SAXParseException ex) throws SAXException {
107                     throw ex;
108                 }
109                 
110             });
111             InputSource in = new InputSource(id);
112             in.setByteStream(fo.getInputStream());
113             reader.setContentHandler(impl);
114             reader.parse(in);
115         } catch (IOException ex) {
116             //!!! handle somehow
117
tm.notifyException(ex);
118         } catch (SAXException ex) {
119             //!!! handle somehow
120
// it is user document error!
121
tm.notifyException(ex);
122         } finally {
123             repository.endTrans();
124             tm.setStatusText ("XML parsing finished.");
125         }
126     }
127     
128     private class ParserImpl implements ContentHandler {
129         
130         private final StringBuffer JavaDoc content = new StringBuffer JavaDoc();
131         private boolean root = true;
132         private final Stack parents = new Stack();
133         private final String JavaDoc name;
134         
135         public ParserImpl(String JavaDoc documentID) {
136             name = documentID;
137         }
138         
139         public void characters(char[] values, int offset, int len) throws org.xml.sax.SAXException JavaDoc {
140             if (DEBUG) System.err.println("characters()");
141             content.append(values, offset, len);
142         }
143         
144         public void endDocument() throws org.xml.sax.SAXException JavaDoc {
145         }
146
147         public void startDocument() throws org.xml.sax.SAXException JavaDoc {
148             parents.push(null);
149         }
150         
151         public void startElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname, org.xml.sax.Attributes JavaDoc attrs) throws org.xml.sax.SAXException JavaDoc {
152             
153             if (DEBUG) System.err.println("startElement");
154             
155             String JavaDoc data = content.toString();
156             content.setLength(0);
157             ElementNode me = (ElementNode) parents.peek();
158             if (data.trim().length() > 0) {
159                 Node text = textNodeClass.createTextNode(data);
160                 
161                 // create Contains: text, me
162
contains.add(text, me);
163             }
164             
165             // handle root node
166
ElementNode node;
167             if (root) {
168                 node = rootNodeClass.createRootNode(qname, name);
169                 root = false;
170             } else {
171                 node = nodeClass.createElementNode(qname);
172             }
173             
174             // fill attributes
175
for (int i = 0; i<attrs.getLength(); i++) {
176                 String JavaDoc name = attrs.getQName(i);
177                 String JavaDoc val = attrs.getValue(i);
178                 xmlmodel.AttributeNode attr = attributeClass.createAttributeNode(name, val);
179                 contains.add(attr, node);
180             }
181             
182             // note it as parent
183
parents.push(node);
184         }
185         
186         public void endElement(String JavaDoc str, String JavaDoc str1, String JavaDoc str2) throws org.xml.sax.SAXException JavaDoc {
187             
188             if (DEBUG) System.err.println("endElement");
189             
190             String JavaDoc data = content.toString();
191             content.setLength(0);
192             ElementNode me = (ElementNode) parents.pop();
193             if (data.trim().length() > 0) {
194                 Node text = textNodeClass.createTextNode(data);
195                 
196                 // create Contains: text, me
197
contains.add(text, me);
198             }
199             
200             ElementNode parent = (ElementNode) parents.peek();
201             if (parent != null) {
202                 // create Contains: me, parent
203
contains.add(me, parent);
204             }
205         }
206         
207         public void endPrefixMapping(String JavaDoc str) throws org.xml.sax.SAXException JavaDoc {
208         }
209         
210         public void ignorableWhitespace(char[] values, int param, int param2) throws org.xml.sax.SAXException JavaDoc {
211         }
212         
213         public void processingInstruction(String JavaDoc str, String JavaDoc str1) throws org.xml.sax.SAXException JavaDoc {
214         }
215         
216         public void setDocumentLocator(org.xml.sax.Locator JavaDoc locator) {
217         }
218         
219         public void skippedEntity(String JavaDoc str) throws org.xml.sax.SAXException JavaDoc {
220         }
221                         
222         public void startPrefixMapping(String JavaDoc str, String JavaDoc str1) throws org.xml.sax.SAXException JavaDoc {
223         }
224         
225     }
226 }
227
Popular Tags