1 19 package org.netbeans.modules.xml.schema.refactoring; 20 21 import java.io.IOException ; 22 import org.netbeans.modules.xml.refactoring.FileRenameRequest; 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.Schema; 27 import org.netbeans.modules.xml.schema.model.SchemaModel; 28 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 29 import org.netbeans.modules.xml.xam.ModelSource; 30 import org.netbeans.modules.xml.retriever.catalog.Utilities; 31 import org.netbeans.modules.xml.wsdl.model.Import; 32 import org.netbeans.modules.xml.schema.model.SchemaComponent; 33 import org.netbeans.modules.xml.schema.model.SchemaModelReference; 34 import org.netbeans.modules.xml.xam.Component; 35 import org.netbeans.modules.xml.xam.Model; 36 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.util.NbBundle; 40 41 45 public class RefactoringUtil { 46 public static final String XSD_MIME_TYPE = "application/x-schema+xml"; 48 public static Schema getSchema(FileObject fo) throws IOException { 49 if (! XSD_MIME_TYPE.equals(FileUtil.getMIMEType(fo))) { 50 return null; 51 } 52 Schema ret = null; 53 ModelSource modelSource = Utilities.getModelSource(fo, true); 54 SchemaModel sm = SchemaModelFactory.getDefault().getModel(modelSource); 55 assert sm != null : "Unexpected null model returned from factory"; 56 if (sm.getState().equals(Model.State.VALID)) { 57 return sm.getSchema(); 58 } else { 59 String msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_SchemaModelNotWellFormed", fo.getPath()); 60 throw new IOException (msg); 61 } 62 } 63 64 public static void prepareDescription(RenameRequest request, Class <? extends Model> referencingModelType) { 65 SchemaComponent target = (SchemaComponent) request.getTarget(); 66 for (UsageGroup usage : request.getUsages().getUsages()) { 67 if (! (referencingModelType.isAssignableFrom(usage.getModel().getClass()))) { 68 continue; 69 } 70 String ns = ((SchemaModel)target.getModel()).getEffectiveNamespace(target); 71 for (Object o : usage.getItems()) { 72 Usage i = (Usage) o; String prefix = ((AbstractDocumentComponent)i.getComponent()).lookupPrefix(ns); 74 String refString = prefix + ":" + request.getNewName(); String refAttribute = "ref"; String msg = NbBundle.getMessage(RefactoringUtil.class, 78 "MSG_SetReferenceStringTo", refAttribute, refString); 79 i.setRefactoringDescription(msg); 80 } 81 } 82 } 83 84 public static void prepareDescription(FileRenameRequest request, Class <? extends Model> referencingModelType) { 85 for (UsageGroup usage : request.getUsages().getUsages()) { 86 if (! (referencingModelType.isAssignableFrom(usage.getModel().getClass()))) { 87 continue; 88 } 89 for (Usage i : usage.getItems()) { 90 String refAttribute = getLocationReferenceAttributeName(i.getComponent()); 91 String msg = NbBundle.getMessage(RefactoringUtil.class, 92 "MSG_SetLocationStringTo", refAttribute, getNewLocationValue(request, i.getComponent())); 93 i.setRefactoringDescription(msg); 94 } 95 } 96 } 97 98 public static String getLocationReferenceAttributeName(Component usageComponent) { 99 if (usageComponent instanceof org.netbeans.modules.xml.wsdl.model.Import) { 100 return "location"; } else if (usageComponent instanceof SchemaModelReference) { 102 return "schemaLocation"; } else { 104 return "ref"; } 106 } 107 108 private static String getNewLocationValue(FileRenameRequest request, Component usageComponent) { 109 String current = ""; if (usageComponent instanceof Import) { 111 current =((Import)usageComponent).getLocation(); 112 } else if (usageComponent instanceof SchemaModelReference) { 113 current = ((SchemaModelReference)usageComponent).getSchemaLocation(); 114 } 115 116 return request.calculateNewLocationString(current); 117 } 118 } 119 | Popular Tags |