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 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
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
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
168 }
169 try{
170 e.setErrorPos(Integer.parseInt(le.getMessage().split(",")[1].split("\\]")[0]));
171 }catch (NumberFormatException nfE){
172
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
191
192
193 public List <Object> getWarnings() {
194 throw new UnsupportedOperationException("This method is not implemented for WSML parsing");
195 }
196
197
198
199
200
201 public List <Object> getErrors() {
202 throw new UnsupportedOperationException("This method is not implemented for WSML parsing");
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
229
230
231
232
233
234
235
236