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 |
0
| public ParserImpl(Map map) {
|
51 |
0
| Object o = map.get(Factory.WSMO_FACTORY);
|
52 |
0
| if (o == null || !(o instanceof WsmoFactory)) {
|
53 |
0
| o = Factory.createWsmoFactory(new HashMap <String, Object> ());
|
54 |
| } |
55 |
0
| factory = (WsmoFactory) o;
|
56 |
0
| assert (factory != null);
|
57 |
| |
58 |
0
| o = map.get(Factory.LE_FACTORY);
|
59 |
0
| if (o == null || !(o instanceof LogicalExpressionFactory)) {
|
60 |
0
| o = Factory.createLogicalExpressionFactory(new HashMap <String, Object> ());
|
61 |
| } |
62 |
0
| LEFactory = (LogicalExpressionFactory) o;
|
63 |
0
| assert (LEFactory != null);
|
64 |
| |
65 |
0
| o = map.get(Factory.DATA_FACTORY);
|
66 |
0
| if (o == null || !(o instanceof DataFactory)) {
|
67 |
0
| o = Factory.createDataFactory(new HashMap <String, Object> ());
|
68 |
| } |
69 |
0
| dataFactory = (DataFactory) o;
|
70 |
0
| assert (dataFactory != null);
|
71 |
| |
72 |
0
| o = map.get(Parser.CLEAR_MODEL);
|
73 |
0
| if (o != null && !o.toString().equals("false")){
|
74 |
0
| cleanOnParse=true;
|
75 |
| } |
76 |
| |
77 |
0
| o = map.get(Parser.CACHE_LOGICALEXPRESSION_STRING);
|
78 |
0
| if (o != null && !o.toString().equals("false")){
|
79 |
0
| memorizeLELook=true;
|
80 |
| } |
81 |
| } |
82 |
| |
83 |
0
| public TopEntity[] parse(Reader src) throws IOException, ParserException, InvalidModelException {
|
84 |
0
| return _parse(src);
|
85 |
| } |
86 |
| |
87 |
0
| public TopEntity[] parse(Reader src, Map options) throws IOException, ParserException,
|
88 |
| InvalidModelException { |
89 |
0
| throw new UnsupportedOperationException("Use method parse(Reader) instead!");
|
90 |
| } |
91 |
| |
92 |
0
| public TopEntity[] parse(StringBuffer src) throws ParserException, InvalidModelException {
|
93 |
0
| try {
|
94 |
0
| return _parse(new StringReader(src.toString()));
|
95 |
| } |
96 |
| catch (IOException e) { |
97 |
| |
98 |
0
| throw new RuntimeException("I/O error occured!", e);
|
99 |
| } |
100 |
| } |
101 |
| |
102 |
0
| public TopEntity[] parse(StringBuffer src, Map options) throws ParserException,
|
103 |
| InvalidModelException { |
104 |
0
| throw new UnsupportedOperationException("Use method parse(StringBuffer) instead!");
|
105 |
| } |
106 |
| |
107 |
0
| protected ASTAnalysisContainer createContainer() {
|
108 |
0
| ASTAnalysisContainer container = new ASTAnalysisContainer();
|
109 |
| |
110 |
0
| new IdentifierAnalysis(container, factory);
|
111 |
0
| new OntologyAnalysis(container, factory, dataFactory).setCleanOnParse(cleanOnParse);
|
112 |
0
| new MediatorAnalysis(container, factory).setCleanOnParse(cleanOnParse);
|
113 |
0
| new NFPAnalysis(container);
|
114 |
0
| new ValueAnalysis(container, dataFactory, LEFactory, factory);
|
115 |
0
| new TopEntityAnalysis(container, factory);
|
116 |
0
| new ServiceAnalysis(container, factory, LEFactory).setCleanOnParse(cleanOnParse);
|
117 |
0
| new AxiomAnalysis(container, factory);
|
118 |
0
| new VariableAnalysis(container, LEFactory);
|
119 |
0
| new AtomicExpressionAnalysis(container, factory,LEFactory);
|
120 |
0
| new CompoundExpressionAnalysis(container, LEFactory);
|
121 |
0
| return container;
|
122 |
| } |
123 |
| |
124 |
0
| private TopEntity[] _parse(Reader src) throws ParserException,
|
125 |
| InvalidModelException, IOException { |
126 |
0
| ASTAnalysisContainer container = createContainer();
|
127 |
| |
128 |
0
| if (memorizeLELook){
|
129 |
0
| src = LogExprParserImpl.findLogicalExpressions(
|
130 |
| src,container.getStack(String.class)); |
131 |
| } |
132 |
| |
133 |
0
| Lexer lexer = new Lexer(new PushbackReader(src, 16384));
|
134 |
0
| org.wsmo.wsml.compiler.parser.Parser parser =
|
135 |
| new org.wsmo.wsml.compiler.parser.Parser(lexer); |
136 |
| |
137 |
0
| try {
|
138 |
0
| Start head = parser.parse();
|
139 |
0
| head.apply(container);
|
140 |
0
| Stack stack = container.getStack(TopEntity.class);
|
141 |
0
| TopEntity[] topEntities = new TopEntity[stack.size()];
|
142 |
0
| for (int i = 0; i < topEntities.length; i++) {
|
143 |
0
| topEntities[i] = (TopEntity) stack.remove(0);
|
144 |
| } |
145 |
0
| return topEntities;
|
146 |
| } |
147 |
| catch (org.wsmo.wsml.compiler.parser.ParserException pe) { |
148 |
0
| ParserException e = new ParserException(ParserException.NOT_VALID_PARSETREE,pe);
|
149 |
0
| Token t = pe.getToken();
|
150 |
0
| e.setErrorLine(t.getLine());
|
151 |
0
| e.setErrorPos(t.getPos());
|
152 |
0
| e.setFoundToken(t.getText());
|
153 |
0
| try{
|
154 |
0
| e.setExpectedToken(pe.getMessage().split("expecting: ")[1].trim());
|
155 |
| } |
156 |
| catch (IndexOutOfBoundsException iobE){ |
157 |
| |
158 |
0
| e.setExpectedToken(pe.getMessage());
|
159 |
| } |
160 |
0
| throw e;
|
161 |
| } |
162 |
| catch (org.wsmo.wsml.compiler.lexer.LexerException le) { |
163 |
0
| ParserException e = new ParserException(ParserException.NOT_VALID_PARSETREE, le);
|
164 |
0
| try{
|
165 |
0
| e.setErrorLine(Integer.parseInt(le.getMessage().split(",")[0].split("\\[")[1]));
|
166 |
| }catch (NumberFormatException nfE){ |
167 |
| |
168 |
| } |
169 |
0
| try{
|
170 |
0
| e.setErrorPos(Integer.parseInt(le.getMessage().split(",")[1].split("\\]")[0]));
|
171 |
| }catch (NumberFormatException nfE){ |
172 |
| |
173 |
| } |
174 |
0
| throw e;
|
175 |
| } |
176 |
| catch (WrappedParsingException wpe) { |
177 |
0
| if (wpe.getWrappedException() instanceof ParserException){
|
178 |
0
| throw (ParserException) wpe.getWrappedException();
|
179 |
| } |
180 |
0
| throw new RuntimeException(wpe);
|
181 |
| } |
182 |
| } |
183 |
| |
184 |
0
| public Set <String> listKeywords() {
|
185 |
0
| String[] wsmlTokens = ASTAnalysisContainer.WSML_TOKENS;
|
186 |
0
| return new HashSet <String>(Arrays.asList(wsmlTokens));
|
187 |
| } |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
0
| public List <Object> getWarnings() {
|
194 |
0
| throw new UnsupportedOperationException("This method is not implemented for WSML parsing");
|
195 |
| } |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
0
| public List <Object> getErrors() {
|
202 |
0
| 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 |
| |