1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.ontotext.wsmo4j.parser.wsml;
28
29 import java.util.*;
30
31 import org.deri.wsmo4j.common.*;
32 import org.deri.wsmo4j.io.parser.*;
33 import org.wsmo.common.*;
34 import org.wsmo.common.exception.*;
35 import org.wsmo.factory.*;
36 import org.wsmo.mediator.*;
37 import org.wsmo.wsml.*;
38 import org.wsmo.wsml.compiler.node.*;
39
40 import com.ontotext.wsmo4j.parser.*;
41
42 public class MediatorAnalysis extends ASTAnalysis {
43
44 private WsmoFactory factory;
45
46 private ASTAnalysisContainer container;
47
48 private boolean cleanOnParse=false;
49
50 public MediatorAnalysis(ASTAnalysisContainer container, WsmoFactory factory) {
51 if (container == null || factory == null) {
52 throw new IllegalArgumentException();
53 }
54 this.factory = factory;
55 this.container = container;
56
57
58 container.registerNodeHandler(AOomediator.class, this);
59 container.registerNodeHandler(AGgmediator.class, this);
60 container.registerNodeHandler(AWgmediator.class, this);
61 container.registerNodeHandler(AWwmediator.class, this);
62 container.registerNodeHandler(ASource.class, this);
63 container.registerNodeHandler(AMsources.class, this);
64 container.registerNodeHandler(ATarget.class, this);
65 container.registerNodeHandler(AUseService.class, this);
66 }
67
68 public void setCleanOnParse(boolean cleanOnParse){
69 this.cleanOnParse=cleanOnParse;
70 }
71
72 private void cleanMediator(Mediator m){
73
74 try {
75 ClearTopEntity.clearTopEntity(m);
76 }
77 catch (SynchronisationException e) {
78
79 throw new RuntimeException("Error During Cleaning TopEntity from previous defintions",e);
80 }
81 catch (InvalidModelException e) {
82
83 throw new RuntimeException("Error During Cleaning TopEntity from previous defintions",e);
84 }
85
86 }
87
88 public void inAOomediator(AOomediator node) {
89 TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTOomediator());
90 node.getId().apply(container.getNodeHandler(PId.class));
91 IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
92 Mediator mediator = factory.createOOMediator(iri);
93 if (cleanOnParse){
94 cleanMediator(mediator);
95 }
96 container.getStack(Entity.class).push(mediator);
97 container.getStack(TopEntity.class).push(mediator);
98
99 TopEntityAnalysis.addNamespaceAndVariant(mediator, container.getStack(Namespace.class),
100 container.getStack(AWsmlvariant.class));
101
102 }
103
104 public void inAGgmediator(AGgmediator node) {
105 TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTGgmediator());
106 node.getId().apply(container.getNodeHandler(PId.class));
107 IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
108 Mediator mediator = factory.createGGMediator(iri);
109 if (cleanOnParse){
110 cleanMediator(mediator);
111 }
112 container.getStack(Entity.class).push(mediator);
113 container.getStack(TopEntity.class).push(mediator);
114
115 TopEntityAnalysis.addNamespaceAndVariant(mediator, container.getStack(Namespace.class),
116 container.getStack(AWsmlvariant.class));
117 }
118
119 public void inAWgmediator(AWgmediator node) {
120 TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTWgmediator());
121 node.getId().apply(container.getNodeHandler(PId.class));
122 IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
123 Mediator mediator = factory.createWGMediator(iri);
124 if (cleanOnParse){
125 cleanMediator(mediator);
126 }
127 container.getStack(Entity.class).push(mediator);
128 container.getStack(TopEntity.class).push(mediator);
129
130 TopEntityAnalysis.addNamespaceAndVariant(mediator, container.getStack(Namespace.class),
131 container.getStack(AWsmlvariant.class));
132 }
133
134 public void inAWwmediator(AWwmediator node) {
135 TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTWwmediator());
136 node.getId().apply(container.getNodeHandler(PId.class));
137 IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
138 Mediator mediator = factory.createWWMediator(iri);
139 if (cleanOnParse){
140 cleanMediator(mediator);
141 }
142 container.getStack(Entity.class).push(mediator);
143 container.getStack(TopEntity.class).push(mediator);
144
145 TopEntityAnalysis.addNamespaceAndVariant(mediator, container.getStack(Namespace.class),
146 container.getStack(AWsmlvariant.class));
147 }
148
149 public void outAOomediator(AOomediator node) {
150 container.popFromStack(Entity.class, OOMediator.class);
151 }
152
153 public void outAGgmediator(AGgmediator node) {
154 container.popFromStack(Entity.class, GGMediator.class);
155 }
156
157 public void outAWgmediator(AWgmediator node) {
158 container.popFromStack(Entity.class, WGMediator.class);
159 }
160
161 public void outAWwmediator(AWwmediator node) {
162 container.popFromStack(Entity.class, WWMediator.class);
163 }
164
165 public void inASource(ASource node) {
166 TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTSource());
167 }
168
169 public void outASource(ASource node) {
170 if (container.getStack(TopEntity.class).peek() instanceof Mediator == false) {
171
172 container.popFromStack(Identifier.class, IRI.class);
173 return;
174 }
175 Mediator mediator = (Mediator) container.peekFromStack(TopEntity.class, Mediator.class);
176 IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
177 try {
178 mediator.addSource(iri);
179 }
180 catch (InvalidModelException e) {
181 throw new WrappedInvalidModelException(e);
182 }
183 }
184
185 private int idStackSize;
186
187 public void inAMsources(AMsources node) {
188 Stack identifierStack = container.getStack(Identifier.class);
189 idStackSize = identifierStack.size();
190 }
191
192 public void outAMsources(AMsources node) {
193 Mediator mediator = (Mediator) container.peekFromStack(TopEntity.class, Mediator.class);
194 Stack identifierStack = container.getStack(Identifier.class);
195 while (identifierStack.size() > idStackSize) {
196 Identifier id = (Identifier) identifierStack.remove(idStackSize);
197 if (id instanceof IRI == false) {
198 ParserException pe = new ParserException("Anonymous sources "
199 + "are not supported!", null);
200 pe.setExpectedToken("IRI");
201 pe.setFoundToken(id.toString());
202 pe.setErrorLine(node.getLbrace().getLine());
203 pe.setErrorPos(node.getLbrace().getPos());
204 throw new WrappedParsingException(pe);
205 }
206
207 try {
208 mediator.addSource((IRI) id);
209 }
210 catch (InvalidModelException e) {
211 throw new WrappedInvalidModelException(e);
212 }
213 }
214 }
215
216 public void inATarget(ATarget node) {
217 TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTTarget());
218 }
219
220 public void outATarget(ATarget node) {
221 if (container.getStack(TopEntity.class).peek() instanceof Mediator == false) {
222
223 container.popFromStack(Identifier.class, IRI.class);
224 return;
225 }
226 Mediator mediator = (Mediator) container.peekFromStack(TopEntity.class, Mediator.class);
227 IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
228
229 try {
230 mediator.setTarget(iri);
231 }
232 catch (InvalidModelException e) {
233 throw new WrappedInvalidModelException(e);
234 }
235 }
236
237 public void inAUseService(AUseService node) {
238 TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTUseservice());
239 }
240
241 public void outAUseService(AUseService node) {
242 if (container.getStack(TopEntity.class).peek() instanceof Mediator == false) {
243
244 container.popFromStack(Identifier.class, IRI.class);
245 return;
246 }
247 Mediator mediator = (Mediator) container.peekFromStack(TopEntity.class, Mediator.class);
248 IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
249
250 try {
251 mediator.setMediationService(iri);
252 }
253 catch (InvalidModelException e) {
254 throw new WrappedInvalidModelException(e);
255 }
256 }
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299