1 19 package org.netbeans.modules.xml.wsdl.refactoring; 20 21 import java.io.IOException ; 22 import java.util.Collections ; 23 import java.util.List ; 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 53 public class WSDLRefactoringEngine extends RefactoringEngine { 54 public static final String WSDL_MIME_TYPE = "text/xml-wsdl"; 56 57 public WSDLRefactoringEngine() { 58 } 59 60 public Component getSearchRoot(FileObject fo) throws IOException { 61 return getWSDLDefinitions(fo); 62 } 63 64 public static Definitions getWSDLDefinitions(FileObject fo) throws IOException { 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 msg = NbBundle.getMessage(WSDLRefactoringEngine.class, 75 "MSG_ModelSourceMalformed", fo.getPath()); 76 throw new IOException (msg); 77 } 78 } 79 return null; 80 } 81 82 public List <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 { 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 } 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 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 152 public UIHelper getUIHelper() { 153 return new WSDLUIHelper(); 154 } 155 156 public List <UsageGroup> findUsages(Model target, Component searchRoot) { 157 if (target instanceof WSDLModel && 158 searchRoot instanceof Definitions) { 159 Definitions definitions = (Definitions) searchRoot; 160 String 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 ns = ((WSDLModel)target.getModel()).getDefinitions().getTargetNamespace(); 188 for (Usage i : usage.getItems()) { 189 String prefix = ((AbstractDocumentComponent)i.getComponent()).lookupPrefix(ns); 190 String refString = prefix + ":" + request.getNewName(); String refAttribute = "ref"; String 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 refAttribute = "location"; String 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 getNewLocationValue(FileRenameRequest request, Component usageComponent) { 219 String current = ""; if (usageComponent instanceof Import) { 221 current =((Import)usageComponent).getLocation(); 222 } 223 224 return request.calculateNewLocationString(current); 225 } 226 227 @Override 228 public String getModelReference(Component component) { 229 if (component instanceof Import) { 230 return ((Import)component).getLocation(); 231 } 232 return null; 233 } 234 } 235 | Popular Tags |