001: /*
002: * WSDLRefactoringPlugin.java
003: *
004: * Created on February 20, 2007, 2:02 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.netbeans.modules.xml.wsdl.refactoring;
011:
012: import java.io.IOException;
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.Collections;
016: import java.util.Collections;
017: import java.util.HashMap;
018: import java.util.HashSet;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Set;
023: import org.netbeans.modules.refactoring.api.Problem;
024: import org.netbeans.modules.refactoring.api.RefactoringSession;
025: import org.netbeans.modules.refactoring.spi.ProgressProviderAdapter;
026: import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
027: import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
028: import org.netbeans.modules.xml.refactoring.ErrorItem;
029: import org.netbeans.modules.xml.refactoring.XMLRefactoringPlugin;
030: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
031: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
032: import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
033: import org.netbeans.modules.xml.schema.model.SchemaModel;
034: import org.netbeans.modules.xml.wsdl.model.Definitions;
035: import org.netbeans.modules.xml.wsdl.model.Import;
036: import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
037: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
038: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
039: import org.netbeans.modules.xml.wsdl.refactoring.xsd.FindSchemaUsageVisitor;
040: import org.netbeans.modules.xml.wsdl.refactoring.xsd.SchemaUsageRefactoringEngine;
041: import org.netbeans.modules.xml.xam.Component;
042: import org.netbeans.modules.xml.xam.Model;
043: import org.netbeans.modules.xml.xam.ModelSource;
044: import org.netbeans.modules.xml.xam.Referenceable;
045: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
046: import org.openide.ErrorManager;
047: import org.openide.filesystems.FileObject;
048: import org.openide.filesystems.FileUtil;
049:
050: /**
051: *
052: * @author Sonali
053: */
054: public abstract class WSDLRefactoringPlugin extends
055: ProgressProviderAdapter implements RefactoringPlugin,
056: XMLRefactoringPlugin {
057:
058: /**
059: * Creates a new instance of WSDLRefactoringPlugin
060: */
061: List<ErrorItem> findErrors;
062: RefactoringSession session;
063: XMLRefactoringTransaction transaction;
064: public static final String WSDL_MIME_TYPE = "text/x-wsdl+xml";
065:
066: public WSDLRefactoringPlugin() {
067: }
068:
069: public List<WSDLRefactoringElement> find(Referenceable target,
070: Component searchRoot) {
071: if (target instanceof Model) {
072: return findUsages((Model) target, searchRoot);
073: } else if (target instanceof Component) {
074: return findUsages((Component) target, searchRoot);
075: } else {
076: return null;
077: }
078: }
079:
080: public List<WSDLRefactoringElement> findUsages(Model target,
081: Component searchRoot) {
082: List<WSDLRefactoringElement> elements = new ArrayList<WSDLRefactoringElement>();
083: if (target instanceof WSDLModel
084: && searchRoot instanceof Definitions) {
085: Definitions definitions = (Definitions) searchRoot;
086: String namespace = ((WSDLModel) target).getDefinitions()
087: .getTargetNamespace();
088: // UsageGroup ug = new UsageGroup(this, searchRoot.getModel(), (WSDLModel) target);
089: for (Import i : definitions.getImports()) {
090: Model imported = null;
091: if (namespace.equals(i.getNamespace())) {
092: try {
093: imported = i.getImportedWSDLModel();
094: } catch (CatalogModelException ex) {
095: findErrors.add(new ErrorItem(searchRoot, ex
096: .getMessage()));
097: }
098: }
099: if (imported == target) {
100: elements.add(new WSDLRefactoringElement(searchRoot
101: .getModel(), target, i));
102: }
103: }
104: }
105:
106: SchemaUsageRefactoringEngine engine = new SchemaUsageRefactoringEngine();
107: List<WSDLRefactoringElement> elem = engine.findUsages(target,
108: searchRoot);
109: if (elem != null)
110: elements.addAll(elem);
111:
112: if (elements.size() > 0)
113: return elements;
114: else
115: return Collections.emptyList();
116: }
117:
118: public List<WSDLRefactoringElement> findUsages(Component target,
119: Component searchRoot) {
120: List<WSDLRefactoringElement> elements = new ArrayList<WSDLRefactoringElement>();
121: List<WSDLRefactoringElement> temp = null;
122:
123: if (target instanceof ReferenceableWSDLComponent
124: && searchRoot instanceof Definitions) {
125: temp = new FindWSDLUsageVisitor().findUsages(
126: (ReferenceableWSDLComponent) target,
127: (Definitions) searchRoot);
128: if (temp != null && temp.size() > 0)
129: elements.addAll(temp);
130: }
131:
132: if (target instanceof ReferenceableSchemaComponent
133: && searchRoot instanceof Definitions) {
134: temp = new FindSchemaUsageVisitor().findUsages(
135: (ReferenceableSchemaComponent) target,
136: (Definitions) searchRoot, session, transaction);
137: if (temp != null && temp.size() > 0)
138: elements.addAll(temp);
139: }
140:
141: if (elements.size() == 0) {
142: return Collections.emptyList();
143: } else {
144: return elements;
145: }
146: }
147:
148: public List<Model> getModels(List<WSDLRefactoringElement> elements) {
149: List<Model> models = new ArrayList<Model>();
150: for (WSDLRefactoringElement element : elements) {
151: models.add(((Component) element.getLookup().lookup(
152: Component.class)).getModel());
153: }
154: return models;
155: }
156:
157: public Set<Component> getSearchRoots(Referenceable target) {
158: Set<Component> searchRoots = new HashSet<Component>();
159: Set<FileObject> files = SharedUtils.getSearchFiles(target);
160: for (FileObject file : files) {
161: WSDLRefactoringEngine engine = new WSDLRefactoringEngine();
162: try {
163: Component root = engine.getSearchRoot(file);
164: searchRoots.add(root);
165: } catch (IOException ex) {
166: ErrorManager.getDefault().log(
167: ErrorManager.INFORMATIONAL, ex.getMessage());
168: }
169: }
170:
171: return searchRoots;
172: }
173:
174: public Map<Model, Set<RefactoringElementImplementation>> getModelMap(
175: List<RefactoringElementImplementation> elements) {
176: Map<Model, Set<RefactoringElementImplementation>> results = new HashMap<Model, Set<RefactoringElementImplementation>>();
177: for (RefactoringElementImplementation element : elements) {
178: Component comp = element.getLookup()
179: .lookup(Component.class);
180: Model model = null;
181: if (comp instanceof org.netbeans.modules.xml.schema.model.Import) {
182: //special case of embedded schema import statements in WSDLModel
183: //for embedded schema, group the RE impls by Foreign Model
184: SchemaModel mod = (SchemaModel) comp.getModel();
185: if (mod != null) {
186: Component wsdlImport = mod.getSchema()
187: .getForeignParent();
188: if (wsdlImport != null) {
189: model = wsdlImport.getModel();
190: }
191: }
192: } else
193: model = comp.getModel();
194: if (model == null)
195: continue;
196: Set<RefactoringElementImplementation> elementsInModel = results
197: .get(model);
198: if (elementsInModel == null) {
199: elementsInModel = new HashSet<RefactoringElementImplementation>();
200: elementsInModel.add(element);
201: results.put(model, elementsInModel);
202: } else
203: elementsInModel.add(element);
204: }
205: return results;
206: }
207:
208: public boolean isFatal(ErrorItem error) {
209: if (error.getLevel() == ErrorItem.Level.FATAL)
210: return true;
211: else
212: return false;
213: }
214:
215: public Problem processErrors(List<ErrorItem> errorItems) {
216:
217: if (errorItems == null || errorItems.size() == 0) {
218: return null;
219: }
220: Problem parent = null;
221: Problem child = null;
222: Problem head = null;
223: Iterator<ErrorItem> iterator = errorItems.iterator();
224:
225: while (iterator.hasNext()) {
226: ErrorItem error = iterator.next();
227: if (parent == null) {
228: parent = new Problem(isFatal(error), error.getMessage());
229: child = parent;
230: head = parent;
231: continue;
232: }
233: child = new Problem(isFatal(error), error.getMessage());
234: parent.setNext(child);
235: parent = child;
236:
237: }
238:
239: return head;
240: }
241:
242: public String getModelReference(Component component) {
243: if (component instanceof Import) {
244: return ((Import) component).getLocation();
245: }
246: return null;
247: }
248:
249: public void setModelReference(Component component, String location) {
250: //do nothing
251: }
252:
253: public Collection<Component> getExternalReferences(Model model) {
254: Collection<Component> refs = new ArrayList<Component>();
255: if (model instanceof WSDLModel) {
256: refs.addAll(((WSDLModel) model).getDefinitions()
257: .getImports());
258: }
259: return refs;
260: }
261:
262: public Model getModel(ModelSource source) {
263: FileObject fo = source.getLookup().lookup(FileObject.class);
264: if (WSDL_MIME_TYPE.equals(FileUtil.getMIMEType(fo))) {
265: WSDLModel model = WSDLModelFactory.getDefault().getModel(
266: source);
267: return model;
268: }
269: return null;
270: }
271: }
|