KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beansdev > GraphNode


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.schema2beansdev;
21
22 import org.netbeans.modules.schema2beans.*;
23
24 import java.util.*;
25
26 /**
27  * This class is a node of the schema graph that the TreeBuilder is creating
28  * from the parsed schema. For every type we get 1 GraphNode. Every java
29  * bean property (or java class attribute), has an associated GraphLink.
30  * Groupings are represented in GraphLink's.
31  *
32  * Each element of the graph tree is a node (root, intermediate node and
33  * leaves). Each node references a GraphLink object which takes care
34  * of linking the different nodes of the graph.
35  *
36  * The schema instances information (*, + or ?) and the relationship
37  * as a sequence (,) or choice (|) information are held in the GraphLink
38  * object. There is only one GraphNode instance of an element in the graph
39  * event if the element is referenced by several graph links.
40  *
41  * A GrapNode is a leaf if its element GraphLink is null.
42  *
43  * To see a pretty graph of GraphNode's and GraphLink's, run with these
44  * options: -t dot -genDotGraph out.dot
45  * And run `dotty out.dot`.
46  *
47  * <xsd:element name="bill">
48  * <xsd:complexType>
49  * <xsd:sequence maxOccurs="unbounded">
50  * <xsd:element name="shipTo" type="addressType"/>
51  * <xsd:element name="billTo" type="addressType"/>
52  * </xsd:sequence>
53  * </xsd:complexType>
54  * </xsd:element>
55  * ...
56  * 1 GraphNode for type "bill"
57  * 1 GraphLink for xsd:sequence (storing the grouping info)
58  * 1 GraphNode for type "addressType"
59  * 1 GraphLink for "shipTo" java bean property
60  * 1 GraphLink for "billTo" java bean property
61  */

