KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > impl > RefactorVisitor


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.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import org.netbeans.modules.xml.schema.model.*;
25 import org.netbeans.modules.xml.schema.model.visitor.*;
26 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
27
28 /**
29  *
30  * @author Samaresh
31  */

32 class RefactorVisitor extends DefaultSchemaVisitor {
33
34     /**
35      * The global component being modified.
36      */

37     private ReferenceableSchemaComponent global_component = null;
38     private String JavaDoc oldName = null;
39
40     /**
41      * Creates a new instance of RefactorVisitor
42      */

43     public RefactorVisitor() {
44     }
45     
46     public void rename(ReferenceableSchemaComponent component, String JavaDoc newName) {
47         FindUsageVisitor usage = new FindUsageVisitor();
48         Preview preview = usage.findUsages(
49                 Collections.singletonList(component.getModel().getSchema()),
50                 component);
51         String JavaDoc originalName = component.getName();
52         component.setName(newName);
53         setRenamedElement(component, originalName);
54         rename(preview);
55     }
56     
57     /**
58      * Sets the global component that has been renamed.
59      */

60     public void setRenamedElement(ReferenceableSchemaComponent component, String JavaDoc originalName) {
61         global_component = component;
62         oldName = originalName;
63     }
64     
65     /**
66      * Fixes the referneces for all schema components in this preview
67      * to the new global component that has been renamed.
68      *
69      * User must call setRenamedElement() prior to this call.
70      */

71     public void rename(Preview preview) {
72         if (global_component == null || oldName == null)
73             return;
74         
75         for(SchemaComponent component : preview.getUsages().keySet()) {
76             component.accept(this);
77         }
78     }
79
80     <T extends ReferenceableSchemaComponent> NamedComponentReference<T>
81             createReference(Class JavaDoc<T> type, SchemaComponent referencing) {
82         if (type.isAssignableFrom(global_component.getClass())) {
83             return referencing.createReferenceTo(type.cast(global_component), type);
84         }
85         return null;
86     }
87     
88     /**
89      * For CommonSimpleRestriction, GlobalReference will be:
90      * getBase(), when a GlobalSimpleType is modified.
91      */

92     public void visit(SimpleTypeRestriction str) {
93         NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, str);
94         if (ref != null) {
95             str.setBase(ref);
96         }
97     }
98         
99     /**
100      * For LocalElement, GlobalReference will be:
101      * getType(), when a GlobalType is modified,
102      * getRef(), when a GlobalElement is modified.
103      */

104     public void visit(LocalElement element) {
105         NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, element);
106         if (ref != null) {
107             element.setType(ref);
108         }
109     }
110     
111      public void visit(ElementReference element) {
112         NamedComponentReference<GlobalElement> ref = createReference(GlobalElement.class, element);
113         if (ref != null) {
114             element.setRef(ref);
115         }
116     }
117     
118     /**
119      * For GlobalElement, GlobalReference will be:
120      * getType(), when a GlobalType is modified,
121      * getSubstitutionGroup(), when a GlobalElement is modified.
122      */

123     public void visit(GlobalElement element) {
124         NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, element);
125         if (ref != null) {
126             element.setType(ref);
127         } else {
128             NamedComponentReference<GlobalElement> ref2 = createReference(GlobalElement.class, element);
129             if (ref2 != null) {
130                 element.setSubstitutionGroup(ref2);
131             }
132         }
133     }
134         
135     /**
136      * For LocalAttribute, GlobalReference will be:
137      * getType(), when a GlobalSimpleType is modified,
138      * getRef(), when a GlobalAttribute is modified.
139      */

140     public void visit(LocalAttribute attribute) {
141         NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, attribute);
142         if (ref != null) {
143             attribute.setType(ref);
144         }
145     }
146     
147     /**
148      * For LocalAttribute, GlobalReference will be:
149      * getType(), when a GlobalSimpleType is modified,
150      * getRef(), when a GlobalAttribute is modified.
151      */

152     public void visit(AttributeReference attribute) {
153         NamedComponentReference<GlobalAttribute> ref = createReference(GlobalAttribute.class, attribute);
154         if (ref != null) {
155             attribute.setRef(ref);
156         }
157     }
158         
159     /**
160      * For AttributeGroupReference, GlobalReference will be:
161      * getGroup(), when a GlobalAttributeGroup is modified.
162      */

163     public void visit(AttributeGroupReference agr) {
164         NamedComponentReference<GlobalAttributeGroup> ref = createReference(GlobalAttributeGroup.class, agr);
165         if (ref != null) {
166             agr.setGroup(ref);
167         }
168     }
169         
170     /**
171      * For ComplexContentRestriction, GlobalReference will be:
172      * getBase(), when a GlobalComplexType is modified.
173      */

174     public void visit(ComplexContentRestriction ccr) {
175         NamedComponentReference<GlobalComplexType> ref = createReference(GlobalComplexType.class, ccr);
176         if (ref != null) {
177             ccr.setBase(ref);
178         }
179     }
180     
181     /**
182      * For SimpleExtension, GlobalReference will be:
183      * getBase(), when a GlobalType is modified.
184      */

185     public void visit(SimpleExtension extension) {
186         NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, extension);
187         if (ref != null) {
188             extension.setBase(ref);
189         }
190     }
191     
192     /**
193      * For ComplexExtension, GlobalReference will be:
194      * getBase(), when a GlobalType is modified.
195      */

196     public void visit(ComplexExtension extension) {
197         NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, extension);
198         if (ref != null) {
199             extension.setBase(ref);
200         }
201     }
202     
203     /**
204      * For GroupReference, GlobalReference will be:
205      * getRef(), when a GlobalGroup is modified.
206      */

207     public void visit(GroupReference gr) {
208         NamedComponentReference<GlobalGroup> ref = createReference(GlobalGroup.class, gr);
209         if (ref != null) {
210             gr.setRef(ref);
211         }
212     }
213     
214     /**
215      * For List, GlobalReference will be:
216      * getType(), when a GlobalSimpleType is modified.
217      */

218     public void visit(List JavaDoc list) {
219         NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, list);
220         if (ref != null) {
221             list.setType(ref);
222         }
223     }
224
225     public void visit(Union u) {
226         NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, u);
227         if (ref != null) {
228             ArrayList JavaDoc<NamedComponentReference<GlobalSimpleType>> members =
229                     new ArrayList JavaDoc(u.getMemberTypes());
230             for (int i=0; i<members.size(); i++) {
231                 if (members.get(i).getRefString().indexOf(oldName) > -1) {
232                     members.remove(i);
233                     members.add(i, ref);
234                     break;
235                 }
236             }
237             u.setMemberTypes(members);
238         }
239     }
240 }
241
Popular Tags