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.HashSet;
029: import java.util.Iterator;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import com.sun.tools.internal.xjc.reader.Ring;
034: import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIDeclaration;
035: import com.sun.xml.internal.xsom.XSAnnotation;
036: import com.sun.xml.internal.xsom.XSAttContainer;
037: import com.sun.xml.internal.xsom.XSAttGroupDecl;
038: import com.sun.xml.internal.xsom.XSAttributeDecl;
039: import com.sun.xml.internal.xsom.XSAttributeUse;
040: import com.sun.xml.internal.xsom.XSComplexType;
041: import com.sun.xml.internal.xsom.XSComponent;
042: import com.sun.xml.internal.xsom.XSContentType;
043: import com.sun.xml.internal.xsom.XSElementDecl;
044: import com.sun.xml.internal.xsom.XSFacet;
045: import com.sun.xml.internal.xsom.XSIdentityConstraint;
046: import com.sun.xml.internal.xsom.XSListSimpleType;
047: import com.sun.xml.internal.xsom.XSModelGroup;
048: import com.sun.xml.internal.xsom.XSModelGroupDecl;
049: import com.sun.xml.internal.xsom.XSNotation;
050: import com.sun.xml.internal.xsom.XSParticle;
051: import com.sun.xml.internal.xsom.XSRestrictionSimpleType;
052: import com.sun.xml.internal.xsom.XSSchema;
053: import com.sun.xml.internal.xsom.XSSchemaSet;
054: import com.sun.xml.internal.xsom.XSSimpleType;
055: import com.sun.xml.internal.xsom.XSUnionSimpleType;
056: import com.sun.xml.internal.xsom.XSWildcard;
057: import com.sun.xml.internal.xsom.XSXPath;
058: import com.sun.xml.internal.xsom.visitor.XSSimpleTypeVisitor;
059: import com.sun.xml.internal.xsom.visitor.XSVisitor;
060:
061: /**
062: * Reports all unacknowledged customizations as errors.
063: *
064: * @author
065: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
066: */
067: class UnusedCustomizationChecker extends BindingComponent implements
068: XSVisitor, XSSimpleTypeVisitor {
069: private final BGMBuilder builder = Ring.get(BGMBuilder.class);
070:
071: private final Set<XSComponent> visitedComponents = new HashSet<XSComponent>();
072:
073: /**
074: * Runs the check.
075: */
076: void run() {
077: for (XSSchema s : Ring.get(XSSchemaSet.class).getSchemas()) {
078: schema(s);
079: run(s.getAttGroupDecls());
080: run(s.getAttributeDecls());
081: run(s.getComplexTypes());
082: run(s.getElementDecls());
083: run(s.getModelGroupDecls());
084: run(s.getNotations());
085: run(s.getSimpleTypes());
086: }
087: }
088:
089: private void run(Map<String, ? extends XSComponent> col) {
090: for (XSComponent c : col.values())
091: c.visit(this );
092: }
093:
094: /**
095: * Checks unused customizations on this component
096: * and returns true if this is the first time this
097: * component is checked.
098: */
099: private boolean check(XSComponent c) {
100: if (!visitedComponents.add(c))
101: return false; // already processed
102:
103: for (BIDeclaration decl : builder.getBindInfo(c).getDecls())
104: check(decl, c);
105:
106: return true;
107: }
108:
109: private void check(BIDeclaration decl, XSComponent c) {
110: if (!decl.isAcknowledged()) {
111: getErrorReporter().error(decl.getLocation(),
112: Messages.ERR_UNACKNOWLEDGED_CUSTOMIZATION,
113: decl.getName().getLocalPart());
114: getErrorReporter().error(c.getLocator(),
115: Messages.ERR_UNACKNOWLEDGED_CUSTOMIZATION_LOCATION);
116: // mark it as acknowledged to avoid
117: // duplicated error messages.
118: decl.markAsAcknowledged();
119: }
120: for (BIDeclaration d : decl.getChildren())
121: check(d, c);
122: }
123:
124: public void annotation(XSAnnotation ann) {
125: }
126:
127: public void attGroupDecl(XSAttGroupDecl decl) {
128: if (check(decl))
129: attContainer(decl);
130: }
131:
132: public void attributeDecl(XSAttributeDecl decl) {
133: if (check(decl))
134: decl.getType().visit((XSSimpleTypeVisitor) this );
135: }
136:
137: public void attributeUse(XSAttributeUse use) {
138: if (check(use))
139: use.getDecl().visit(this );
140: }
141:
142: public void complexType(XSComplexType type) {
143: if (check(type)) {
144: // don't need to check the base type -- it must be global, thus
145: // it is covered already
146: type.getContentType().visit(this );
147: attContainer(type);
148: }
149: }
150:
151: private void attContainer(XSAttContainer cont) {
152: for (Iterator itr = cont.iterateAttGroups(); itr.hasNext();)
153: ((XSAttGroupDecl) itr.next()).visit(this );
154:
155: for (Iterator itr = cont.iterateDeclaredAttributeUses(); itr
156: .hasNext();)
157: ((XSAttributeUse) itr.next()).visit(this );
158:
159: XSWildcard wc = cont.getAttributeWildcard();
160: if (wc != null)
161: wc.visit(this );
162: }
163:
164: public void schema(XSSchema schema) {
165: check(schema);
166: }
167:
168: public void facet(XSFacet facet) {
169: check(facet);
170: }
171:
172: public void notation(XSNotation notation) {
173: check(notation);
174: }
175:
176: public void wildcard(XSWildcard wc) {
177: check(wc);
178: }
179:
180: public void modelGroupDecl(XSModelGroupDecl decl) {
181: if (check(decl))
182: decl.getModelGroup().visit(this );
183: }
184:
185: public void modelGroup(XSModelGroup group) {
186: if (check(group)) {
187: for (int i = 0; i < group.getSize(); i++)
188: group.getChild(i).visit(this );
189: }
190: }
191:
192: public void elementDecl(XSElementDecl decl) {
193: if (check(decl)) {
194: decl.getType().visit(this );
195: for (XSIdentityConstraint id : decl
196: .getIdentityConstraints())
197: id.visit(this );
198: }
199: }
200:
201: public void simpleType(XSSimpleType simpleType) {
202: if (check(simpleType))
203: simpleType.visit((XSSimpleTypeVisitor) this );
204: }
205:
206: public void particle(XSParticle particle) {
207: if (check(particle))
208: particle.getTerm().visit(this );
209: }
210:
211: public void empty(XSContentType empty) {
212: check(empty);
213: }
214:
215: public void listSimpleType(XSListSimpleType type) {
216: if (check(type))
217: type.getItemType().visit((XSSimpleTypeVisitor) this );
218: }
219:
220: public void restrictionSimpleType(XSRestrictionSimpleType type) {
221: if (check(type))
222: type.getBaseType().visit(this );
223: }
224:
225: public void unionSimpleType(XSUnionSimpleType type) {
226: if (check(type)) {
227: for (int i = 0; i < type.getMemberSize(); i++)
228: type.getMember(i).visit((XSSimpleTypeVisitor) this );
229: }
230: }
231:
232: public void identityConstraint(XSIdentityConstraint id) {
233: if (check(id)) {
234: id.getSelector().visit(this );
235: for (XSXPath xp : id.getFields())
236: xp.visit(this );
237: }
238: }
239:
240: public void xpath(XSXPath xp) {
241: check(xp);
242: }
243:
244: }
|