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.XSComplexType;
023: import com.sun.xml.xsom.XSType;
024:
025: import java.util.ArrayList;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Set;
029:
030: /**
031: *
032: *
033: * @author
034: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
035: */
036: class Util {
037: private static XSType[] listDirectSubstitutables(XSType _this ) {
038: ArrayList r = new ArrayList();
039:
040: // TODO: handle @block
041: Iterator itr = ((SchemaImpl) _this .getOwnerSchema()).parent
042: .iterateTypes();
043: while (itr.hasNext()) {
044: XSType t = (XSType) itr.next();
045: if (t.getBaseType() == _this )
046: r.add(t);
047: }
048: return (XSType[]) r.toArray(new XSType[r.size()]);
049: }
050:
051: public static XSType[] listSubstitutables(XSType _this ) {
052: Set substitables = new HashSet();
053: buildSubstitutables(_this , substitables);
054: return (XSType[]) substitables.toArray(new XSType[substitables
055: .size()]);
056: }
057:
058: public static void buildSubstitutables(XSType _this ,
059: Set substitutables) {
060: if (_this .isLocal())
061: return;
062: buildSubstitutables(_this , _this , substitutables);
063: }
064:
065: private static void buildSubstitutables(XSType head, XSType _this ,
066: Set substitutables) {
067: if (!isSubstitutable(head, _this ))
068: return; // no derived type of _this can substitute head.
069:
070: if (substitutables.add(_this )) {
071: XSType[] child = listDirectSubstitutables(_this );
072: for (int i = 0; i < child.length; i++)
073: buildSubstitutables(head, child[i], substitutables);
074: }
075: }
076:
077: /**
078: * Implements
079: * <code>Validation Rule: Schema-Validity Assessment (Element) 1.2.1.2.4</code>
080: */
081: private static boolean isSubstitutable(XSType _base, XSType derived) {
082: // too ugly to the point that it's almost unbearable.
083: // I mean, it's not even transitive. Thus we end up calling this method
084: // for each candidate
085: if (_base.isComplexType()) {
086: XSComplexType base = _base.asComplexType();
087:
088: for (; base != derived; derived = derived.getBaseType()) {
089: if (base.isSubstitutionProhibited(derived
090: .getDerivationMethod()))
091: return false; // Type Derivation OK (Complex)-1
092: }
093: return true;
094: } else {
095: // simple type don't have any @block
096: return true;
097: }
098: }
099:
100: }
|