KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > refactoring > xsd > SchemaUsageRefactoringEngine


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 package org.netbeans.modules.xml.wsdl.refactoring.xsd;
20
21 import java.io.IOException JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import org.netbeans.modules.xml.refactoring.DeleteRequest;
30 import org.netbeans.modules.xml.refactoring.FileRenameRequest;
31 import org.netbeans.modules.xml.refactoring.RefactorRequest;
32 import org.netbeans.modules.xml.refactoring.RenameRequest;
33 import org.netbeans.modules.xml.refactoring.Usage;
34 import org.netbeans.modules.xml.refactoring.UsageGroup;
35 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine;
36 import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
37 import org.netbeans.modules.xml.refactoring.spi.UIHelper;
38 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
39 import org.netbeans.modules.xml.schema.model.Schema;
40 import org.netbeans.modules.xml.schema.model.SchemaComponent;
41 import org.netbeans.modules.xml.schema.model.SchemaModel;
42 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
43 import org.netbeans.modules.xml.wsdl.model.Definitions;
44 import org.netbeans.modules.xml.wsdl.model.Import;
45 import org.netbeans.modules.xml.wsdl.model.Types;
46 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
47 import org.netbeans.modules.xml.wsdl.refactoring.WSDLRefactoringEngine;
48 import org.netbeans.modules.xml.wsdl.refactoring.WSDLUIHelper;
49 import org.netbeans.modules.xml.xam.Component;
50 import org.netbeans.modules.xml.xam.Model;
51 import org.netbeans.modules.xml.xam.ModelSource;
52 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
53 import org.netbeans.modules.xml.xam.locator.CatalogModel;
54 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
55 import org.openide.filesystems.FileObject;
56 import org.openide.loaders.DataObject;
57 import org.openide.util.NbBundle;
58
59 /**
60  * Provides capability to search for usages of schema components in WSDL models.
61  * Provides capability to refactor schema component references in WSDL models.
62  *
63  * @author Nam Nguyen
64  */

