1 19 package org.netbeans.modules.xml.wsdl.refactoring.xsd; 20 21 import java.io.IOException ; 22 import java.util.Collection ; 23 import org.netbeans.modules.xml.refactoring.RenameRequest; 24 import org.netbeans.modules.xml.refactoring.Usage; 25 import org.netbeans.modules.xml.refactoring.UsageGroup; 26 import org.netbeans.modules.xml.schema.model.GlobalElement; 27 import org.netbeans.modules.xml.schema.model.GlobalType; 28 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 29 import org.netbeans.modules.xml.wsdl.model.Part; 30 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 31 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 32 import org.netbeans.modules.xml.wsdl.model.visitor.DefaultVisitor; 33 import org.netbeans.modules.xml.wsdl.model.visitor.WSDLVisitor; 34 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 35 36 40 public class RenameSchemaReferenceVisitor extends DefaultVisitor implements WSDLVisitor { 41 private ReferenceableSchemaComponent renamedReferenced; 42 private String oldName; 43 44 45 public RenameSchemaReferenceVisitor() { 46 } 47 48 public void rename(RenameRequest request, UsageGroup usage) throws IOException { 49 if (request == null || usage == null || usage.getModel() == null) return; 50 if (! (usage.getModel() instanceof WSDLModel)) return; 51 if (! (request.getTarget() instanceof ReferenceableSchemaComponent) || 52 request.getOldName() == null) { 53 return; 54 } 55 56 WSDLModel model = (WSDLModel) usage.getModel(); 57 boolean startTransaction = ! model.isIntransaction(); 58 renamedReferenced = (ReferenceableSchemaComponent) request.getRenamedTarget(); 59 oldName = request.getOldName(); 60 try { 61 if (startTransaction) { 62 model.startTransaction(); 63 } 64 Collection <Usage> items = usage.getItems(); 65 for (Usage item : items) { 66 if (item.isIncludedInRefactoring() && 67 item.getComponent() instanceof WSDLComponent) { 68 WSDLComponent referencing = (WSDLComponent)item.getComponent(); 69 referencing.accept(this); 70 } 71 } 72 } finally { 73 if (startTransaction && model.isIntransaction()) { 74 model.endTransaction(); 75 } 76 } 77 } 78 79 private <T extends ReferenceableSchemaComponent> NamedComponentReference<T> 80 createReference(Class <T> type, WSDLComponent referencing) { 81 if (type.isAssignableFrom(renamedReferenced.getClass())) { 82 return referencing.createSchemaReference(type.cast(renamedReferenced), type); 83 } else { 84 assert false : type.getName()+" is not assignable from "+renamedReferenced.getClass().getName(); 85 return null; 86 } 87 } 88 89 public void visit(Part part) { 90 if (renamedReferenced instanceof GlobalElement && 91 part.getElement().getQName().getLocalPart().equals(oldName)) 92 { 93 NamedComponentReference<GlobalElement> ref = createReference(GlobalElement.class, part); 94 if (ref != null) { 95 part.setElement(ref); 96 part.setType(null); 97 } 98 } else if (renamedReferenced instanceof GlobalType && 99 part.getType().getQName().getLocalPart().equals(oldName)) 100 { 101 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, part); 102 if (ref != null) { 103 part.setType(ref); 104 part.setElement(null); 105 } 106 } 107 } 108 } | Popular Tags |