Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.xsom.impl;
28 import com.sun.xml.internal.xsom.ForeignAttributes;
29 import com.sun.xml.internal.xsom.SCD;
30 import com.sun.xml.internal.xsom.XSAnnotation;
31 import com.sun.xml.internal.xsom.XSAttGroupDecl;
32 import com.sun.xml.internal.xsom.XSAttributeDecl;
33 import com.sun.xml.internal.xsom.XSComplexType;
34 import com.sun.xml.internal.xsom.XSComponent;
35 import com.sun.xml.internal.xsom.XSElementDecl;
36 import com.sun.xml.internal.xsom.XSIdentityConstraint;
37 import com.sun.xml.internal.xsom.XSModelGroupDecl;
38 import com.sun.xml.internal.xsom.XSNotation;
39 import com.sun.xml.internal.xsom.XSSchema;
40 import com.sun.xml.internal.xsom.XSSimpleType;
41 import com.sun.xml.internal.xsom.XSType;
42 import com.sun.xml.internal.xsom.parser.SchemaDocument;
43 import com.sun.xml.internal.xsom.visitor.XSFunction;
44 import com.sun.xml.internal.xsom.visitor.XSVisitor;
45 import org.xml.sax.Locator;
47 import javax.xml.namespace.NamespaceContext;
48 import java.text.ParseException;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.HashMap;
53 import java.util.Iterator;
54 import java.util.List;
55 import java.util.Map;
57 public class SchemaImpl implements XSSchema
58 {
59 public SchemaImpl(SchemaSetImpl _parent, Locator loc, String tns) {
60 if (tns == null)
61 // the empty string must be used.
62 throw new IllegalArgumentException();
63 this.targetNamespace = tns;
64 this.parent = _parent;
65 this.locator = loc;
66 }
68 public SchemaDocument getSourceDocument() {
69 return null;
70 }
72 public SchemaSetImpl getRoot() {
73 return parent;
74 }
76 protected final SchemaSetImpl parent;
78 private final String targetNamespace;
79 public String getTargetNamespace() {
80 return targetNamespace;
81 }
83 public XSSchema getOwnerSchema() {
84 return this;
85 }
87 private XSAnnotation annotation;
88 public void setAnnotation(XSAnnotation a) {
89 annotation = a;
90 }
91 public XSAnnotation getAnnotation() {
92 return annotation;
93 }
95 public XSAnnotation getAnnotation(boolean createIfNotExist) {
96 if(createIfNotExist && annotation==null)
97 annotation = new AnnotationImpl();
98 return annotation;
99 }
101 // it's difficult to determine the source location for the schema
102 // component as one schema can be defined across multiple files.
103 // so this locator might not correctly reflect all the locations
104 // where the schema is defined.
105 // but partial information would be better than nothing.
107 private final Locator locator;
108 public Locator getLocator() {
109 return locator;
110 }
113 private final Map<String,XSAttributeDecl> atts = new HashMap<String,XSAttributeDecl>();
114 private final Map<String,XSAttributeDecl> attsView = Collections.unmodifiableMap(atts);
115 public void addAttributeDecl(XSAttributeDecl newDecl) {
116 atts.put(newDecl.getName(), newDecl);
117 }
118 public Map<String,XSAttributeDecl> getAttributeDecls() {
119 return attsView;
120 }
121 public XSAttributeDecl getAttributeDecl(String name) {
122 return atts.get(name);
123 }
124 public Iterator<XSAttributeDecl> iterateAttributeDecls() {
125 return atts.values().iterator();
126 }
128 private final Map<String,XSElementDecl> elems = new HashMap<String,XSElementDecl>();
129 private final Map<String,XSElementDecl> elemsView = Collections.unmodifiableMap(elems);
130 public void addElementDecl(XSElementDecl newDecl) {
131 elems.put(newDecl.getName(), newDecl);
132 }
133 public Map<String,XSElementDecl> getElementDecls() {
134 return elemsView;
135 }
136 public XSElementDecl getElementDecl(String name) {
137 return elems.get(name);
138 }
139 public Iterator<XSElementDecl> iterateElementDecls() {
140 return elems.values().iterator();
141 }
143 private final Map<String,XSAttGroupDecl> attGroups = new HashMap<String,XSAttGroupDecl>();
144 private final Map<String,XSAttGroupDecl> attGroupsView = Collections.unmodifiableMap(attGroups);
145 public void addAttGroupDecl(XSAttGroupDecl newDecl, boolean overwrite) {
146 if(overwrite || !attGroups.containsKey(newDecl.getName()))
147 attGroups.put(newDecl.getName(), newDecl);
148 }
149 public Map<String,XSAttGroupDecl> getAttGroupDecls() {
150 return attGroupsView;
151 }
152 public XSAttGroupDecl getAttGroupDecl(String name) {
153 return attGroups.get(name);
154 }
155 public Iterator<XSAttGroupDecl> iterateAttGroupDecls() {
156 return attGroups.values().iterator();
157 }
160 private final Map<String,XSNotation> notations = new HashMap<String,XSNotation>();
161 private final Map<String,XSNotation> notationsView = Collections.unmodifiableMap(notations);
162 public void addNotation( XSNotation newDecl ) {
163 notations.put( newDecl.getName(), newDecl );
164 }
165 public Map<String,XSNotation> getNotations() {
166 return notationsView;
167 }
168 public XSNotation getNotation( String name ) {
169 return notations.get(name);
170 }
171 public Iterator<XSNotation> iterateNotations() {
172 return notations.values().iterator();
173 }
175 private final Map<String,XSModelGroupDecl> modelGroups = new HashMap<String,XSModelGroupDecl>();
176 private final Map<String,XSModelGroupDecl> modelGroupsView = Collections.unmodifiableMap(modelGroups);
177 public void addModelGroupDecl(XSModelGroupDecl newDecl, boolean overwrite) {
178 if(overwrite || !modelGroups.containsKey(newDecl.getName()))
179 modelGroups.put(newDecl.getName(), newDecl);
180 }
181 public Map<String,XSModelGroupDecl> getModelGroupDecls() {
182 return modelGroupsView;
183 }
184 public XSModelGroupDecl getModelGroupDecl(String name) {
185 return modelGroups.get(name);
186 }
187 public Iterator<XSModelGroupDecl> iterateModelGroupDecls() {
188 return modelGroups.values().iterator();
189 }
192 private final Map<String,XSIdentityConstraint> idConstraints = new HashMap<String,XSIdentityConstraint>();
193 private final Map<String,XSIdentityConstraint> idConstraintsView = Collections.unmodifiableMap(idConstraints);
195 protected void addIdentityConstraint(IdentityConstraintImpl c) {
196 idConstraints.put(c.getName(),c);
197 }
199 public Map<String, XSIdentityConstraint> getIdentityConstraints() {
200 return idConstraintsView;
201 }
203 public XSIdentityConstraint getIdentityConstraint(String localName) {
204 return idConstraints.get(localName);
205 }
207 private final Map<String,XSType> allTypes = new HashMap<String,XSType>();
208 private final Map<String,XSType> allTypesView = Collections.unmodifiableMap(allTypes);
210 private final Map<String,XSSimpleType> simpleTypes = new HashMap<String,XSSimpleType>();
211 private final Map<String,XSSimpleType> simpleTypesView = Collections.unmodifiableMap(simpleTypes);
212 public void addSimpleType(XSSimpleType newDecl, boolean overwrite) {
213 if(overwrite || !simpleTypes.containsKey(newDecl.getName())) {
214 simpleTypes.put(newDecl.getName(), newDecl);
215 allTypes.put(newDecl.getName(), newDecl);
216 }
217 }
218 public Map<String,XSSimpleType> getSimpleTypes() {
219 return simpleTypesView;
220 }
221 public XSSimpleType getSimpleType(String name) {
222 return simpleTypes.get(name);
223 }
224 public Iterator<XSSimpleType> iterateSimpleTypes() {
225 return simpleTypes.values().iterator();
226 }
228 private final Map<String,XSComplexType> complexTypes = new HashMap<String,XSComplexType>();
229 private final Map<String,XSComplexType> complexTypesView = Collections.unmodifiableMap(complexTypes);
230 public void addComplexType(XSComplexType newDecl, boolean overwrite) {
231 if(overwrite || !complexTypes.containsKey(newDecl.getName())) {
232 complexTypes.put(newDecl.getName(), newDecl);
233 allTypes.put(newDecl.getName(), newDecl);
234 }
235 }
236 public Map<String,XSComplexType> getComplexTypes() {
237 return complexTypesView;
238 }
239 public XSComplexType getComplexType(String name) {
240 return complexTypes.get(name);
241 }
242 public Iterator<XSComplexType> iterateComplexTypes() {
243 return complexTypes.values().iterator();
244 }
246 public Map<String,XSType> getTypes() {
247 return allTypesView;
248 }
249 public XSType getType(String name) {
250 return allTypes.get(name);
251 }
252 public Iterator<XSType> iterateTypes() {
253 return allTypes.values().iterator();
254 }
256 public void visit(XSVisitor visitor) {
257 visitor.schema(this);
258 }
260 public Object apply(XSFunction function) {
261 return function.schema(this);
262 }
264 /**
265 * Lazily created list of {@link ForeignAttributesImpl}s.
266 */
267 private List<ForeignAttributes> foreignAttributes = null;
268 private List<ForeignAttributes> readOnlyForeignAttributes = null;
270 public void addForeignAttributes(ForeignAttributesImpl fa) {
271 if(foreignAttributes==null)
272 foreignAttributes = new ArrayList<ForeignAttributes>();
273 foreignAttributes.add(fa);
274 }
276 public List<ForeignAttributes> getForeignAttributes() {
277 if(readOnlyForeignAttributes==null) {
278 if(foreignAttributes==null)
279 readOnlyForeignAttributes = Collections.EMPTY_LIST;
280 else
281 readOnlyForeignAttributes = Collections.unmodifiableList(foreignAttributes);
282 }
283 return readOnlyForeignAttributes;
284 }
286 public String getForeignAttribute(String nsUri, String localName) {
287 for( ForeignAttributes fa : getForeignAttributes() ) {
288 String v = fa.getValue(nsUri,localName);
289 if(v!=null) return v;
290 }
291 return null;
292 }
294 public Collection<XSComponent> select(String scd, NamespaceContext nsContext) {
295 try {
296 return SCD.create(scd,nsContext).select(this);
297 } catch (ParseException e) {
298 throw new IllegalArgumentException(e);
299 }
300 }
302 public XSComponent selectSingle(String scd, NamespaceContext nsContext) {
303 try {
304 return SCD.create(scd,nsContext).selectSingle(this);
305 } catch (ParseException e) {
306 throw new IllegalArgumentException(e);
307 }
308 }
309 }