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.io.*;
28  import java.util.*;
29  
30  import javax.xml.parsers.*;
31  
32  import org.eclipse.jface.dialogs.MessageDialog;
33  import org.eclipse.jface.viewers.StructuredSelection;
34  import org.eclipse.swt.dnd.DropTargetEvent;
35  import org.w3c.dom.*;
36  import org.wsmo.grounding.sawsdl.SAWSDL;
37  import org.wsmostudio.grounding.sawsdl.ui.UiPlugin;
38  import org.wsmostudio.grounding.sawsdl.ui.WSDLElementChooser;
39  import org.wsmostudio.runtime.LogManager;
40  
41  import com.ontotext.wsmo4j.grounding.sawsdl.WSDLUtils;
42  import com.sun.org.apache.xml.internal.serialize.*;
43  
44  public class Utils {
45  
46      public static final String PORT_TYPE_ELEMENT = "portType";
47      public static final String PART_ELEMENT = "part";
48      public static final String FAULT_ELEMENT = "fault";
49      public static final String INTERFACE_ELEMENT = "interface";
50      public static final String OPERATION_ELEMENT = "operation";
51      public static final String ELEMENT_ELEMENT = "element";
52      public static final String ATTRIBUTE_ELEMENT = "attribute";
53      public static final String SIMPLE_TYPE_ELEMENT = "simpleType";
54      public static final String COMPLEX_TYPE_ELEMENT = "complexType";
55      public static final String REF_ATTRIBUTE = "modelReference";
56      public static final String LOWERING_ATTRIBUTE = "loweringSchemaMapping";
57      public static final String LIFTING_ATTRIBUTE = "liftingSchemaMapping";
58  
59      public static final String ATTR_EXTENSIONS = "attrExtensions";
60  
61      public static String getLocalName(Node el) {
62          String result = (el.getLocalName() != null) ? el.getLocalName() : el.getNodeName();
63          if (result.indexOf(':') != -1) {
64              return result.substring(result.lastIndexOf(':') + 1);
65          }
66          return result;
67      }
68      
69      public static Attr getLoweringAttribute(Element element, String sawsdlPrefix) {
70          return getNamedAttribute(element, LOWERING_ATTRIBUTE,  sawsdlPrefix);
71      }
72      public static Attr getLiftingAttribute(Element element, String sawsdlPrefix) {
73          return getNamedAttribute(element, LIFTING_ATTRIBUTE,  sawsdlPrefix);
74      }
75      public static Attr getReferenceAttribute(Element element, String sawsdlPrefix) {
76          return getNamedAttribute(element, REF_ATTRIBUTE,  sawsdlPrefix);
77      }
78      
79      private static Attr getNamedAttribute(Element element, String attrName,  String sawsdlPrefix) {
80          
81          NamedNodeMap attrs = element.getAttributes();
82          if (attrs == null) {
83              return null;
84          }
85          
86          Attr targetAttr = null;
87          for(int i = 0; i < attrs.getLength(); i++) {
88              Attr attr = (Attr)attrs.item(i);
89              if (WSDLUtils.matchName(attr.getName(), 
90                      sawsdlPrefix, 
91                      attrName)) {
92                  targetAttr = attr;
93                  break;
94              }
95          }
96          return targetAttr;
97      }
98  
99      public static String addExtInfo(String input, Element el, WSDLAnnotationManager manager) {
100         
101     	if (false == isSupportedElement(el, manager)) {
102     		return input;
103     	}
104     	
105         NamedNodeMap attrs = el.getAttributes();
106         if (attrs == null || attrs.getLength() == 0) {
107             return input;
108         }
109         if (manager != null) {
110             String attrName = (manager.getSAWSDLPrefix().equals("")) ?
111                     REF_ATTRIBUTE
112                     : manager.getSAWSDLPrefix() + ':' + REF_ATTRIBUTE;
113             for (int i = 0; i < attrs.getLength(); i++) {
114                 if (attrs.item(i).getNodeName().equals(attrName)) {
115                     return input + " [=> " 
116                             + preprocessIRIList(attrs.item(i).getNodeValue()) 
117                             + "]";
118                 }
119             }
120             return input;
121         }
122         for (int i = 0; i < attrs.getLength(); i++) {
123             if (getLocalName(attrs.item(i)).equalsIgnoreCase(REF_ATTRIBUTE)) {
124                 return input + " [=> " 
125                         + preprocessIRIList(attrs.item(i).getNodeValue()) 
126                         + "]";
127             }
128         }
129         return input;
130     }
131     
132     private static String preprocessIRIList(String dataAll) {
133         if (false == UiPlugin.isShowingLocalNames()) {
134             return dataAll;
135         }
136         List<String> iris = WSDLUtils.splitIRIsFromString(dataAll);
137         if(iris.size() == 0) {
138             return dataAll;
139         }
140         StringBuffer result = new StringBuffer();
141         for (int i = 0; i < iris.size(); i++) {
142             if (i > 0) {
143                 result.append("  => ");
144             }
145             result.append(org.wsmostudio.ui.Utils.getEntityLocalName(iris.get(i), false, true, null));
146         }
147         return result.toString();
148     }
149     
150     public static boolean checkConceptTargetSelection(DropTargetEvent event, WSDLAnnotationManager manager) {
151         if (event.item == null) {
152             return false;
153         }
154         Object data = event.item.getData();
155         if (data == null 
156                 || false == data instanceof Element) {
157             return false;
158         }
159         return isSupportedElement((Element)data, manager);
160     }
161     
162     public static boolean isSupportedElement(Element el, WSDLAnnotationManager manager) {
163         
164     	String tag = el.getNodeName();
165     	// checking wsdl elements
166         if ((WSDLUtils.matchName(tag, manager.getWSDLPrefix(), OPERATION_ELEMENT) 
167                     && WSDLUtils.matchName(el.getParentNode().getNodeName(), manager.getWSDLPrefix(), INTERFACE_ELEMENT))
168                 || WSDLUtils.matchName(tag, manager.getWSDLPrefix(), INTERFACE_ELEMENT)) {
169             return true;
170         }
171         if (WSDLUtils.matchName(tag, manager.getWSDLPrefix(), FAULT_ELEMENT) 
172                 && WSDLUtils.matchName(el.getParentNode().getNodeName(), manager.getWSDLPrefix(), INTERFACE_ELEMENT)) {
173             return true;
174         }
175         
176         // checking XSD elements
177         if (WSDLUtils.matchName(tag, manager.getXSDPrefix(), ELEMENT_ELEMENT)
178                 || WSDLUtils.matchName(tag, manager.getXSDPrefix(), COMPLEX_TYPE_ELEMENT)
179                 || WSDLUtils.matchName(tag, manager.getXSDPrefix(), SIMPLE_TYPE_ELEMENT)
180                 || WSDLUtils.matchName(tag, manager.getXSDPrefix(), ATTRIBUTE_ELEMENT)
181                 ) {
182             return true;
183         }
184         // WSDL 1.1 support:
185         if ((UiPlugin.hasWSDL11Support() && Utils.isWSDL11(el.getOwnerDocument()))
186         		&& (WSDLUtils.matchName(tag, manager.getWSDLPrefix(), PART_ELEMENT)
187                     || WSDLUtils.matchName(tag, manager.getSAWSDLPrefix(), ATTR_EXTENSIONS)
188                     || WSDLUtils.matchName(tag, manager.getWSDLPrefix(), PORT_TYPE_ELEMENT)
189                     || (WSDLUtils.matchName(tag, manager.getWSDLPrefix(), OPERATION_ELEMENT) 
190                             && WSDLUtils.matchName(el.getParentNode().getNodeName(), manager.getWSDLPrefix(), PORT_TYPE_ELEMENT)))) {
191         	return true;
192         }
193         return false;
194     }
195     
196     public static boolean isSupportingSchema(Element el, String sawsdlPrefix, String wsdlPrefix) {
197         return WSDLUtils.matchName(el.getNodeName(), sawsdlPrefix, ELEMENT_ELEMENT)
198                 || WSDLUtils.matchName(el.getNodeName(), sawsdlPrefix, COMPLEX_TYPE_ELEMENT)
199                 || WSDLUtils.matchName(el.getNodeName(), sawsdlPrefix, SIMPLE_TYPE_ELEMENT)
200                 || (UiPlugin.hasWSDL11Support() && WSDLUtils.matchName(el.getNodeName(), wsdlPrefix, PART_ELEMENT));
201     }
202 
203     public static void annotateWithModelRef(Element el, String ref, WSDLSEditor viewer) {
204         annotateElement(el, REF_ATTRIBUTE, ref, viewer);
205     }
206     public static void annotateWithLoweringRef(Element el, String ref, WSDLSEditor viewer) {
207         annotateElement(el, LOWERING_ATTRIBUTE, ref, viewer);
208     }
209     public static void annotateWithLiftingRef(Element el, String ref, WSDLSEditor viewer) {
210         annotateElement(el, LIFTING_ATTRIBUTE, ref, viewer);
211     }
212     
213     private static void annotateElement(Element el, String attrName, String ref, WSDLSEditor viewer) {
214         
215         WSDLAnnotationManager manager = viewer.getAnnotationManager(); 
216         String sawsdlPrefix = manager.getSAWSDLPrefix();
217         if (sawsdlPrefix.length() > 0) {
218             el.setAttributeNS(WSDLAnnotationManager.SAWSDL_NS, 
219                               sawsdlPrefix + ':' + attrName, ref);
220         }
221         else {
222             el.setAttribute(attrName, ref);
223         }
224         viewer.setDirty(true);
225         viewer.getUITree().refresh(el.getParentNode(), true);
226         viewer.getUITree().setSelection(new StructuredSelection(el));
227         viewer.getMappingsView().selectItem(el);
228     }
229 
230     public static void saveContent(WSDLSEditor viewer, String targetLocation) {
231         
232         if (targetLocation == null) {
233             return;
234         }
235         Document doc = viewer.getModel();
236         FileOutputStream outFile = null;
237         
238         try {
239             outFile = new FileOutputStream(targetLocation);
240             
241             OutputFormat of = new OutputFormat("XML", "UTF-8", true);
242             XMLSerializer serializer = new XMLSerializer(outFile, of);
243             serializer.asDOMSerializer();
244             serializer.serialize(doc);
245             outFile.flush();
246             outFile.close();
247             viewer.setDirty(false);
248         }
249         catch(Exception ioEx) {
250             MessageDialog.openError(viewer.getSite().getShell(),
251                     "Error Writing WSDLS File",
252                     ioEx.getMessage());
253             LogManager.logError(ioEx);
254         }
255         finally {
256             try {
257                 if (outFile != null) {
258                     outFile.close();
259                 }
260             }
261             catch(IOException ioe) {
262                 LogManager.logError(ioe);
263             }
264         }
265     }
266     
267     public static Document loadContent(WSDLSEditor viewer, String fileName) {
268         Document doc = null;
269         try {
270             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
271             factory.setNamespaceAware(true);
272             factory.setIgnoringElementContentWhitespace(true);
273 
274             DocumentBuilder builder = factory.newDocumentBuilder();
275             doc = builder.parse(new File(fileName));
276         }
277         catch(Exception ex) {
278             MessageDialog.openError(viewer.getSite().getShell(),
279                     "Error Opening WSDL",
280                     ex.getMessage());
281             LogManager.logError(ex);
282         }
283         if (doc == null) {
284             return null;
285         }
286         return doc;
287     }
288     
289     public static List<String> retrieveReferences(
290             Element element, 
291             String sawsdlPrefix) {
292         
293         Attr targetAttr = getReferenceAttribute(element, sawsdlPrefix);
294         if (targetAttr == null) {
295             return Collections.EMPTY_LIST;
296         }
297         return WSDLUtils.splitIRIsFromString(targetAttr.getValue());
298     }
299 
300     public static boolean isWSDL11(Document doc) {
301         return doc.getDocumentElement().getTagName().endsWith("definitions");
302     }
303     
304     public static Element getAttrExtensionsElement(Element element, String sawsdlPrefix) {
305         Element attrExtensions = null;
306         NodeList children = element.getChildNodes();
307         for(int i = 0; i < children.getLength(); i++) {
308             if (children.item(i).getNodeType() == Node.ELEMENT_NODE
309                 && WSDLUtils.matchName(children.item(i).getNodeName(), 
310                                        sawsdlPrefix, 
311                                        ATTR_EXTENSIONS)) {
312                 attrExtensions = (Element)children.item(i);
313                 break;
314             }
315         }
316         if (attrExtensions == null) {
317             attrExtensions = 
318                 element.getOwnerDocument().createElementNS(
319                         SAWSDL.SAWSDL_NS_URI, 
320                         (sawsdlPrefix != null && sawsdlPrefix.length() > 0) 
321                             ? sawsdlPrefix + ":" + ATTR_EXTENSIONS
322                             : ATTR_EXTENSIONS);
323             if (element.getFirstChild() != null) {
324                 element.insertBefore(attrExtensions, element.getFirstChild());
325             }
326             else {
327                 element.appendChild(attrExtensions);
328             }
329         }
330         return attrExtensions;
331     }
332 
333 }
334 
335 /*
336  * $Log$
337  * Revision 1.10  2007/07/19 12:54:08  alex_simov
338  * refactoring in WSMO UI plug-in
339  *
340  * Revision 1.9  2007/05/03 09:42:00  alex_simov
341  * WSDL 11 support improved with attrExtensions for operations
342  *
343  * Revision 1.8  2007/04/25 16:53:38  alex_simov
344  * no message
345  *
346  * Revision 1.7  2007/04/23 17:34:22  alex_simov
347  * SAWSDL namespace automatic replacement fix
348  *
349  * Revision 1.6  2006/10/12 16:25:27  alex_simov
350  * 1) multiple URI references support added
351  * 2) basic lowering/lifting schema support added
352  *
353  * Revision 1.5  2006/10/04 14:27:39  alex_simov
354  * interface fault support added
355  *
356  * Revision 1.4  2006/07/19 11:27:40  alex_simov
357  * WSDL 1.1 annotation support added (this option is controlled by preference
358  *  pages)
359  *
360  * Revision 1.3  2006/07/07 13:02:25  alex_simov
361  * commented code removed
362  *
363  * Revision 1.2  2006/07/06 15:27:18  alex_simov
364  * xml parser's input is now File instead of String (path)
365  *
366  * Revision 1.1  2006/07/05 15:37:29  alex_simov
367  * no message
368  *
369  * Revision 1.5  2006/07/03 15:43:16  alex_simov
370  * no message
371  *
372  * Revision 1.4  2006/06/29 15:29:41  alex_simov
373  * no message
374  *
375  * Revision 1.3  2006/06/23 14:48:34  alex_simov
376  * DOM factory is now namespaces aware
377  *
378  * Revision 1.2  2006/06/22 11:51:17  alex_simov
379  * no message
380  *
381  * Revision 1.1  2006/06/21 12:55:13  alex_simov
382  * no message
383  *
384  */