View Javadoc
1   /*
2    * Copyright (c) 1997, 2011, 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   */
25  
26  package com.sun.tools.internal.xjc.reader.xmlschema;
27  
28  import java.util.HashMap;
29  import java.util.HashSet;
30  import java.util.Map;
31  import java.util.Set;
32  import java.util.Collections;
33  
34  import com.sun.xml.internal.xsom.XSAnnotation;
35  import com.sun.xml.internal.xsom.XSAttGroupDecl;
36  import com.sun.xml.internal.xsom.XSAttributeDecl;
37  import com.sun.xml.internal.xsom.XSAttributeUse;
38  import com.sun.xml.internal.xsom.XSComplexType;
39  import com.sun.xml.internal.xsom.XSComponent;
40  import com.sun.xml.internal.xsom.XSContentType;
41  import com.sun.xml.internal.xsom.XSElementDecl;
42  import com.sun.xml.internal.xsom.XSFacet;
43  import com.sun.xml.internal.xsom.XSIdentityConstraint;
44  import com.sun.xml.internal.xsom.XSModelGroup;
45  import com.sun.xml.internal.xsom.XSModelGroupDecl;
46  import com.sun.xml.internal.xsom.XSNotation;
47  import com.sun.xml.internal.xsom.XSParticle;
48  import com.sun.xml.internal.xsom.XSSchema;
49  import com.sun.xml.internal.xsom.XSSchemaSet;
50  import com.sun.xml.internal.xsom.XSSimpleType;
51  import com.sun.xml.internal.xsom.XSType;
52  import com.sun.xml.internal.xsom.XSWildcard;
53  import com.sun.xml.internal.xsom.XSXPath;
54  import com.sun.xml.internal.xsom.visitor.XSVisitor;
55  
56  /**
57   * Finds which {@link XSComponent}s refer to which {@link XSComplexType}s.
58   *
59   * @author Kohsuke Kawaguchi
60   */
61  final class RefererFinder implements XSVisitor {
62      private final Set<Object> visited = new HashSet<Object>();
63  
64      private final Map<XSComponent,Set<XSComponent>> referers = new HashMap<XSComponent,Set<XSComponent>>();
65  
66      public Set<XSComponent> getReferer(XSComponent src) {
67          Set<XSComponent> r = referers.get(src);
68          if(r==null) return Collections.emptySet();
69          return r;
70      }
71  
72  
73      public void schemaSet(XSSchemaSet xss) {
74          if(!visited.add(xss))       return;
75  
76          for (XSSchema xs : xss.getSchemas()) {
77              schema(xs);
78          }
79      }
80  
81      public void schema(XSSchema xs) {
82          if(!visited.add(xs))       return;
83  
84          for (XSComplexType ct : xs.getComplexTypes().values()) {
85              complexType(ct);
86          }
87  
88          for (XSElementDecl e : xs.getElementDecls().values()) {
89              elementDecl(e);
90          }
91      }
92  
93      public void elementDecl(XSElementDecl e) {
94          if(!visited.add(e))       return;
95  
96          refer(e,e.getType());
97          e.getType().visit(this);
98      }
99  
100     public void complexType(XSComplexType ct) {
101         if(!visited.add(ct))       return;
102 
103         refer(ct,ct.getBaseType());
104         ct.getBaseType().visit(this);
105         ct.getContentType().visit(this);
106     }
107 
108     public void modelGroupDecl(XSModelGroupDecl decl) {
109         if(!visited.add(decl))  return;
110 
111         modelGroup(decl.getModelGroup());
112     }
113 
114     public void modelGroup(XSModelGroup group) {
115         if(!visited.add(group))  return;
116 
117         for (XSParticle p : group.getChildren()) {
118             particle(p);
119         }
120     }
121 
122     public void particle(XSParticle particle) {
123         // since the particle method is side-effect free, no need to check for double-visit.
124         particle.getTerm().visit(this);
125     }
126 
127 
128     // things we don't care
129     public void simpleType(XSSimpleType simpleType) {}
130     public void annotation(XSAnnotation ann) {}
131     public void attGroupDecl(XSAttGroupDecl decl) {}
132     public void attributeDecl(XSAttributeDecl decl) {}
133     public void attributeUse(XSAttributeUse use) {}
134     public void facet(XSFacet facet) {}
135     public void notation(XSNotation notation) {}
136     public void identityConstraint(XSIdentityConstraint decl) {}
137     public void xpath(XSXPath xp) {}
138     public void wildcard(XSWildcard wc) {}
139     public void empty(XSContentType empty) {}
140 
141     /**
142      * Called for each reference to record the fact.
143      *
144      * So far we only care about references to types.
145      */
146     private void refer(XSComponent source, XSType target) {
147         Set<XSComponent> r = referers.get(target);
148         if(r==null) {
149             r = new HashSet<XSComponent>();
150             referers.put(target,r);
151         }
152         r.add(source);
153     }
154 }