1 19 package org.netbeans.modules.xml.wsdl.refactoring.xsd; 20 21 import java.io.IOException ; 22 import java.net.URI ; 23 import java.net.URISyntaxException ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 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 65 public class SchemaUsageRefactoringEngine extends RefactoringEngine { 66 67 68 public SchemaUsageRefactoringEngine() { 69 } 70 71 76 public UIHelper getUIHelper() { 77 return new WSDLUIHelper(); 78 } 79 80 public Component getSearchRoot(FileObject file) throws IOException { 81 return WSDLRefactoringEngine.getWSDLDefinitions(file); 82 } 83 84 public List <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 <UsageGroup> findUsages(Model target, Component searchRoot) { 94 if (target instanceof SchemaModel && 95 searchRoot instanceof Definitions) { 96 Definitions definitions = (Definitions) searchRoot; 97 String 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 <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 { 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 } else if (request instanceof FileRenameRequest) { 136 _refactorUsages((FileRenameRequest)request, usage); 137 } 138 } 139 } 140 } 141 142 void _refactorUsages(RenameRequest request, UsageGroup usage) throws IOException { 143 new RenameSchemaReferenceVisitor().rename(request, usage); 144 } 145 146 void _refactorUsages(DeleteRequest request, UsageGroup usage) throws IOException { 147 } 149 150 void _refactorUsages(FileRenameRequest request, UsageGroup usage) throws IOException { 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 newLocation = request.calculateNewLocationString(im.getLocation()); 164 im.setLocation(newLocation); 165 } else if (u.getComponent() instanceof SchemaModelReference) { 166 SchemaModelReference ref = (SchemaModelReference) u.getComponent(); 167 String 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 ns = ((SchemaModel)target.getModel()).getEffectiveNamespace(target); 198 for (Object o : usage.getItems()) { 199 Usage i = (Usage) o; String prefix = ((AbstractDocumentComponent)i.getComponent()).lookupPrefix(ns); 201 String refString = prefix + ":" + request.getNewName(); String refAttribute = "ref"; String 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 refAttribute = getLocationReferenceAttributeName(i.getComponent()); 219 String 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 getLocationReferenceAttributeName(Component usageComponent) { 227 if (usageComponent instanceof org.netbeans.modules.xml.wsdl.model.Import) { 228 return "location"; } else if (usageComponent instanceof SchemaModelReference) { 230 return "schemaLocation"; } else { 232 return "ref"; } 234 } 235 236 private static String getNewLocationValue(FileRenameRequest request, Component usageComponent) { 237 String current = ""; 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 location, Model currentModel, CatalogModel currentCatalog) { 249 ModelSource ms = null; 250 try { 251 if (location != null) { 252 URI uri = new URI (location); 253 ms = currentCatalog.getModelSource(uri, currentModel.getModelSource()); 254 } 255 } catch (URISyntaxException ex) { 256 log(ex.getMessage()); 257 } catch (CatalogModelException nse) { 258 log(nse.getMessage()); 260 } 261 return ms; 262 } 263 264 private static void log(String message) { 265 Logger.getLogger(SchemaUsageRefactoringEngine.class.getName()).log(Level.FINE, message); 266 } 267 268 public static ModelSource resolve(Model currentModel, String hint, String backup) { 269 CatalogModel nr = (CatalogModel) currentModel.getModelSource().getLookup().lookup(CatalogModel.class); 270 271 ModelSource ms = resolveModelSource(hint, currentModel, nr); 273 274 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 299 public String getModelReference(Component component) { 300 if (component instanceof SchemaModelReference) { 301 return ((SchemaModelReference)component).getSchemaLocation(); 302 } 303 return null; 304 } 305 } 306 | Popular Tags |