Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, 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.XSAttGroupDecl; |
aoqi@0 | 29 | import com.sun.xml.internal.xsom.XSAttributeDecl; |
aoqi@0 | 30 | import com.sun.xml.internal.xsom.XSAttributeUse; |
aoqi@0 | 31 | import com.sun.xml.internal.xsom.XSComplexType; |
aoqi@0 | 32 | import com.sun.xml.internal.xsom.XSContentType; |
aoqi@0 | 33 | import com.sun.xml.internal.xsom.XSElementDecl; |
aoqi@0 | 34 | import com.sun.xml.internal.xsom.XSSchema; |
aoqi@0 | 35 | import com.sun.xml.internal.xsom.XSSchemaSet; |
aoqi@0 | 36 | import com.sun.xml.internal.xsom.XSSimpleType; |
aoqi@0 | 37 | import com.sun.xml.internal.xsom.XSType; |
aoqi@0 | 38 | import com.sun.xml.internal.xsom.XSWildcard; |
aoqi@0 | 39 | import com.sun.xml.internal.xsom.impl.parser.DelayedRef; |
aoqi@0 | 40 | import com.sun.xml.internal.xsom.impl.parser.SchemaDocumentImpl; |
aoqi@0 | 41 | import com.sun.xml.internal.xsom.impl.scd.Iterators; |
aoqi@0 | 42 | import com.sun.xml.internal.xsom.visitor.XSFunction; |
aoqi@0 | 43 | import com.sun.xml.internal.xsom.visitor.XSVisitor; |
aoqi@0 | 44 | import java.util.ArrayList; |
aoqi@0 | 45 | import java.util.List; |
aoqi@0 | 46 | import org.xml.sax.Locator; |
aoqi@0 | 47 | |
aoqi@0 | 48 | import java.util.Collection; |
aoqi@0 | 49 | import java.util.HashMap; |
aoqi@0 | 50 | import java.util.Iterator; |
aoqi@0 | 51 | import java.util.Map; |
aoqi@0 | 52 | |
aoqi@0 | 53 | public class ComplexTypeImpl extends AttributesHolder implements XSComplexType, Ref.ComplexType |
aoqi@0 | 54 | { |
aoqi@0 | 55 | public ComplexTypeImpl( SchemaDocumentImpl _parent, |
aoqi@0 | 56 | AnnotationImpl _annon, Locator _loc, ForeignAttributesImpl _fa, |
aoqi@0 | 57 | String _name, boolean _anonymous, |
aoqi@0 | 58 | |
aoqi@0 | 59 | boolean _abstract, int _derivationMethod, |
aoqi@0 | 60 | Ref.Type _base, int _final, int _block, boolean _mixed ) { |
aoqi@0 | 61 | |
aoqi@0 | 62 | super(_parent,_annon,_loc,_fa,_name,_anonymous); |
aoqi@0 | 63 | |
aoqi@0 | 64 | if(_base==null) |
aoqi@0 | 65 | throw new IllegalArgumentException(); |
aoqi@0 | 66 | |
aoqi@0 | 67 | this._abstract = _abstract; |
aoqi@0 | 68 | this.derivationMethod = _derivationMethod; |
aoqi@0 | 69 | this.baseType = _base; |
aoqi@0 | 70 | this.finalValue = _final; |
aoqi@0 | 71 | this.blockValue = _block; |
aoqi@0 | 72 | this.mixed = _mixed; |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | public XSComplexType asComplexType(){ return this; } |
aoqi@0 | 76 | |
aoqi@0 | 77 | public boolean isDerivedFrom(XSType t) { |
aoqi@0 | 78 | XSType x = this; |
aoqi@0 | 79 | while(true) { |
aoqi@0 | 80 | if(t==x) |
aoqi@0 | 81 | return true; |
aoqi@0 | 82 | XSType s = x.getBaseType(); |
aoqi@0 | 83 | if(s==x) |
aoqi@0 | 84 | return false; |
aoqi@0 | 85 | x = s; |
aoqi@0 | 86 | } |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | public XSSimpleType asSimpleType() { return null; } |
aoqi@0 | 90 | public final boolean isSimpleType() { return false; } |
aoqi@0 | 91 | public final boolean isComplexType(){ return true; } |
aoqi@0 | 92 | |
aoqi@0 | 93 | private int derivationMethod; |
aoqi@0 | 94 | public int getDerivationMethod() { return derivationMethod; } |
aoqi@0 | 95 | |
aoqi@0 | 96 | private Ref.Type baseType; |
aoqi@0 | 97 | public XSType getBaseType() { return baseType.getType(); } |
aoqi@0 | 98 | |
aoqi@0 | 99 | /** |
aoqi@0 | 100 | * Called when this complex type redefines the specified complex type. |
aoqi@0 | 101 | */ |
aoqi@0 | 102 | public void redefine( ComplexTypeImpl ct ) { |
aoqi@0 | 103 | if( baseType instanceof DelayedRef ) |
aoqi@0 | 104 | ((DelayedRef)baseType).redefine(ct); |
aoqi@0 | 105 | else |
aoqi@0 | 106 | this.baseType = ct; |
aoqi@0 | 107 | ct.redefinedBy = this; |
aoqi@0 | 108 | redefiningCount = (short)(ct.redefiningCount+1); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | |
aoqi@0 | 111 | /** |
aoqi@0 | 112 | * Number of times this component redefines other components. |
aoqi@0 | 113 | */ |
aoqi@0 | 114 | private short redefiningCount = 0; |
aoqi@0 | 115 | |
aoqi@0 | 116 | private ComplexTypeImpl redefinedBy = null; |
aoqi@0 | 117 | |
aoqi@0 | 118 | public XSComplexType getRedefinedBy() { |
aoqi@0 | 119 | return redefinedBy; |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | public int getRedefinedCount() { |
aoqi@0 | 123 | int i=0; |
aoqi@0 | 124 | for( ComplexTypeImpl ct=this.redefinedBy; ct!=null; ct=ct.redefinedBy) |
aoqi@0 | 125 | i++; |
aoqi@0 | 126 | return i; |
aoqi@0 | 127 | } |
aoqi@0 | 128 | |
aoqi@0 | 129 | |
aoqi@0 | 130 | private XSElementDecl scope; |
aoqi@0 | 131 | public XSElementDecl getScope() { return scope; } |
aoqi@0 | 132 | public void setScope( XSElementDecl _scope ) { this.scope=_scope; } |
aoqi@0 | 133 | |
aoqi@0 | 134 | private final boolean _abstract; |
aoqi@0 | 135 | public boolean isAbstract() { return _abstract; } |
aoqi@0 | 136 | |
aoqi@0 | 137 | private WildcardImpl localAttWildcard; |
aoqi@0 | 138 | /** |
aoqi@0 | 139 | * Set the local attribute wildcard. |
aoqi@0 | 140 | */ |
aoqi@0 | 141 | public void setWildcard( WildcardImpl wc ) { |
aoqi@0 | 142 | this.localAttWildcard = wc; |
aoqi@0 | 143 | } |
aoqi@0 | 144 | public XSWildcard getAttributeWildcard() { |
aoqi@0 | 145 | WildcardImpl complete = localAttWildcard; |
aoqi@0 | 146 | |
aoqi@0 | 147 | Iterator itr = iterateAttGroups(); |
aoqi@0 | 148 | while( itr.hasNext() ) { |
aoqi@0 | 149 | WildcardImpl w = (WildcardImpl)((XSAttGroupDecl)itr.next()).getAttributeWildcard(); |
aoqi@0 | 150 | |
aoqi@0 | 151 | if(w==null) continue; |
aoqi@0 | 152 | |
aoqi@0 | 153 | if(complete==null) |
aoqi@0 | 154 | complete = w; |
aoqi@0 | 155 | else |
aoqi@0 | 156 | // TODO: the spec says it's intersection, |
aoqi@0 | 157 | // but I think it has to be union. |
aoqi@0 | 158 | complete = complete.union(ownerDocument,w); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | |
aoqi@0 | 161 | if( getDerivationMethod()==RESTRICTION ) return complete; |
aoqi@0 | 162 | |
aoqi@0 | 163 | WildcardImpl base=null; |
aoqi@0 | 164 | XSType baseType = getBaseType(); |
aoqi@0 | 165 | if(baseType.asComplexType()!=null) |
aoqi@0 | 166 | base = (WildcardImpl)baseType.asComplexType().getAttributeWildcard(); |
aoqi@0 | 167 | |
aoqi@0 | 168 | if(complete==null) return base; |
aoqi@0 | 169 | if(base==null) return complete; |
aoqi@0 | 170 | |
aoqi@0 | 171 | return complete.union(ownerDocument,base); |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | private final int finalValue; |
aoqi@0 | 175 | public boolean isFinal( int derivationMethod ) { |
aoqi@0 | 176 | return (finalValue&derivationMethod)!=0; |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | private final int blockValue; |
aoqi@0 | 180 | public boolean isSubstitutionProhibited( int method ) { |
aoqi@0 | 181 | return (blockValue&method)!=0; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | |
aoqi@0 | 185 | private Ref.ContentType contentType; |
aoqi@0 | 186 | public void setContentType( Ref.ContentType v ) { contentType = v; } |
aoqi@0 | 187 | public XSContentType getContentType() { return contentType.getContentType(); } |
aoqi@0 | 188 | |
aoqi@0 | 189 | private XSContentType explicitContent; |
aoqi@0 | 190 | public void setExplicitContent( XSContentType v ) { |
aoqi@0 | 191 | this.explicitContent = v; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | public XSContentType getExplicitContent() { return explicitContent; } |
aoqi@0 | 194 | |
aoqi@0 | 195 | private final boolean mixed; |
aoqi@0 | 196 | public boolean isMixed() { return mixed; } |
aoqi@0 | 197 | |
aoqi@0 | 198 | |
aoqi@0 | 199 | |
aoqi@0 | 200 | |
aoqi@0 | 201 | public XSAttributeUse getAttributeUse( String nsURI, String localName ) { |
aoqi@0 | 202 | UName name = new UName(nsURI,localName); |
aoqi@0 | 203 | |
aoqi@0 | 204 | if(prohibitedAtts.contains(name)) return null; |
aoqi@0 | 205 | |
aoqi@0 | 206 | XSAttributeUse o = attributes.get(name); |
aoqi@0 | 207 | |
aoqi@0 | 208 | |
aoqi@0 | 209 | if(o==null) { |
aoqi@0 | 210 | Iterator itr = iterateAttGroups(); |
aoqi@0 | 211 | while(itr.hasNext() && o==null) |
aoqi@0 | 212 | o = ((XSAttGroupDecl)itr.next()).getAttributeUse(nsURI,localName); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | if(o==null) { |
aoqi@0 | 216 | XSType base = getBaseType(); |
aoqi@0 | 217 | if(base.asComplexType()!=null) |
aoqi@0 | 218 | o = base.asComplexType().getAttributeUse(nsURI,localName); |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | return o; |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | public Iterator<XSAttributeUse> iterateAttributeUses() { |
aoqi@0 | 225 | |
aoqi@0 | 226 | XSComplexType baseType = getBaseType().asComplexType(); |
aoqi@0 | 227 | |
aoqi@0 | 228 | if( baseType==null ) return super.iterateAttributeUses(); |
aoqi@0 | 229 | |
aoqi@0 | 230 | return new Iterators.Union<XSAttributeUse>( |
aoqi@0 | 231 | new Iterators.Filter<XSAttributeUse>(baseType.iterateAttributeUses()) { |
aoqi@0 | 232 | protected boolean matches(XSAttributeUse value) { |
aoqi@0 | 233 | XSAttributeDecl u = value.getDecl(); |
aoqi@0 | 234 | UName n = new UName(u.getTargetNamespace(),u.getName()); |
aoqi@0 | 235 | return !prohibitedAtts.contains(n); |
aoqi@0 | 236 | } |
aoqi@0 | 237 | }, |
aoqi@0 | 238 | super.iterateAttributeUses() ); |
aoqi@0 | 239 | } |
aoqi@0 | 240 | |
aoqi@0 | 241 | public Collection<XSAttributeUse> getAttributeUses() { |
aoqi@0 | 242 | XSComplexType baseType = getBaseType().asComplexType(); |
aoqi@0 | 243 | |
aoqi@0 | 244 | if( baseType==null ) return super.getAttributeUses(); |
aoqi@0 | 245 | |
aoqi@0 | 246 | // TODO: this is fairly inefficient |
aoqi@0 | 247 | Map<UName,XSAttributeUse> uses = new HashMap<UName, XSAttributeUse>(); |
aoqi@0 | 248 | for( XSAttributeUse a : baseType.getAttributeUses()) |
aoqi@0 | 249 | uses.put(new UName(a.getDecl()),a); |
aoqi@0 | 250 | |
aoqi@0 | 251 | uses.keySet().removeAll(prohibitedAtts); |
aoqi@0 | 252 | |
aoqi@0 | 253 | for( XSAttributeUse a : super.getAttributeUses()) |
aoqi@0 | 254 | uses.put(new UName(a.getDecl()),a); |
aoqi@0 | 255 | |
aoqi@0 | 256 | return uses.values(); |
aoqi@0 | 257 | } |
aoqi@0 | 258 | |
aoqi@0 | 259 | |
aoqi@0 | 260 | public XSType[] listSubstitutables() { |
aoqi@0 | 261 | return Util.listSubstitutables(this); |
aoqi@0 | 262 | } |
aoqi@0 | 263 | |
aoqi@0 | 264 | public void visit( XSVisitor visitor ) { |
aoqi@0 | 265 | visitor.complexType(this); |
aoqi@0 | 266 | } |
aoqi@0 | 267 | public <T> T apply( XSFunction<T> function ) { |
aoqi@0 | 268 | return function.complexType(this); |
aoqi@0 | 269 | } |
aoqi@0 | 270 | |
aoqi@0 | 271 | // Ref.ComplexType implementation |
aoqi@0 | 272 | public XSComplexType getType() { return this; } |
aoqi@0 | 273 | |
aoqi@0 | 274 | public List<XSComplexType> getSubtypes() { |
aoqi@0 | 275 | ArrayList subtypeList = new ArrayList(); |
aoqi@0 | 276 | Iterator<XSComplexType> cTypes = getRoot().iterateComplexTypes(); |
aoqi@0 | 277 | while (cTypes.hasNext()) { |
aoqi@0 | 278 | XSComplexType cType= cTypes.next(); |
aoqi@0 | 279 | XSType base = cType.getBaseType(); |
aoqi@0 | 280 | if ((base != null) && (base.equals(this))) { |
aoqi@0 | 281 | subtypeList.add(cType); |
aoqi@0 | 282 | } |
aoqi@0 | 283 | } |
aoqi@0 | 284 | return subtypeList; |
aoqi@0 | 285 | } |
aoqi@0 | 286 | |
aoqi@0 | 287 | public List<XSElementDecl> getElementDecls() { |
aoqi@0 | 288 | ArrayList declList = new ArrayList(); |
aoqi@0 | 289 | XSSchemaSet schemaSet = getRoot(); |
aoqi@0 | 290 | for (XSSchema sch : schemaSet.getSchemas()) { |
aoqi@0 | 291 | for (XSElementDecl decl : sch.getElementDecls().values()) { |
aoqi@0 | 292 | if (decl.getType().equals(this)) { |
aoqi@0 | 293 | declList.add(decl); |
aoqi@0 | 294 | } |
aoqi@0 | 295 | } |
aoqi@0 | 296 | } |
aoqi@0 | 297 | return declList; |
aoqi@0 | 298 | } |
aoqi@0 | 299 | } |