001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.wsdl.document.schema;
027:
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033:
034: import javax.xml.namespace.QName;
035:
036: import com.sun.tools.internal.ws.wsdl.framework.ValidationException;
037: import com.sun.xml.internal.ws.util.NullIterator;
038: import com.sun.xml.internal.ws.util.xml.XmlUtil;
039:
040: /**
041: *
042: * @author WS Development Team
043: */
044: public class SchemaElement {
045:
046: public SchemaElement() {
047: }
048:
049: public SchemaElement(String localName) {
050: _localName = localName;
051: }
052:
053: public SchemaElement(QName name) {
054: _qname = name;
055: _localName = name.getLocalPart();
056: _nsURI = name.getNamespaceURI();
057: }
058:
059: public String getNamespaceURI() {
060: return _nsURI;
061: }
062:
063: public void setNamespaceURI(String s) {
064: _nsURI = s;
065: }
066:
067: public String getLocalName() {
068: return _localName;
069: }
070:
071: public void setLocalName(String s) {
072: _localName = s;
073: }
074:
075: public QName getQName() {
076: if (_qname == null) {
077: _qname = new QName(_nsURI, _localName);
078: }
079: return _qname;
080: }
081:
082: public SchemaElement getParent() {
083: return _parent;
084: }
085:
086: public void setParent(SchemaElement e) {
087: _parent = e;
088: }
089:
090: public SchemaElement getRoot() {
091: return _parent == null ? this : _parent.getRoot();
092: }
093:
094: public Schema getSchema() {
095: return _parent == null ? _schema : _parent.getSchema();
096: }
097:
098: public void setSchema(Schema s) {
099: _schema = s;
100: }
101:
102: public void addChild(SchemaElement e) {
103: if (_children == null) {
104: _children = new ArrayList();
105: }
106:
107: _children.add(e);
108: e.setParent(this );
109: }
110:
111: public void insertChildAtTop(SchemaElement e) {
112: if (_children == null) {
113: _children = new ArrayList();
114: }
115:
116: _children.add(0, e);
117: e.setParent(this );
118: }
119:
120: public Iterator children() {
121: if (_children == null) {
122: return NullIterator.getInstance();
123: } else {
124: return _children.iterator();
125: }
126: }
127:
128: public void addAttribute(SchemaAttribute a) {
129: if (_attributes == null) {
130: _attributes = new ArrayList();
131: }
132:
133: _attributes.add(a);
134: a.setParent(this );
135: a.getValue();
136: // this is a hack to force namespace declarations to be added, if needed
137: }
138:
139: public void addAttribute(String name, String value) {
140: SchemaAttribute attr = new SchemaAttribute();
141: attr.setLocalName(name);
142: attr.setValue(value);
143: addAttribute(attr);
144: }
145:
146: public void addAttribute(String name, QName value) {
147: SchemaAttribute attr = new SchemaAttribute();
148: attr.setLocalName(name);
149: attr.setValue(value);
150: addAttribute(attr);
151: }
152:
153: public Iterator attributes() {
154: if (_attributes == null) {
155: return NullIterator.getInstance();
156: } else {
157: return _attributes.iterator();
158: }
159: }
160:
161: public SchemaAttribute getAttribute(String localName) {
162: if (_attributes != null) {
163: for (Iterator iter = _attributes.iterator(); iter.hasNext();) {
164: SchemaAttribute attr = (SchemaAttribute) iter.next();
165: if (localName.equals(attr.getLocalName())) {
166: return attr;
167: }
168: }
169: }
170: return null;
171: }
172:
173: public String getValueOfMandatoryAttribute(String localName) {
174: SchemaAttribute attr = getAttribute(localName);
175: if (attr == null) {
176: throw new ValidationException(
177: "validation.missingRequiredAttribute",
178: new Object[] { localName, _localName });
179: }
180: return attr.getValue();
181: }
182:
183: public String getValueOfAttributeOrNull(String localName) {
184: SchemaAttribute attr = getAttribute(localName);
185: if (attr == null) {
186: return null;
187: } else {
188: return attr.getValue();
189: }
190: }
191:
192: public boolean getValueOfBooleanAttributeOrDefault(
193: String localName, boolean defaultValue) {
194: String stringValue = getValueOfAttributeOrNull(localName);
195: if (stringValue == null) {
196: return defaultValue;
197: }
198: if (stringValue.equals("true") || stringValue.equals("1")) {
199: return true;
200: } else if (stringValue.equals("false")
201: || stringValue.equals("0")) {
202: return false;
203: } else {
204: throw new ValidationException(
205: "validation.invalidAttributeValue", new Object[] {
206: localName, stringValue });
207: }
208: }
209:
210: public int getValueOfIntegerAttributeOrDefault(String localName,
211: int defaultValue) {
212: String stringValue = getValueOfAttributeOrNull(localName);
213: if (stringValue == null) {
214: return defaultValue;
215: }
216: try {
217: return Integer.parseInt(stringValue);
218: } catch (NumberFormatException e) {
219: throw new ValidationException(
220: "validation.invalidAttributeValue", new Object[] {
221: localName, stringValue });
222: }
223: }
224:
225: public QName getValueOfQNameAttributeOrNull(String localName) {
226: String stringValue = getValueOfAttributeOrNull(localName);
227: if (stringValue == null)
228: return null;
229:
230: String prefix = XmlUtil.getPrefix(stringValue);
231: String uri = (prefix == null ? getURIForPrefix("")
232: : getURIForPrefix(prefix));
233: if (uri == null) {
234: throw new ValidationException(
235: "validation.invalidAttributeValue", new Object[] {
236: localName, stringValue });
237: }
238: return new QName(uri, XmlUtil.getLocalPart(stringValue));
239: }
240:
241: public void addPrefix(String prefix, String uri) {
242: if (_nsPrefixes == null) {
243: _nsPrefixes = new HashMap();
244: }
245:
246: _nsPrefixes.put(prefix, uri);
247: }
248:
249: public String getURIForPrefix(String prefix) {
250: if (_nsPrefixes != null) {
251: String result = (String) _nsPrefixes.get(prefix);
252: if (result != null)
253: return result;
254: }
255: if (_parent != null) {
256: return _parent.getURIForPrefix(prefix);
257: }
258: if (_schema != null) {
259: return _schema.getURIForPrefix(prefix);
260: }
261: // give up
262: return null;
263: }
264:
265: public boolean declaresPrefixes() {
266: return _nsPrefixes != null;
267: }
268:
269: public Iterator prefixes() {
270: if (_nsPrefixes == null) {
271: return NullIterator.getInstance();
272: } else {
273: return _nsPrefixes.keySet().iterator();
274: }
275: }
276:
277: public QName asQName(String s) {
278: String prefix = XmlUtil.getPrefix(s);
279: if (prefix == null) {
280: prefix = "";
281: }
282: String uri = getURIForPrefix(prefix);
283: if (uri == null) {
284: throw new ValidationException("validation.invalidPrefix",
285: prefix);
286: }
287: String localPart = XmlUtil.getLocalPart(s);
288: return new QName(uri, localPart);
289: }
290:
291: public String asString(QName name) {
292: if (name.getNamespaceURI().equals("")) {
293: return name.getLocalPart();
294: } else {
295: // look for a prefix
296: for (Iterator iter = prefixes(); iter.hasNext();) {
297: String prefix = (String) iter.next();
298: String uri = getURIForPrefix(prefix);
299: if (uri.equals(name.getNamespaceURI())) {
300: if (prefix.equals("")) {
301: return name.getLocalPart();
302: } else {
303: return prefix + ":" + name.getLocalPart();
304: }
305: }
306: }
307:
308: // not found
309: if (_parent != null) {
310: return _parent.asString(name);
311: }
312: if (_schema != null) {
313: String result = _schema.asString(name);
314: if (result != null) {
315: return result;
316: }
317: }
318:
319: // not found and no parent
320: String prefix = getNewPrefix();
321: addPrefix(prefix, name.getNamespaceURI());
322: return asString(name);
323: }
324: }
325:
326: protected String getNewPrefix() {
327: String base = NEW_NS_PREFIX_BASE;
328: int count = 2;
329: String prefix = null;
330: for (boolean needNewOne = true; needNewOne; ++count) {
331: prefix = base + Integer.toString(count);
332: needNewOne = getURIForPrefix(prefix) != null;
333: }
334: return prefix;
335: }
336:
337: private String _nsURI;
338: private String _localName;
339: private List _children;
340: private List _attributes;
341: private Map _nsPrefixes;
342: private SchemaElement _parent;
343: private QName _qname;
344: private Schema _schema;
345:
346: private static final String NEW_NS_PREFIX_BASE = "ns";
347: }
|