65 public class SchemaUsageRefactoringEngine extends RefactoringEngine {
66     
67     /** Creates a new instance of WSDLRefactoringEngine */
68     public SchemaUsageRefactoringEngine() {
69     }
70
71     /**
72      * Returns UI helper in displaying the usages. Implementation could override
73      * the default UI to help display usages in a more intuitive way than the
74      * generic helper.
75      */

76     public UIHelper getUIHelper() {
77         return new WSDLUIHelper();
78     }
79     
80     public Component getSearchRoot(FileObject file) throws IOException JavaDoc {
81         return WSDLRefactoringEngine.getWSDLDefinitions(file);
82     }
83     
84     public List JavaDoc<UsageGroup> findUsages(Component target, Component searchRoot) {
85         if (target instanceof ReferenceableSchemaComponent &&
86             searchRoot instanceof Definitions) {
87             return new FindSchemaUsageVisitor().findUsages(
88                     (ReferenceableSchemaComponent)target, (Definitions)searchRoot, this);
89         }
90         return null;
91     }
92
93     public List JavaDoc<UsageGroup> findUsages(Model target, Component searchRoot) {
94         if (target instanceof SchemaModel &&
95             searchRoot instanceof Definitions) {
96             Definitions definitions = (Definitions) searchRoot;
97             String JavaDoc namespace = ((SchemaModel)target).getSchema().getTargetNamespace();
98             if (namespace == null) return null;
99             for (Import i : definitions.getImports()) {
100                 if (! namespace.equals(i.getNamespace())) {
101                     continue;
102                 }
103                 ModelSource ms = resolve(definitions.getModel(), i.getLocation(), namespace);
104                 if (areSameSource(ms, target.getModelSource())) {
105                     UsageGroup ug = new UsageGroup(this, searchRoot.getModel(), (SchemaModel) target);
106                     ug.addItem(i);
107                     return Collections.singletonList(ug);
108                 }
109             }
110             Types types = definitions.getTypes();
111             Collection JavaDoc<Schema> schemas = Collections.emptyList();
112             if (types != null && types.getSchemas() != null) {
113                 schemas = types.getSchemas();
114             }
115             for (Schema schema : schemas) {
116                 for (SchemaModelReference ref : schema.getSchemaReferences()) {
117                     if (isReferenceTo(ref, (SchemaModel) target)) {
118                         UsageGroup ug = new UsageGroup(this, searchRoot.getModel(), (SchemaModel) target);
119                         ug.addItem(ref);
120                         return Collections.singletonList(ug);
121                     }
122                 }
123             }
124         }
125         return null;
126     }
127     
128     public void refactorUsages(RefactorRequest request) throws IOException JavaDoc {
129         for (UsageGroup usage : request.getUsages().getUsages()) {
130             if (usage.getEngine() instanceof SchemaUsageRefactoringEngine) {
131                 if (request instanceof RenameRequest) {
132                     _refactorUsages((RenameRequest)request, usage);
133                 } else if (request instanceof DeleteRequest) {
134                     // NOOP
135
} else if (request instanceof FileRenameRequest) {
136                     _refactorUsages((FileRenameRequest)request, usage);
137                 }
138             }
139         }
140     }
141
142     void _refactorUsages(RenameRequest request, UsageGroup usage) throws IOException JavaDoc {
143         new RenameSchemaReferenceVisitor().rename(request, usage);
144     }
145     
146     void _refactorUsages(DeleteRequest request, UsageGroup usage) throws IOException JavaDoc {
147         //NOOP currently do not support cascade delete
148
}
149
150     void _refactorUsages(FileRenameRequest request, UsageGroup usage) throws IOException JavaDoc {
151         if (request == null || usage == null || usage.getModel() == null) return;
152         if (! (usage.getModel() instanceof WSDLModel)) return;
153         WSDLModel model = (WSDLModel) usage.getModel();
154         boolean startTransaction = ! model.isIntransaction();
155         
156         try {
157             if (startTransaction) {
158                 model.startTransaction();
159             }
160             for (Usage u : usage.getItems()) {
161                 if (u.getComponent() instanceof Import) {
162                     Import im = (Import) u.getComponent();
163                     String JavaDoc newLocation = request.calculateNewLocationString(im.getLocation());
164                     im.setLocation(newLocation);
165                 } else if (u.getComponent() instanceof SchemaModelReference) {
166                     SchemaModelReference ref = (SchemaModelReference) u.getComponent();
167                     String JavaDoc newLocation = request.calculateNewLocationString(ref.getSchemaLocation());
168                     ref.setSchemaLocation(newLocation);
169                 }
170             }
171         } finally {
172             if (startTransaction && model.isIntransaction()) {
173                 model.endTransaction();
174             }
175         }
176     }
177     
178     public void precheck(RefactorRequest request) {
179         if (request.getTarget() instanceof SchemaComponent ||
180             request.getTarget() instanceof SchemaModel) {
181             if (request instanceof RenameRequest) {
182                 prepareDescription((RenameRequest)request);
183             } else if (request instanceof DeleteRequest) {
184                 SharedUtils.addCascadeDeleteErrors((DeleteRequest)request, WSDLModel.class);
185             } else if (request instanceof FileRenameRequest) {
186                 prepareDescription((FileRenameRequest)request);
187             }
188         }
189     }
190
191     private void prepareDescription(RenameRequest request) {
192         SchemaComponent target = (SchemaComponent) request.getTarget();
193         for (UsageGroup usage : request.getUsages().getUsages()) {
194             if (! (usage.getModel() instanceof WSDLModel)) {
195                 continue;
196             }
197             String JavaDoc ns = ((SchemaModel)target.getModel()).getEffectiveNamespace(target);
198             for (Object JavaDoc o : usage.getItems()) {
199                 Usage i = (Usage) o; //strange i have to do this
200
String JavaDoc prefix = ((AbstractDocumentComponent)i.getComponent()).lookupPrefix(ns);
201                 String JavaDoc refString = prefix + ":" + request.getNewName(); //NOI18N
202
//TODO a visitor to get the right attribute name from i.getComponent().
203
String JavaDoc refAttribute = "ref"; //NOI18N
204
String JavaDoc msg = NbBundle.getMessage(SchemaUsageRefactoringEngine.class,
205                         "MSG_SetReferenceStringTo", refAttribute, refString);
206                 i.setRefactoringDescription(msg);
207             }
208         }
209     }
210
211     private void prepareDescription(FileRenameRequest request) {
212         SchemaModel target = (SchemaModel) request.getTargetModel();
213         for (UsageGroup usage : request.getUsages().getUsages()) {
214             if (! (usage.getModel() instanceof WSDLModel)) {
215                 continue;
216             }
217             for (Usage i : usage.getItems()) {
218                 String JavaDoc refAttribute = getLocationReferenceAttributeName(i.getComponent());
219                 String JavaDoc msg = NbBundle.getMessage(SchemaUsageRefactoringEngine.class,
220                         "MSG_SetLocationStringTo", refAttribute, getNewLocationValue(request, i.getComponent()));
221                 i.setRefactoringDescription(msg);
222             }
223         }
224     }
225     
226     public static String JavaDoc getLocationReferenceAttributeName(Component usageComponent) {
227         if (usageComponent instanceof org.netbeans.modules.xml.wsdl.model.Import) {
228             return "location"; //NOI18N
229
} else if (usageComponent instanceof SchemaModelReference) {
230             return "schemaLocation"; //NOI18N
231
} else {
232             return "ref"; //NO18N
233
}
234     }
235     
236     private static String JavaDoc getNewLocationValue(FileRenameRequest request, Component usageComponent) {
237         String JavaDoc current = ""; //NOI18N
238
if (usageComponent instanceof Import) {
239             current =((Import)usageComponent).getLocation();
240         } else if (usageComponent instanceof SchemaModelReference) {
241             current = ((SchemaModelReference)usageComponent).getSchemaLocation();
242         }
243
244         return request.calculateNewLocationString(current);
245     }
246
247     public static ModelSource resolveModelSource(
248             String JavaDoc location, Model currentModel, CatalogModel currentCatalog) {
249         ModelSource ms = null;
250         try {
251             if (location != null) {
252                 URI JavaDoc uri = new URI JavaDoc(location);
253                 ms = currentCatalog.getModelSource(uri, currentModel.getModelSource());
254             }
255         } catch (URISyntaxException JavaDoc ex) {
256             log(ex.getMessage());
257         } catch (CatalogModelException nse) {
258             // unable to resolve location
259
log(nse.getMessage());
260         }
261         return ms;
262     }
263     
264     private static void log(String JavaDoc message) {
265         Logger.getLogger(SchemaUsageRefactoringEngine.class.getName()).log(Level.FINE, message);
266     }
267     
268     public static ModelSource resolve(Model currentModel, String JavaDoc hint, String JavaDoc backup) {
269         CatalogModel nr = (CatalogModel) currentModel.getModelSource().getLookup().lookup(CatalogModel.class);
270         
271         // try hint
272
ModelSource ms = resolveModelSource(hint, currentModel, nr);
273         
274         // hint didn't work now try backup
275
if (ms == null) {
276             ms = resolveModelSource(backup, currentModel, nr);
277         }
278         
279         return ms;
280     }
281     
282     public static boolean areSameSource(ModelSource m1, ModelSource m2) {
283         if (m1 == null || m2 == null) return false;
284         DataObject dobj1 = (DataObject) m1.getLookup().lookup(DataObject.class);
285         DataObject dobj2 = (DataObject) m2.getLookup().lookup(DataObject.class);
286         return dobj1 != null && dobj1.equals(dobj2);
287     }
288
289     public static boolean isReferenceTo(SchemaModelReference ref, SchemaModel target) {
290         try {
291             return target == ref.resolveReferencedModel();
292         } catch(CatalogModelException ex) {
293             log(ex.getMessage());
294         }
295         return false;
296     }
297
298     @Override JavaDoc
299     public String JavaDoc getModelReference(Component component) {
300         if (component instanceof SchemaModelReference) {
301             return ((SchemaModelReference)component).getSchemaLocation();
302         }
303         return null;
304     }
305 }
306
Popular Tags