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.xml.wsdl.refactoring;
042:
043: import java.io.IOException;
044: import java.util.Collections;
045: import java.util.List;
046: import java.util.Set;
047: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
048: import org.netbeans.modules.refactoring.api.MoveRefactoring;
049: import org.netbeans.modules.refactoring.api.RenameRefactoring;
050: import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
051: import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine;
052: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
053: import org.netbeans.modules.xml.refactoring.spi.UIHelper;
054: import org.netbeans.modules.xml.retriever.catalog.Utilities;
055: import org.netbeans.modules.xml.wsdl.model.Definitions;
056: import org.netbeans.modules.xml.wsdl.model.Import;
057: import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
058: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
059: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
060: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
061: import org.netbeans.modules.xml.xam.Component;
062: import org.netbeans.modules.xml.xam.Model;
063: import org.netbeans.modules.xml.xam.ModelSource;
064: import org.netbeans.modules.xml.xam.Referenceable;
065: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
066: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
067: import org.openide.filesystems.FileObject;
068: import org.openide.filesystems.FileUtil;
069: import org.openide.util.NbBundle;
070:
071: /**
072: *
073: * @author Nam Nguyen
074: */
075: public class WSDLRefactoringEngine {//extends RefactoringEngine {
076: public static final String WSDL_MIME_TYPE = "text/x-wsdl+xml"; // NOI18N
077:
078: /** Creates a new instance of WSDLRefactoringEngine */
079: public WSDLRefactoringEngine() {
080: }
081:
082: public Component getSearchRoot(FileObject fo) throws IOException {
083: return getWSDLDefinitions(fo);
084: }
085:
086: public static Definitions getWSDLDefinitions(FileObject fo)
087: throws IOException {
088: if (!WSDL_MIME_TYPE.equals(FileUtil.getMIMEType(fo))) {
089: return null;
090: }
091: ModelSource modelSource = Utilities.getModelSource(fo, true);
092: WSDLModel model = WSDLModelFactory.getDefault().getModel(
093: modelSource);
094: if (model != null) {
095: if (model.getState().equals(Model.State.VALID)) {
096: return model.getDefinitions();
097: } else {
098: String msg = NbBundle.getMessage(
099: WSDLRefactoringEngine.class,
100: "MSG_ModelSourceMalformed", fo.getPath());
101: throw new IOException(msg);
102: }
103: }
104: return null;
105: }
106:
107: public List<WSDLRefactoringElement> findUsages(Component target,
108: Component searchRoot) {
109: if (target instanceof ReferenceableWSDLComponent
110: && searchRoot instanceof Definitions) {
111: return new FindWSDLUsageVisitor().findUsages(
112: (ReferenceableWSDLComponent) target,
113: (Definitions) searchRoot);
114: }
115: return Collections.emptyList();
116: }
117:
118: public void _refactorUsages(Model model,
119: Set<RefactoringElementImplementation> elements,
120: AbstractRefactoring request) {
121: if (request == null || elements == null || model == null)
122: return;
123: if (!(model instanceof WSDLModel))
124: return;
125:
126: boolean startTransaction = !model.isIntransaction();
127: try {
128: if (startTransaction) {
129: model.startTransaction();
130: }
131:
132: for (RefactoringElementImplementation u : elements) {
133: Import ref = u.getLookup().lookup(Import.class);
134: if (ref != null) {
135: String newLocation = ref.getLocation();
136: if (request instanceof RenameRefactoring) {
137: newLocation = SharedUtils
138: .calculateNewLocationString(ref
139: .getLocation(),
140: (RenameRefactoring) request);
141: } else if (request instanceof MoveRefactoring) {
142: try {
143: newLocation = SharedUtils
144: .calculateNewLocationString(model,
145: (MoveRefactoring) request);
146: } catch (Exception io) {
147: //do nothing, let the old schema location remain
148: }
149: }
150: ref.setLocation(newLocation);
151: }
152: }
153: } finally {
154: if (startTransaction && model.isIntransaction()) {
155: model.endTransaction();
156: }
157: }
158: }
159:
160: public String calculateNewLocationString(
161: String currentLocationString, RenameRefactoring request) {
162: StringBuilder sb = new StringBuilder();
163: int i = currentLocationString.lastIndexOf('/');
164: if (i > -1) {
165: sb.append(currentLocationString.substring(0, i + 1));
166: }
167: sb.append(request.getNewName());
168: sb.append("."); //NOI18N
169: Referenceable ref = request.getRefactoringSource().lookup(
170: Referenceable.class);
171: Model model = SharedUtils.getModel(ref);
172: FileObject fo = model.getModelSource().getLookup().lookup(
173: FileObject.class);
174: sb.append(fo.getExt());
175: return sb.toString();
176: }
177:
178: public String getModelReference(Component component) {
179: if (component instanceof Import) {
180: return ((Import) component).getLocation();
181: }
182: return null;
183: }
184:
185: }
|