1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/*******************************************************************************
* Copyright (c) 2001, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.wsdl.ui.internal.commands;
import java.util.Iterator;
import java.util.List;
import javax.xml.namespace.QName;
import org.eclipse.wst.wsdl.Definition;
import org.eclipse.wst.wsdl.Message;
import org.eclipse.wst.wsdl.Part;
import org.eclipse.wst.wsdl.WSDLElement;
import org.eclipse.wst.wsdl.WSDLFactory;
import org.eclipse.wst.wsdl.XSDSchemaExtensibilityElement;
import org.eclipse.wst.wsdl.internal.impl.PartImpl;
import org.eclipse.wst.wsdl.internal.impl.WSDLElementImpl;
import org.eclipse.wst.wsdl.util.WSDLConstants;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
public final class AddPartCommand extends WSDLElementCommand
{
private Message message;
private String name;
private Part part;
private Part originalPart;
private QName typeOrElementName;
private boolean useType = true;
public AddPartCommand
(Message message,
String name)
{
this.message = message;
this.name = name;
}
public AddPartCommand
(Message message,
String name,
String namespace,
String localName,
boolean useType)
{
this.message = message;
this.name = name;
this.useType = useType;
typeOrElementName = new QName(namespace,localName);
}
/*
* Constructor used to create a Part based on the given Part. In essence, this will
* create a copy of the given Part.
*/
public AddPartCommand
(Message message,
Part originalPart,
String name)
{
this.message = message;
this.name = name;
this.originalPart = originalPart;
}
/**
* @deprecated
*/
public AddPartCommand
(Message message,
String name,
String namespace,
String localName)
{
this.message = message;
this.name = name;
typeOrElementName = new QName(namespace,localName);
}
public void setName(String name)
{
this.name = name;
}
public WSDLElement getWSDLElement()
{
return part;
}
public void run()
{
if (typeOrElementName == null)
typeOrElementName = createDefaultTypeName();
// Add namespace to the Definitions if it is not there yet.
addNamespaceDeclaration();
part = WSDLFactory.eINSTANCE.createPart();
part.setName(name);
if (originalPart == null) {
if (useType)
part.setTypeName(typeOrElementName);
else
part.setElementName(typeOrElementName);
}
else {
if (originalPart.getTypeName() != null)
part.setTypeName(originalPart.getTypeName());
String newElementName = null;
if (originalPart.getElementName() != null) {
boolean createNewElement = true;
if (originalPart.getElementDeclaration() != null && originalPart.getElementDeclaration().getElement() != null) {
if (!(originalPart.getElementDeclaration().getElement() instanceof IDOMNode)) {
createNewElement = false;
}
}
if (createNewElement) {
newElementName = getNewNameHelper(name, originalPart.getEnclosingDefinition(), false);
part.setElementName(new QName(originalPart.getElementName().getNamespaceURI(), newElementName));
if (originalPart.getEnclosingDefinition() != null) {
if (newElementName == null) {
newElementName = getNewNameHelper(name, originalPart.getEnclosingDefinition(), false);
}
AddXSDElementDeclarationCommand elementAction = new AddXSDElementDeclarationCommand(originalPart.getEnclosingDefinition(), newElementName);
elementAction.run();
}
}
else {
part.setElementName(new QName(originalPart.getElementName().getNamespaceURI(), originalPart.getElementName().getLocalPart()));
}
}
}
part.setEnclosingDefinition(message.getEnclosingDefinition());
((PartImpl)part).reconcileReferences(false);
message.addPart(part);
}
private void addNamespaceDeclaration()
{
Definition definition = message.getEnclosingDefinition();
if (!definition.getNamespaces().containsValue(typeOrElementName.getNamespaceURI()))
{
definition.addNamespace("p",typeOrElementName.getNamespaceURI());
((WSDLElementImpl)definition).updateElement(false);
}
}
private QName createDefaultTypeName()
{
QName qname = new QName(WSDLConstants.SCHEMA_FOR_SCHEMA_URI_2001,"string");
return qname;
}
private String getNewNameHelper(String base, Definition def, boolean isType)
{
String name = base;
int count = 0;
// Ugly.... Redo this...
// Get a list of Elements...
List elementList = null;
if (def.getETypes() != null) {
List xsdsList = def.getETypes().getEExtensibilityElements();
if (xsdsList != null) {
Iterator xsdsIterator = xsdsList.iterator();
XSDSchemaExtensibilityElement xsdElement = (XSDSchemaExtensibilityElement) xsdsIterator.next();
XSDSchema schema = xsdElement.getSchema();
if (schema != null) {
elementList = schema.getElementDeclarations();
}
}
}
if (elementList != null) {
int index = 0;
while (index < elementList.size()) {
XSDElementDeclaration elementDeclaration = (XSDElementDeclaration) elementList.get(index);
if (name.equals(elementDeclaration.getName())) {
count++;
name = name + count;
index = 0;
}
else {
index++;
}
}
}
return name;
}
}
|