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.io.parser.*;
32  import org.wsmo.common.*;
33  import org.wsmo.factory.*;
34  import org.wsmo.wsml.*;
35  import org.wsmo.wsml.compiler.node.*;
36  
37  public class TopEntityAnalysis extends ASTAnalysis {
38  
39      private ASTAnalysisContainer container;
40  
41      private WsmoFactory factory;
42  
43      private Stack topEntityStack;
44  
45      public TopEntityAnalysis(ASTAnalysisContainer container, WsmoFactory factory) {
46          if (container == null) {
47              throw new IllegalArgumentException();
48          }
49          this.container = container;
50          this.factory = factory;
51          topEntityStack = container.getStack(TopEntity.class);
52  
53          // register the handled nodes
54          container.registerNodeHandler(AImportsontology.class, this);
55          container.registerNodeHandler(AUsesmediator.class, this);
56          container.registerNodeHandler(AWsmlvariant.class, this);
57      }
58  
59      public void outAImportsontology(AImportsontology node) {
60          if (topEntityStack.isEmpty()) {
61              return;
62          }
63          Identifier[] iris = (Identifier[]) container.popFromStack(Identifier[].class,
64                  Identifier[].class);
65          TopEntity topEntity = (TopEntity) topEntityStack.peek();
66          for (int i = 0; i < iris.length; i++) {
67              topEntity.addOntology(factory.getOntology((IRI) iris[i]));
68          }
69      }
70  
71      public void outAUsesmediator(AUsesmediator node) {
72          if (topEntityStack.isEmpty()) {
73              return;
74          }
75          Identifier[] iris = (Identifier[]) container.popFromStack(Identifier[].class,
76                  Identifier[].class);
77          TopEntity topEntity = (TopEntity) topEntityStack.peek();
78          for (int i = 0; i < iris.length; i++) {
79              if (iris[i] instanceof IRI) {
80                  topEntity.addMediator((IRI) iris[i]);
81                      }
82              else {
83                  ParserException pe = new ParserException("usesMediator could "
84                          + "refer Mediators only by IRI!", null);
85                  pe.setErrorLine(node.getTUsemediator().getLine());
86                  pe.setErrorPos(node.getTUsemediator().getPos());
87                  throw new WrappedParsingException(pe);
88                  }
89              }
90      }
91  
92      public void inAWsmlvariant(AWsmlvariant node) {
93          String wsmlVariant = node.getFullIri().toString().trim();
94          wsmlVariant = wsmlVariant.substring(2, wsmlVariant.length() - 1);
95          container.getStack(AWsmlvariant.class).push(wsmlVariant);
96      }
97  
98      public static void addNamespaceAndVariant(TopEntity entity, Stack namespaces, Stack variant) {
99          for (int i = 0; i < namespaces.size(); i++) {
100             Namespace namespace = (Namespace) namespaces.get(i);
101             if (namespace.getPrefix().equals("_")) {
102                 entity.setDefaultNamespace(namespace);
103             }
104             else {
105                 entity.addNamespace(namespace);
106             }
107         }
108 
109         if (!variant.isEmpty()) {
110             entity.setWsmlVariant((String) variant.peek());
111         }
112     }
113 
114     /**
115      * 
116      * @param id id of top entitiy
117      * @param token: token of parent node of id 
118      * (used for error msg in case id is null)
119      */
120     public static void isValidTopEntityIdentifier(PId id, Token token) {
121         if (id instanceof AIriId == false) {
122             ParserException pe = 
123                 new ParserException("Expected IRI but found Identifier,", null);
124             pe.setExpectedToken("IRI");
125             if (id!=null){
126                 pe.setFoundToken(id.toString());
127             }
128             if (id instanceof AAnonymousId) {
129                 Token t = ((AAnonymousId) id).getAnonymous();
130                 pe.setErrorLine(t.getLine());
131                 pe.setErrorPos(t.getPos());
132             }
133             //in case id is null take end position of parent element
134             if (id == null && token != null){
135                 pe.setErrorLine(token.getLine());
136                 pe.setErrorPos(token.getPos()+token.getText().length());
137             }
138             throw new WrappedParsingException(pe);
139         }
140     }
141 }
142 
143 /*
144  * $Log$
145  * Revision 1.7  2006/11/17 12:02:11  vassil_momtchev
146  * validation of Identifier = IRI is not longer specific to TopEntity types only
147  *
148  * Revision 1.6  2006/04/24 08:04:58  holgerlausen
149  * improved error handling in case of topentities without identifier
150  * moved thomas unit test to "open" package, since it does not break expected behavior, but just document some derivations from the spec
151  *
152  * Revision 1.5  2006/04/05 13:24:45  vassil_momtchev
153  * usesMediator now refer mediators by  IRI instead of handle to object
154  *
155  * Revision 1.4  2006/03/07 10:43:20  vassil_momtchev
156  * parser tries to get one of the 4 kind of mediators in the usesMediator constructions
157  *
158  * Revision 1.3  2006/02/13 09:48:52  vassil_momtchev
159  * the code to handle the topentities identifier validity refactored
160  *
161  * Revision 1.2  2006/02/10 14:37:25  vassil_momtchev
162  * parser addapted to the grammar changes; unused class variables removed;
163  *
164  * Revision 1.1  2005/11/28 13:55:26  vassil_momtchev
165  * AST analyses
166  *
167  */