325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.xjc.reader.xmlschema;
325N/A
325N/Aimport java.util.HashSet;
325N/Aimport java.util.Iterator;
325N/Aimport java.util.Map;
325N/Aimport java.util.Set;
325N/A
325N/Aimport com.sun.tools.internal.xjc.reader.Const;
325N/Aimport com.sun.tools.internal.xjc.reader.Ring;
325N/Aimport com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIDeclaration;
325N/Aimport com.sun.xml.internal.bind.v2.WellKnownNamespace;
325N/Aimport com.sun.xml.internal.xsom.XSAnnotation;
325N/Aimport com.sun.xml.internal.xsom.XSAttContainer;
325N/Aimport com.sun.xml.internal.xsom.XSAttGroupDecl;
325N/Aimport com.sun.xml.internal.xsom.XSAttributeDecl;
325N/Aimport com.sun.xml.internal.xsom.XSAttributeUse;
325N/Aimport com.sun.xml.internal.xsom.XSComplexType;
325N/Aimport com.sun.xml.internal.xsom.XSComponent;
325N/Aimport com.sun.xml.internal.xsom.XSContentType;
325N/Aimport com.sun.xml.internal.xsom.XSElementDecl;
325N/Aimport com.sun.xml.internal.xsom.XSFacet;
325N/Aimport com.sun.xml.internal.xsom.XSIdentityConstraint;
325N/Aimport com.sun.xml.internal.xsom.XSListSimpleType;
325N/Aimport com.sun.xml.internal.xsom.XSModelGroup;
325N/Aimport com.sun.xml.internal.xsom.XSModelGroupDecl;
325N/Aimport com.sun.xml.internal.xsom.XSNotation;
325N/Aimport com.sun.xml.internal.xsom.XSParticle;
325N/Aimport com.sun.xml.internal.xsom.XSRestrictionSimpleType;
325N/Aimport com.sun.xml.internal.xsom.XSSchema;
325N/Aimport com.sun.xml.internal.xsom.XSSchemaSet;
325N/Aimport com.sun.xml.internal.xsom.XSSimpleType;
325N/Aimport com.sun.xml.internal.xsom.XSUnionSimpleType;
325N/Aimport com.sun.xml.internal.xsom.XSWildcard;
325N/Aimport com.sun.xml.internal.xsom.XSXPath;
325N/Aimport com.sun.xml.internal.xsom.visitor.XSSimpleTypeVisitor;
325N/Aimport com.sun.xml.internal.xsom.visitor.XSVisitor;
325N/A
325N/A/**
325N/A * Reports all unacknowledged customizations as errors.
325N/A *
325N/A * <p>
325N/A * Since we scan the whole content tree, we use this to check for unused
325N/A * <tt>xmime:expectedContentTypes</tt> attributes. TODO: if we find this kind of error checks more
325N/A * common, use the visitors so that we don't have to mix everything in one class.
325N/A *
325N/A * @author
325N/A * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
325N/A */
325N/Aclass UnusedCustomizationChecker extends BindingComponent implements XSVisitor, XSSimpleTypeVisitor {
325N/A private final BGMBuilder builder = Ring.get(BGMBuilder.class);
325N/A private final SimpleTypeBuilder stb = Ring.get(SimpleTypeBuilder.class);
325N/A
325N/A private final Set<XSComponent> visitedComponents = new HashSet<XSComponent>();
325N/A
325N/A /**
325N/A * Runs the check.
325N/A */
325N/A void run() {
325N/A for( XSSchema s : Ring.get(XSSchemaSet.class).getSchemas() ) {
325N/A schema(s);
325N/A run( s.getAttGroupDecls() );
325N/A run( s.getAttributeDecls() );
325N/A run( s.getComplexTypes() );
325N/A run( s.getElementDecls() );
325N/A run( s.getModelGroupDecls() );
325N/A run( s.getNotations() );
325N/A run( s.getSimpleTypes() );
325N/A }
325N/A }
325N/A
325N/A private void run( Map<String,? extends XSComponent> col ) {
325N/A for( XSComponent c : col.values() )
325N/A c.visit(this);
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Checks unused customizations on this component
325N/A * and returns true if this is the first time this
325N/A * component is checked.
325N/A */
325N/A private boolean check( XSComponent c ) {
325N/A if( !visitedComponents.add(c) )
325N/A return false; // already processed
325N/A
325N/A for( BIDeclaration decl : builder.getBindInfo(c).getDecls() )
325N/A check(decl, c);
325N/A
325N/A checkExpectedContentTypes(c);
325N/A
325N/A return true;
325N/A }
325N/A
325N/A private void checkExpectedContentTypes(XSComponent c) {
325N/A if(c.getForeignAttribute(WellKnownNamespace.XML_MIME_URI, Const.EXPECTED_CONTENT_TYPES)==null)
325N/A return; // no such attribute
325N/A if(c instanceof XSParticle)
325N/A return; // particles get the same foreign attributes as local element decls,
325N/A // so we need to skip them
325N/A
325N/A if(!stb.isAcknowledgedXmimeContentTypes(c)) {
325N/A // this is not used
325N/A getErrorReporter().warning(c.getLocator(),Messages.WARN_UNUSED_EXPECTED_CONTENT_TYPES);
325N/A }
325N/A }
325N/A
325N/A private void check(BIDeclaration decl, XSComponent c) {
325N/A if( !decl.isAcknowledged() ) {
325N/A getErrorReporter().error(
325N/A decl.getLocation(),
325N/A Messages.ERR_UNACKNOWLEDGED_CUSTOMIZATION,
325N/A decl.getName().getLocalPart()
325N/A );
325N/A getErrorReporter().error(
325N/A c.getLocator(),
325N/A Messages.ERR_UNACKNOWLEDGED_CUSTOMIZATION_LOCATION);
325N/A // mark it as acknowledged to avoid
325N/A // duplicated error messages.
325N/A decl.markAsAcknowledged();
325N/A }
325N/A for (BIDeclaration d : decl.getChildren())
325N/A check(d,c);
325N/A }
325N/A
325N/A
325N/A public void annotation(XSAnnotation ann) {}
325N/A
325N/A public void attGroupDecl(XSAttGroupDecl decl) {
325N/A if(check(decl))
325N/A attContainer(decl);
325N/A }
325N/A
325N/A public void attributeDecl(XSAttributeDecl decl) {
325N/A if(check(decl))
325N/A decl.getType().visit((XSSimpleTypeVisitor)this);
325N/A }
325N/A
325N/A public void attributeUse(XSAttributeUse use) {
325N/A if(check(use))
325N/A use.getDecl().visit(this);
325N/A }
325N/A
325N/A public void complexType(XSComplexType type) {
325N/A if(check(type)) {
325N/A // don't need to check the base type -- it must be global, thus
325N/A // it is covered already
325N/A type.getContentType().visit(this);
325N/A attContainer(type);
325N/A }
325N/A }
325N/A
325N/A private void attContainer( XSAttContainer cont ) {
325N/A for( Iterator itr = cont.iterateAttGroups(); itr.hasNext(); )
325N/A ((XSAttGroupDecl)itr.next()).visit(this);
325N/A
325N/A for( Iterator itr = cont.iterateDeclaredAttributeUses(); itr.hasNext(); )
325N/A ((XSAttributeUse)itr.next()).visit(this);
325N/A
325N/A XSWildcard wc = cont.getAttributeWildcard();
325N/A if(wc!=null) wc.visit(this);
325N/A }
325N/A
325N/A public void schema(XSSchema schema) {
325N/A check(schema);
325N/A }
325N/A
325N/A public void facet(XSFacet facet) {
325N/A check(facet);
325N/A }
325N/A
325N/A public void notation(XSNotation notation) {
325N/A check(notation);
325N/A }
325N/A
325N/A public void wildcard(XSWildcard wc) {
325N/A check(wc);
325N/A }
325N/A
325N/A public void modelGroupDecl(XSModelGroupDecl decl) {
325N/A if(check(decl))
325N/A decl.getModelGroup().visit(this);
325N/A }
325N/A
325N/A public void modelGroup(XSModelGroup group) {
325N/A if(check(group)) {
325N/A for( int i=0; i<group.getSize(); i++ )
325N/A group.getChild(i).visit(this);
325N/A }
325N/A }
325N/A
325N/A public void elementDecl(XSElementDecl decl) {
325N/A if(check(decl)) {
325N/A decl.getType().visit(this);
325N/A for( XSIdentityConstraint id : decl.getIdentityConstraints() )
325N/A id.visit(this);
325N/A }
325N/A }
325N/A
325N/A public void simpleType(XSSimpleType simpleType) {
325N/A if(check(simpleType))
325N/A simpleType.visit( (XSSimpleTypeVisitor)this );
325N/A }
325N/A
325N/A public void particle(XSParticle particle) {
325N/A if(check(particle))
325N/A particle.getTerm().visit(this);
325N/A }
325N/A
325N/A public void empty(XSContentType empty) {
325N/A check(empty);
325N/A }
325N/A
325N/A public void listSimpleType(XSListSimpleType type) {
325N/A if(check(type))
325N/A type.getItemType().visit((XSSimpleTypeVisitor)this);
325N/A }
325N/A
325N/A public void restrictionSimpleType(XSRestrictionSimpleType type) {
325N/A if(check(type))
325N/A type.getBaseType().visit(this);
325N/A }
325N/A
325N/A public void unionSimpleType(XSUnionSimpleType type) {
325N/A if(check(type)) {
325N/A for( int i=0; i<type.getMemberSize(); i++ )
325N/A type.getMember(i).visit((XSSimpleTypeVisitor)this);
325N/A }
325N/A }
325N/A
325N/A public void identityConstraint(XSIdentityConstraint id) {
325N/A if(check(id)) {
325N/A id.getSelector().visit(this);
325N/A for( XSXPath xp : id.getFields() )
325N/A xp.visit(this);
325N/A }
325N/A }
325N/A
325N/A public void xpath(XSXPath xp) {
325N/A check(xp);
325N/A }
325N/A
325N/A}