KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > refactoring > WSDLRefactoringEngine


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;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.xml.refactoring.DeleteRequest;
25 import org.netbeans.modules.xml.refactoring.FileRenameRequest;
26 import org.netbeans.modules.xml.refactoring.RefactorRequest;
27 import org.netbeans.modules.xml.refactoring.RenameRequest;
28 import org.netbeans.modules.xml.refactoring.Usage;
29 import org.netbeans.modules.xml.refactoring.UsageGroup;
30 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine;
31 import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
32 import org.netbeans.modules.xml.refactoring.spi.UIHelper;
33 import org.netbeans.modules.xml.retriever.catalog.Utilities;
34 import org.netbeans.modules.xml.wsdl.model.Definitions;
35 import org.netbeans.modules.xml.wsdl.model.Import;
36 import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
37 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
38 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
39 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
40 import org.netbeans.modules.xml.xam.Component;
41 import org.netbeans.modules.xml.xam.Model;
42 import org.netbeans.modules.xml.xam.ModelSource;
43 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
44 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.util.NbBundle;
48
49 /**
50  *
51  * @author Nam Nguyen
52  */

53 public class WSDLRefactoringEngine extends RefactoringEngine {
54     public static final String JavaDoc WSDL_MIME_TYPE = "text/xml-wsdl"; // NOI18N
55

56     /** Creates a new instance of WSDLRefactoringEngine */
57     public WSDLRefactoringEngine() {
58     }
59
60     public Component getSearchRoot(FileObject fo) throws IOException JavaDoc {
61         return getWSDLDefinitions(fo);
62     }
63     
64     public static Definitions getWSDLDefinitions(FileObject fo) throws IOException JavaDoc {
65         if (! WSDL_MIME_TYPE.equals(FileUtil.getMIMEType(fo))) {
66             return null;
67         }
68         ModelSource modelSource = Utilities.getModelSource(fo, true);
69         WSDLModel model = WSDLModelFactory.getDefault().getModel(modelSource);
70         if (model != null) {
71             if (model.getState().equals(Model.State.VALID)) {
72                 return model.getDefinitions();
73             } else {
74                 String JavaDoc msg = NbBundle.getMessage(WSDLRefactoringEngine.class,
75                         "MSG_ModelSourceMalformed", fo.getPath());
76                 throw new IOException JavaDoc(msg);
77             }
78         }
79         return null;
80     }
81
82     public List JavaDoc<UsageGroup> findUsages(Component target, Component searchRoot) {
83         if (target instanceof ReferenceableWSDLComponent &&
84             searchRoot instanceof Definitions)
85         {
86             return new FindWSDLUsageVisitor().findUsages(
87                     (ReferenceableWSDLComponent)target, (Definitions)searchRoot, this);
88         }
89         return Collections.emptyList();
90     }
91
92     public void refactorUsages(RefactorRequest request) throws IOException JavaDoc {
93         for (UsageGroup g : request.getUsages().getUsages()) {
94             if (g.getModel() instanceof WSDLModel) {
95                 if (request.getTarget() instanceof WSDLComponent) {
96                     if (request instanceof RenameRequest) {
97                         new WSDLRenameReferenceVisitor().refactor((RenameRequest) request, g);
98                     } else if (request instanceof DeleteRequest) {
99                         // cascade delete or reset reference is not supported
100
}
101                 } else if (request.getTarget() instanceof WSDLModel &&
102                            request instanceof FileRenameRequest) {
103                     _refactorUsages((FileRenameRequest) request, g);
104                     
105                 }
106             }
107         }
108     }
109     
110     void _refactorUsages(FileRenameRequest request, UsageGroup usage) {
111         if (request == null || usage == null || usage.getModel() == null) return;
112         if (! (usage.getModel() instanceof WSDLModel)) return;
113
114         Model model = usage.getModel();
115         boolean startTransaction = ! model.isIntransaction();
116         try {
117             if (startTransaction) {
118                 model.startTransaction();
119             }
120         
121             for (Usage u : usage.getItems()) {
122                 if (u.getComponent() instanceof Import) {
123                     Import ref = (Import) u.getComponent();
124                     String JavaDoc newLocation = request.calculateNewLocationString(ref.getLocation());
125                     ref.setLocation(newLocation);
126                 }
127             }
128         } finally {
129             if (startTransaction && model.isIntransaction()) {
130                 model.endTransaction();
131             }
132         }
133     }
134
135     public void precheck(RefactorRequest request) {
136         if (request.getTarget() instanceof WSDLComponent) {
137             if (request instanceof DeleteRequest) {
138                 SharedUtils.addCascadeDeleteErrors((DeleteRequest)request, WSDLModel.class);
139             } else if (request instanceof RenameRequest) {
140                 prepareDescription((RenameRequest) request);
141             } else if (request instanceof FileRenameRequest) {
142                 prepareDescription((FileRenameRequest) request);
143             }
144         }
145     }
146
147     /**
148      * Returns UI helper in displaying the usages. Implementation could override
149      * the default UI to help display usages in a more intuitive way than the
150      * generic helper.
151      */

152     public UIHelper getUIHelper() {
153         return new WSDLUIHelper();
154     }
155
156     public List JavaDoc<UsageGroup> findUsages(Model target, Component searchRoot) {
157         if (target instanceof WSDLModel &&
158             searchRoot instanceof Definitions) {
159             Definitions definitions = (Definitions) searchRoot;
160             String JavaDoc namespace = ((WSDLModel)target).getDefinitions().getTargetNamespace();
161             UsageGroup ug = new UsageGroup(this, searchRoot.getModel(), (WSDLModel) target);
162             for (Import i : definitions.getImports()) {
163                 Model imported = null;
164                 if (namespace.equals(i.getNamespace())) {
165                     try {
166                         imported = i.getImportedWSDLModel();
167                     } catch(CatalogModelException ex) {
168                         ug.addError(searchRoot, ex.getMessage());
169                     }
170                 }
171
172                 if (imported == target) {
173                     ug.addItem(i);
174                 }
175             }
176             return Collections.singletonList(ug);
177         }
178         return Collections.emptyList();
179     }
180     
181     private void prepareDescription(RenameRequest request) {
182         WSDLComponent target = (WSDLComponent) request.getTarget();
183         for (UsageGroup usage : request.getUsages().getUsages()) {
184             if (! (usage.getModel() instanceof WSDLModel)) {
185                 continue;
186             }
187             String JavaDoc ns = ((WSDLModel)target.getModel()).getDefinitions().getTargetNamespace();
188             for (Usage i : usage.getItems()) {
189                 String JavaDoc prefix = ((AbstractDocumentComponent)i.getComponent()).lookupPrefix(ns);
190                 String JavaDoc refString = prefix + ":" + request.getNewName(); //NOI18N
191
//TODO a visitor to get the right attribute name from i.getComponent().
192
String JavaDoc refAttribute = "ref"; //NOI18N
193
String JavaDoc msg = NbBundle.getMessage(WSDLRefactoringEngine.class,
194                         "MSG_SetReferenceStringTo", refAttribute, refString);
195                 i.setRefactoringDescription(msg);
196             }
197         }
198     }
199
200     private void prepareDescription(FileRenameRequest request) {
201         WSDLModel target = (WSDLModel) request.getTargetModel();
202         for (UsageGroup usage : request.getUsages().getUsages()) {
203             if (! (usage.getModel() instanceof WSDLModel) &&
204                 ! (usage.getEngine() instanceof WSDLRefactoringEngine)) {
205                 continue;
206             }
207             for (Usage i : usage.getItems()) {
208                 if (i.getComponent() instanceof Import) {
209                     String JavaDoc refAttribute = "location"; //NOI18N
210
String JavaDoc msg = NbBundle.getMessage(WSDLRefactoringEngine.class,
211                             "MSG_SetLocationStringTo", refAttribute, getNewLocationValue(request, i.getComponent()));
212                     i.setRefactoringDescription(msg);
213                 }
214             }
215         }
216     }
217     
218     private static String JavaDoc getNewLocationValue(FileRenameRequest request, Component usageComponent) {
219         String JavaDoc current = ""; //NOI18N
220
if (usageComponent instanceof Import) {
221             current =((Import)usageComponent).getLocation();
222         }
223
224         return request.calculateNewLocationString(current);
225     }
226     
227     @Override JavaDoc
228     public String JavaDoc getModelReference(Component component) {
229         if (component instanceof Import) {
230             return ((Import)component).getLocation();
231         }
232         return null;
233     }
234 }
235
Popular Tags