001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.xml.xsd;
042:
043: import org.w3c.dom.Element;
044: import org.xml.sax.Attributes;
045: import org.xml.sax.helpers.AttributesImpl;
046: import java.util.List;
047: import java.util.ArrayList;
048:
049: /**
050: * Represents an XML element
051: * @author anovak
052: */
053: class SchemaElement extends AbstractResultNode implements Element {
054:
055: /** namespace */
056: protected final String namespaceURI;
057: /** qname */
058: protected final String qname;
059: /** Attributes of this Element */
060: protected final Attributes attributes;
061: /** Sub elements */
062: protected final List subelements;
063:
064: private String prefix;
065:
066: /** http://www.w3.org/2001/XMLSchema namespace prefix */
067: private String schemaPrefix;
068:
069: /** Creates empty element */
070: protected SchemaElement() {
071: this .namespaceURI = null;
072: this .qname = null;
073: this .attributes = null;
074: this .subelements = null;
075: this .schemaPrefix = null;
076: }
077:
078: /** Creates a new instance of Element */
079: protected SchemaElement(String namespaceURI, String qname,
080: Attributes attributes, String schemaPrefix) {
081: this .namespaceURI = namespaceURI;
082: this .qname = qname;
083: this .attributes = (attributes == null ? null
084: : new AttributesImpl(attributes));
085: this .subelements = new ArrayList();
086: this .schemaPrefix = schemaPrefix;
087: }
088:
089: /** Creates a new SchemaElement */
090: public static final SchemaElement createSchemaElement(
091: String namespaceURI, String qname, Attributes attributes,
092: String schemaPrefix) {
093: String simpleType = null;
094: String complexType = null;
095:
096: if (schemaPrefix == null) {
097: simpleType = Type.XS_SIMPLE_TYPE;
098: complexType = Type.XS_COMPLEX_TYPE;
099: } else {
100: simpleType = schemaPrefix + ':' + Type.XS_SIMPLE_TYPE;
101: complexType = schemaPrefix + ':' + Type.XS_COMPLEX_TYPE;
102: }
103:
104: if (qname.equalsIgnoreCase(simpleType)
105: || qname.equalsIgnoreCase(complexType)) {
106: return new Type(namespaceURI, qname, attributes,
107: schemaPrefix);
108: } else {
109: return new SchemaElement(namespaceURI, qname, attributes,
110: schemaPrefix);
111: }
112: }
113:
114: public final void addSubelement(SchemaElement e) {
115: subelements.add(e);
116: }
117:
118: public final java.util.Iterator getSubelements() {
119: return subelements.iterator();
120: }
121:
122: public final boolean isComposite() {
123: String sequenceToken = getSchemaPrefix() == null ? "sequence"
124: : getSchemaPrefix() + ':' + "sequence";
125: return (this instanceof Type)
126: || getQname().equalsIgnoreCase(sequenceToken);
127: }
128:
129: public String toString() {
130: StringBuffer sb = new StringBuffer(100);
131: sb.append("SchemaElement ").append(qname);
132:
133: if (attributes != null) {
134: sb.append("Attrs size: ").append(attributes.getLength());
135: for (int i = 0; i < attributes.getLength(); i++) {
136: sb.append("\n Attr[").append(i).append("] localname: ")
137: .append(attributes.getLocalName(i)).append(
138: " qname: ").append(
139: attributes.getQName(i)).append(
140: " value: ").append(
141: attributes.getValue(i))
142: .append(" URI: ").append(attributes.getURI(i))
143: .append(" type: ")
144: .append(attributes.getType(i));
145: }
146: }
147:
148: return sb.toString();
149: }
150:
151: /**
152: * Getter for property qname.
153: * @return Value of property qname.
154: */
155: public java.lang.String getQname() {
156: return qname;
157: }
158:
159: /**
160: * Getter for property attributes.
161: * @return Value of property attributes.
162: */
163: public org.xml.sax.Attributes getSAXAttributes() {
164: return attributes;
165: }
166:
167: // org.w3c.Node methods
168:
169: public short getNodeType() {
170: return org.w3c.dom.Node.ELEMENT_NODE;
171: }
172:
173: public String getNodeName() {
174: String name = getSAXAttributes().getValue("name");
175: if (prefix != null) {
176: name = prefix + ':' + name;
177: }
178: return name;
179: }
180:
181: public String getTagName() {
182: return this .getNodeName();
183: }
184:
185: public void setPrefix(String str) throws org.w3c.dom.DOMException {
186: this .prefix = str;
187: }
188:
189: public String getPrefix() {
190: return prefix;
191: }
192:
193: /**
194: * Getter for property schemaPrefix.
195: * @return Value of property schemaPrefix.
196: */
197: public java.lang.String getSchemaPrefix() {
198: return schemaPrefix;
199: }
200:
201: /**
202: * Setter for property schemaPrefix.
203: * @param schemaPrefix New value of property schemaPrefix.
204: */
205: public void setSchemaPrefix(java.lang.String schemaPrefix) {
206: this.schemaPrefix = schemaPrefix;
207: }
208:
209: }
|