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