KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > visitor > FindUsageVisitor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.model.visitor;
21
22 import java.util.Collection JavaDoc;
23
24 import org.netbeans.modules.xml.schema.model.*;
25 import org.netbeans.modules.xml.xam.NamedReferenceable;
26 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
27
28 /**
29  *
30  * @author Samaresh
31  */

32 public class FindUsageVisitor extends DeepSchemaVisitor {
33
34     /**
35      * The global component being modified.
36      */

37     private NamedReferenceable<SchemaComponent> globalSchemaComponent = null;
38
39     /**
40      * Preview
41      */

42     private PreviewImpl preview = null;
43     
44     /**
45      * Find usages for the specified type in the list of the schemas.
46      */

47     public Preview findUsages(Collection JavaDoc<Schema> roots, NamedReferenceable<SchemaComponent> component ) {
48     preview = new PreviewImpl();
49         globalSchemaComponent = component;
50         return findUsages(roots);
51     }
52
53     /**
54      * All the usage methods eventually call this to get the preview.
55      */

56     private Preview findUsages(Collection JavaDoc<Schema> roots) {
57         for(Schema schema : roots) {
58             schema.accept(this);
59         }
60         
61         return preview;
62     }
63         
64     public void visit(Union u) {
65         if (u.getMemberTypes() != null) {
66             for (NamedComponentReference<GlobalSimpleType> t : u.getMemberTypes()) {
67                 checkReference(t, u);
68             }
69         }
70         super.visit(u);
71     }
72
73     /**
74      * For CommonSimpleRestriction, GlobalReference will be:
75      * getBase(), when a GlobalSimpleType is modified.
76      */

77     public void visit(SimpleTypeRestriction str) {
78         checkReference(str.getBase(), str);
79         super.visit(str);
80     }
81         
82     /**
83      * For LocalElement, GlobalReference will be:
84      * getType(), when a GlobalType is modified,
85      * getRef(), when a GlobalElement is modified.
86      */

87     public void visit(LocalElement element) {
88         checkReference((NamedComponentReference<GlobalType>)element.getType(), element);
89         super.visit(element);
90     }
91     
92     /**
93      * For LocalElement, GlobalReference will be:
94      * getType(), when a GlobalType is modified,
95      * getRef(), when a GlobalElement is modified.
96      */

97     public void visit(ElementReference element) {
98         checkReference(element.getRef(), element);
99         super.visit(element);
100     }
101     
102     /**
103      * For GlobalElement, GlobalReference will be:
104      * getType(), when a GlobalType is modified,
105      * getSubstitutionGroup(), when a GlobalElement is modified.
106      */

107     public void visit(GlobalElement element) {
108         checkReference(element.getType(), element);
109         checkReference(element.getSubstitutionGroup(), element);
110         super.visit(element);
111     }
112         
113     /**
114      * For LocalAttribute, GlobalReference will be:
115      * getType(), when a GlobalSimpleType is modified,
116      * getRef(), when a GlobalAttribute is modified.
117      */

118     public void visit(LocalAttribute attribute) {
119         checkReference(attribute.getType(), attribute);
120         super.visit(attribute);
121     }
122     
123      /**
124      * For LocalAttribute, GlobalReference will be:
125      * getType(), when a GlobalSimpleType is modified,
126      * getRef(), when a GlobalAttribute is modified.
127      */

128     public void visit(AttributeReference attribute) {
129         checkReference(attribute.getRef(), attribute);
130         super.visit(attribute);
131     }
132         
133     /**
134      * For AttributeGroupReference, GlobalReference will be:
135      * getGroup(), when a GlobalAttributeGroup is modified.
136      */

137     public void visit(AttributeGroupReference agr) {
138         checkReference(agr.getGroup(), agr);
139         super.visit(agr);
140     }
141         
142     /**
143      * For ComplexContentRestriction, GlobalReference will be:
144      * getBase(), when a GlobalComplexType is modified.
145      */

146     public void visit(ComplexContentRestriction ccr) {
147         checkReference(ccr.getBase(), ccr);
148         super.visit(ccr);
149     }
150     
151     /**
152      * For SimpleExtension, GlobalReference will be:
153      * getBase(), when a GlobalType is modified.
154      */

155     public void visit(SimpleExtension extension) {
156         checkReference(extension.getBase(), extension);
157         super.visit(extension);
158     }
159     
160     /**
161      * For ComplexExtension, GlobalReference will be:
162      * getBase(), when a GlobalType is modified.
163      */

164     public void visit(ComplexExtension extension) {
165         checkReference(extension.getBase(), extension);
166         super.visit(extension);
167     }
168     
169     /**
170      * For GroupReference, GlobalReference will be:
171      * getRef(), when a GlobalGroup is modified.
172      */

173     public void visit(GroupReference gr) {
174         checkReference(gr.getRef(), gr);
175         super.visit(gr);
176     }
177     
178     /**
179      * For List, GlobalReference will be:
180      * getType(), when a GlobalSimpleType is modified.
181      */

182     public void visit(List list) {
183         checkReference(list.getType(), list);
184         super.visit(list);
185     }
186     
187     private <T extends NamedReferenceable<SchemaComponent>> void checkReference(
188             NamedComponentReference<T> ref, SchemaComponent component) {
189         if (ref == null || ! ref.getType().isAssignableFrom(globalSchemaComponent.getClass())) return;
190         if (ref.references(ref.getType().cast(globalSchemaComponent))) {
191             preview.addToUsage(component);
192         }
193     }
194 }
195
Popular Tags