~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/wsdl/refactoring/src/org/netbeans/modules/xml/wsdl/refactoring/WSDLRenameReferenceVisitor.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.modules.xml.wsdl.refactoring;
 
42
 
 
43
import java.io.IOException;
 
44
import java.util.List;
 
45
import java.util.Set;
 
46
import org.netbeans.modules.refactoring.api.RenameRefactoring;
 
47
import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
 
48
import org.netbeans.modules.xml.wsdl.model.Binding;
 
49
import org.netbeans.modules.xml.wsdl.model.BindingFault;
 
50
import org.netbeans.modules.xml.wsdl.model.BindingInput;
 
51
import org.netbeans.modules.xml.wsdl.model.BindingOperation;
 
52
import org.netbeans.modules.xml.wsdl.model.BindingOutput;
 
53
import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
 
54
import org.netbeans.modules.xml.wsdl.model.Fault;
 
55
import org.netbeans.modules.xml.wsdl.model.Input;
 
56
import org.netbeans.modules.xml.wsdl.model.Message;
 
57
import org.netbeans.modules.xml.wsdl.model.Operation;
 
58
import org.netbeans.modules.xml.wsdl.model.Output;
 
59
import org.netbeans.modules.xml.wsdl.model.Part;
 
60
import org.netbeans.modules.xml.wsdl.model.Port;
 
61
import org.netbeans.modules.xml.wsdl.model.PortType;
 
62
import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
 
63
import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
 
64
import org.netbeans.modules.xml.wsdl.model.WSDLModel;
 
65
import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPAddress;
 
66
import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBinding;
 
67
import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBody;
 
68
import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPComponent;
 
69
import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPFault;
 
70
import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPHeader;
 
71
import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPHeaderFault;
 
72
import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPOperation;
 
73
import org.netbeans.modules.xml.wsdl.model.visitor.DefaultVisitor;
 
74
import org.netbeans.modules.xml.wsdl.model.visitor.WSDLVisitor;
 
75
import org.netbeans.modules.xml.xam.Model;
 
76
import org.netbeans.modules.xml.xam.Referenceable;
 
77
import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
 
78
 
 
79
/**
 
80
 *
 
81
 * @author Nam Nguyen
 
82
 */
 
