001: /*
002: * 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.xml.internal.xsom.impl;
027:
028: import com.sun.xml.internal.xsom.ForeignAttributes;
029: import com.sun.xml.internal.xsom.XSAnnotation;
030: import com.sun.xml.internal.xsom.XSAttGroupDecl;
031: import com.sun.xml.internal.xsom.XSAttributeDecl;
032: import com.sun.xml.internal.xsom.XSComplexType;
033: import com.sun.xml.internal.xsom.XSElementDecl;
034: import com.sun.xml.internal.xsom.XSIdentityConstraint;
035: import com.sun.xml.internal.xsom.XSModelGroupDecl;
036: import com.sun.xml.internal.xsom.XSNotation;
037: import com.sun.xml.internal.xsom.XSSchema;
038: import com.sun.xml.internal.xsom.XSSimpleType;
039: import com.sun.xml.internal.xsom.XSType;
040: import com.sun.xml.internal.xsom.parser.SchemaDocument;
041: import com.sun.xml.internal.xsom.visitor.XSFunction;
042: import com.sun.xml.internal.xsom.visitor.XSVisitor;
043: import org.xml.sax.Locator;
044:
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Map;
051:
052: public class SchemaImpl implements XSSchema {
053: public SchemaImpl(SchemaSetImpl _parent, Locator loc, String tns) {
054: if (tns == null)
055: // the empty string must be used.
056: throw new IllegalArgumentException();
057: this .targetNamespace = tns;
058: this .parent = _parent;
059: this .locator = loc;
060: }
061:
062: public SchemaDocument getSourceDocument() {
063: return null;
064: }
065:
066: protected final SchemaSetImpl parent;
067:
068: public SchemaSetImpl getParent() {
069: return parent;
070: }
071:
072: private final String targetNamespace;
073:
074: public String getTargetNamespace() {
075: return targetNamespace;
076: }
077:
078: public XSSchema getOwnerSchema() {
079: return this ;
080: }
081:
082: private XSAnnotation annotation;
083:
084: public void setAnnotation(XSAnnotation a) {
085: annotation = a;
086: }
087:
088: public XSAnnotation getAnnotation() {
089: return annotation;
090: }
091:
092: // it's difficult to determine the source location for the schema
093: // component as one schema can be defined across multiple files.
094: // so this locator might not correctly reflect all the locations
095: // where the schema is defined.
096: // but partial information would be better than nothing.
097:
098: private final Locator locator;
099:
100: public Locator getLocator() {
101: return locator;
102: }
103:
104: private final Map<String, XSAttributeDecl> atts = new HashMap<String, XSAttributeDecl>();
105: private final Map<String, XSAttributeDecl> attsView = Collections
106: .unmodifiableMap(atts);
107:
108: public void addAttributeDecl(XSAttributeDecl newDecl) {
109: atts.put(newDecl.getName(), newDecl);
110: }
111:
112: public Map<String, XSAttributeDecl> getAttributeDecls() {
113: return attsView;
114: }
115:
116: public XSAttributeDecl getAttributeDecl(String name) {
117: return atts.get(name);
118: }
119:
120: public Iterator<XSAttributeDecl> iterateAttributeDecls() {
121: return atts.values().iterator();
122: }
123:
124: private final Map<String, XSElementDecl> elems = new HashMap<String, XSElementDecl>();
125: private final Map<String, XSElementDecl> elemsView = Collections
126: .unmodifiableMap(elems);
127:
128: public void addElementDecl(XSElementDecl newDecl) {
129: elems.put(newDecl.getName(), newDecl);
130: }
131:
132: public Map<String, XSElementDecl> getElementDecls() {
133: return elemsView;
134: }
135:
136: public XSElementDecl getElementDecl(String name) {
137: return elems.get(name);
138: }
139:
140: public Iterator<XSElementDecl> iterateElementDecls() {
141: return elems.values().iterator();
142: }
143:
144: private final Map<String, XSAttGroupDecl> attGroups = new HashMap<String, XSAttGroupDecl>();
145: private final Map<String, XSAttGroupDecl> attGroupsView = Collections
146: .unmodifiableMap(attGroups);
147:
148: public void addAttGroupDecl(XSAttGroupDecl newDecl) {
149: attGroups.put(newDecl.getName(), newDecl);
150: }
151:
152: public Map<String, XSAttGroupDecl> getAttGroupDecls() {
153: return attGroupsView;
154: }
155:
156: public XSAttGroupDecl getAttGroupDecl(String name) {
157: return attGroups.get(name);
158: }
159:
160: public Iterator<XSAttGroupDecl> iterateAttGroupDecls() {
161: return attGroups.values().iterator();
162: }
163:
164: private final Map<String, XSNotation> notations = new HashMap<String, XSNotation>();
165: private final Map<String, XSNotation> notationsView = Collections
166: .unmodifiableMap(notations);
167:
168: public void addNotation(XSNotation newDecl) {
169: notations.put(newDecl.getName(), newDecl);
170: }
171:
172: public Map<String, XSNotation> getNotations() {
173: return notationsView;
174: }
175:
176: public XSNotation getNotation(String name) {
177: return notations.get(name);
178: }
179:
180: public Iterator<XSNotation> iterateNotations() {
181: return notations.values().iterator();
182: }
183:
184: private final Map<String, XSModelGroupDecl> modelGroups = new HashMap<String, XSModelGroupDecl>();
185: private final Map<String, XSModelGroupDecl> modelGroupsView = Collections
186: .unmodifiableMap(modelGroups);
187:
188: public void addModelGroupDecl(XSModelGroupDecl newDecl) {
189: modelGroups.put(newDecl.getName(), newDecl);
190: }
191:
192: public Map<String, XSModelGroupDecl> getModelGroupDecls() {
193: return modelGroupsView;
194: }
195:
196: public XSModelGroupDecl getModelGroupDecl(String name) {
197: return modelGroups.get(name);
198: }
199:
200: public Iterator<XSModelGroupDecl> iterateModelGroupDecls() {
201: return modelGroups.values().iterator();
202: }
203:
204: private final Map<String, XSIdentityConstraint> idConstraints = new HashMap<String, XSIdentityConstraint>();
205: private final Map<String, XSIdentityConstraint> idConstraintsView = Collections
206: .unmodifiableMap(idConstraints);
207:
208: protected void addIdentityConstraint(IdentityConstraintImpl c) {
209: idConstraints.put(c.getName(), c);
210: }
211:
212: public Map<String, XSIdentityConstraint> getIdentityConstraints() {
213: return idConstraintsView;
214: }
215:
216: public XSIdentityConstraint getIdentityConstraint(String localName) {
217: return idConstraints.get(localName);
218: }
219:
220: private final Map<String, XSType> allTypes = new HashMap<String, XSType>();
221: private final Map<String, XSType> allTypesView = Collections
222: .unmodifiableMap(allTypes);
223:
224: private final Map<String, XSSimpleType> simpleTypes = new HashMap<String, XSSimpleType>();
225: private final Map<String, XSSimpleType> simpleTypesView = Collections
226: .unmodifiableMap(simpleTypes);
227:
228: public void addSimpleType(XSSimpleType newDecl) {
229: simpleTypes.put(newDecl.getName(), newDecl);
230: allTypes.put(newDecl.getName(), newDecl);
231: }
232:
233: public Map<String, XSSimpleType> getSimpleTypes() {
234: return simpleTypesView;
235: }
236:
237: public XSSimpleType getSimpleType(String name) {
238: return simpleTypes.get(name);
239: }
240:
241: public Iterator<XSSimpleType> iterateSimpleTypes() {
242: return simpleTypes.values().iterator();
243: }
244:
245: private final Map<String, XSComplexType> complexTypes = new HashMap<String, XSComplexType>();
246: private final Map<String, XSComplexType> complexTypesView = Collections
247: .unmodifiableMap(complexTypes);
248:
249: public void addComplexType(XSComplexType newDecl) {
250: complexTypes.put(newDecl.getName(), newDecl);
251: allTypes.put(newDecl.getName(), newDecl);
252: }
253:
254: public Map<String, XSComplexType> getComplexTypes() {
255: return complexTypesView;
256: }
257:
258: public XSComplexType getComplexType(String name) {
259: return complexTypes.get(name);
260: }
261:
262: public Iterator<XSComplexType> iterateComplexTypes() {
263: return complexTypes.values().iterator();
264: }
265:
266: public Map<String, XSType> getTypes() {
267: return allTypesView;
268: }
269:
270: public XSType getType(String name) {
271: return allTypes.get(name);
272: }
273:
274: public Iterator<XSType> iterateTypes() {
275: return allTypes.values().iterator();
276: }
277:
278: public void visit(XSVisitor visitor) {
279: visitor.schema(this );
280: }
281:
282: public Object apply(XSFunction function) {
283: return function.schema(this );
284: }
285:
286: /**
287: * Lazily created list of {@link ForeignAttributesImpl}s.
288: */
289: private List<ForeignAttributes> foreignAttributes = null;
290: private List<ForeignAttributes> readOnlyForeignAttributes = null;
291:
292: public void addForeignAttributes(ForeignAttributesImpl fa) {
293: if (foreignAttributes == null)
294: foreignAttributes = new ArrayList<ForeignAttributes>();
295: foreignAttributes.add(fa);
296: }
297:
298: public List<ForeignAttributes> getForeignAttributes() {
299: if (readOnlyForeignAttributes == null) {
300: if (foreignAttributes == null)
301: readOnlyForeignAttributes = Collections.EMPTY_LIST;
302: else
303: readOnlyForeignAttributes = Collections
304: .unmodifiableList(foreignAttributes);
305: }
306: return readOnlyForeignAttributes;
307: }
308:
309: public String getForeignAttribute(String nsUri, String localName) {
310: for (ForeignAttributes fa : getForeignAttributes()) {
311: String v = fa.getValue(nsUri, localName);
312: if (v != null)
313: return v;
314: }
315: return null;
316: }
317: }
|