KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xsd > XSDGrammarResolver


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
20 package org.netbeans.modules.xml.xsd;
21
22 import java.util.*;
23 import org.w3c.dom.Node JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25 import org.xml.sax.EntityResolver JavaDoc;
26
27
28 import org.openide.filesystems.FileObject;
29 import org.netbeans.modules.xml.api.model.*;
30 import org.netbeans.api.xml.services.UserCatalog;
31
32 /**
33  * Resolves XSDGrammars and Namespaces
34  * @author Ales Novak
35  */

36 class XSDGrammarResolver implements GrammarQuery {
37     
38     /** Map<String, Namespace> find ns for a prefix*/
39     private final Map prefix2Namespace;
40     /** Map<String,Namespace> find ns for na URI*/
41     private final Map uri2Namespace;
42     /** Default Namespace of top levele Element */
43     private Namespace defaultNamespace;
44     
45     private Node JavaDoc target;
46     private FileObject document;
47     
48     /** Creates new XSDGrammar */
49     private XSDGrammarResolver() {
50         this.uri2Namespace = new HashMap();
51         this.prefix2Namespace = new HashMap();
52         this.defaultNamespace = null;
53         this.target = null;
54         this.document = null;
55     }
56     
57     /** Factory method */
58     public static XSDGrammarResolver createResolver(org.netbeans.modules.xml.api.model.GrammarEnvironment ctx) {
59         XSDGrammarResolver ret = null;
60         
61         Enumeration en = ctx.getDocumentChildren();
62         while (en.hasMoreElements()) {
63             Node JavaDoc next = (Node JavaDoc) en.nextElement();
64             // resolve top level
65
if (next.getNodeType() == next.ELEMENT_NODE) {
66                 org.w3c.dom.Element JavaDoc element = (org.w3c.dom.Element JavaDoc) next;
67                 org.w3c.dom.NamedNodeMap JavaDoc atts = element.getAttributes();
68                 String JavaDoc eprefix = Namespace.getPrefix(element.getNodeName());
69                 
70                 // process namespaces
71
for (int i = 0; i < atts.getLength(); i++) {
72                     Node JavaDoc attribute = atts.item(i);
73                     String JavaDoc name = attribute.getNodeName();
74                     if (name.startsWith(Namespace.XMLNS_ATTR)) {
75                         String JavaDoc uri = attribute.getNodeValue();
76                         String JavaDoc prefix = Namespace.getSufix(name);
77                         Namespace ns = new Namespace(uri, prefix);
78                         if (ret == null) {
79                             ret = new XSDGrammarResolver();
80                         }
81                         ret.addNamespace(ns);
82                         
83                         if (prefix == null || prefix.equals(eprefix)) {
84                             ret.defaultNamespace = ns;
85                         }
86                     }
87                 }
88                 
89                 if (ret == null) {
90                     continue;
91                 }
92                 
93                 assert ret.defaultNamespace != null;
94                 
95                 // find location of schema
96
ret.resolveSchemaLocation(element);
97
98                 if (ret.defaultNamespace.getSchemaLocation() == null) {
99                     // bail out
100
org.openide.ErrorManager.getDefault().log(org.openide.ErrorManager.WARNING, "SCHEMA is null: " + element.getLocalName());
101                     // not necessarily a bad thing - namespace URI should suffice
102
}
103                 
104                 ret.setTarget(next);
105                 ret.setDocument(ctx.getFileObject());
106                 System.err.println("create resolver - success");
107                 return ret;
108             } // if Element Node
109
} // while
110

111         return null;
112     }
113     
114     /** sets location of schema into default namespace */
115     private void resolveSchemaLocation(org.w3c.dom.Element JavaDoc element) {
116         // find location of schema
117
Namespace ns = findNamespaceByURI(Namespace.XSI_NAMESPACE_URI);
118         String JavaDoc prefix = ns.getPrefix().concat(":");
119         String JavaDoc schema = element.getAttribute(prefix.concat(Namespace.XSI_LOCATION));
120         if (schema == null) {
121             schema = element.getAttribute(prefix.concat(Namespace.XSI_NO_NAMESPACE_LOCATION));
122         }
123         
124         System.err.println("SCHEMA LOC: " + schema);
125         defaultNamespace.setSchemaLocation(schema);
126     }
127     
128     private void setDocument(FileObject fileObject) {
129         this.document = fileObject;
130     }
131     
132     private XSDGrammar findGrammar(HintContext virtualElementCtx) throws java.io.IOException JavaDoc {
133         Namespace ns = findNamespace(virtualElementCtx);
134         XSDGrammar grammar = ns.getGrammar();
135         if (grammar == null) {
136             grammar = createGrammar(ns);
137             ns.setGrammar(grammar);
138             grammar.setNamespace(ns);
139         }
140         
141         return grammar;
142     }
143     
144     private XSDGrammar createGrammar(Namespace ns) throws java.io.IOException JavaDoc {
145     String JavaDoc uri = ns.getSchemaLocation(); // either use schemaLocation or URI of xmlns="URI"
146

147         if (uri == null) {
148             uri = ns.getURI();
149         }
150         System.err.println("findSchema: " + uri);
151         
152         int idx = uri.indexOf(' ');
153         if (idx >= 0) {
154             uri = uri.substring(idx + 1);
155         }
156         
157         // first try std way
158
try {
159             UserCatalog catalog = UserCatalog.getDefault();
160             if (catalog != null) {
161                 EntityResolver JavaDoc resolver = catalog.getEntityResolver();
162                 if (resolver != null) {
163                     InputSource JavaDoc inputSource = resolver.resolveEntity(uri, null);
164                     if (inputSource != null) {
165                         return new XSDParser().parse(inputSource);
166                     }
167                 }
168             }
169         } catch (org.xml.sax.SAXException JavaDoc e) {
170             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.EXCEPTION, e);
171         }
172         
173         // try an URL first
174
try {
175             java.net.URL JavaDoc url = new java.net.URL JavaDoc(uri);
176             return new XSDParser().parse(new InputSource JavaDoc(url.openStream()));
177         } catch (java.net.MalformedURLException JavaDoc e) { // sort of expected
178
// debug only
179
// ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "URL not found: " + schema);
180
}
181         
182         // try files
183
if (document == null) {
184             return null;
185         }
186         
187         FileObject fo = document.getParent().getFileObject(uri);
188
189         if (fo == null) {
190             // debug only
191
// ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "File not found: " + schema);
192
return null;
193         }
194
195         return new XSDParser().parse(new InputSource JavaDoc(fo.getInputStream()));
196     }
197     
198     private void addNamespace(Namespace ns) {
199         if (ns.getPrefix() != null) {
200             prefix2Namespace.put(ns.getPrefix(), ns);
201         }
202         
203         uri2Namespace.put(ns.getURI(), ns);
204     }
205     
206     private Namespace findNamespaceByURI(String JavaDoc uri) {
207         return (Namespace) uri2Namespace.get(uri);
208     }
209     
210     private Namespace findNamespace(Node JavaDoc node) {
211         // [ TODO] examine node or search through parents up to defaultNamespace
212
System.err.println("find namespace");
213         System.err.println("PARENT: " + node.getParentNode().getNodeName());
214         /*
215         node = node.getParentNode();
216         System.err.println("findNamespace: " + node);
217         System.err.flush();
218         try {
219             System.err.println("resolved namespace URI: " + node.getNamespaceURI());
220         } catch (Exception e) {
221             e.printStackTrace();
222         }
223         System.err.flush();
224         try {
225             System.err.println(" prefix: " + node.getPrefix());
226         } catch (Exception e) {
227             e.printStackTrace();
228         }
229         System.err.flush();
230          */

231         return defaultNamespace;
232     }
233     
234     /** not implemented
235      * @return true
236      */

