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.AbstractDocument;
037: import com.sun.tools.internal.ws.wsdl.framework.Defining;
038: import com.sun.tools.internal.ws.wsdl.framework.Extension;
039: import com.sun.tools.internal.ws.wsdl.framework.Kind;
040: import com.sun.tools.internal.ws.wsdl.framework.ValidationException;
041: import com.sun.tools.internal.ws.wsdl.parser.Constants;
042:
043: /**
044: *
045: * @author WS Development Team
046: */
047: public class Schema extends Extension implements Defining {
048:
049: public Schema(AbstractDocument document) {
050: _document = document;
051: _nsPrefixes = new HashMap();
052: _definedEntities = new ArrayList();
053: }
054:
055: public QName getElementName() {
056: return SchemaConstants.QNAME_SCHEMA;
057: }
058:
059: public SchemaElement getContent() {
060: return _content;
061: }
062:
063: public void setContent(SchemaElement entity) {
064: _content = entity;
065: _content.setSchema(this );
066: }
067:
068: public void setTargetNamespaceURI(String uri) {
069: _targetNamespaceURI = uri;
070: }
071:
072: public String getTargetNamespaceURI() {
073: return _targetNamespaceURI;
074: }
075:
076: public void addPrefix(String prefix, String uri) {
077: _nsPrefixes.put(prefix, uri);
078: }
079:
080: public String getURIForPrefix(String prefix) {
081: return (String) _nsPrefixes.get(prefix);
082: }
083:
084: public Iterator prefixes() {
085: return _nsPrefixes.keySet().iterator();
086: }
087:
088: public void defineAllEntities() {
089: if (_content == null) {
090: throw new ValidationException("validation.shouldNotHappen",
091: "missing schema content");
092: }
093:
094: for (Iterator iter = _content.children(); iter.hasNext();) {
095: SchemaElement child = (SchemaElement) iter.next();
096: if (child.getQName()
097: .equals(SchemaConstants.QNAME_ATTRIBUTE)) {
098: QName name = new QName(
099: _targetNamespaceURI,
100: child
101: .getValueOfMandatoryAttribute(Constants.ATTR_NAME));
102: defineEntity(child, SchemaKinds.XSD_ATTRIBUTE, name);
103: } else if (child.getQName().equals(
104: SchemaConstants.QNAME_ATTRIBUTE_GROUP)) {
105: QName name = new QName(
106: _targetNamespaceURI,
107: child
108: .getValueOfMandatoryAttribute(Constants.ATTR_NAME));
109: defineEntity(child, SchemaKinds.XSD_ATTRIBUTE_GROUP,
110: name);
111: } else if (child.getQName().equals(
112: SchemaConstants.QNAME_ELEMENT)) {
113: QName name = new QName(
114: _targetNamespaceURI,
115: child
116: .getValueOfMandatoryAttribute(Constants.ATTR_NAME));
117: defineEntity(child, SchemaKinds.XSD_ELEMENT, name);
118: } else if (child.getQName().equals(
119: SchemaConstants.QNAME_GROUP)) {
120: QName name = new QName(
121: _targetNamespaceURI,
122: child
123: .getValueOfMandatoryAttribute(Constants.ATTR_NAME));
124: defineEntity(child, SchemaKinds.XSD_GROUP, name);
125: } else if (child.getQName().equals(
126: SchemaConstants.QNAME_COMPLEX_TYPE)) {
127: QName name = new QName(
128: _targetNamespaceURI,
129: child
130: .getValueOfMandatoryAttribute(Constants.ATTR_NAME));
131: defineEntity(child, SchemaKinds.XSD_TYPE, name);
132: } else if (child.getQName().equals(
133: SchemaConstants.QNAME_SIMPLE_TYPE)) {
134: QName name = new QName(
135: _targetNamespaceURI,
136: child
137: .getValueOfMandatoryAttribute(Constants.ATTR_NAME));
138: defineEntity(child, SchemaKinds.XSD_TYPE, name);
139: }
140: }
141: }
142:
143: public void defineEntity(SchemaElement element, Kind kind,
144: QName name) {
145: SchemaEntity entity = new SchemaEntity(this , element, kind,
146: name);
147: _document.define(entity);
148: _definedEntities.add(entity);
149: }
150:
151: public Iterator definedEntities() {
152: return _definedEntities.iterator();
153: }
154:
155: public void validateThis() {
156: if (_content == null) {
157: throw new ValidationException("validation.shouldNotHappen",
158: "missing schema content");
159: }
160: }
161:
162: public String asString(QName name) {
163: if (name.getNamespaceURI().equals("")) {
164: return name.getLocalPart();
165: } else {
166: // look for a prefix
167: for (Iterator iter = prefixes(); iter.hasNext();) {
168: String prefix = (String) iter.next();
169: if (prefix.equals(name.getNamespaceURI())) {
170: return prefix + ":" + name.getLocalPart();
171: }
172: }
173:
174: // not found
175: return null;
176: }
177: }
178:
179: private AbstractDocument _document;
180: private String _targetNamespaceURI;
181: private SchemaElement _content;
182: private List _definedEntities;
183: private Map _nsPrefixes;
184: }
|