Thu, 12 Oct 2017 19:44:07 +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.XSComplexType;
29 import com.sun.xml.internal.xsom.XSType;
31 import java.util.ArrayList;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.Set;
36 /**
37 *
38 *
39 * @author
40 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
41 */
42 class Util {
43 private static XSType[] listDirectSubstitutables( XSType _this ) {
44 ArrayList r = new ArrayList();
46 // TODO: handle @block
47 Iterator itr = ((SchemaImpl)_this.getOwnerSchema()).parent.iterateTypes();
48 while( itr.hasNext() ) {
49 XSType t = (XSType)itr.next();
50 if( t.getBaseType()==_this )
51 r.add(t);
52 }
53 return (XSType[]) r.toArray(new XSType[r.size()]);
54 }
56 public static XSType[] listSubstitutables( XSType _this ) {
57 Set substitables = new HashSet();
58 buildSubstitutables( _this, substitables );
59 return (XSType[]) substitables.toArray(new XSType[substitables.size()]);
60 }
62 public static void buildSubstitutables( XSType _this, Set substitutables ) {
63 if( _this.isLocal() ) return;
64 buildSubstitutables( _this, _this, substitutables );
65 }
67 private static void buildSubstitutables( XSType head, XSType _this, Set substitutables ) {
68 if(!isSubstitutable(head,_this))
69 return; // no derived type of _this can substitute head.
71 if(substitutables.add(_this)) {
72 XSType[] child = listDirectSubstitutables(_this);
73 for( int i=0; i<child.length; i++ )
74 buildSubstitutables( head, child[i], substitutables );
75 }
76 }
78 /**
79 * Implements
80 * <code>Validation Rule: Schema-Validity Assessment (Element) 1.2.1.2.4</code>
81 */
82 private static boolean isSubstitutable( XSType _base, XSType derived ) {
83 // too ugly to the point that it's almost unbearable.
84 // I mean, it's not even transitive. Thus we end up calling this method
85 // for each candidate
86 if( _base.isComplexType() ) {
87 XSComplexType base = _base.asComplexType();
89 for( ; base!=derived; derived=derived.getBaseType() ) {
90 if( base.isSubstitutionProhibited( derived.getDerivationMethod() ) )
91 return false; // Type Derivation OK (Complex)-1
92 }
93 return true;
94 } else {
95 // simple type don't have any @block
96 return true;
97 }
98 }
100 }