1 19 package org.netbeans.modules.xml.schema.refactoring; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import org.netbeans.modules.xml.refactoring.DeleteRequest; 26 import org.netbeans.modules.xml.refactoring.FileRenameRequest; 27 import org.netbeans.modules.xml.refactoring.RefactorRequest; 28 import org.netbeans.modules.xml.refactoring.RenameRequest; 29 import org.netbeans.modules.xml.refactoring.Usage; 30 import org.netbeans.modules.xml.refactoring.UsageGroup; 31 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine; 32 import org.netbeans.modules.xml.refactoring.spi.SharedUtils; 33 import org.netbeans.modules.xml.refactoring.spi.UIHelper; 34 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 35 import org.netbeans.modules.xml.schema.model.Schema; 36 import org.netbeans.modules.xml.schema.model.SchemaComponent; 37 import org.netbeans.modules.xml.schema.model.SchemaModel; 38 import org.netbeans.modules.xml.schema.model.SchemaModelReference; 39 import org.netbeans.modules.xml.schema.model.visitor.FindUsageVisitor; 40 import org.netbeans.modules.xml.schema.model.visitor.Preview; 41 import org.netbeans.modules.xml.xam.Component; 42 import org.netbeans.modules.xml.xam.Model; 43 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 44 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 45 import org.openide.filesystems.FileObject; 46 47 51 public class SchemaRefactoringEngine extends RefactoringEngine { 52 53 54 public SchemaRefactoringEngine() { 55 } 56 57 public List <String > getSearchMimeTypes() { 58 return Collections.singletonList(RefactoringUtil.XSD_MIME_TYPE); 59 } 60 61 public Component getSearchRoot(FileObject file) throws IOException { 62 return RefactoringUtil.getSchema(file); 63 } 64 65 public List <UsageGroup> findUsages(Model target, Component searchRoot) { 66 if (! (target instanceof SchemaModel) || 67 ! (searchRoot instanceof Schema)) { 68 return null; 69 } 70 SchemaModel targetModel = (SchemaModel) target; 71 Schema schema = (Schema) searchRoot; 72 UsageGroup ug = new UsageGroup(this, schema.getModel(), targetModel); 73 List <SchemaModelReference> refs = new ArrayList <SchemaModelReference>(); 74 refs.addAll(schema.getImports()); 75 refs.addAll(schema.getIncludes()); 76 refs.addAll(schema.getRedefines()); 77 for (SchemaModelReference ref : refs) { 78 SchemaModel importedModel = null; 79 try { 80 importedModel = ref.resolveReferencedModel(); 81 } catch (CatalogModelException e) { 82 ug.addError(searchRoot, e.getMessage()); 83 } 84 if (targetModel.equals(importedModel)) { 85 ug.addItem(ref); 86 return Collections.singletonList(ug); 87 } 88 } 89 return Collections.emptyList(); 90 } 91 92 public List <UsageGroup> findUsages(Component target, Component searchRoot) { 93 if (! (target instanceof ReferenceableSchemaComponent) || 94 ! (searchRoot instanceof Schema)) { 95 return Collections.emptyList(); 96 } 97 98 ReferenceableSchemaComponent referenceable = 99 (ReferenceableSchemaComponent) target; 100 Schema schema = (Schema) searchRoot; 101 UsageGroup u = null; 102 Preview p = new FindUsageVisitor().findUsages(Collections.singleton(schema), referenceable); 103 if (! p.getUsages().keySet().isEmpty()) { 104 u = new UsageGroup(this, schema.getModel(), referenceable); 105 for (SchemaComponent c : p.getUsages().keySet()) { 106 u.addItem(c); 107 } 108 } 109 if (u == null) { 110 return Collections.emptyList(); 111 } else { 112 return Collections.singletonList(u); 113 } 114 } 115 116 public UIHelper getUIHelper() { 117 return new SchemaUIHelper(); 118 } 119 120 public void refactorUsages(RefactorRequest request) throws IOException { 121 for (UsageGroup usage : request.getUsages().getUsages()) { 122 if (usage.getEngine() instanceof SchemaRefactoringEngine) { 123 if (request instanceof RenameRequest) { 124 new RenameReferenceVisitor().rename((RenameRequest)request, usage); 125 } else if (request instanceof DeleteRequest) { 126 } else if (request instanceof FileRenameRequest) { 128 _refactorUsages((FileRenameRequest)request, usage); 129 } 130 } 131 } 132 } 133 134 void _refactorUsages(FileRenameRequest request, UsageGroup usage) { 135 if (request == null || usage == null || usage.getModel() == null) return; 136 if (! (usage.getModel() instanceof SchemaModel)) return; 137 SchemaModel model = (SchemaModel) usage.getModel(); 138 boolean startTransaction = ! model.isIntransaction(); 139 try { 140 if (startTransaction) { 141 model.startTransaction(); 142 } 143 for (Usage u : usage.getItems()) { 144 if (u.getComponent() instanceof SchemaModelReference) { 145 SchemaModelReference ref = (SchemaModelReference) u.getComponent(); 146 String newLocation = request.calculateNewLocationString(ref.getSchemaLocation()); 147 ref.setSchemaLocation(newLocation); 148 } 149 } 150 } finally { 151 if (startTransaction && model.isIntransaction()) { 152 model.endTransaction(); 153 } 154 } 155 } 156 157 public <T extends RefactorRequest> boolean supportsRefactorType(Class <T> type) { 158 return (type == RenameRequest.class || 159 type == DeleteRequest.class || 160 type == FileRenameRequest.class); 161 } 162 163 public void precheck(RefactorRequest request) { 164 if (request.getTarget() instanceof SchemaComponent) { 165 if (request instanceof RenameRequest) { 166 RefactoringUtil.prepareDescription((RenameRequest)request, SchemaModel.class); 167 } else if (request instanceof DeleteRequest) { 168 SharedUtils.addCascadeDeleteErrors((DeleteRequest)request, SchemaModel.class); 169 } else if (request instanceof FileRenameRequest) { 170 RefactoringUtil.prepareDescription((FileRenameRequest)request, SchemaModel.class); 171 } 172 } 173 } 174 175 @Override 176 public String getModelReference(Component component) { 177 if (component instanceof SchemaModelReference) { 178 return ((SchemaModelReference)component).getSchemaLocation(); 179 } 180 return null; 181 } 182 } 183 | Popular Tags |