001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.xsom.impl;
021:
022: import com.sun.xml.xsom.ForeignAttributes;
023: import com.sun.xml.xsom.SCD;
024: import com.sun.xml.xsom.XSAnnotation;
025: import com.sun.xml.xsom.XSAttGroupDecl;
026: import com.sun.xml.xsom.XSAttributeDecl;
027: import com.sun.xml.xsom.XSComplexType;
028: import com.sun.xml.xsom.XSComponent;
029: import com.sun.xml.xsom.XSElementDecl;
030: import com.sun.xml.xsom.XSIdentityConstraint;
031: import com.sun.xml.xsom.XSModelGroupDecl;
032: import com.sun.xml.xsom.XSNotation;
033: import com.sun.xml.xsom.XSSchema;
034: import com.sun.xml.xsom.XSSimpleType;
035: import com.sun.xml.xsom.XSType;
036: import com.sun.xml.xsom.parser.SchemaDocument;
037: import com.sun.xml.xsom.visitor.XSFunction;
038: import com.sun.xml.xsom.visitor.XSVisitor;
039: import org.xml.sax.Locator;
040:
041: import javax.xml.namespace.NamespaceContext;
042: import java.text.ParseException;
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050:
051: public class SchemaImpl implements XSSchema {
052: public SchemaImpl(SchemaSetImpl _parent, Locator loc, String tns) {
053: if (tns == null)
054: // the empty string must be used.
055: throw new IllegalArgumentException();
056: this .targetNamespace = tns;
057: this .parent = _parent;
058: this .locator = loc;
059: }
060:
061: public SchemaDocument getSourceDocument() {
062: return null;
063: }
064:
065: public SchemaSetImpl getRoot() {
066: return parent;
067: }
068:
069: protected final SchemaSetImpl parent;
070:
071: private final String targetNamespace;
072:
073: public String getTargetNamespace() {
074: return targetNamespace;
075: }
076:
077: public XSSchema getOwnerSchema() {
078: return this ;
079: }
080:
081: private XSAnnotation annotation;
082:
083: public void setAnnotation(XSAnnotation a) {
084: annotation = a;
085: }
086:
087: public XSAnnotation getAnnotation() {
088: return annotation;
089: }
090:
091: public XSAnnotation getAnnotation(boolean createIfNotExist) {
092: if (createIfNotExist && annotation == null)
093: annotation = new AnnotationImpl();
094: return annotation;
095: }
096:
097: // it's difficult to determine the source location for the schema
098: // component as one schema can be defined across multiple files.
099: // so this locator might not correctly reflect all the locations
100: // where the schema is defined.
101: // but partial information would be better than nothing.
102:
103: private final Locator locator;
104:
105: public Locator getLocator() {
106: return locator;
107: }
108:
109: private final Map<String, XSAttributeDecl> atts = new HashMap<String, XSAttributeDecl>();
110: private final Map<String, XSAttributeDecl> attsView = Collections
111: .unmodifiableMap(atts);
112:
113: public void addAttributeDecl(XSAttributeDecl newDecl) {
114: atts.put(newDecl.getName(), newDecl);
115: }
116:
117: public Map<String, XSAttributeDecl> getAttributeDecls() {
118: return attsView;
119: }
120:
121: public XSAttributeDecl getAttributeDecl(String name) {
122: return atts.get(name);
123: }
124:
125: public Iterator<XSAttributeDecl> iterateAttributeDecls() {
126: return atts.values().iterator();
127: }
128:
129: private final Map<String, XSElementDecl> elems = new HashMap<String, XSElementDecl>();
130: private final Map<String, XSElementDecl> elemsView = Collections
131: .unmodifiableMap(elems);
132:
133: public void addElementDecl(XSElementDecl newDecl) {
134: elems.put(newDecl.getName(), newDecl);
135: }
136:
137: public Map<String, XSElementDecl> getElementDecls() {
138: return elemsView;
139: }
140:
141: public XSElementDecl getElementDecl(String name) {
142: return elems.get(name);
143: }
144:
145: public Iterator<XSElementDecl> iterateElementDecls() {
146: return elems.values().iterator();
147: }
148:
149: private final Map<String, XSAttGroupDecl> attGroups = new HashMap<String, XSAttGroupDecl>();
150: private final Map<String, XSAttGroupDecl> attGroupsView = Collections
151: .unmodifiableMap(attGroups);
152:
153: public void addAttGroupDecl(XSAttGroupDecl newDecl,
154: boolean overwrite) {
155: if (overwrite || !attGroups.containsKey(newDecl.getName()))
156: attGroups.put(newDecl.getName(), newDecl);
157: }
158:
159: public Map<String, XSAttGroupDecl> getAttGroupDecls() {
160: return attGroupsView;
161: }
162:
163: public XSAttGroupDecl getAttGroupDecl(String name) {
164: return attGroups.get(name);
165: }
166:
167: public Iterator<XSAttGroupDecl> iterateAttGroupDecls() {
168: return attGroups.values().iterator();
169: }
170:
171: private final Map<String, XSNotation> notations = new HashMap<String, XSNotation>();
172: private final Map<String, XSNotation> notationsView = Collections
173: .unmodifiableMap(notations);
174:
175: public void addNotation(XSNotation newDecl) {
176: notations.put(newDecl.getName(), newDecl);
177: }
178:
179: public Map<String, XSNotation> getNotations() {
180: return notationsView;
181: }
182:
183: public XSNotation getNotation(String name) {
184: return notations.get(name);
185: }
186:
187: public Iterator<XSNotation> iterateNotations() {
188: return notations.values().iterator();
189: }
190:
191: private final Map<String, XSModelGroupDecl> modelGroups = new HashMap<String, XSModelGroupDecl>();
192: private final Map<String, XSModelGroupDecl> modelGroupsView = Collections
193: .unmodifiableMap(modelGroups);
194:
195: public void addModelGroupDecl(XSModelGroupDecl newDecl,
196: boolean overwrite) {
197: if (overwrite || !modelGroups.containsKey(newDecl.getName()))
198: modelGroups.put(newDecl.getName(), newDecl);
199: }
200:
201: public Map<String, XSModelGroupDecl> getModelGroupDecls() {
202: return modelGroupsView;
203: }
204:
205: public XSModelGroupDecl getModelGroupDecl(String name) {
206: return modelGroups.get(name);
207: }
208:
209: public Iterator<XSModelGroupDecl> iterateModelGroupDecls() {
210: return modelGroups.values().iterator();
211: }
212:
213: private final Map<String, XSIdentityConstraint> idConstraints = new HashMap<String, XSIdentityConstraint>();
214: private final Map<String, XSIdentityConstraint> idConstraintsView = Collections
215: .unmodifiableMap(idConstraints);
216:
217: protected void addIdentityConstraint(IdentityConstraintImpl c) {
218: idConstraints.put(c.getName(), c);
219: }
220:
221: public Map<String, XSIdentityConstraint> getIdentityConstraints() {
222: return idConstraintsView;
223: }
224:
225: public XSIdentityConstraint getIdentityConstraint(String localName) {
226: return idConstraints.get(localName);
227: }
228:
229: private final Map<String, XSType> allTypes = new HashMap<String, XSType>();
230: private final Map<String, XSType> allTypesView = Collections
231: .unmodifiableMap(allTypes);
232:
233: private final Map<String, XSSimpleType> simpleTypes = new HashMap<String, XSSimpleType>();
234: private final Map<String, XSSimpleType> simpleTypesView = Collections
235: .unmodifiableMap(simpleTypes);
236:
237: public void addSimpleType(XSSimpleType newDecl, boolean overwrite) {
238: if (overwrite || !simpleTypes.containsKey(newDecl.getName())) {
239: simpleTypes.put(newDecl.getName(), newDecl);
240: allTypes.put(newDecl.getName(), newDecl);
241: }
242: }
243:
244: public Map<String, XSSimpleType> getSimpleTypes() {
245: return simpleTypesView;
246: }
247:
248: public XSSimpleType getSimpleType(String name) {
249: return simpleTypes.get(name);
250: }
251:
252: public Iterator<XSSimpleType> iterateSimpleTypes() {
253: return simpleTypes.values().iterator();
254: }
255:
256: private final Map<String, XSComplexType> complexTypes = new HashMap<String, XSComplexType>();
257: private final Map<String, XSComplexType> complexTypesView = Collections
258: .unmodifiableMap(complexTypes);
259:
260: public void addComplexType(XSComplexType newDecl, boolean overwrite) {
261: if (overwrite || !complexTypes.containsKey(newDecl.getName())) {
262: complexTypes.put(newDecl.getName(), newDecl);
263: allTypes.put(newDecl.getName(), newDecl);
264: }
265: }
266:
267: public Map<String, XSComplexType> getComplexTypes() {
268: return complexTypesView;
269: }
270:
271: public XSComplexType getComplexType(String name) {
272: return complexTypes.get(name);
273: }
274:
275: public Iterator<XSComplexType> iterateComplexTypes() {
276: return complexTypes.values().iterator();
277: }
278:
279: public Map<String, XSType> getTypes() {
280: return allTypesView;
281: }
282:
283: public XSType getType(String name) {
284: return allTypes.get(name);
285: }
286:
287: public Iterator<XSType> iterateTypes() {
288: return allTypes.values().iterator();
289: }
290:
291: public void visit(XSVisitor visitor) {
292: visitor.schema(this );
293: }
294:
295: public Object apply(XSFunction function) {
296: return function.schema(this );
297: }
298:
299: /**
300: * Lazily created list of {@link ForeignAttributesImpl}s.
301: */
302: private List<ForeignAttributes> foreignAttributes = null;
303: private List<ForeignAttributes> readOnlyForeignAttributes = null;
304:
305: public void addForeignAttributes(ForeignAttributesImpl fa) {
306: if (foreignAttributes == null)
307: foreignAttributes = new ArrayList<ForeignAttributes>();
308: foreignAttributes.add(fa);
309: }
310:
311: public List<ForeignAttributes> getForeignAttributes() {
312: if (readOnlyForeignAttributes == null) {
313: if (foreignAttributes == null)
314: readOnlyForeignAttributes = Collections.EMPTY_LIST;
315: else
316: readOnlyForeignAttributes = Collections
317: .unmodifiableList(foreignAttributes);
318: }
319: return readOnlyForeignAttributes;
320: }
321:
322: public String getForeignAttribute(String nsUri, String localName) {
323: for (ForeignAttributes fa : getForeignAttributes()) {
324: String v = fa.getValue(nsUri, localName);
325: if (v != null)
326: return v;
327: }
328: return null;
329: }
330:
331: public Collection<XSComponent> select(String scd,
332: NamespaceContext nsContext) {
333: try {
334: return SCD.create(scd, nsContext).select(this );
335: } catch (ParseException e) {
336: throw new IllegalArgumentException(e);
337: }
338: }
339:
340: public XSComponent selectSingle(String scd,
341: NamespaceContext nsContext) {
342: try {
343: return SCD.create(scd, nsContext).selectSingle(this );
344: } catch (ParseException e) {
345: throw new IllegalArgumentException(e);
346: }
347: }
348: }
|