62 public class GraphNode {
63     /**
64      * The name of the element as it appears in the schema. In a DTD, this
65      * is unique within the tree since each DTD element
66      * can only be defined once. An XML Schema may reuse an element name.
67      */

68     private String JavaDoc name;
69     private String JavaDoc namespace = null;
70
71     /**
72      * The unique name of the element in the schema.
73      */

74     private String JavaDoc uniqueName;
75
76     /**
77      * The reference to the link structure defining all the siblings and
78      * subnode of this element, as defined in the DTD.
79      *
80      * For example, if we have <!ELEMENT a (b?, (c | d)+)>
81      * This GraphNode name is a, and the link points to a GraphLink
82      * object reference the GraphNode b and pointing to another
83      * graphLink object (a subnode link), defining the (c | d)
84      * declaration:
85      *
86      * Node[a]
87      * |------> Link(null, ',')
88      * |
89      * / \
90      * / \
91      * V V
92      * Link(b, '?')->Node(b) Link(null, '+')
93      * |
94      * Link(null, '|')
95      * / \
96      * / \
97      * V V
98      * Link(c)->Node(c) Link(d)->Node(d)
99      *
100      * @see GraphLink
101      *
102      */

103     private GraphLink link;
104     
105     /**
106      * Set to true by the TreeBuilder class when the schema parser calls back
107      * with the element name definition. (This is object might be created
108      * when referenced, before the element definition is found in the DTD)
109      * If this stays false, then it's created outside of our code generation
110      * (e.g., java.lang.String).
111      */

112     private boolean created;
113     
114     /**
115      * Number of time this node is referenced in the graph.
116      */

117     private int refCount;
118     
119     /**
120      * Object reference where the bean builder can store whatever it needs
121      */

122     private Object JavaDoc object;
123     
124     /**
125      * List of the attributes associated to this element (might be null)
126      */

127     private List attributes;
128     
129     // Used to mark an element to avoid infinite loop for recurse parsing
130
private boolean marked;
131
132     // Used to suggest what type the of the property should be, only
133
// really useful on leaf nodes.
134
private String JavaDoc javaType;
135
136     private Map extendedProperties = new LinkedHashMap();
137
138     private Set extraData = new LinkedHashSet();
139     private boolean isUnion = false;
140     private boolean isAbstract = false;
141
142     private GraphNode extendsNode;
143     private GraphNode alias;
144     
145     GraphNode(String JavaDoc name, String JavaDoc uniqueName) {
146         //System.out.println("Created GraphNode "+uniqueName+" name="+name);
147
setName(name);
148         this.uniqueName = uniqueName;
149         this.created = false;
150         this.attributes = null;
151         this.marked = false;
152     }
153     
154     void addAttribute(AttrProp attr) {
155         if (alias != null) {
156             alias.addAttribute(attr);
157             return;
158         }
159         if (this.attributes == null)
160             this.attributes = new ArrayList();
161         this.attributes.add(attr);
162         //attr.setSourceGraphNode(this);
163
}
164     
165     AttrProp[] getAttributes() {
166         if (alias != null) {
167             return alias.getAttributes();
168         }
169         int size = 0;
170     
171         if (this.attributes != null)
172             size = this.attributes.size();
173     
174         AttrProp[] ret = new AttrProp[size];
175     
176         if (size > 0)
177             return (AttrProp[])this.attributes.toArray(ret);
178         else
179             return ret;
180     }
181     
182     void setObject(Object JavaDoc obj) {
183         this.object = obj;
184     }
185     
186     Object JavaDoc getObject() {
187         return this.object;
188     }
189     
190     String JavaDoc getNameWithNamespace() {
191         if (namespace == null)
192             return name;
193         return "{"+namespace+"}"+name;
194     }
195
196     /**
197      * This method will return the name without any namespace info.
198      */

199     String JavaDoc getName() {
200         return name;
201     }
202
203     /**
204      * May return null.
205      */

206     String JavaDoc getNamespace() {
207         if (alias != null)
208             return alias.getNamespace();
209         return namespace;
210     }
211     
212     void setName(String JavaDoc name) {
213         // separate out the namespace
214
if (name.charAt(0) == '{') {
215             int closingBracket = name.indexOf('}');
216             this.name = name.substring(closingBracket+1, name.length());
217             this.namespace = name.substring(1, closingBracket);
218         } else {
219             this.name = name;
220             this.namespace = null;
221         }
222     }
223     
224     GraphLink getGraphLink() {
225         if (alias != null)
226             return alias.getGraphLink();
227         return this.link;
228     }
229
230     void setGraphLink(GraphLink l) {
231         if (alias != null) {
232             alias.setGraphLink(l);
233             return;
234         }
235         link = l;
236     }
237
238     void setMarked(boolean value) {
239         if (alias != null) {
240             alias.setMarked(value);
241             return;
242         }
243         marked = value;
244     }
245
246     boolean getMarked() {
247         if (alias != null) {
248             return alias.getMarked();
249         }
250         return marked;
251     }
252     
253     boolean isAbstract() {
254         return isAbstract;
255     }
256     
257     void setAbstract(boolean value) {
258         isAbstract = value;
259     }
260     
261     void setCreated(boolean value) {
262         if (alias != null) {
263             alias.setCreated(value);
264             return;
265         }
266         created = value;
267     }
268
269     boolean isCreated() {
270         if (alias != null) {
271             return alias.isCreated();
272         }
273         return created;
274     }
275
276     /**
277      * These are generic properties that the schema parser wants to pass
278      * to the code generation phase.
279      */

280     public void setExtendedProperty(String JavaDoc name, Object JavaDoc value) {
281         if (alias != null) {
282             alias.setExtendedProperty(name, value);
283             return;
284         }
285         extendedProperties.put(name, value);
286     }
287
288     /**
289      * These are generic properties that the schema parser wants to pass
290      * to the code generation phase.
291      */

292     public Object JavaDoc getExtendedProperty(String JavaDoc name) {
293         if (alias != null) {
294             return alias.getExtendedProperty(name);
295         }
296         return extendedProperties.get(name);
297     }
298
299     void incrRefCount() {
300         /*
301         if (alias != null) {
302             alias.incrRefCount();
303             return;
304         }
305         */

306         ++refCount;
307     }
308
309     int getRefCount() {
310         /*
311         if (alias != null) {
312             return alias.getRefCount();
313         }
314         */

315         return refCount;
316     }
317
318     void setAlias(GraphNode n) {
319         alias = n;
320     }
321
322     GraphNode getAlias() {
323         return alias;
324     }
325
326     /**
327      * Returns the list of all the GraphNodes used directly by this
328      * element (this represent all the elements used to declare the
329      * current GraphNode definition in the DTD (only one level
330      * of the subtree of this element).
331      */

332     GraphNode[] getNodes() {
333         if (alias != null)
334             return alias.getNodes();
335         Map list = new HashMap();
336         gatherElements(this.link, list);
337         GraphNode[] ret = new GraphNode[list.size()];
338         return (GraphNode[])list.values().toArray(ret);
339     }
340     
341     //
342
// Fill the hash table with the elements referenced by this element
343
// (All the elements in the right part of the DTD declaration)
344
//
345
private void gatherElements(GraphLink l, Map list) {
346         while (l != null) {
347             if (l.element != null)
348                 list.put(l.element.getName(), l.element);
349         
350             gatherElements(l.getFirstChild(), list);
351         
352             l = l.getSibling();
353         }
354     }
355
356     public void setJavaType(String JavaDoc jt) {
357         if (alias != null) {
358             alias.setJavaType(jt);
359             return;
360         }
361         javaType = jt;
362     }
363
364     public String JavaDoc getJavaType() {
365         if (alias != null) {
366             return alias.getJavaType();
367         }
368         return javaType;
369     }
370     
371     public void setExtension(GraphNode extendsNode) {
372         if (alias != null) {
373             alias.setExtension(extendsNode);
374             return;
375         }
376         this.extendsNode = extendsNode;
377     }
378     
379     public GraphNode getExtension() {
380         if (alias != null)
381             return alias.extendsNode;
382         return extendsNode;
383     }
384
385     public boolean isUnion() {
386         return isUnion;
387     }
388
389     public void setUnion(boolean value) {
390         isUnion = value;
391     }
392
393     public void addExtraData(Object JavaDoc data) {
394         extraData.add(data);
395     }
396
397     public void addExtraDataIncludeAlias(Object JavaDoc data) {
398         extraData.add(data);
399         if (alias != null)
400             alias.addExtraData(data);
401     }
402
403     public Object JavaDoc searchExtraData(Class JavaDoc type) {
404         for (Iterator it = extraData.iterator(); it.hasNext(); ) {
405             Object JavaDoc o = it.next();
406             //System.out.println("searchExtraData: o="+o);
407
if (type.isAssignableFrom(o.getClass()))
408                 return o;
409         }
410         return null;
411     }
412
413     public Iterator extraDataIterator() {
414         return extraData.iterator();
415     }
416
417     public Set getExtraData() {
418         return extraData;
419     }
420     
421     public String JavaDoc toString() {
422         if (alias != null) {
423             return name+" (is a "+alias.toString()+")";
424         }
425         return getNameWithNamespace();
426     }
427 }
428
Popular Tags