KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > impl > SchemaComponentImpl


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.schema.model.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.xml.XMLConstants JavaDoc;
26 import org.netbeans.modules.xml.schema.model.Annotation;
27 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
28 import org.netbeans.modules.xml.schema.model.SchemaComponent;
29 import org.netbeans.modules.xml.schema.model.impl.xdm.SyncUpdateVisitor;
30 import org.netbeans.modules.xml.schema.model.visitor.SchemaVisitor;
31 import org.netbeans.modules.xml.xam.Component;
32 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
33 import org.netbeans.modules.xml.xam.dom.Attribute;
34 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
35 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
36 import org.netbeans.modules.xml.xam.dom.DocumentModelAccess;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40
41 /**
42  *
43  * @author rico
44  * @author Vidhya Narayanan
45  */

46 public abstract class SchemaComponentImpl
47         extends AbstractDocumentComponent<SchemaComponent>
48         implements SchemaComponent, DocumentModelAccess.NodeUpdater {
49     
50     public SchemaComponentImpl(SchemaModelImpl model, Element JavaDoc e) {
51         super(model, e);
52     }
53     
54     public SchemaModelImpl getModel() {
55         return (SchemaModelImpl) super.getModel();
56     }
57     public abstract Class JavaDoc<? extends SchemaComponent> getComponentType();
58     
59     protected String JavaDoc getNamespaceURI() {
60         return XMLConstants.W3C_XML_SCHEMA_NS_URI;
61     }
62     
63     /**
64      * Leave this method as abstract
65      */

66     public abstract void accept(SchemaVisitor v);
67     
68     protected static Element JavaDoc createNewComponent(SchemaElements type, SchemaModelImpl model) {
69         String JavaDoc qualified = "xsd:" + type.getName(); //NOI18N
70
return model.getDocument().createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, qualified);
71     }
72     
73     protected void populateChildren(List JavaDoc<SchemaComponent> children) {
74         NodeList JavaDoc nl = getPeer().getChildNodes();
75         if (nl != null){
76             for (int i = 0; i < nl.getLength(); i++) {
77                 Node JavaDoc n = nl.item(i);
78                 if (n instanceof Element JavaDoc) {
79                     SchemaComponent comp = (SchemaComponent)getModel().getFactory().create((Element JavaDoc)n, this);
80                     if (comp != null) {
81                         children.add(comp);
82                     }
83                 }
84             }
85         }
86     }
87     
88     
89     /**
90      * @return true if the elements are from the same schema model.
91      */

92     public final boolean fromSameModel(SchemaComponent other) {
93         return getModel().equals(other.getModel());
94     }
95     
96     /**
97      * Annotation always gets added as the first child.
98      */

99     public void setAnnotation(Annotation annotation) {
100         List JavaDoc<Class JavaDoc<? extends SchemaComponent>> types = new ArrayList JavaDoc<Class JavaDoc<? extends SchemaComponent>>(1);
101         types.add(SchemaComponent.class);
102         setChildBefore(Annotation.class, ANNOTATION_PROPERTY, annotation, types);
103     }
104     
105     public Annotation getAnnotation() {
106         List JavaDoc<Annotation> annotations = getChildren(Annotation.class);
107         return annotations.isEmpty() ? null : annotations.iterator().next();
108     }
109     
110     /**
111      * Returns type of the given attribute.
112      * The type should either be:
113      * 1. String or wrappers for primitive types (Boolean, Integer,...)
114      * 2. An enum with toString() overridden to return string value by XSD specs.
115      * 3. java.util.Set
116      *
117      * @param attribute the attribute enum name
118      */

119     protected Class JavaDoc getAttributeType(Attribute attribute) {
120         return attribute.getType();
121     }
122     
123     /**
124      * Returns type of member in cases attribute type is collections.
125      */

126     protected Class JavaDoc getAttributeMemberType(Attribute attribute) {
127         return attribute.getMemberType();
128     }
129     
130     protected Object JavaDoc getAttributeValueOf(Attribute attr, String JavaDoc s) {
131         if (s == null) {
132             return null;
133         }
134         Class JavaDoc c = getAttributeType(attr);
135         if (String JavaDoc.class.isAssignableFrom(c)) {
136             return s;
137         } else if (Boolean JavaDoc.class.isAssignableFrom(c)) {
138             return Boolean.valueOf(s);
139         } else if (Integer JavaDoc.class.isAssignableFrom(c)) {
140             return Integer.valueOf(s);
141         } else if (Enum JavaDoc.class.isAssignableFrom(c)) {
142             Class JavaDoc<Enum JavaDoc> enumClass = (Class JavaDoc<Enum JavaDoc>) c;
143             return Util.parse(enumClass, s);
144         } else if (Set JavaDoc.class.isAssignableFrom(c)) {
145             return Util.valuesOf(getAttributeMemberType(attr), s);
146         }
147         
148         assert(false); // should never reached within this model implementation
149
return null;
150     }
151     
152     protected <T extends ReferenceableSchemaComponent> GlobalReferenceImpl<T> resolveGlobalReference(Class JavaDoc<T>c, SchemaAttributes attrName){
153         String JavaDoc v = getAttribute(attrName);
154         return v == null ? null : new GlobalReferenceImpl<T>(c, this, v);
155     }
156     
157     protected Element JavaDoc checkNodeRef() {
158         Element JavaDoc e = (Element JavaDoc)getPeer();
159         if (e == null) {
160             throw new IllegalArgumentException JavaDoc("Valid Node reference must exist"); // NOI18N
161
}
162         return e;
163     }
164     
165     public <T extends ReferenceableSchemaComponent> NamedComponentReference<T>
166             createReferenceTo(T referenced, Class JavaDoc<T> type) {
167         return getModel().getFactory().createGlobalReference(referenced, type, this);
168     }
169
170     public void setId(String JavaDoc id) {
171         setAttribute(ID_PROPERTY, SchemaAttributes.ID, id);
172     }
173
174     public String JavaDoc getId() {
175         return getAttribute(SchemaAttributes.ID);
176     }
177     
178     protected String JavaDoc getAttributeValue(SchemaAttributes attr) {
179         return getAttribute(attr);
180     }
181     
182     public boolean canPaste(Component child) {
183         if (! (child instanceof DocumentComponent)) return false;
184         return new SyncUpdateVisitor().canAdd(this, (DocumentComponent) child);
185     }
186
187     public String JavaDoc lookupNamespaceURI(String JavaDoc prefix) {
188         return lookupNamespaceURI(prefix, true);
189     }
190 }
191
192
Popular Tags