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