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  
30  import org.eclipse.jface.viewers.*;
31  import org.w3c.dom.*;
32  
33  import com.ontotext.wsmo4j.grounding.sawsdl.WSDLUtils;
34  
35  public class WSDLTreeContentProvider implements ITreeContentProvider {
36  
37      public static final int MESSAGE_MASK = 1;
38      public static final int INTERFACE_MASK = 1 << 1;
39      public static final int BINDING_MASK = 1 << 2;
40      public static final int SERVICE_MASK = 1 << 3;
41      public static final int INPUT_MSG_MASK = 1 << 4;
42      public static final int OUTPUT_MSG_MASK = 1 << 5;
43      public static final int TYPES_MASK = 1 << 6;
44      public static final int EXTENSIONS_MASK = 1 << 7;
45      
46      public static final int ALL_MASK = 0xFFFFFFFF;
47      
48      static final String LIFTING_PREFIX = "Lifting: ";
49      static final String LOWERING_PREFIX = "Lowering: ";
50      
51      private int mask;
52      
53      public WSDLTreeContentProvider() {
54          this.mask = ALL_MASK;
55      }
56  
57      public WSDLTreeContentProvider(int newMask) {
58          this.mask = newMask;
59          if (((mask & INPUT_MSG_MASK) == INPUT_MSG_MASK)
60                  || ((mask & OUTPUT_MSG_MASK) == OUTPUT_MSG_MASK)) {
61              this.mask = this.mask | INTERFACE_MASK;        
62          }
63      }
64  
65      
66      public Object[] getChildren(Object parentElement) {
67          
68          if (parentElement instanceof Document) {
69              return new Object[] { ((Document)parentElement).getDocumentElement() };
70          }
71          
72          if (parentElement instanceof Element) {
73              NodeList childs = ((Element)parentElement).getChildNodes();
74              List<Object> result = new LinkedList<Object>();
75              for (int i = 0; i < childs.getLength(); i++) {
76                  if (isAllowedElement(childs.item(i))) {
77                      result.add(childs.item(i));
78                  }
79              }
80              Attr loweringAttr = getLoweringSchemaAttr(((Element)parentElement));
81              if (loweringAttr != null) {
82                  List<String> allURIs = WSDLUtils.splitIRIsFromString(loweringAttr.getValue());
83                  for(ListIterator<String> it = allURIs.listIterator(allURIs.size()); it.hasPrevious();) {
84                      result.add(0, LOWERING_PREFIX + it.previous());
85                  }
86              }
87              Attr liftingAttr = getLiftingSchemaAttr(((Element)parentElement));
88              if (liftingAttr != null) {
89                  List<String> allURIs = WSDLUtils.splitIRIsFromString(liftingAttr.getValue());
90                  for(ListIterator<String> it = allURIs.listIterator(allURIs.size()); it.hasPrevious();) {
91                      result.add(0, LIFTING_PREFIX + it.previous());
92                  }
93              }
94              return result.toArray();
95          }
96          return null;
97      }
98  
99      public Object getParent(Object element) {
100         return null;
101     }
102 
103     public boolean hasChildren(Object element) {
104         if (element instanceof Document) {
105             return ((Document)element).getDocumentElement() != null;
106         }
107         if (element instanceof Element) {
108             return ((Element)element).getChildNodes().getLength() > 0
109                     || getLoweringSchemaAttr(((Element)element)) != null
110                     || getLiftingSchemaAttr(((Element)element)) != null;
111         }
112         return false;
113     }
114 
115     public Object[] getElements(Object inputElement) {
116         if (inputElement instanceof Document) {
117             return getChildren(inputElement);
118         }
119         return null;
120     }
121 
122     public void dispose() {
123     }
124 
125     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
126     }
127     
128     private boolean accepts(int test) {
129         return (this.mask & test) == test;
130     }
131     
132     private boolean isAllowedElement(Node el) {
133         if (el instanceof Text) {
134             return ((Text)el).getData().trim().length() > 0;
135         }
136         if (false == el instanceof Element) {
137             return false;
138         }
139         String name = Utils.getLocalName((Element)el);
140         
141         if (name.equals("interface")) {
142             return accepts(INTERFACE_MASK);
143         }
144         if (name.equals("binding")) {
145             return accepts(BINDING_MASK);
146         }
147         if (name.equals("service")) {
148             return accepts(SERVICE_MASK);
149         }
150         if (name.equals("input")) {
151             return accepts(INPUT_MSG_MASK);
152         }
153         if (name.equals("output")) {
154             return accepts(OUTPUT_MSG_MASK);
155         }
156         if (name.equals("types")) {
157             return accepts(TYPES_MASK);
158         }
159         if (name.equals("category")
160                 || name.equals("precondition")
161                 || name.equals("effect")) {
162             return accepts(EXTENSIONS_MASK);
163         }
164         return true;
165     }
166     
167     private Attr getLoweringSchemaAttr(Element el) {
168         String name = Utils.getLocalName(el);
169         if (name.equals("element") 
170                 || name.equals("complexType") 
171                 || name.equals("simpleType")
172                 || name.equals("part")) {
173             
174             NamedNodeMap attrs = el.getAttributes();
175             for(int i = 0; i < attrs.getLength(); i++) {
176                 if (Utils.getLocalName(attrs.item(i)).equals(Utils.LOWERING_ATTRIBUTE)) {
177                     return (Attr)attrs.item(i);
178                 }
179             }
180         }
181         return null;
182     }
183     private Attr getLiftingSchemaAttr(Element el) {
184         String name = Utils.getLocalName(el);
185         if (name.equals("element") 
186                 || name.equals("complexType") 
187                 || name.equals("simpleType")) {
188             
189             NamedNodeMap attrs = el.getAttributes();
190             for(int i = 0; i < attrs.getLength(); i++) {
191                 if (Utils.getLocalName(attrs.item(i)).equals(Utils.LIFTING_ATTRIBUTE)) {
192                     return (Attr)attrs.item(i);
193                 }
194             }
195         }
196         return null;
197     }
198 
199 }
200 
201 /*
202  * $Log$
203  * Revision 1.5  2007/05/02 17:05:58  alex_simov
204  * update (muitliple lifting/lowering schema refs)
205  *
206  * Revision 1.4  2007/04/23 17:34:22  alex_simov
207  * SAWSDL namespace automatic replacement fix
208  *
209  * Revision 1.3  2006/10/12 16:25:27  alex_simov
210  * 1) multiple URI references support added
211  * 2) basic lowering/lifting schema support added
212  *
213  * Revision 1.2  2006/07/07 12:58:08  alex_simov
214  * Not empty textual nodes are visualized now
215  *
216  * Revision 1.1  2006/07/05 15:37:29  alex_simov
217  * no message
218  *
219  * Revision 1.3  2006/06/22 11:50:45  alex_simov
220  * no message
221  *
222  * Revision 1.2  2006/06/21 12:55:13  alex_simov
223  * no message
224  *
225  * Revision 1.1  2006/06/06 13:15:28  alex_simov
226  * no message
227  *
228  */