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.HashMap;
325N/Aimport java.util.HashSet;
325N/Aimport java.util.Map;
325N/Aimport java.util.Set;
325N/Aimport java.util.Collections;
325N/A
325N/Aimport com.sun.xml.internal.xsom.XSAnnotation;
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.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.XSSchema;
325N/Aimport com.sun.xml.internal.xsom.XSSchemaSet;
325N/Aimport com.sun.xml.internal.xsom.XSSimpleType;
325N/Aimport com.sun.xml.internal.xsom.XSType;
325N/Aimport com.sun.xml.internal.xsom.XSWildcard;
325N/Aimport com.sun.xml.internal.xsom.XSXPath;
325N/Aimport com.sun.xml.internal.xsom.visitor.XSVisitor;
325N/A
325N/A/**
325N/A * Finds which {@link XSComponent}s refer to which {@link XSComplexType}s.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Afinal class RefererFinder implements XSVisitor {
325N/A private final Set<Object> visited = new HashSet<Object>();
325N/A
325N/A private final Map<XSComponent,Set<XSComponent>> referers = new HashMap<XSComponent,Set<XSComponent>>();
325N/A
325N/A public Set<XSComponent> getReferer(XSComponent src) {
325N/A Set<XSComponent> r = referers.get(src);
325N/A if(r==null) return Collections.emptySet();
325N/A return r;
325N/A }
325N/A
325N/A
325N/A public void schemaSet(XSSchemaSet xss) {
325N/A if(!visited.add(xss)) return;
325N/A
325N/A for (XSSchema xs : xss.getSchemas()) {
325N/A schema(xs);
325N/A }
325N/A }
325N/A
325N/A public void schema(XSSchema xs) {
325N/A if(!visited.add(xs)) return;
325N/A
325N/A for (XSComplexType ct : xs.getComplexTypes().values()) {
325N/A complexType(ct);
325N/A }
325N/A
325N/A for (XSElementDecl e : xs.getElementDecls().values()) {
325N/A elementDecl(e);
325N/A }
325N/A }
325N/A
325N/A public void elementDecl(XSElementDecl e) {
325N/A if(!visited.add(e)) return;
325N/A
325N/A refer(e,e.getType());
325N/A e.getType().visit(this);
325N/A }
325N/A
325N/A public void complexType(XSComplexType ct) {
325N/A if(!visited.add(ct)) return;
325N/A
325N/A refer(ct,ct.getBaseType());
325N/A ct.getBaseType().visit(this);
325N/A ct.getContentType().visit(this);
325N/A }
325N/A
325N/A public void modelGroupDecl(XSModelGroupDecl decl) {
325N/A if(!visited.add(decl)) return;
325N/A
325N/A modelGroup(decl.getModelGroup());
325N/A }
325N/A
325N/A public void modelGroup(XSModelGroup group) {
325N/A if(!visited.add(group)) return;
325N/A
325N/A for (XSParticle p : group.getChildren()) {
325N/A particle(p);
325N/A }
325N/A }
325N/A
325N/A public void particle(XSParticle particle) {
325N/A // since the particle method is side-effect free, no need to check for double-visit.
325N/A particle.getTerm().visit(this);
325N/A }
325N/A
325N/A
325N/A // things we don't care
325N/A public void simpleType(XSSimpleType simpleType) {}
325N/A public void annotation(XSAnnotation ann) {}
325N/A public void attGroupDecl(XSAttGroupDecl decl) {}
325N/A public void attributeDecl(XSAttributeDecl decl) {}
325N/A public void attributeUse(XSAttributeUse use) {}
325N/A public void facet(XSFacet facet) {}
325N/A public void notation(XSNotation notation) {}
325N/A public void identityConstraint(XSIdentityConstraint decl) {}
325N/A public void xpath(XSXPath xp) {}
325N/A public void wildcard(XSWildcard wc) {}
325N/A public void empty(XSContentType empty) {}
325N/A
325N/A /**
325N/A * Called for each reference to record the fact.
325N/A *
325N/A * So far we only care about references to types.
325N/A */
325N/A private void refer(XSComponent source, XSType target) {
325N/A Set<XSComponent> r = referers.get(target);
325N/A if(r==null) {
325N/A r = new HashSet<XSComponent>();
325N/A referers.put(target,r);
325N/A }
325N/A r.add(source);
325N/A }
325N/A}