83
public class WSDLRenameReferenceVisitor extends DefaultVisitor implements WSDLVisitor {
 
84
    RenameRefactoring request;
 
85
    String oldName;
 
86
    Referenceable target;
 
87
    
 
88
    /** Creates a new instance of WSDLRenameRefactorVisitor */
 
89
    public WSDLRenameReferenceVisitor() {
 
90
    }
 
91
    
 
92
    public void refactor(Model mod, Set<RefactoringElementImplementation> elements, RenameRefactoring request) throws IOException {
 
93
        if (request == null || elements == null || mod == null) return;
 
94
        if (! (mod instanceof WSDLModel)) return;
 
95
        
 
96
        this.request = request;
 
97
        this.target = request.getRefactoringSource().lookup(Referenceable.class);
 
98
        this.oldName = request.getContext().lookup(String.class);
 
99
        WSDLModel model = (WSDLModel) mod;
 
100
        boolean startTransaction = ! model.isIntransaction();
 
101
        try {
 
102
            if (startTransaction) {
 
103
                model.startTransaction();
 
104
            }
 
105
            for (RefactoringElementImplementation element: elements) {
 
106
                assert element.getLookup().lookup(WSDLComponent.class)!=null : "Wrong component type in WSDL usage group"; //NOI18N
 
107
                element.getLookup().lookup(WSDLComponent.class).accept(this);
 
108
            }
 
109
        } finally {
 
110
            if (startTransaction && model.isIntransaction())
 
111
                model.endTransaction();
 
112
        }
 
113
    }
 
114
    
 
115
    private <T extends ReferenceableWSDLComponent> NamedComponentReference<T>
 
116
            createReference(Class<T> type, WSDLComponent referencing) {
 
117
        T referenced = type.cast(target);
 
118
        return referencing.createReferenceTo(referenced, type);
 
119
    }
 
120
    
 
121
    public void visit(BindingOperation referencing) {
 
122
        assert target instanceof Operation : "Invalid type, expect Operation"; //NOI18N
 
123
        referencing.setName(request.getNewName());
 
124
    }
 
125
    
 
126
    public void visit(Input referencing) {
 
127
        referencing.setMessage(createReference(Message.class, referencing));
 
128
    }
 
129
    
 
130
    public void visit(Output referencing) {
 
131
        referencing.setMessage(createReference(Message.class, referencing));
 
132
    }
 
133
    
 
134
    public void visit(Fault referencing) {
 
135
        referencing.setMessage(createReference(Message.class, referencing));
 
136
    }
 
137
    
 
138
    public void visit(Port referencing) {
 
139
        referencing.setBinding(createReference(Binding.class, referencing));
 
140
    }
 
141
    
 
142
    private boolean isOverloaded(BindingOperation bindingOperation) {
 
143
        assert ! bindingOperation.getOperation().isBroken() :
 
144
            "Broken operation reference: "+bindingOperation.getName();
 
145
        Operation operation = bindingOperation.getOperation().get();
 
146
        PortType pt = (PortType) operation.getParent();
 
147
        assert pt != null : "Operation not in tree";
 
148
        for (Operation o : pt.getOperations()) {
 
149
            if (o != operation && operation.getName().equals(o.getName())) {
 
150
                return true;
 
151
            }
 
152
        }
 
153
        return false;
 
154
    }
 
155
    
 
156
    public void visit(BindingInput referencing) {
 
157
        if (referencing.getName() != null && referencing.getName().equals(oldName) ||
 
158
                isOverloaded((BindingOperation) referencing.getParent())) {
 
159
            referencing.setName(request.getNewName());
 
160
        }
 
161
    }
 
162
    
 
163
    public void visit(BindingOutput referencing) {
 
164
        if (referencing.getName() != null && referencing.getName().equals(oldName) ||
 
165
                isOverloaded((BindingOperation) referencing.getParent())) {
 
166
            referencing.setName(request.getNewName());
 
167
        }
 
168
    }
 
169
    
 
170
    public void visit(BindingFault referencing) {
 
171
        referencing.setName(request.getNewName());
 
172
    }
 
173
    
 
174
    public void visit(Binding referencing) {
 
175
        referencing.setType(createReference(PortType.class, referencing));
 
176
    }
 
177
 
 
178
    public void visit(ExtensibilityElement referencing) {
 
179
        if (referencing instanceof SOAPComponent) {
 
180
            ((SOAPComponent) referencing).accept(new SOAPReferencingVisitor());
 
181
        }
 
182
    }
 
183
    
 
184
    // SOAPComponent.Visitor
 
185
 
 
186
    public class SOAPReferencingVisitor implements SOAPComponent.Visitor {
 
187
        public void visit(SOAPHeader referencing) {
 
188
            if (target instanceof Message) {
 
189
                referencing.setMessage(createReference(Message.class, referencing));
 
190
            } else if (target instanceof Part) {
 
191
                referencing.setPartRef(createReference(Part.class, referencing));
 
192
            }
 
193
        }
 
194
 
 
195
        public void visit(SOAPFault referencing) {
 
196
            referencing.setFault(createReference(Fault.class, referencing));
 
197
        }
 
198
 
 
199
        public void visit(SOAPHeaderFault referencing) {
 
200
            if (target instanceof Message) {
 
201
                referencing.setMessage(createReference(Message.class, referencing));
 
202
            } else if (target instanceof Part) {
 
203
                referencing.setPartRef(createReference(Part.class, referencing));
 
204
            }
 
205
        }
 
206
        
 
207
        public void visit(SOAPOperation referencing) {
 
208
            // no explicit reference
 
209
        }
 
210
 
 
211
        public void visit(SOAPBinding referencing) {
 
212
            // no explicit reference
 
213
        }
 
214
 
 
215
        public void visit(SOAPBody referencing) {
 
216
            if (target instanceof Part) {
 
217
                List<String> parts = referencing.getParts();
 
218
                int i = parts.indexOf(oldName);
 
219
                parts.remove(i);
 
220
                parts.add(i, request.getNewName());
 
221
                referencing.setParts(parts);
 
222
            }
 
223
        }
 
224
 
 
225
        public void visit(SOAPAddress referencing) {
 
226
            // no explicit reference
 
227
        }
 
228
    }
 
229
}