237     public boolean isAllowed(Enumeration en) {
238         return true;
239     }
240     
241     public Enumeration queryAttributes(HintContext ownerElementCtx) {
242         Thread.dumpStack();
243         return Collections.enumeration(new ArrayList());
244     }
245     
246     public GrammarResult queryDefault(HintContext parentNodeCtx) {
247         Thread.dumpStack();
248         return null;
249     }
250     
251     public Enumeration queryElements(HintContext virtualElementCtx) {
252         try {
253             XSDGrammar grammar = findGrammar(virtualElementCtx);
254             return grammar.queryElements(virtualElementCtx);
255         } catch (java.io.IOException JavaDoc e) {
256             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.EXCEPTION, e);
257             return Collections.enumeration(new ArrayList(0));
258         }
259     }
260     
261     public Enumeration queryEntities(String JavaDoc prefix) {
262         Thread.dumpStack();
263         return Collections.enumeration(new ArrayList());
264     }
265     
266     public Enumeration queryNotations(String JavaDoc prefix) {
267         Thread.dumpStack();
268         return Collections.enumeration(new ArrayList());
269     }
270     
271     public Enumeration queryValues(HintContext virtualTextCtx) {
272         Thread.dumpStack();
273         return Collections.enumeration(new ArrayList());
274     }
275
276     // Legacy methods
277

278     /** @return null */
279     public java.awt.Component JavaDoc getCustomizer(HintContext nodeCtx) {
280         return null;
281     }
282     
283     /** @return null */
284     public org.openide.nodes.Node.Property[] getProperties(HintContext nodeCtx) {
285         return null;
286     }
287     
288     /** @return false */
289     public boolean hasCustomizer(HintContext nodeCtx) {
290         return false;
291     }
292     
293     /**
294      * Getter for property target.
295      * @return Value of property target.
296      */

297     public org.w3c.dom.Node JavaDoc getTarget() {
298         return target;
299     }
300     
301     /**
302      * Setter for property target.
303      * @param target New value of property target.
304      */

305     public void setTarget(org.w3c.dom.Node JavaDoc target) {
306         this.target = target;
307     }
308     
309 }
Popular Tags