src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/UnusedCustomizationChecker.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.xjc.reader.xmlschema;
aoqi@0 27
aoqi@0 28 import java.util.HashSet;
aoqi@0 29 import java.util.Iterator;
aoqi@0 30 import java.util.Map;
aoqi@0 31 import java.util.Set;
aoqi@0 32
aoqi@0 33 import com.sun.tools.internal.xjc.reader.Const;
aoqi@0 34 import com.sun.tools.internal.xjc.reader.Ring;
aoqi@0 35 import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIDeclaration;
aoqi@0 36 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
aoqi@0 37 import com.sun.xml.internal.xsom.XSAnnotation;
aoqi@0 38 import com.sun.xml.internal.xsom.XSAttContainer;
aoqi@0 39 import com.sun.xml.internal.xsom.XSAttGroupDecl;
aoqi@0 40 import com.sun.xml.internal.xsom.XSAttributeDecl;
aoqi@0 41 import com.sun.xml.internal.xsom.XSAttributeUse;
aoqi@0 42 import com.sun.xml.internal.xsom.XSComplexType;
aoqi@0 43 import com.sun.xml.internal.xsom.XSComponent;
aoqi@0 44 import com.sun.xml.internal.xsom.XSContentType;
aoqi@0 45 import com.sun.xml.internal.xsom.XSElementDecl;
aoqi@0 46 import com.sun.xml.internal.xsom.XSFacet;
aoqi@0 47 import com.sun.xml.internal.xsom.XSIdentityConstraint;
aoqi@0 48 import com.sun.xml.internal.xsom.XSListSimpleType;
aoqi@0 49 import com.sun.xml.internal.xsom.XSModelGroup;
aoqi@0 50 import com.sun.xml.internal.xsom.XSModelGroupDecl;
aoqi@0 51 import com.sun.xml.internal.xsom.XSNotation;
aoqi@0 52 import com.sun.xml.internal.xsom.XSParticle;
aoqi@0 53 import com.sun.xml.internal.xsom.XSRestrictionSimpleType;
aoqi@0 54 import com.sun.xml.internal.xsom.XSSchema;
aoqi@0 55 import com.sun.xml.internal.xsom.XSSchemaSet;
aoqi@0 56 import com.sun.xml.internal.xsom.XSSimpleType;
aoqi@0 57 import com.sun.xml.internal.xsom.XSUnionSimpleType;
aoqi@0 58 import com.sun.xml.internal.xsom.XSWildcard;
aoqi@0 59 import com.sun.xml.internal.xsom.XSXPath;
aoqi@0 60 import com.sun.xml.internal.xsom.visitor.XSSimpleTypeVisitor;
aoqi@0 61 import com.sun.xml.internal.xsom.visitor.XSVisitor;
aoqi@0 62
aoqi@0 63 /**
aoqi@0 64 * Reports all unacknowledged customizations as errors.
aoqi@0 65 *
aoqi@0 66 * <p>
aoqi@0 67 * Since we scan the whole content tree, we use this to check for unused
aoqi@0 68 * <tt>xmime:expectedContentTypes</tt> attributes. TODO: if we find this kind of error checks more
aoqi@0 69 * common, use the visitors so that we don't have to mix everything in one class.
aoqi@0 70 *
aoqi@0 71 * @author
aoqi@0 72 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
aoqi@0 73 */
aoqi@0 74 class UnusedCustomizationChecker extends BindingComponent implements XSVisitor, XSSimpleTypeVisitor {
aoqi@0 75 private final BGMBuilder builder = Ring.get(BGMBuilder.class);
aoqi@0 76 private final SimpleTypeBuilder stb = Ring.get(SimpleTypeBuilder.class);
aoqi@0 77
aoqi@0 78 private final Set<XSComponent> visitedComponents = new HashSet<XSComponent>();
aoqi@0 79
aoqi@0 80 /**
aoqi@0 81 * Runs the check.
aoqi@0 82 */
aoqi@0 83 void run() {
aoqi@0 84 for( XSSchema s : Ring.get(XSSchemaSet.class).getSchemas() ) {
aoqi@0 85 schema(s);
aoqi@0 86 run( s.getAttGroupDecls() );
aoqi@0 87 run( s.getAttributeDecls() );
aoqi@0 88 run( s.getComplexTypes() );
aoqi@0 89 run( s.getElementDecls() );
aoqi@0 90 run( s.getModelGroupDecls() );
aoqi@0 91 run( s.getNotations() );
aoqi@0 92 run( s.getSimpleTypes() );
aoqi@0 93 }
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 private void run( Map<String,? extends XSComponent> col ) {
aoqi@0 97 for( XSComponent c : col.values() )
aoqi@0 98 c.visit(this);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101
aoqi@0 102 /**
aoqi@0 103 * Checks unused customizations on this component
aoqi@0 104 * and returns true if this is the first time this
aoqi@0 105 * component is checked.
aoqi@0 106 */
aoqi@0 107 private boolean check( XSComponent c ) {
aoqi@0 108 if( !visitedComponents.add(c) )
aoqi@0 109 return false; // already processed
aoqi@0 110
aoqi@0 111 for( BIDeclaration decl : builder.getBindInfo(c).getDecls() )
aoqi@0 112 check(decl, c);
aoqi@0 113
aoqi@0 114 checkExpectedContentTypes(c);
aoqi@0 115
aoqi@0 116 return true;
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 private void checkExpectedContentTypes(XSComponent c) {
aoqi@0 120 if(c.getForeignAttribute(WellKnownNamespace.XML_MIME_URI, Const.EXPECTED_CONTENT_TYPES)==null)
aoqi@0 121 return; // no such attribute
aoqi@0 122 if(c instanceof XSParticle)
aoqi@0 123 return; // particles get the same foreign attributes as local element decls,
aoqi@0 124 // so we need to skip them
aoqi@0 125
aoqi@0 126 if(!stb.isAcknowledgedXmimeContentTypes(c)) {
aoqi@0 127 // this is not used
aoqi@0 128 getErrorReporter().warning(c.getLocator(),Messages.WARN_UNUSED_EXPECTED_CONTENT_TYPES);
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 private void check(BIDeclaration decl, XSComponent c) {
aoqi@0 133 if( !decl.isAcknowledged() ) {
aoqi@0 134 getErrorReporter().error(
aoqi@0 135 decl.getLocation(),
aoqi@0 136 Messages.ERR_UNACKNOWLEDGED_CUSTOMIZATION,
aoqi@0 137 decl.getName().getLocalPart()
aoqi@0 138 );
aoqi@0 139 getErrorReporter().error(
aoqi@0 140 c.getLocator(),
aoqi@0 141 Messages.ERR_UNACKNOWLEDGED_CUSTOMIZATION_LOCATION);
aoqi@0 142 // mark it as acknowledged to avoid
aoqi@0 143 // duplicated error messages.
aoqi@0 144 decl.markAsAcknowledged();
aoqi@0 145 }
aoqi@0 146 for (BIDeclaration d : decl.getChildren())
aoqi@0 147 check(d,c);
aoqi@0 148 }
aoqi@0 149
aoqi@0 150
aoqi@0 151 public void annotation(XSAnnotation ann) {}
aoqi@0 152
aoqi@0 153 public void attGroupDecl(XSAttGroupDecl decl) {
aoqi@0 154 if(check(decl))
aoqi@0 155 attContainer(decl);
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 public void attributeDecl(XSAttributeDecl decl) {
aoqi@0 159 if(check(decl))
aoqi@0 160 decl.getType().visit((XSSimpleTypeVisitor)this);
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 public void attributeUse(XSAttributeUse use) {
aoqi@0 164 if(check(use))
aoqi@0 165 use.getDecl().visit(this);
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 public void complexType(XSComplexType type) {
aoqi@0 169 if(check(type)) {
aoqi@0 170 // don't need to check the base type -- it must be global, thus
aoqi@0 171 // it is covered already
aoqi@0 172 type.getContentType().visit(this);
aoqi@0 173 attContainer(type);
aoqi@0 174 }
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 private void attContainer( XSAttContainer cont ) {
aoqi@0 178 for( Iterator itr = cont.iterateAttGroups(); itr.hasNext(); )
aoqi@0 179 ((XSAttGroupDecl)itr.next()).visit(this);
aoqi@0 180
aoqi@0 181 for( Iterator itr = cont.iterateDeclaredAttributeUses(); itr.hasNext(); )
aoqi@0 182 ((XSAttributeUse)itr.next()).visit(this);
aoqi@0 183
aoqi@0 184 XSWildcard wc = cont.getAttributeWildcard();
aoqi@0 185 if(wc!=null) wc.visit(this);
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 public void schema(XSSchema schema) {
aoqi@0 189 check(schema);
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 public void facet(XSFacet facet) {
aoqi@0 193 check(facet);
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 public void notation(XSNotation notation) {
aoqi@0 197 check(notation);
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 public void wildcard(XSWildcard wc) {
aoqi@0 201 check(wc);
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 public void modelGroupDecl(XSModelGroupDecl decl) {
aoqi@0 205 if(check(decl))
aoqi@0 206 decl.getModelGroup().visit(this);
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 public void modelGroup(XSModelGroup group) {
aoqi@0 210 if(check(group)) {
aoqi@0 211 for( int i=0; i<group.getSize(); i++ )
aoqi@0 212 group.getChild(i).visit(this);
aoqi@0 213 }
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 public void elementDecl(XSElementDecl decl) {
aoqi@0 217 if(check(decl)) {
aoqi@0 218 decl.getType().visit(this);
aoqi@0 219 for( XSIdentityConstraint id : decl.getIdentityConstraints() )
aoqi@0 220 id.visit(this);
aoqi@0 221 }
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 public void simpleType(XSSimpleType simpleType) {
aoqi@0 225 if(check(simpleType))
aoqi@0 226 simpleType.visit( (XSSimpleTypeVisitor)this );
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 public void particle(XSParticle particle) {
aoqi@0 230 if(check(particle))
aoqi@0 231 particle.getTerm().visit(this);
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 public void empty(XSContentType empty) {
aoqi@0 235 check(empty);
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 public void listSimpleType(XSListSimpleType type) {
aoqi@0 239 if(check(type))
aoqi@0 240 type.getItemType().visit((XSSimpleTypeVisitor)this);
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 public void restrictionSimpleType(XSRestrictionSimpleType type) {
aoqi@0 244 if(check(type))
aoqi@0 245 type.getBaseType().visit(this);
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 public void unionSimpleType(XSUnionSimpleType type) {
aoqi@0 249 if(check(type)) {
aoqi@0 250 for( int i=0; i<type.getMemberSize(); i++ )
aoqi@0 251 type.getMember(i).visit((XSSimpleTypeVisitor)this);
aoqi@0 252 }
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 public void identityConstraint(XSIdentityConstraint id) {
aoqi@0 256 if(check(id)) {
aoqi@0 257 id.getSelector().visit(this);
aoqi@0 258 for( XSXPath xp : id.getFields() )
aoqi@0 259 xp.visit(this);
aoqi@0 260 }
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 public void xpath(XSXPath xp) {
aoqi@0 264 check(xp);
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 }

mercurial