001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.reader.xmlschema;
027:
028: import java.util.HashMap;
029: import java.util.HashSet;
030: import java.util.Map;
031: import java.util.Set;
032: import java.util.Collections;
033:
034: import com.sun.xml.internal.xsom.XSAnnotation;
035: import com.sun.xml.internal.xsom.XSAttGroupDecl;
036: import com.sun.xml.internal.xsom.XSAttributeDecl;
037: import com.sun.xml.internal.xsom.XSAttributeUse;
038: import com.sun.xml.internal.xsom.XSComplexType;
039: import com.sun.xml.internal.xsom.XSComponent;
040: import com.sun.xml.internal.xsom.XSContentType;
041: import com.sun.xml.internal.xsom.XSElementDecl;
042: import com.sun.xml.internal.xsom.XSFacet;
043: import com.sun.xml.internal.xsom.XSIdentityConstraint;
044: import com.sun.xml.internal.xsom.XSModelGroup;
045: import com.sun.xml.internal.xsom.XSModelGroupDecl;
046: import com.sun.xml.internal.xsom.XSNotation;
047: import com.sun.xml.internal.xsom.XSParticle;
048: import com.sun.xml.internal.xsom.XSSchema;
049: import com.sun.xml.internal.xsom.XSSchemaSet;
050: import com.sun.xml.internal.xsom.XSSimpleType;
051: import com.sun.xml.internal.xsom.XSType;
052: import com.sun.xml.internal.xsom.XSWildcard;
053: import com.sun.xml.internal.xsom.XSXPath;
054: import com.sun.xml.internal.xsom.visitor.XSVisitor;
055:
056: /**
057: * Finds which {@link XSComponent}s refer to which {@link XSComplexType}s.
058: *
059: * @author Kohsuke Kawaguchi
060: */
061: final class RefererFinder implements XSVisitor {
062: private final Set<Object> visited = new HashSet<Object>();
063:
064: private final Map<XSComponent, Set<XSComponent>> referers = new HashMap<XSComponent, Set<XSComponent>>();
065:
066: public Set<XSComponent> getReferer(XSComponent src) {
067: Set<XSComponent> r = referers.get(src);
068: if (r == null)
069: return Collections.emptySet();
070: return r;
071: }
072:
073: public void schemaSet(XSSchemaSet xss) {
074: if (!visited.add(xss))
075: return;
076:
077: for (XSSchema xs : xss.getSchemas()) {
078: schema(xs);
079: }
080: }
081:
082: public void schema(XSSchema xs) {
083: if (!visited.add(xs))
084: return;
085:
086: for (XSComplexType ct : xs.getComplexTypes().values()) {
087: complexType(ct);
088: }
089:
090: for (XSElementDecl e : xs.getElementDecls().values()) {
091: elementDecl(e);
092: }
093: }
094:
095: public void elementDecl(XSElementDecl e) {
096: if (!visited.add(e))
097: return;
098:
099: refer(e, e.getType());
100: e.getType().visit(this );
101: }
102:
103: public void complexType(XSComplexType ct) {
104: if (!visited.add(ct))
105: return;
106:
107: refer(ct, ct.getBaseType());
108: ct.getBaseType().visit(this );
109: ct.getContentType().visit(this );
110: }
111:
112: public void modelGroupDecl(XSModelGroupDecl decl) {
113: if (!visited.add(decl))
114: return;
115:
116: modelGroup(decl.getModelGroup());
117: }
118:
119: public void modelGroup(XSModelGroup group) {
120: if (!visited.add(group))
121: return;
122:
123: for (XSParticle p : group.getChildren()) {
124: particle(p);
125: }
126: }
127:
128: public void particle(XSParticle particle) {
129: // since the particle method is side-effect free, no need to check for double-visit.
130: particle.getTerm().visit(this );
131: }
132:
133: // things we don't care
134: public void simpleType(XSSimpleType simpleType) {
135: }
136:
137: public void annotation(XSAnnotation ann) {
138: }
139:
140: public void attGroupDecl(XSAttGroupDecl decl) {
141: }
142:
143: public void attributeDecl(XSAttributeDecl decl) {
144: }
145:
146: public void attributeUse(XSAttributeUse use) {
147: }
148:
149: public void facet(XSFacet facet) {
150: }
151:
152: public void notation(XSNotation notation) {
153: }
154:
155: public void identityConstraint(XSIdentityConstraint decl) {
156: }
157:
158: public void xpath(XSXPath xp) {
159: }
160:
161: public void wildcard(XSWildcard wc) {
162: }
163:
164: public void empty(XSContentType empty) {
165: }
166:
167: /**
168: * Called for each reference to record the fact.
169: *
170: * So far we only care about references to types.
171: */
172: private void refer(XSComponent source, XSType target) {
173: Set<XSComponent> r = referers.get(target);
174: if (r == null) {
175: r = new HashSet<XSComponent>();
176: referers.put(target, r);
177: }
178: r.add(source);
179: }
180: }
|