1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: Ontotext Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.grounding.sawsdl.ui.editor;
26
27 import java.util.*;
28
29 import javax.xml.XMLConstants;
30
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.swt.dnd.DropTargetEvent;
33 import org.w3c.dom.*;
34 import org.wsmo.common.Entity;
35
36 public class WSDLAnnotationManager {
37
38 public static final String SAWSDL_NS_OLD = "http://www.w3.org/2002/ws/sawsdl/spec/sawsdl#";
39 public static final String WSDL_NS_OLD = "http://www.w3.org/2006/01/wsdl";
40
41 public static final String SAWSDL_NS = "http://www.w3.org/ns/sawsdl";
42 public static final String WSDL_NS = "http://www.w3.org/ns/wsdl";
43
44 public static final String DEFAULT_SAWSDL_PREFIX = "sawsdl";
45 public static final String DEFAULT_XSD_PREFIX = "xs";
46 public static final String DEFAULT_WSDL_PREFIX = "wsdl";
47
48 private String sawsdlPrefix, xsdPrefix, wsdlPrefix;
49 private Map<String, String> nsMap;
50
51 private WSDLSEditor viewer;
52 private boolean initDirtyState = false;
53
54 public WSDLAnnotationManager(WSDLSEditor editor, Document input) {
55 this.viewer = editor;
56 this.nsMap = new LinkedHashMap<String, String>();
57 readNSInfo(input.getDocumentElement());
58 }
59
60 public void dispose() {
61 this.viewer = null;
62 }
63
64 public void performMapping(Entity wsmoObject, Object wsdlObject, DropTargetEvent e) {
65 if (false == wsdlObject instanceof Element) {
66 return;
67 }
68 ViewActionHandler.performAddReference(
69 (Element)wsdlObject,
70 wsmoObject.getIdentifier().toString(),
71 this.viewer);
72 }
73
74 public String getSAWSDLPrefix() {
75 return this.sawsdlPrefix;
76 }
77 public String getXSDPrefix() {
78 return this.xsdPrefix;
79 }
80 public String getWSDLPrefix() {
81 return this.wsdlPrefix;
82 }
83
84 private void readNSInfo(Element nsHolder) {
85 nsMap.clear();
86 sawsdlPrefix = null;
87 xsdPrefix = null;
88 wsdlPrefix = null;
89
90 String oldSAWSDLNSDef = null;
91 String oldWSDLNSDef = null;
92
93 NamedNodeMap attrs = nsHolder.getAttributes();
94 for(int i = 0; i < attrs.getLength(); i++) {
95 Attr attr = (Attr)attrs.item(i);
96 String attrName = attr.getName();
97 String attrValue = attr.getValue();
98 String prefix = null;
99 if (attrName.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
100 prefix = "";
101 }
102 else if (attrName.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ':')) {
103 prefix = attrName.substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1);
104 }
105 else {
106 continue;
107 }
108 nsMap.put(prefix, attrValue);
109 if (attrValue.equals(SAWSDL_NS)) {
110 sawsdlPrefix = prefix;
111 }
112 if (attrValue.equals(WSDL_NS)) {
113 wsdlPrefix = prefix;
114 }
115 if (attrValue.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
116 xsdPrefix = prefix;
117 }
118 }
119 attrs = nsHolder.getAttributes();
120 for(int i = 0; i < attrs.getLength(); i++) {
121 Attr attr = (Attr)attrs.item(i);
122 String attrName = attr.getName();
123 String attrValue = attr.getValue();
124 String prefix = null;
125 if (attrName.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
126 prefix = "";
127 }
128 else if (attrName.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ':')) {
129 prefix = attrName.substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1);
130 }
131 else {
132 continue;
133 }
134 if (sawsdlPrefix == null
135 && attrValue.equals(SAWSDL_NS_OLD)) {
136 sawsdlPrefix = prefix;
137 if (prefix.equals("")) {
138 nsHolder.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
139 XMLConstants.XMLNS_ATTRIBUTE,
140 SAWSDL_NS);
141 }
142 else {
143 nsHolder.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
144 XMLConstants.XMLNS_ATTRIBUTE + ':' + sawsdlPrefix,
145 SAWSDL_NS);
146 }
147 oldSAWSDLNSDef = attrValue;
148 }
149 if (wsdlPrefix == null
150 && attrValue.equals(WSDL_NS_OLD)) {
151 wsdlPrefix = prefix;
152 if (prefix.equals("")) {
153 nsHolder.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
154 XMLConstants.XMLNS_ATTRIBUTE,
155 WSDL_NS);
156 }
157 else {
158 nsHolder.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
159 XMLConstants.XMLNS_ATTRIBUTE + ':' + wsdlPrefix,
160 WSDL_NS);
161 }
162 oldWSDLNSDef = attrValue;
163 }
164
165 }
166 if (sawsdlPrefix == null) {
167 sawsdlPrefix = DEFAULT_SAWSDL_PREFIX;
168 nsHolder.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
169 XMLConstants.XMLNS_ATTRIBUTE + ':' + sawsdlPrefix,
170 SAWSDL_NS);
171 nsMap.put(sawsdlPrefix, SAWSDL_NS);
172 }
173 if (xsdPrefix == null) {
174 xsdPrefix = DEFAULT_XSD_PREFIX;
175 nsHolder.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
176 XMLConstants.XMLNS_ATTRIBUTE + ':' + xsdPrefix,
177 XMLConstants.W3C_XML_SCHEMA_NS_URI);
178 nsMap.put(xsdPrefix, XMLConstants.W3C_XML_SCHEMA_NS_URI);
179 }
180 if (wsdlPrefix == null) {
181 wsdlPrefix = DEFAULT_WSDL_PREFIX;
182 nsHolder.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
183 XMLConstants.XMLNS_ATTRIBUTE + ':' + wsdlPrefix,
184 WSDL_NS);
185 nsMap.put(wsdlPrefix, WSDL_NS);
186 }
187
188 if (oldSAWSDLNSDef != null) {
189 MessageDialog.openWarning(viewer.getSite().getShell(),
190 "Obsolete SAWSDL Namespace",
191 "The selected document uses an obsolete SAWSDL namespace ("
192 + oldSAWSDLNSDef
193 + ") which has been REPLACED by the latest one ("
194 + SAWSDL_NS +")");
195 initDirtyState = true;
196 translateSAWSDLNamespaceURIs(nsHolder);
197 }
198 if (oldWSDLNSDef != null) {
199 MessageDialog.openWarning(viewer.getSite().getShell(),
200 "Obsolete WSDL Namespace",
201 "The selected document uses an obsolete WSDL namespace ("
202 + oldWSDLNSDef
203 + ") which has been REPLACED by the latest one ("
204 + WSDL_NS +")");
205 initDirtyState = true;
206 }
207
208 }
209 public boolean getInitialDirtyState() {
210 return this.initDirtyState;
211 }
212
213 private void translateSAWSDLNamespaceURIs(Element el) {
214 NamedNodeMap attrs = el.getAttributes();
215 for(int i = 0; i < attrs.getLength(); i++) {
216 Attr attr = (Attr)attrs.item(i);
217 if ((attr.getLocalName().equals(Utils.REF_ATTRIBUTE)
218 || attr.getLocalName().equals(Utils.LIFTING_ATTRIBUTE)
219 || attr.getLocalName().equals(Utils.LOWERING_ATTRIBUTE))
220 && SAWSDL_NS_OLD.equals(attr.getNamespaceURI())) {
221 String value = attr.getValue();
222 el.removeAttributeNode(attr);
223 el.setAttributeNS(SAWSDL_NS,
224 (sawsdlPrefix == null || sawsdlPrefix.equals(""))
225 ? attr.getLocalName()
226 : sawsdlPrefix + ":" + attr.getLocalName(),
227 value);
228 }
229 }
230 NodeList children = el.getChildNodes();
231 for(int i = 0; i < children.getLength(); i++) {
232 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
233 translateSAWSDLNamespaceURIs((Element)children.item(i));
234 }
235 }
236
237 }
238
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270