001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.bpel.refactoring;
042:
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.HashSet;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Set;
050: import org.netbeans.modules.bpel.model.api.events.VetoException;
051:
052: import org.openide.ErrorManager;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.loaders.DataObject;
056: import org.openide.loaders.DataObjectNotFoundException;
057: import org.openide.util.Lookup;
058:
059: import org.netbeans.modules.refactoring.api.Problem;
060: import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
061:
062: import org.netbeans.modules.xml.refactoring.ErrorItem;
063: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
064: import org.netbeans.modules.xml.wsdl.model.Definitions;
065: import org.netbeans.modules.xml.wsdl.refactoring.WSDLRefactoringEngine;
066: import org.netbeans.modules.xml.xam.Component;
067: import org.netbeans.modules.xml.xam.Model;
068: import org.netbeans.modules.xml.xam.ModelSource;
069: import org.netbeans.modules.xml.xam.Referenceable;
070: import org.netbeans.modules.xml.refactoring.XMLRefactoringPlugin;
071:
072: import org.netbeans.modules.bpel.core.BPELDataLoader;
073: import org.netbeans.modules.bpel.core.BPELDataObject;
074: import org.netbeans.modules.bpel.model.api.BpelModel;
075: import org.netbeans.modules.bpel.model.spi.BpelModelFactory;
076: import org.netbeans.modules.bpel.model.api.Import;
077: import org.netbeans.modules.bpel.model.api.Process;
078: import static org.netbeans.modules.soa.ui.util.UI.*;
079:
080: /**
081: * @author Vladimir Yaroslavskiy
082: * @version 2007.03.16
083: */
084: abstract class Plugin implements RefactoringPlugin,
085: XMLRefactoringPlugin {
086:
087: protected final Set<Component> getRoots(Referenceable target) {
088: Set<FileObject> files = SharedUtils.getSearchFiles(target);
089: Set<Component> roots = new HashSet<Component>();
090:
091: for (FileObject file : files) {
092: try {
093: Component root = getFileRoot(file);
094: roots.add(root);
095: } catch (IOException e) {
096: ErrorManager.getDefault().log(
097: ErrorManager.INFORMATIONAL, e.getMessage());
098: }
099: }
100: return roots;
101: }
102:
103: protected final List<Element> find(Referenceable target,
104: Component root) {
105: List<Component> componets = new ArrayList<Component>();
106: List<Element> elements = new ArrayList<Element>();
107:
108: if (root == null) {
109: return elements;
110: }
111: if (!(target instanceof Referenceable)) {
112: return elements;
113: }
114: if (root instanceof Process) {
115: BpelVisitor visitor = new BpelVisitor(componets, target);
116: ((Process) root).accept(visitor);
117: } else if (root instanceof Definitions) {
118: WsdlVisitor visitor = new WsdlVisitor(componets, target);
119: ((Definitions) root).accept(visitor);
120: }
121: for (Component componet : componets) {
122: elements.add(new Element(componet));
123: }
124: return elements;
125: }
126:
127: private Component getFileRoot(FileObject file) throws IOException {
128: Component root = WSDLRefactoringEngine.getWSDLDefinitions(file);
129: //out();
130: //out("getRoot: " + file);
131: //out(" root: " + Util.getName(root));
132:
133: if (root != null) {
134: return root;
135: }
136: if (!BPELDataLoader.MIME_TYPE
137: .equals(FileUtil.getMIMEType(file))) {
138: return null;
139: }
140: //out();
141: //out("Find usages");
142: //out(" FileObject: " + file);
143: DataObject dataObject = null;
144:
145: try {
146: dataObject = DataObject.find(file);
147: } catch (DataObjectNotFoundException e) {
148: //out(" DataObject is NULL");
149: return null;
150: }
151: //out(" DataObject: " + dataObject);
152:
153: if (!(dataObject instanceof BPELDataObject)) {
154: //out(" DataObject is not BPELDataObject");
155: return null;
156: }
157: BpelModel model = (BpelModel) ((BPELDataObject) dataObject)
158: .getLookup().lookup(BpelModel.class);
159:
160: if (model == null) {
161: //out(" Bpel model is null");
162: return null;
163: }
164: return model.getProcess();
165: }
166:
167: public Problem preCheck() {
168: return null;
169: }
170:
171: public void cancelRequest() {
172: }
173:
174: protected List<Model> getModels(List<Element> elements) {
175: List<Model> models = new ArrayList<Model>();
176:
177: for (Element element : elements) {
178: models.add((element.getLookup().lookup(Component.class))
179: .getModel());
180: }
181: return models;
182: }
183:
184: protected Problem processErrors(List<ErrorItem> items) {
185: if (items == null || items.size() == 0) {
186: return null;
187: }
188: Problem parent = null;
189: Problem child = null;
190: Problem head = null;
191: Iterator<ErrorItem> iterator = items.iterator();
192:
193: while (iterator.hasNext()) {
194: ErrorItem error = iterator.next();
195:
196: if (parent == null) {
197: parent = new Problem(isFatal(error), error.getMessage());
198: child = parent;
199: head = parent;
200: continue;
201: }
202: child = new Problem(isFatal(error), error.getMessage());
203: parent.setNext(child);
204: parent = child;
205: }
206: return head;
207: }
208:
209: protected boolean isFatal(ErrorItem error) {
210: return error.getLevel() == ErrorItem.Level.FATAL;
211: }
212:
213: public String getModelReference(Component component) {
214: if (component instanceof Import) {
215: return ((Import) component).getLocation();
216: }
217: return null;
218: }
219:
220: public void setModelReference(Component component, String location) {
221: if (component instanceof Import) {
222: try {
223: ((Import) component).setLocation(location);
224: } catch (VetoException ex) {
225: return;
226: }
227: }
228: }
229:
230: public Collection<Component> getExternalReferences(Model model) {
231: return new ArrayList<Component>();
232: }
233:
234: public Model getModel(ModelSource source) {
235: FileObject file = source.getLookup().lookup(FileObject.class);
236:
237: if (BPEL_MIME_TYPE.equals(FileUtil.getMIMEType(file))) {
238: BpelModelFactory factory = (BpelModelFactory) Lookup
239: .getDefault().lookup(BpelModelFactory.class);
240: return factory.getModel(source);
241: }
242: return null;
243: }
244:
245: private static final String BPEL_MIME_TYPE = "x-bpel+xml"; // NOI18N
246: }
|