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
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.schema.model.visitor;
043:
044: import java.util.Collection;
045:
046: import org.netbeans.modules.xml.schema.model.*;
047: import org.netbeans.modules.xml.xam.NamedReferenceable;
048: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
049:
050: /**
051: *
052: * @author Samaresh
053: */
054: public class FindUsageVisitor extends DeepSchemaVisitor {
055:
056: /**
057: * The global component being modified.
058: */
059: private NamedReferenceable<SchemaComponent> globalSchemaComponent = null;
060:
061: /**
062: * Preview
063: */
064: private PreviewImpl preview = null;
065:
066: /**
067: * Find usages for the specified type in the list of the schemas.
068: */
069: public Preview findUsages(Collection<Schema> roots,
070: NamedReferenceable<SchemaComponent> component) {
071: preview = new PreviewImpl();
072: globalSchemaComponent = component;
073: return findUsages(roots);
074: }
075:
076: /**
077: * All the usage methods eventually call this to get the preview.
078: */
079: private Preview findUsages(Collection<Schema> roots) {
080: for (Schema schema : roots) {
081: schema.accept(this );
082: }
083:
084: return preview;
085: }
086:
087: public void visit(Union u) {
088: if (u.getMemberTypes() != null) {
089: for (NamedComponentReference<GlobalSimpleType> t : u
090: .getMemberTypes()) {
091: checkReference(t, u);
092: }
093: }
094: super .visit(u);
095: }
096:
097: /**
098: * For CommonSimpleRestriction, GlobalReference will be:
099: * getBase(), when a GlobalSimpleType is modified.
100: */
101: public void visit(SimpleTypeRestriction str) {
102: checkReference(str.getBase(), str);
103: super .visit(str);
104: }
105:
106: /**
107: * For LocalElement, GlobalReference will be:
108: * getType(), when a GlobalType is modified,
109: * getRef(), when a GlobalElement is modified.
110: */
111: public void visit(LocalElement element) {
112: checkReference((NamedComponentReference<GlobalType>) element
113: .getType(), element);
114: super .visit(element);
115: }
116:
117: /**
118: * For LocalElement, GlobalReference will be:
119: * getType(), when a GlobalType is modified,
120: * getRef(), when a GlobalElement is modified.
121: */
122: public void visit(ElementReference element) {
123: checkReference(element.getRef(), element);
124: super .visit(element);
125: }
126:
127: /**
128: * For GlobalElement, GlobalReference will be:
129: * getType(), when a GlobalType is modified,
130: * getSubstitutionGroup(), when a GlobalElement is modified.
131: */
132: public void visit(GlobalElement element) {
133: checkReference(element.getType(), element);
134: checkReference(element.getSubstitutionGroup(), element);
135: super .visit(element);
136: }
137:
138: /**
139: * For LocalAttribute, GlobalReference will be:
140: * getType(), when a GlobalSimpleType is modified,
141: * getRef(), when a GlobalAttribute is modified.
142: */
143: public void visit(LocalAttribute attribute) {
144: checkReference(attribute.getType(), attribute);
145: super .visit(attribute);
146: }
147:
148: /**
149: * For LocalAttribute, GlobalReference will be:
150: * getType(), when a GlobalSimpleType is modified,
151: * getRef(), when a GlobalAttribute is modified.
152: */
153: public void visit(AttributeReference attribute) {
154: checkReference(attribute.getRef(), attribute);
155: super .visit(attribute);
156: }
157:
158: /**
159: * For AttributeGroupReference, GlobalReference will be:
160: * getGroup(), when a GlobalAttributeGroup is modified.
161: */
162: public void visit(AttributeGroupReference agr) {
163: checkReference(agr.getGroup(), agr);
164: super .visit(agr);
165: }
166:
167: /**
168: * For ComplexContentRestriction, GlobalReference will be:
169: * getBase(), when a GlobalComplexType is modified.
170: */
171: public void visit(ComplexContentRestriction ccr) {
172: checkReference(ccr.getBase(), ccr);
173: super .visit(ccr);
174: }
175:
176: /**
177: * For SimpleExtension, GlobalReference will be:
178: * getBase(), when a GlobalType is modified.
179: */
180: public void visit(SimpleExtension extension) {
181: checkReference(extension.getBase(), extension);
182: super .visit(extension);
183: }
184:
185: /**
186: * For ComplexExtension, GlobalReference will be:
187: * getBase(), when a GlobalType is modified.
188: */
189: public void visit(ComplexExtension extension) {
190: checkReference(extension.getBase(), extension);
191: super .visit(extension);
192: }
193:
194: /**
195: * For GroupReference, GlobalReference will be:
196: * getRef(), when a GlobalGroup is modified.
197: */
198: public void visit(GroupReference gr) {
199: checkReference(gr.getRef(), gr);
200: super .visit(gr);
201: }
202:
203: /**
204: * For List, GlobalReference will be:
205: * getType(), when a GlobalSimpleType is modified.
206: */
207: public void visit(List list) {
208: checkReference(list.getType(), list);
209: super .visit(list);
210: }
211:
212: private <T extends NamedReferenceable<SchemaComponent>> void checkReference(
213: NamedComponentReference<T> ref, SchemaComponent component) {
214: if (ref == null
215: || !ref.getType().isAssignableFrom(
216: globalSchemaComponent.getClass()))
217: return;
218: if (ref.references(ref.getType().cast(globalSchemaComponent))) {
219: preview.addToUsage(component);
220: }
221: }
222: }
|