View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, Ontotext Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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; // will ask the user to  migrate to the new NS
91          String oldWSDLNSDef = null; // will ask the user to  migrate to the new NS
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; // do not preceed
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; // do not preceed
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  * $Log$
243  * Revision 1.6  2007/04/25 16:53:38  alex_simov
244  * no message
245  *
246  * Revision 1.5  2007/04/23 17:34:22  alex_simov
247  * SAWSDL namespace automatic replacement fix
248  *
249  * Revision 1.4  2007/04/21 13:50:52  alex_simov
250  * SAWSDL and WSDL namespaces are changed according to the latest
251  * SA-WSDL draft and the system automatically replaces occurences of older
252  * versions, followed by a warning message
253  *
254  * Revision 1.3  2006/10/12 16:25:27  alex_simov
255  * 1) multiple URI references support added
256  * 2) basic lowering/lifting schema support added
257  *
258  * Revision 1.2  2006/07/07 13:02:25  alex_simov
259  * commented code removed
260  *
261  * Revision 1.1  2006/07/05 15:37:29  alex_simov
262  * no message
263  *
264  * Revision 1.2  2006/07/04 13:10:21  alex_simov
265  * invalid namespace prefix used
266  *
267  * Revision 1.1  2006/07/03 15:40:43  alex_simov
268  * refactoring: controller separated from the viewer
269  *
270  */