KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.netbeans.modules.xml.api.model.GrammarQuery;
25 import org.netbeans.modules.xml.api.model.GrammarResult;
26 import org.netbeans.modules.xml.api.model.HintContext;
27
28
29
30 /**
31  * Rather simple query implemetation based on XSD grammar.
32  * It is produced by {@link XSDParser}.
33  * Hints given by this grammar do not guarantee that valid XML document is created.
34  *
35  * @author Ales Novak
36  */

37 class XSDGrammar implements GrammarQuery {
38     
39     /** All elements */
40     private Map elements;
41     /** All types */
42     private Map types;
43     /** namespace */
44     private Namespace namespace;
45     
46     private Namespace schemaNamespace;
47     private Namespace targetNamespace;
48
49     /** Creates new XSDGrammar */
50     XSDGrammar(Map elements, Map types, Namespace targetNamespace, Namespace schemaNamespace) {
51         this.elements = elements;
52         this.types = types;
53         this.namespace = null;
54         this.schemaNamespace = schemaNamespace;
55         this.targetNamespace = targetNamespace;
56     }
57
58     /** @return null */
59     public java.awt.Component JavaDoc getCustomizer(HintContext nodeCtx) {
60         return null;
61     }
62     
63     /** @return null */
64     public org.openide.nodes.Node.Property[] getProperties(HintContext nodeCtx) {
65         return null;
66     }
67     
68     /** @return false */
69     public boolean hasCustomizer(HintContext nodeCtx) {
70         return false;
71     }
72     
73     /** not implemented
74      * @return true
75      */

76     public boolean isAllowed(Enumeration en) {
77         return true;
78     }
79     
80     public Enumeration queryAttributes(HintContext ownerElementCtx) {
81         Thread.dumpStack();
82         return Collections.enumeration(new ArrayList());
83     }
84     
85     public GrammarResult queryDefault(HintContext parentNodeCtx) {
86         Thread.dumpStack();
87         return null;
88     }
89     
90     public Enumeration queryElements(HintContext virtualElementCtx) {
91         String JavaDoc parentName = computeUnprefixedName(virtualElementCtx.getParentNode().getNodeName());
92         SchemaElement parent = (SchemaElement) elements.get(parentName);
93         
94         if (parent == null) {
95             return Collections.enumeration(new ArrayList());
96         }
97         
98         ArrayList list = new ArrayList(50);
99         String JavaDoc previous = (virtualElementCtx.getPreviousSibling() == null ? null : computeUnprefixedName(virtualElementCtx.getPreviousSibling().getNodeName()));
100         System.err.println("PREVIOUS: " + previous);
101         //parent.setPrefix(getNamespace().getPrefix());
102

103         resolveChildren(parent, list, getNamespace());
104         if (previous != null) {
105             System.err.println("list size: " + list.size());
106             while (list.size() > 0) {
107                 SchemaElement e = (SchemaElement) list.get(0);
108                 String JavaDoc ename = e.getSAXAttributes().getValue("name");
109                 System.err.println("ENAME: " + ename);
110                 
111                 if (ename == null || (! ename.equalsIgnoreCase(previous))) {
112                     list.remove(0);
113                     System.err.println("REMOVED ENAME");
114                 } else {
115                     list.remove(0);
116                     System.err.println("BUILD DONE");
117                     break;
118                 }
119             }
120         }
121         
122         return Collections.enumeration(list);
123     }
124     
125     /** divide by : to two parts */
126     private String JavaDoc computeUnprefixedName(String JavaDoc s) {
127         assert namespace != null;
128         assert s != null;
129         
130         int i = s.indexOf(':');
131         if (i >= 0) {
132             String JavaDoc ret = s.substring(i + 1);
133             System.err.println("computeUN nsPref: " + namespace.getPrefix() + " name: " + s + " ret: " + ret);
134             assert namespace.getPrefix().equals(s.substring(0, i));
135             return ret;
136         }
137         return s;
138     }
139     
140     private static void resolveChildren(SchemaElement parent, List list, Namespace ns) {
141         Iterator it = parent.getSubelements();
142         while (it.hasNext()) {
143             SchemaElement e = (SchemaElement) it.next();
144             if (e.isComposite()) {
145                 System.err.println("RESOLVE COMPOSITE: " + e.getQname());
146                 resolveChildren(e, list, ns);
147             } else {
148                 System.err.println("ADDING NON COMPOSITE: " + e.getQname());
149                 e.setPrefix(ns.getPrefix());
150                 list.add(e);
151             }
152         }
153     }
154     
155     public Enumeration queryEntities(String JavaDoc prefix) {
156         Thread.dumpStack();
157         return Collections.enumeration(new ArrayList());
158     }
159     
160     public Enumeration queryNotations(String JavaDoc prefix) {
161         Thread.dumpStack();
162         return Collections.enumeration(new ArrayList());
163     }
164     
165     public Enumeration queryValues(HintContext virtualTextCtx) {
166         Thread.dumpStack();
167         return Collections.enumeration(new ArrayList());
168     }
169     
170     /**
171      * Getter for property namespace.
172      * @return Value of property namespace.
173      */

174     public org.netbeans.modules.xml.xsd.Namespace getNamespace() {
175         return namespace;
176     }
177     
178     /**
179      * Setter for property namespace.
180      * @param namespace New value of property namespace.
181      */

182     public void setNamespace(org.netbeans.modules.xml.xsd.Namespace namespace) {
183         this.namespace = namespace;
184     }
185     
186 }
Popular Tags