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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228