1 19 20 package org.netbeans.modules.xml.schema.model.impl; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import org.netbeans.modules.xml.schema.model.*; 25 import org.netbeans.modules.xml.schema.model.visitor.*; 26 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 27 28 32 class RefactorVisitor extends DefaultSchemaVisitor { 33 34 37 private ReferenceableSchemaComponent global_component = null; 38 private String oldName = null; 39 40 43 public RefactorVisitor() { 44 } 45 46 public void rename(ReferenceableSchemaComponent component, String newName) { 47 FindUsageVisitor usage = new FindUsageVisitor(); 48 Preview preview = usage.findUsages( 49 Collections.singletonList(component.getModel().getSchema()), 50 component); 51 String originalName = component.getName(); 52 component.setName(newName); 53 setRenamedElement(component, originalName); 54 rename(preview); 55 } 56 57 60 public void setRenamedElement(ReferenceableSchemaComponent component, String originalName) { 61 global_component = component; 62 oldName = originalName; 63 } 64 65 71 public void rename(Preview preview) { 72 if (global_component == null || oldName == null) 73 return; 74 75 for(SchemaComponent component : preview.getUsages().keySet()) { 76 component.accept(this); 77 } 78 } 79 80 <T extends ReferenceableSchemaComponent> NamedComponentReference<T> 81 createReference(Class <T> type, SchemaComponent referencing) { 82 if (type.isAssignableFrom(global_component.getClass())) { 83 return referencing.createReferenceTo(type.cast(global_component), type); 84 } 85 return null; 86 } 87 88 92 public void visit(SimpleTypeRestriction str) { 93 NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, str); 94 if (ref != null) { 95 str.setBase(ref); 96 } 97 } 98 99 104 public void visit(LocalElement element) { 105 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, element); 106 if (ref != null) { 107 element.setType(ref); 108 } 109 } 110 111 public void visit(ElementReference element) { 112 NamedComponentReference<GlobalElement> ref = createReference(GlobalElement.class, element); 113 if (ref != null) { 114 element.setRef(ref); 115 } 116 } 117 118 123 public void visit(GlobalElement element) { 124 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, element); 125 if (ref != null) { 126 element.setType(ref); 127 } else { 128 NamedComponentReference<GlobalElement> ref2 = createReference(GlobalElement.class, element); 129 if (ref2 != null) { 130 element.setSubstitutionGroup(ref2); 131 } 132 } 133 } 134 135 140 public void visit(LocalAttribute attribute) { 141 NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, attribute); 142 if (ref != null) { 143 attribute.setType(ref); 144 } 145 } 146 147 152 public void visit(AttributeReference attribute) { 153 NamedComponentReference<GlobalAttribute> ref = createReference(GlobalAttribute.class, attribute); 154 if (ref != null) { 155 attribute.setRef(ref); 156 } 157 } 158 159 163 public void visit(AttributeGroupReference agr) { 164 NamedComponentReference<GlobalAttributeGroup> ref = createReference(GlobalAttributeGroup.class, agr); 165 if (ref != null) { 166 agr.setGroup(ref); 167 } 168 } 169 170 174 public void visit(ComplexContentRestriction ccr) { 175 NamedComponentReference<GlobalComplexType> ref = createReference(GlobalComplexType.class, ccr); 176 if (ref != null) { 177 ccr.setBase(ref); 178 } 179 } 180 181 185 public void visit(SimpleExtension extension) { 186 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, extension); 187 if (ref != null) { 188 extension.setBase(ref); 189 } 190 } 191 192 196 public void visit(ComplexExtension extension) { 197 NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, extension); 198 if (ref != null) { 199 extension.setBase(ref); 200 } 201 } 202 203 207 public void visit(GroupReference gr) { 208 NamedComponentReference<GlobalGroup> ref = createReference(GlobalGroup.class, gr); 209 if (ref != null) { 210 gr.setRef(ref); 211 } 212 } 213 214 218 public void visit(List list) { 219 NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, list); 220 if (ref != null) { 221 list.setType(ref); 222 } 223 } 224 225 public void visit(Union u) { 226 NamedComponentReference<GlobalSimpleType> ref = createReference(GlobalSimpleType.class, u); 227 if (ref != null) { 228 ArrayList <NamedComponentReference<GlobalSimpleType>> members = 229 new ArrayList (u.getMemberTypes()); 230 for (int i=0; i<members.size(); i++) { 231 if (members.get(i).getRefString().indexOf(oldName) > -1) { 232 members.remove(i); 233 members.add(i, ref); 234 break; 235 } 236 } 237 u.setMemberTypes(members); 238 } 239 } 240 } 241 | Popular Tags |