001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.reader.xmlschema;
038:
039: import java.util.HashMap;
040: import java.util.HashSet;
041: import java.util.Map;
042: import java.util.Set;
043: import java.util.Collections;
044:
045: import com.sun.xml.xsom.XSAnnotation;
046: import com.sun.xml.xsom.XSAttGroupDecl;
047: import com.sun.xml.xsom.XSAttributeDecl;
048: import com.sun.xml.xsom.XSAttributeUse;
049: import com.sun.xml.xsom.XSComplexType;
050: import com.sun.xml.xsom.XSComponent;
051: import com.sun.xml.xsom.XSContentType;
052: import com.sun.xml.xsom.XSElementDecl;
053: import com.sun.xml.xsom.XSFacet;
054: import com.sun.xml.xsom.XSIdentityConstraint;
055: import com.sun.xml.xsom.XSModelGroup;
056: import com.sun.xml.xsom.XSModelGroupDecl;
057: import com.sun.xml.xsom.XSNotation;
058: import com.sun.xml.xsom.XSParticle;
059: import com.sun.xml.xsom.XSSchema;
060: import com.sun.xml.xsom.XSSchemaSet;
061: import com.sun.xml.xsom.XSSimpleType;
062: import com.sun.xml.xsom.XSType;
063: import com.sun.xml.xsom.XSWildcard;
064: import com.sun.xml.xsom.XSXPath;
065: import com.sun.xml.xsom.visitor.XSVisitor;
066:
067: /**
068: * Finds which {@link XSComponent}s refer to which {@link XSComplexType}s.
069: *
070: * @author Kohsuke Kawaguchi
071: */
072: final class RefererFinder implements XSVisitor {
073: private final Set<Object> visited = new HashSet<Object>();
074:
075: private final Map<XSComponent, Set<XSComponent>> referers = new HashMap<XSComponent, Set<XSComponent>>();
076:
077: public Set<XSComponent> getReferer(XSComponent src) {
078: Set<XSComponent> r = referers.get(src);
079: if (r == null)
080: return Collections.emptySet();
081: return r;
082: }
083:
084: public void schemaSet(XSSchemaSet xss) {
085: if (!visited.add(xss))
086: return;
087:
088: for (XSSchema xs : xss.getSchemas()) {
089: schema(xs);
090: }
091: }
092:
093: public void schema(XSSchema xs) {
094: if (!visited.add(xs))
095: return;
096:
097: for (XSComplexType ct : xs.getComplexTypes().values()) {
098: complexType(ct);
099: }
100:
101: for (XSElementDecl e : xs.getElementDecls().values()) {
102: elementDecl(e);
103: }
104: }
105:
106: public void elementDecl(XSElementDecl e) {
107: if (!visited.add(e))
108: return;
109:
110: refer(e, e.getType());
111: e.getType().visit(this );
112: }
113:
114: public void complexType(XSComplexType ct) {
115: if (!visited.add(ct))
116: return;
117:
118: refer(ct, ct.getBaseType());
119: ct.getBaseType().visit(this );
120: ct.getContentType().visit(this );
121: }
122:
123: public void modelGroupDecl(XSModelGroupDecl decl) {
124: if (!visited.add(decl))
125: return;
126:
127: modelGroup(decl.getModelGroup());
128: }
129:
130: public void modelGroup(XSModelGroup group) {
131: if (!visited.add(group))
132: return;
133:
134: for (XSParticle p : group.getChildren()) {
135: particle(p);
136: }
137: }
138:
139: public void particle(XSParticle particle) {
140: // since the particle method is side-effect free, no need to check for double-visit.
141: particle.getTerm().visit(this );
142: }
143:
144: // things we don't care
145: public void simpleType(XSSimpleType simpleType) {
146: }
147:
148: public void annotation(XSAnnotation ann) {
149: }
150:
151: public void attGroupDecl(XSAttGroupDecl decl) {
152: }
153:
154: public void attributeDecl(XSAttributeDecl decl) {
155: }
156:
157: public void attributeUse(XSAttributeUse use) {
158: }
159:
160: public void facet(XSFacet facet) {
161: }
162:
163: public void notation(XSNotation notation) {
164: }
165:
166: public void identityConstraint(XSIdentityConstraint decl) {
167: }
168:
169: public void xpath(XSXPath xp) {
170: }
171:
172: public void wildcard(XSWildcard wc) {
173: }
174:
175: public void empty(XSContentType empty) {
176: }
177:
178: /**
179: * Called for each reference to record the fact.
180: *
181: * So far we only care about references to types.
182: */
183: private void refer(XSComponent source, XSType target) {
184: Set<XSComponent> r = referers.get(target);
185: if (r == null) {
186: r = new HashSet<XSComponent>();
187: referers.put(target, r);
188: }
189: r.add(source);
190: }
191: }
|