View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  
19  /**
20   * <p>Title: WSMO4J</p>
21   * <p>Description: WSMO API and a Reference Implementation</p>
22   * <p>Copyright:  Copyright (c) 2004-2005</p>
23   * <p>Company: OntoText Lab. / SIRMA </p>
24   */
25  
26  package com.ontotext.wsmo4j.parser.wsml;
27  
28  import java.io.*;
29  import java.util.*;
30  
31  import org.deri.wsmo4j.io.parser.*;
32  import org.deri.wsmo4j.io.parser.wsml.*;
33  import org.wsmo.common.*;
34  import org.wsmo.common.exception.*;
35  import org.wsmo.factory.*;
36  import org.wsmo.validator.ValidationError;
37  import org.wsmo.validator.ValidationWarning;
38  import org.wsmo.wsml.*;
39  import org.wsmo.wsml.compiler.lexer.*;
40  import org.wsmo.wsml.compiler.node.*;
41  
42  public class ParserImpl implements Parser {
43  
44      protected WsmoFactory factory;
45      protected LogicalExpressionFactory LEFactory;
46      protected DataFactory dataFactory;
47      protected boolean cleanOnParse=false;
48      protected boolean memorizeLELook=false;
49  
50      public ParserImpl(Map map) {
51          Object o = map.get(Factory.WSMO_FACTORY);
52          if (o == null || !(o instanceof WsmoFactory)) {
53              o = Factory.createWsmoFactory(new HashMap <String, Object> ());
54          }
55          factory = (WsmoFactory) o;
56          assert (factory != null);
57  
58          o = map.get(Factory.LE_FACTORY);
59          if (o == null || !(o instanceof LogicalExpressionFactory)) {
60              o = Factory.createLogicalExpressionFactory(new HashMap <String, Object> ());
61          }
62          LEFactory = (LogicalExpressionFactory) o;
63          assert (LEFactory != null);
64  
65          o = map.get(Factory.DATA_FACTORY);
66          if (o == null || !(o instanceof DataFactory)) {
67              o = Factory.createDataFactory(new HashMap <String, Object> ());
68          }
69          dataFactory = (DataFactory) o;
70          assert (dataFactory != null);
71          
72          o = map.get(Parser.CLEAR_MODEL);
73          if (o != null && !o.toString().equals("false")){
74              cleanOnParse=true;
75          }
76          
77          o = map.get(Parser.CACHE_LOGICALEXPRESSION_STRING);
78          if (o != null && !o.toString().equals("false")){
79              memorizeLELook=true;
80          }
81      }
82  
83      public TopEntity[] parse(Reader src) throws IOException, ParserException, InvalidModelException {
84          return _parse(src);
85      }
86  
87      public TopEntity[] parse(Reader src, Map options) throws IOException, ParserException,
88              InvalidModelException {
89          throw new UnsupportedOperationException("Use method parse(Reader) instead!");
90      }
91  
92      public TopEntity[] parse(StringBuffer src) throws ParserException, InvalidModelException {
93          try {
94              return _parse(new StringReader(src.toString()));
95          }
96          catch (IOException e) {
97              // should never happens
98              throw new RuntimeException("I/O error occured!", e);
99          }
100     }
101 
102     public TopEntity[] parse(StringBuffer src, Map options) throws ParserException,
103             InvalidModelException {
104         throw new UnsupportedOperationException("Use method parse(StringBuffer) instead!");
105     }
106 
107     protected ASTAnalysisContainer createContainer() {
108         ASTAnalysisContainer container = new ASTAnalysisContainer();
109 
110         new IdentifierAnalysis(container, factory);
111         new OntologyAnalysis(container, factory, dataFactory).setCleanOnParse(cleanOnParse);
112         new MediatorAnalysis(container, factory).setCleanOnParse(cleanOnParse);
113         new NFPAnalysis(container);
114         new ValueAnalysis(container, dataFactory, LEFactory, factory);
115         new TopEntityAnalysis(container, factory);
116         new ServiceAnalysis(container, factory, LEFactory).setCleanOnParse(cleanOnParse);
117         new AxiomAnalysis(container, factory);
118         new VariableAnalysis(container, LEFactory);
119         new AtomicExpressionAnalysis(container, factory,LEFactory);
120         new CompoundExpressionAnalysis(container, LEFactory);
121         return container;
122     }
123 
124     private TopEntity[] _parse(Reader src) throws ParserException,
125     InvalidModelException, IOException {
126         ASTAnalysisContainer container = createContainer();
127 
128         if (memorizeLELook){
129             src = LogExprParserImpl.findLogicalExpressions(
130                 src,container.getStack(String.class));
131         }
132 
133         Lexer lexer = new Lexer(new PushbackReader(src, 16384));
134         org.wsmo.wsml.compiler.parser.Parser parser =
135             new org.wsmo.wsml.compiler.parser.Parser(lexer);
136 
137         try {
138             Start head = parser.parse();
139             head.apply(container);
140             Stack stack = container.getStack(TopEntity.class);
141             TopEntity[] topEntities = new TopEntity[stack.size()];
142             for (int i = 0; i < topEntities.length; i++) {
143                 topEntities[i] = (TopEntity) stack.remove(0);
144             }
145             return topEntities;
146         }
147         catch (org.wsmo.wsml.compiler.parser.ParserException pe) {
148             ParserException e = new ParserException(ParserException.NOT_VALID_PARSETREE,pe);
149             Token t = pe.getToken();
150             e.setErrorLine(t.getLine());
151             e.setErrorPos(t.getPos());
152             e.setFoundToken(t.getText());
153             try{
154                 e.setExpectedToken(pe.getMessage().split("expecting: ")[1].trim());
155             }
156             catch (IndexOutOfBoundsException iobE){
157                 //if error message does not follow usual pattern
158                 e.setExpectedToken(pe.getMessage());
159             }
160             throw e;
161         }
162         catch (org.wsmo.wsml.compiler.lexer.LexerException le) {
163             ParserException e = new ParserException(ParserException.NOT_VALID_PARSETREE, le);
164             try{
165                 e.setErrorLine(Integer.parseInt(le.getMessage().split(",")[0].split("\\[")[1]));
166             }catch (NumberFormatException nfE){
167                 //could not find line so leave default
168             }
169             try{
170                 e.setErrorPos(Integer.parseInt(le.getMessage().split(",")[1].split("\\]")[0]));
171             }catch (NumberFormatException nfE){
172                 //could not find pos so leave default
173             }
174             throw e;
175         }
176         catch (WrappedParsingException wpe) {
177             if (wpe.getWrappedException() instanceof ParserException){
178                 throw (ParserException) wpe.getWrappedException();
179             }
180             throw new RuntimeException(wpe);
181         }
182     }
183 
184     public Set <String> listKeywords() {
185         String[] wsmlTokens = ASTAnalysisContainer.WSML_TOKENS;
186         return new HashSet <String>(Arrays.asList(wsmlTokens));
187     }
188     
189     /*
190      *  (non-Javadoc)
191      * @see org.wsmo.wsml.Parser#getWarnings()
192      */
193     public List <Object> getWarnings() {
194 		throw new UnsupportedOperationException("This method is not implemented for WSML parsing");
195 	}
196     
197     /*
198      *  (non-Javadoc)
199      * @see org.wsmo.wsml.Parser#getErrors()
200      */
201 	public List <Object> getErrors() {
202 		throw new UnsupportedOperationException("This method is not implemented for WSML parsing");
203 	}
204 }
205 
206 /*
207  * $Log$
208  * Revision 1.9  2007/04/02 12:13:28  morcen
209  * Generics support added to wsmo-api, wsmo4j and wsmo-test
210  *
211  * Revision 1.8  2006/11/17 15:07:59  holgerlausen
212  * added an option to the parser to "remember" the original format of logical expressions, added the representation of the original String to LEImpl, and added to the serializer to check for the orginal formating to not not make complex expressions unreadable when serialized.
213  *
214  * Revision 1.7  2006/11/10 11:08:53  nathaliest
215  * added getWarnings() and getErrors() methods to Parser interface, implemented them in the rdf parser implementation and added UnsupportedOperationException to the other parser implementations
216  *
217  * Revision 1.6  2006/06/21 07:46:29  vassil_momtchev
218  * createVariable(String) method moved from WsmoFactory to LogicalExpressionFactory interface
219  *
220  * Revision 1.5  2006/04/11 16:06:58  holgerlausen
221  * addressed RFE 1468651 ( http://sourceforge.net/tracker/index.php?func=detail&aid=1468651&group_id=113501&atid=665349)
222  * currently the default behaviour of the parser is still as before
223  *
224  * Revision 1.4  2006/01/11 13:03:30  marin_dimitrov
225  * common constants moved to Factory
226  *
227  * Revision 1.3  2005/12/14 08:58:56  vassil_momtchev
228  * removed hardcoded wsmo and datafactory impl
229  *
230  * Revision 1.2  2005/12/09 11:28:42  vassil_momtchev
231  * listkeywords method added; returns all known tokens from the grammar
232  *
233  * Revision 1.1  2005/11/28 13:55:26  vassil_momtchev
234  * AST analyses
235  *
236 */