KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.xml.xsd;
20
21 import org.w3c.dom.Element JavaDoc;
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.helpers.AttributesImpl JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 /**
28  * Represents an XML element
29  * @author anovak
30  */

31 class SchemaElement extends AbstractResultNode implements Element JavaDoc {
32
33     /** namespace */
34     protected final String JavaDoc namespaceURI;
35     /** qname */
36     protected final String JavaDoc qname;
37     /** Attributes of this Element */
38     protected final Attributes JavaDoc attributes;
39     /** Sub elements */
40     protected final List JavaDoc subelements;
41     
42     private String JavaDoc prefix;
43     
44     /** http://www.w3.org/2001/XMLSchema namespace prefix */
45     private String JavaDoc schemaPrefix;
46
47     /** Creates empty element */
48     protected SchemaElement() {
49         this.namespaceURI = null;
50         this.qname = null;
51         this.attributes = null;
52         this.subelements = null;
53         this.schemaPrefix = null;
54     }
55     
56     /** Creates a new instance of Element */
57     protected SchemaElement(String JavaDoc namespaceURI, String JavaDoc qname, Attributes JavaDoc attributes, String JavaDoc schemaPrefix) {
58         this.namespaceURI = namespaceURI;
59         this.qname = qname;
60         this.attributes = (attributes == null ? null : new AttributesImpl JavaDoc(attributes));
61         this.subelements = new ArrayList JavaDoc();
62         this.schemaPrefix = schemaPrefix;
63     }
64     
65     /** Creates a new SchemaElement */
66     public static final SchemaElement createSchemaElement(String JavaDoc namespaceURI, String JavaDoc qname, Attributes JavaDoc attributes, String JavaDoc schemaPrefix) {
67         String JavaDoc simpleType = null;
68         String JavaDoc complexType = null;
69         
70         if (schemaPrefix == null) {
71            simpleType = Type.XS_SIMPLE_TYPE;
72            complexType = Type.XS_COMPLEX_TYPE;
73         } else {
74            simpleType = schemaPrefix + ':' + Type.XS_SIMPLE_TYPE;
75            complexType = schemaPrefix + ':' + Type.XS_COMPLEX_TYPE;
76         }
77         
78         if (qname.equalsIgnoreCase(simpleType) || qname.equalsIgnoreCase(complexType)) {
79             return new Type(namespaceURI, qname, attributes, schemaPrefix);
80         } else {
81             return new SchemaElement(namespaceURI, qname, attributes, schemaPrefix);
82         }
83     }
84     
85     public final void addSubelement(SchemaElement e) {
86         subelements.add(e);
87     }
88     
89     public final java.util.Iterator JavaDoc getSubelements() {
90         return subelements.iterator();
91     }
92     
93     public final boolean isComposite() {
94         String JavaDoc sequenceToken = getSchemaPrefix() == null ? "sequence" : getSchemaPrefix() + ':' + "sequence";
95         return (this instanceof Type) || getQname().equalsIgnoreCase(sequenceToken);
96     }
97     
98     public String JavaDoc toString() {
99         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
100         sb.append("SchemaElement ").append(qname);
101         
102         if (attributes != null) {
103             sb.append("Attrs size: ").append(attributes.getLength());
104             for (int i = 0; i < attributes.getLength(); i++) {
105                 sb.append("\n Attr[").append(i).append("] localname: ").
106                         append(attributes.getLocalName(i)).
107                         append(" qname: ").append(attributes.getQName(i)).
108                         append(" value: ").append(attributes.getValue(i)).
109                         append(" URI: ").append(attributes.getURI(i)).
110                         append(" type: ").append(attributes.getType(i));
111             }
112         }
113         
114         return sb.toString();
115     }
116     
117     /**
118      * Getter for property qname.
119      * @return Value of property qname.
120      */

121     public java.lang.String JavaDoc getQname() {
122         return qname;
123     }
124     
125     /**
126      * Getter for property attributes.
127      * @return Value of property attributes.
128      */

129     public org.xml.sax.Attributes JavaDoc getSAXAttributes() {
130         return attributes;
131     }
132
133     // org.w3c.Node methods
134

135     public short getNodeType() {
136         return org.w3c.dom.Node.ELEMENT_NODE;
137     }
138
139     public String JavaDoc getNodeName() {
140         String JavaDoc name = getSAXAttributes().getValue("name");
141         if (prefix != null) {
142             name = prefix + ':' + name;
143         }
144         return name;
145     }
146
147     public String JavaDoc getTagName() {
148         return this.getNodeName();
149     }
150     
151     public void setPrefix(String JavaDoc str) throws org.w3c.dom.DOMException JavaDoc {
152         this.prefix = str;
153     }
154     
155     public String JavaDoc getPrefix() {
156         return prefix;
157     }
158     
159     /**
160      * Getter for property schemaPrefix.
161      * @return Value of property schemaPrefix.
162      */

163     public java.lang.String JavaDoc getSchemaPrefix() {
164         return schemaPrefix;
165     }
166     
167     /**
168      * Setter for property schemaPrefix.
169      * @param schemaPrefix New value of property schemaPrefix.
170      */

171     public void setSchemaPrefix(java.lang.String JavaDoc schemaPrefix) {
172         this.schemaPrefix = schemaPrefix;
173     }
174     
175 }
176
Popular Tags