KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > TestComponent2


1 package org.netbeans.modules.xml.xam;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.List JavaDoc;
6 import javax.xml.namespace.QName JavaDoc;
7 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
8 import org.netbeans.modules.xml.xam.dom.AbstractNamedComponentReference;
9 import org.netbeans.modules.xml.xam.dom.Attribute;
10 import org.w3c.dom.Element JavaDoc;
11 import org.w3c.dom.NodeList JavaDoc;
12
13 /**
14  *
15  * @author Nam Nguyen
16  */

17 public class TestComponent2 extends AbstractDocumentComponent<TestComponent2> implements NamedReferenceable<TestComponent2> {
18     public static String JavaDoc NS_URI = "http://www.test.com/TestModel";
19     public static String JavaDoc NS2_URI = "http://www.test2.com/TestModel";
20     public static QName JavaDoc ROOT_QNAME = new QName JavaDoc(NS_URI, "test");
21     
22     public TestComponent2(TestModel2 model, org.w3c.dom.Element JavaDoc e) {
23         super(model, e);
24     }
25     public TestComponent2(TestModel2 model, String JavaDoc name) {
26         this(model, model.getDocument().createElementNS(NS_URI, name));
27     }
28     public TestComponent2(TestModel2 model, String JavaDoc name, int index) {
29         this(model, name);
30         setIndex(index);
31     }
32     public TestComponent2(TestModel2 model, String JavaDoc name, int index, String JavaDoc value) {
33         this(model, name, index);
34         setValue(value);
35     }
36     public String JavaDoc toString() { return getName(); }
37     public String JavaDoc getName() { return getPeer().getLocalName()+getIndex(); }
38     public String JavaDoc getNamespaceURI() {
39         return super.getNamespaceURI();
40     }
41     protected void populateChildren(List JavaDoc<TestComponent2> children) {
42         NodeList JavaDoc nl = getPeer().getChildNodes();
43         if (nl != null){
44             for (int i = 0; i < nl.getLength(); i++) {
45                 org.w3c.dom.Node JavaDoc n = nl.item(i);
46                 if (n instanceof Element JavaDoc) {
47                     Element JavaDoc e = (Element JavaDoc) n;
48                     TestComponent2 comp = createComponent(getModel(), this, e);
49                     if (comp != null) {
50                         children.add(comp);
51                     }
52                 }
53             }
54         }
55     }
56     
57     static TestComponent2 createComponent(TestModel2 model, TestComponent2 parent, Element JavaDoc e) {
58         String JavaDoc namespace = e.getNamespaceURI();
59         if (namespace == null && parent != null) {
60             namespace = parent.lookupNamespaceURI(e.getPrefix());
61         }
62         if (e.getLocalName().equals("a") && NS_URI.equals(namespace)) {
63             return new TestComponent2.A(model, e);
64         } else if (e.getLocalName().equals("a") && NS2_URI.equals(namespace)) {
65             return new TestComponent2.Aa(model, e);
66         } else if (e.getLocalName().equals("b") && NS_URI.equals(namespace)) {
67             return new TestComponent2.B(model, e);
68         } else if (e.getLocalName().equals("c") && NS_URI.equals(namespace)) {
69             return new TestComponent2.C(model, e);
70         } else if (e.getLocalName().equals("d") && NS_URI.equals(namespace)) {
71             return new TestComponent2.D(model, e);
72         } else if (e.getLocalName().equals("e") && NS_URI.equals(namespace)) {
73             return new TestComponent2.E(model, e);
74         } else {
75             throw new RuntimeException JavaDoc("unsupported element type "+ e.getNodeName());
76         }
77     }
78     
79     public void setValue(String JavaDoc v) {
80         setAttribute(TestAttribute.VALUE.getName(), TestAttribute.VALUE, v);
81     }
82     public String JavaDoc getValue() {
83         return getAttribute(TestAttribute.VALUE);
84     }
85
86     public void setIndex(int index) {
87         setAttribute(TestAttribute.INDEX.getName(), TestAttribute.INDEX, Integer.valueOf(index));
88     }
89     public int getIndex() {
90         String JavaDoc s = getAttribute(TestAttribute.INDEX);
91         return s == null ? -1 : Integer.parseInt(s);
92     }
93
94     public void updateReference(Element JavaDoc n) {
95         assert (n != null);
96         assert n.getLocalName().equals(getQName().getLocalPart());
97         super.updateReference(n);
98     }
99     
100     public QName JavaDoc getQName() { return ROOT_QNAME; }
101     
102     protected Object JavaDoc getAttributeValueOf(Attribute attr, String JavaDoc stringValue) {
103         if (stringValue == null) return null;
104         if (String JavaDoc.class.isAssignableFrom(attr.getType())) {
105             return stringValue;
106         } else if (Integer JavaDoc.class.isAssignableFrom(attr.getType())) {
107             return Integer.valueOf(stringValue);
108         }
109         assert false : "unsupported type"+attr.getType();
110         return stringValue;
111     }
112     
113     public static class A extends TestComponent2 {
114         public static final QName JavaDoc QNAME = new QName JavaDoc(NS_URI, "a");
115         public A(TestModel2 model, int i) {
116             super(model, "a", i);
117         }
118         public A(TestModel2 model, Element JavaDoc e) {
119             super(model, e);
120         }
121         public QName JavaDoc getQName() { return QNAME; }
122     }
123     public static class Aa extends TestComponent2 {
124         public static final QName JavaDoc QNAME = new QName JavaDoc(NS2_URI, "a");
125         public Aa(TestModel2 model, int i) {
126             super(model, "a", i);
127         }
128         public Aa(TestModel2 model, Element JavaDoc e) {
129             super(model, e);
130         }
131         public QName JavaDoc getQName() { return QNAME; }
132     }
133     public static class B extends TestComponent2 {
134         public static final QName JavaDoc QNAME = new QName JavaDoc(NS_URI, "b");
135         public B(TestModel2 model, int i) {
136             super(model, "b", i);
137         }
138         public B(TestModel2 model, Element JavaDoc e) {
139             super(model, e);
140         }
141         public QName JavaDoc getQName() { return QNAME; }
142     }
143     public static class C extends TestComponent2 {
144         public static final QName JavaDoc QNAME = new QName JavaDoc(NS_URI, "c");
145         public C(TestModel2 model, int i) {
146             super(model, "c", i);
147         }
148         public C(TestModel2 model, Element JavaDoc e) {
149             super(model, e);
150         }
151         public QName JavaDoc getQName() { return QNAME; }
152     }
153     public static class D extends TestComponent2 {
154         public static final QName JavaDoc QNAME = new QName JavaDoc(NS_URI, "d");
155         public D(TestModel2 model, int i) {
156             super(model, "d", i);
157         }
158         public D(TestModel2 model, Element JavaDoc e) {
159             super(model, e);
160         }
161         public QName JavaDoc getQName() { return QNAME; }
162     }
163     public static class E extends TestComponent2 {
164         public static final QName JavaDoc QNAME = new QName JavaDoc(NS_URI, "e");
165         public E(TestModel2 model, int i) {
166             super(model, "e", i);
167         }
168         public E(TestModel2 model, Element JavaDoc e) {
169             super(model, e);
170         }
171         public QName JavaDoc getQName() { return QNAME; }
172
173         public String JavaDoc getValue() {
174             String JavaDoc retValue;
175             
176             retValue = super.getValue();
177             return retValue;
178         }
179         public String JavaDoc getName() {
180             return super.getAttribute(TestAttribute.NAME);
181         }
182         public void setName(String JavaDoc v) {
183             setAttribute(TestAttribute.NAME.getName(), TestAttribute.NAME, v);
184         }
185     }
186     
187     public static class TestComponentReference<T extends TestComponent2>
188             extends AbstractNamedComponentReference<T> {
189         public TestComponentReference(Class JavaDoc<T> type, TestComponent2 parent, String JavaDoc ref) {
190             super(type, parent, ref);
191         }
192         public TestComponentReference(T ref, Class JavaDoc<T> type, TestComponent2 parent) {
193             super(ref, type, parent);
194         }
195         public TestComponent2 getParent() {
196             return (TestComponent2) super.getParent();
197         }
198         public String JavaDoc getEffectiveNamespace() {
199             return getParent().getModel().getRootComponent().getTargetNamespace();
200         }
201
202         public T get() {
203             if (getReferenced() == null) {
204                 String JavaDoc tns = getQName().getNamespaceURI();
205                 TestComponent2 root = getParent().getModel().getRootComponent();
206                 if (tns != null && tns.equals(root.getTargetNamespace())) {
207                     setReferenced(getType().cast(new ReferencedFinder().
208                             findReferenced(root, getQName().getLocalPart())));
209                 }
210             }
211             return getReferenced();
212         }
213     }
214     
215     public TestModel2 getModel() {
216         return (TestModel2) super.getModel();
217     }
218     
219     public void accept(TestVisitor visitor) {
220         visitor.visit(this);
221     }
222     
223     public String JavaDoc getTargetNamespace() {
224         return getAttribute(TestAttribute.TNS);
225     }
226     public void setTargetNamespace(String JavaDoc v) {
227         setAttribute(TestAttribute.TNS.getName(), TestAttribute.NAME, v);
228     }
229     
230     public TestComponentReference<TestComponent2> getRef() {
231         String JavaDoc v = getAttribute(TestAttribute.REF);
232         return v == null ? null : new TestComponentReference<TestComponent2>(TestComponent2.class, this, v);
233     }
234     
235     public void getRef(TestComponentReference<TestComponent2> ref) {
236         super.setAttribute(TestAttribute.REF.getName(), TestAttribute.REF, ref);
237     }
238     
239     
240     public static class ReferencedFinder extends TestVisitor {
241         String JavaDoc name;
242         TestComponent2 found;
243         
244         public ReferencedFinder() {
245         }
246         public TestComponent2 findReferenced(TestComponent2 root, String JavaDoc name) {
247             this.name = name;
248             root.accept(this);
249             return found;
250         }
251         public void visit(TestComponent2 component) {
252             if (name.equals(component.getName())) {
253                 found = component;
254             } else {
255                 visitChildren(component);
256             }
257         }
258         public void visitChildren(TestComponent2 component) {
259             for (TestComponent2 child : component.getChildren()) {
260                 child.accept(this);
261                 if (found != null) {
262                     return;
263                 }
264             }
265         }
266     }
267     
268     static Collection JavaDoc<Class JavaDoc<? extends TestComponent2>> EMPTY = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
269     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _ANY = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
270     static { _ANY.add(TestComponent2.class); }
271     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _A = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
272     static { _A.add(A.class); }
273     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _B = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
274     static { _B.add(B.class); }
275     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _C = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
276     static { _C.add(C.class); }
277     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _D = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
278     static { _D.add(D.class); }
279     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _AB = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
280     static { _AB.add(A.class); _AB.add(B.class); }
281     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _BC = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
282     static { _BC.add(B.class); _BC.add(C.class); }
283     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _AC = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
284     static { _AC.add(A.class); _AC.add(C.class); }
285     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _ABC = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
286     static { _ABC.add(A.class); _ABC.add(B.class); _ABC.add(C.class); }
287     public static Collection JavaDoc <Class JavaDoc<? extends TestComponent2>> _BAC = new ArrayList JavaDoc<Class JavaDoc<? extends TestComponent2>>();
288     static { _BAC.add(B.class); _BAC.add(A.class); _BAC.add(C.class); }
289 }
290
Popular Tags