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.framework;
027:
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.HashSet;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.Set;
035:
036: import javax.xml.namespace.QName;
037:
038: /**
039: * An abstract class for documents containing entities.
040: *
041: * @author WS Development Team
042: */
043: public abstract class AbstractDocument {
044:
045: protected AbstractDocument() {
046: _kinds = new HashMap();
047: _identifiables = new HashMap();
048: _importedEntities = new ArrayList();
049: _importedDocuments = new HashSet();
050: _includedEntities = new ArrayList();
051: _includedDocuments = new HashSet();
052: }
053:
054: public String getSystemId() {
055: return _systemId;
056: }
057:
058: public void setSystemId(String s) {
059: if (_systemId != null && !_systemId.equals(s)) {
060: // avoid redefinition of a system identifier
061: throw new IllegalArgumentException();
062: }
063:
064: _systemId = s;
065: if (s != null) {
066: _importedDocuments.add(s);
067: }
068: }
069:
070: public void addIncludedDocument(String systemId) {
071: _includedDocuments.add(systemId);
072: }
073:
074: public boolean isIncludedDocument(String systemId) {
075: return _includedDocuments.contains(systemId);
076: }
077:
078: public void addIncludedEntity(Entity entity) {
079: _includedEntities.add(entity);
080: }
081:
082: public void addImportedDocument(String systemId) {
083: _importedDocuments.add(systemId);
084: }
085:
086: public boolean isImportedDocument(String systemId) {
087: return _importedDocuments.contains(systemId);
088: }
089:
090: public void addImportedEntity(Entity entity) {
091: _importedEntities.add(entity);
092: }
093:
094: public void withAllSubEntitiesDo(EntityAction action) {
095: if (getRoot() != null) {
096: action.perform(getRoot());
097: }
098:
099: for (Iterator iter = _importedEntities.iterator(); iter
100: .hasNext();) {
101: action.perform((Entity) iter.next());
102: }
103:
104: for (Iterator iter = _includedEntities.iterator(); iter
105: .hasNext();) {
106: action.perform((Entity) iter.next());
107: }
108: }
109:
110: public Map getMap(Kind k) {
111: Map m = (Map) _kinds.get(k.getName());
112: if (m == null) {
113: m = new HashMap();
114: _kinds.put(k.getName(), m);
115: }
116: return m;
117: }
118:
119: public void define(GloballyKnown e) {
120: Map map = getMap(e.getKind());
121: if (e.getName() == null)
122: return;
123: QName name = new QName(e.getDefining().getTargetNamespaceURI(),
124: e.getName());
125:
126: if (map.containsKey(name))
127: throw new DuplicateEntityException(e);
128: else
129: map.put(name, e);
130: }
131:
132: public void undefine(GloballyKnown e) {
133: Map map = getMap(e.getKind());
134: if (e.getName() == null)
135: return;
136: QName name = new QName(e.getDefining().getTargetNamespaceURI(),
137: e.getName());
138:
139: if (map.containsKey(name))
140: throw new NoSuchEntityException(name);
141: else
142: map.remove(name);
143: }
144:
145: public GloballyKnown find(Kind k, QName name) {
146: Map map = getMap(k);
147: Object result = map.get(name);
148: if (result == null)
149: throw new NoSuchEntityException(name);
150: return (GloballyKnown) result;
151: }
152:
153: public void defineID(Identifiable e) {
154: String id = e.getID();
155: if (id == null)
156: return;
157:
158: if (_identifiables.containsKey(id))
159: throw new DuplicateEntityException(e);
160: else
161: _identifiables.put(id, e);
162: }
163:
164: public void undefineID(Identifiable e) {
165: String id = e.getID();
166:
167: if (id == null)
168: return;
169:
170: if (_identifiables.containsKey(id))
171: throw new NoSuchEntityException(id);
172: else
173: _identifiables.remove(id);
174: }
175:
176: public Identifiable findByID(String id) {
177: Object result = _identifiables.get(id);
178: if (result == null)
179: throw new NoSuchEntityException(id);
180: return (Identifiable) result;
181: }
182:
183: public Set collectAllQNames() {
184: final Set result = new HashSet();
185: EntityAction action = new EntityAction() {
186: public void perform(Entity entity) {
187: entity.withAllQNamesDo(new QNameAction() {
188: public void perform(QName name) {
189: result.add(name);
190: }
191: });
192: entity.withAllSubEntitiesDo(this );
193: }
194: };
195: withAllSubEntitiesDo(action);
196: return result;
197: }
198:
199: public Set collectAllNamespaces() {
200: final Set result = new HashSet();
201:
202: EntityAction action = new EntityAction() {
203: public void perform(Entity entity) {
204: entity.withAllQNamesDo(new QNameAction() {
205: public void perform(QName name) {
206: result.add(name.getNamespaceURI());
207: }
208: });
209:
210: entity.withAllSubEntitiesDo(this );
211: }
212: };
213: withAllSubEntitiesDo(action);
214: return result;
215: }
216:
217: public void validateLocally() {
218: LocallyValidatingAction action = new LocallyValidatingAction();
219: withAllSubEntitiesDo(action);
220: if (action.getException() != null) {
221: throw action.getException();
222: }
223: }
224:
225: public void validate() {
226: validate(null);
227: }
228:
229: public abstract void validate(EntityReferenceValidator validator);
230:
231: protected abstract Entity getRoot();
232:
233: private Map _kinds;
234: private Map _identifiables;
235: private String _systemId;
236: private Set _importedDocuments;
237: private List _importedEntities;
238: private Set _includedDocuments;
239: private List _includedEntities;
240:
241: private class LocallyValidatingAction implements EntityAction {
242: public LocallyValidatingAction() {
243: }
244:
245: public void perform(Entity entity) {
246: try {
247: entity.validateThis();
248: entity.withAllSubEntitiesDo(this );
249: } catch (ValidationException e) {
250: if (_exception == null) {
251: _exception = e;
252: }
253: }
254: }
255:
256: public ValidationException getException() {
257: return _exception;
258: }
259:
260: private ValidationException _exception;
261: }
262: }
|