KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > refactoring > RenameReferenceVisitor


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.refactoring;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import org.netbeans.modules.xml.refactoring.RenameRequest;
26 import org.netbeans.modules.xml.refactoring.Usage;
27 import org.netbeans.modules.xml.refactoring.UsageGroup;
28 import org.netbeans.modules.xml.schema.model.*;
29 import org.netbeans.modules.xml.schema.model.visitor.*;
30 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
31
32 /**
33  *
34  * @author Nam Nguyen
35  * @author Samaresh
36  */

37 class RenameReferenceVisitor extends DefaultSchemaVisitor {
38     
39     /**
40      * The global component being modified.
41      */

42     private ReferenceableSchemaComponent global_component = null;
43     private String JavaDoc oldName = null;
44     
45     /**
46      * Creates a new instance of RefactorVisitor
47      */

48     public RenameReferenceVisitor() {
49     }
50     
51     public void rename(RenameRequest request, UsageGroup usage) {
52         if (request == null || usage == null || usage.getModel() == null) return;
53         if (! (usage.getModel() instanceof SchemaModel)) return;
54
55         if (! (request.getTarget() instanceof ReferenceableSchemaComponent) ||
56                 request.getOldName() == null) {
57             return;
58         }
59         
60         SchemaModel model = (SchemaModel) usage.getModel();
61         boolean startTransaction = ! model.isIntransaction();
62         
63         global_component = (ReferenceableSchemaComponent) request.getRenamedTarget();
64         oldName = request.getOldName();
65         SchemaComponent currentComponent = null;
66         try {
67             if (startTransaction) {
68                 model.startTransaction();
69             }
70             Collection JavaDoc<Usage> items = usage.getItems();
71             for (Usage item : items) {
72                 if (item.isIncludedInRefactoring() &&
73                     item.getComponent() instanceof SchemaComponent) {
74                     currentComponent = (SchemaComponent)item.getComponent();
75                     currentComponent.accept(this);
76                 }
77             }
78         } finally {
79             if (startTransaction && model.isIntransaction()) {
80                 model.endTransaction();
81             }
82         }
83     }
84     
85     private <T extends ReferenceableSchemaComponent> NamedComponentReference<T>
86             createReference(Class JavaDoc<T> type, SchemaComponent referencing) {
87         if (type.isAssignableFrom(global_component.getClass())) {
88             return referencing.createReferenceTo(type.cast(global_component), type);
89         } else {
90             assert false : type.getName()+" is not assignable from "+global_component.getClass().getName();
91             return null;
92         }
93     }
94     
95     /**
96      * For CommonSimpleRestriction, GlobalReference will be:
97      * getBase(), when a GlobalSimpleType is modified.
98      */

99     public void visit(SimpleTypeRestriction str) {
100         NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, str);
101         if (ref != null) {
102             str.setBase(ref);
103         }
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         NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, element);
113         if (ref != null) {
114             element.setType(ref);
115         }
116     }
117     
118     public void visit(ElementReference element) {
119         NamedComponentReference<GlobalElement> ref = createReference(GlobalElement.class, element);
120         if (ref != null) {
121             element.setRef(ref);
122         }
123     }
124     
125     /**
126      * For GlobalElement, GlobalReference will be:
127      * getType(), when a GlobalType is modified,
128      * getSubstitutionGroup(), when a GlobalElement is modified.
129      */

130     public void visit(GlobalElement element) {
131         NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, element);
132         if (ref != null) {
133             element.setType(ref);
134         } else {
135             NamedComponentReference<GlobalElement> ref2 = createReference(GlobalElement.class, element);
136             if (ref2 != null) {
137                 element.setSubstitutionGroup(ref2);
138             }
139         }
140     }
141     
142     /**
143      * For LocalAttribute, GlobalReference will be:
144      * getType(), when a GlobalSimpleType is modified,
145      * getRef(), when a GlobalAttribute is modified.
146      */

147     public void visit(LocalAttribute attribute) {
148         NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, attribute);
149         if (ref != null) {
150             attribute.setType(ref);
151         }
152     }
153     
154     /**
155      * For LocalAttribute, GlobalReference will be:
156      * getType(), when a GlobalSimpleType is modified,
157      * getRef(), when a GlobalAttribute is modified.
158      */

159     public void visit(AttributeReference attribute) {
160         NamedComponentReference<GlobalAttribute> ref = createReference(GlobalAttribute.class, attribute);
161         if (ref != null) {
162             attribute.setRef(ref);
163         }
164     }
165     
166     /**
167      * For AttributeGroupReference, GlobalReference will be:
168      * getGroup(), when a GlobalAttributeGroup is modified.
169      */

170     public void visit(AttributeGroupReference agr) {
171         NamedComponentReference<GlobalAttributeGroup> ref = createReference(GlobalAttributeGroup.class, agr);
172         if (ref != null) {
173             agr.setGroup(ref);
174         }
175     }
176     
177     /**
178      * For ComplexContentRestriction, GlobalReference will be:
179      * getBase(), when a GlobalComplexType is modified.
180      */

181     public void visit(ComplexContentRestriction ccr) {
182         NamedComponentReference<GlobalComplexType> ref = createReference(GlobalComplexType.class, ccr);
183         if (ref != null) {
184             ccr.setBase(ref);
185         }
186     }
187     
188     /**
189      * For SimpleExtension, GlobalReference will be:
190      * getBase(), when a GlobalType is modified.
191      */

192     public void visit(SimpleExtension extension) {
193         NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, extension);
194         if (ref != null) {
195             extension.setBase(ref);
196         }
197     }
198     
199     /**
200      * For ComplexExtension, GlobalReference will be:
201      * getBase(), when a GlobalType is modified.
202      */

203     public void visit(ComplexExtension extension) {
204         NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, extension);
205         if (ref != null) {
206             extension.setBase(ref);
207         }
208     }
209     
210     /**
211      * For GroupReference, GlobalReference will be:
212      * getRef(), when a GlobalGroup is modified.
213      */

214     public void visit(GroupReference gr) {
215         NamedComponentReference<GlobalGroup> ref = createReference(GlobalGroup.class, gr);
216         if (ref != null) {
217             gr.setRef(ref);
218         }
219     }
220     
221     /**
222      * For List, GlobalReference will be:
223      * getType(), when a GlobalSimpleType is modified.
224      */

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