View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5                             University of Innsbruck, Austria
6   
7    This library is free software; you can redistribute it and/or modify it under
8    the terms of the GNU Lesser General Public License as published by the Free
9    Software Foundation; either version 2.1 of the License, or (at your option)
10   any later version.
11   This library is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14   details.
15   You should have received a copy of the GNU Lesser General Public License along
16   with this library; if not, write to the Free Software Foundation, Inc.,
17   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  
20  /**
21   * <p>Title: WSMO4J</p>
22   * <p>Description: WSMO API and a Reference Implementation</p>
23   * <p>Copyright:  Copyright (c) 2004-2005</p>
24   * <p>Company: OntoText Lab. / SIRMA </p>
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          // register the handled nodes
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          //clean previous contents of this mediator:
74          try {
75              ClearTopEntity.clearTopEntity(m);
76          }
77          catch (SynchronisationException e) {
78              // should never happen
79              throw new RuntimeException("Error During Cleaning TopEntity from previous defintions",e);
80          }
81          catch (InvalidModelException e) {
82              // should never happen
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             // if it's unresolved ppMediator ignore it
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             // if it's unresolved ppMediator ignore it
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             // if it's unresolved ppMediator ignore it
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  * $Log$
261  * Revision 1.12  2006/10/25 07:06:06  vassil_momtchev
262  * ignore source, target and useservice when ppmediator are not resolved
263  *
264  * Revision 1.11  2006/04/24 08:04:58  holgerlausen
265  * improved error handling in case of topentities without identifier
266  * moved thomas unit test to "open" package, since it does not break expected behavior, but just document some derivations from the spec
267  *
268  * Revision 1.10  2006/04/11 16:06:58  holgerlausen
269  * addressed RFE 1468651 ( http://sourceforge.net/tracker/index.php?func=detail&aid=1468651&group_id=113501&atid=665349)
270  * currently the default behaviour of the parser is still as before
271  *
272  * Revision 1.9  2006/03/29 08:51:40  vassil_momtchev
273  * mediator reference source/target by IRI (changed from TopEntity); mediator reference mediationService by IRI (changed from Identifier); new checks to not allow anonymous id added
274  *
275  * Revision 1.8  2006/03/07 13:49:41  alex_simov
276  * source/target possible types corrected according to D.29.02
277  *
278  * Revision 1.7  2006/03/07 13:25:46  vassil_momtchev
279  * parser probes all allowed top entity types for a mediator source/target
280  *
281  * Revision 1.6  2006/02/13 09:48:52  vassil_momtchev
282  * the code to handle the topentities identifier validity refactored
283  *
284  * Revision 1.5  2005/12/07 14:46:10  vassil_momtchev
285  * change method signature outAUserService -> outAUseService (spell error)
286  *
287  * Revision 1.4  2005/12/07 14:14:25  vassil_momtchev
288  * bugfix; multiple mediator sources are not ignored
289  *
290  * Revision 1.3  2005/12/06 15:51:39  alex_simov
291  * System.out.println's removed
292  *
293  * Revision 1.2  2005/12/02 14:33:44  holgerlausen
294  * fixed parsing bug due to type when overwriting method, fixed but related to multiple source of mediators during reparsing
295  *
296  * Revision 1.1  2005/11/28 13:55:26  vassil_momtchev
297  * AST analyses
298  *
299 */