1 19 20 package org.netbeans.modules.xml.schema.refactoring; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import org.netbeans.modules.xml.refactoring.RenameRequest; 26 import org.netbeans.modules.xml.refactoring.Usage; 27 import org.netbeans.modules.xml.refactoring.UsageGroup; 28 import org.netbeans.modules.xml.schema.model.*; 29 import org.netbeans.modules.xml.schema.model.visitor.*; 30 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 31 32 37 class RenameReferenceVisitor extends DefaultSchemaVisitor { 38 39 42 private ReferenceableSchemaComponent global_component = null; 43 private String oldName = null; 44 45 48 public RenameReferenceVisitor() { 49 } 50 51 public void rename(RenameRequest request, UsageGroup usage) { 52 if (request == null || usage == null || usage.getModel() == null) return; 53 if (! (usage.getModel() instanceof SchemaModel)) return; 54 55 if (! (request.getTarget() instanceof ReferenceableSchemaComponent) || 56 request.getOldName() == null) { 57 return; 58 } 59 60 SchemaModel model = (SchemaModel) usage.getModel(); 61 boolean startTransaction = ! model.isIntransaction(); 62 63 global_component = (ReferenceableSchemaComponent) request.getRenamedTarget(); 64 oldName = request.getOldName(); 65 SchemaComponent currentComponent = null; 66 try { 67 if (startTransaction) { 68 model.startTransaction(); 69 } 70 Collection <Usage> items = usage.getItems(); 71 for (Usage item : items) { 72 if (item.isIncludedInRefactoring() && 73 item.getComponent() instanceof SchemaComponent) { 74 currentComponent = (SchemaComponent)item.getComponent(); 75 currentComponent.accept(this); 76 } 77 } 78 } finally { 79 if (startTransaction && model.isIntransaction()) { 80 model.endTransaction(); 81 } 82 } 83 } 84 85 private <T extends ReferenceableSchemaComponent> NamedComponentReference<T> 86 createReference(Class <T> type, SchemaComponent referencing) { 87 if (type.isAssignableFrom(global_component.getClass())) { 88 return referencing.createReferenceTo(type.cast(global_component), type); 89 } else { 90 assert false : type.getName()+" is not assignable from "+global_component.getClass().getName(); 91 return null; 92 } 93 } 94 95 99 public void visit(SimpleTypeRestriction str) { 100 NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, str); 101 if (ref != null) { 102 str.setBase(ref); 103 } 104 } 105 106 111 public void visit(LocalElement element) { 112 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, element); 113 if (ref != null) { 114 element.setType(ref); 115 } 116 } 117 118 public void visit(ElementReference element) { 119 NamedComponentReference<GlobalElement> ref = createReference(GlobalElement.class, element); 120 if (ref != null) { 121 element.setRef(ref); 122 } 123 } 124 125 130 public void visit(GlobalElement element) { 131 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, element); 132 if (ref != null) { 133 element.setType(ref); 134 } else { 135 NamedComponentReference<GlobalElement> ref2 = createReference(GlobalElement.class, element); 136 if (ref2 != null) { 137 element.setSubstitutionGroup(ref2); 138 } 139 } 140 } 141 142 147 public void visit(LocalAttribute attribute) { 148 NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, attribute); 149 if (ref != null) { 150 attribute.setType(ref); 151 } 152 } 153 154 159 public void visit(AttributeReference attribute) { 160 NamedComponentReference<GlobalAttribute> ref = createReference(GlobalAttribute.class, attribute); 161 if (ref != null) { 162 attribute.setRef(ref); 163 } 164 } 165 166 170 public void visit(AttributeGroupReference agr) { 171 NamedComponentReference<GlobalAttributeGroup> ref = createReference(GlobalAttributeGroup.class, agr); 172 if (ref != null) { 173 agr.setGroup(ref); 174 } 175 } 176 177 181 public void visit(ComplexContentRestriction ccr) { 182 NamedComponentReference<GlobalComplexType> ref = createReference(GlobalComplexType.class, ccr); 183 if (ref != null) { 184 ccr.setBase(ref); 185 } 186 } 187 188 192 public void visit(SimpleExtension extension) { 193 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, extension); 194 if (ref != null) { 195 extension.setBase(ref); 196 } 197 } 198 199 203 public void visit(ComplexExtension extension) { 204 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, extension); 205 if (ref != null) { 206 extension.setBase(ref); 207 } 208 } 209 210 214 public void visit(GroupReference gr) { 215 NamedComponentReference<GlobalGroup> ref = createReference(GlobalGroup.class, gr); 216 if (ref != null) { 217 gr.setRef(ref); 218 } 219 } 220 221 225 public void visit(List list) { 226 NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, list); 227 if (ref != null) { 228 list.setType(ref); 229 } 230 } 231 232 public void visit(Union u) { 233 NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, u); 234 if (ref != null) { 235 ArrayList <NamedComponentReference<GlobalSimpleType>> members = 236 new ArrayList (u.getMemberTypes()); 237 for (int i=0; i<members.size(); i++) { 238 if (members.get(i).getRefString().indexOf(oldName) > -1) { 239 members.remove(i); 240 members.add(i, ref); 241 break; 242 } 243 } 244 u.setMemberTypes(members); 245 } 246 } 247 } 248 | Popular Tags |