1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package com.ontotext.wsmo4j.parser.xml; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| import java.io.IOException; |
30 |
| import java.io.Reader; |
31 |
| import java.io.StringReader; |
32 |
| import java.util.HashMap; |
33 |
| import java.util.HashSet; |
34 |
| import java.util.List; |
35 |
| import java.util.Map; |
36 |
| import java.util.Set; |
37 |
| import java.util.Vector; |
38 |
| |
39 |
| import javax.xml.parsers.DocumentBuilder; |
40 |
| import javax.xml.parsers.DocumentBuilderFactory; |
41 |
| import javax.xml.parsers.ParserConfigurationException; |
42 |
| |
43 |
| import org.deri.wsmo4j.io.parser.xml.LogExprParserImpl; |
44 |
| import org.w3c.dom.Document; |
45 |
| import org.w3c.dom.Node; |
46 |
| import org.w3c.dom.NodeList; |
47 |
| import org.wsmo.common.TopEntity; |
48 |
| import org.wsmo.common.exception.InvalidModelException; |
49 |
| import org.wsmo.factory.DataFactory; |
50 |
| import org.wsmo.factory.Factory; |
51 |
| import org.wsmo.factory.LogicalExpressionFactory; |
52 |
| import org.wsmo.factory.WsmoFactory; |
53 |
| import org.wsmo.wsml.Parser; |
54 |
| import org.wsmo.wsml.ParserException; |
55 |
| import org.xml.sax.InputSource; |
56 |
| import org.xml.sax.SAXException; |
57 |
| |
58 |
| import com.ontotext.wsmo4j.parser.WrappedInvalidModelException; |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| public class WsmlXmlParser implements Parser { |
66 |
| |
67 |
| private static final boolean isValidating = false; |
68 |
| |
69 |
| private WsmoFactory wsmoFactory; |
70 |
| |
71 |
| private LogicalExpressionFactory leFactory; |
72 |
| |
73 |
| private DataFactory dataFactory; |
74 |
| |
75 |
| private LogExprParserImpl leParser; |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
0
| public WsmlXmlParser(Map map) {
|
88 |
0
| wsmoFactory = (WsmoFactory) map.get(Factory.WSMO_FACTORY);
|
89 |
0
| leFactory = (LogicalExpressionFactory) map.get(Factory.LE_FACTORY);
|
90 |
0
| dataFactory = (DataFactory) map.get(Factory.DATA_FACTORY);
|
91 |
0
| if (wsmoFactory == null) {
|
92 |
0
| wsmoFactory = Factory.createWsmoFactory(new HashMap <String, Object> ());
|
93 |
| } |
94 |
0
| if (leFactory == null) {
|
95 |
0
| leFactory = Factory.createLogicalExpressionFactory(new HashMap <String, Object> ());
|
96 |
| } |
97 |
0
| if (dataFactory == null) {
|
98 |
0
| dataFactory = Factory.createDataFactory(new HashMap <String, Object> ());
|
99 |
| } |
100 |
| |
101 |
0
| assert wsmoFactory != null;
|
102 |
0
| assert leFactory != null;
|
103 |
0
| assert dataFactory != null;
|
104 |
| |
105 |
0
| leParser = new LogExprParserImpl(map);
|
106 |
| } |
107 |
| |
108 |
0
| WsmoFactory getFactory() {
|
109 |
0
| return wsmoFactory;
|
110 |
| } |
111 |
| |
112 |
0
| LogicalExpressionFactory getLEFactory() {
|
113 |
0
| return leFactory;
|
114 |
| } |
115 |
| |
116 |
0
| DataFactory getDataFactory() {
|
117 |
0
| return dataFactory;
|
118 |
| } |
119 |
| |
120 |
0
| LogExprParserImpl getLEParser() {
|
121 |
0
| return leParser;
|
122 |
| } |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
0
| public TopEntity[] parse(Reader src) throws IOException, ParserException {
|
130 |
0
| return parse(new InputSource(src));
|
131 |
| } |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
0
| public TopEntity[] parse(Reader src, Map options) throws IOException, ParserException {
|
140 |
0
| return parse(src);
|
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
0
| public TopEntity[] parse(StringBuffer src) throws ParserException {
|
149 |
0
| try {
|
150 |
0
| return parse(new InputSource(new StringReader(src.toString())));
|
151 |
| } |
152 |
| catch (IOException e) { |
153 |
0
| throw new RuntimeException("IOException occured! " + e.getMessage(), e);
|
154 |
| } |
155 |
| } |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
0
| public TopEntity[] parse(StringBuffer src, Map options) throws ParserException {
|
164 |
0
| return parse(src);
|
165 |
| } |
166 |
| |
167 |
0
| private TopEntity[] parse(InputSource source) throws IOException, ParserException {
|
168 |
0
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
169 |
| |
170 |
0
| try {
|
171 |
0
| factory.setNamespaceAware(true);
|
172 |
| |
173 |
0
| if (isValidating) {
|
174 |
0
| try {
|
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| } |
184 |
| catch (IllegalArgumentException e) { |
185 |
0
| System.out.println("Schema validation is disabled!");
|
186 |
0
| System.out
|
187 |
| .println("Xml parser implementaion does not support JAXP 1.2 " |
188 |
| + "To change the JAXP implementation with Xerces (or another alternative JAXP " |
189 |
| + "1.2 enabled parser start with -Djavax.xml.parsers.DocumentBuilderFactory=" |
190 |
| + "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); |
191 |
| } |
192 |
| } |
193 |
| |
194 |
0
| DocumentBuilder docBuilder = factory.newDocumentBuilder();
|
195 |
0
| Document document = docBuilder.parse(source);
|
196 |
0
| NodeList nodes = document.getElementsByTagName("wsml");
|
197 |
0
| Vector <TopEntity> topEntities = new Vector <TopEntity> ();
|
198 |
| |
199 |
0
| for (int i = 0; i < nodes.getLength(); i++) {
|
200 |
0
| Node node = nodes.item(i);
|
201 |
0
| topEntities.add(NodeWsml.deserialzieTopEntity(node, this));
|
202 |
0
| assert topEntities.elementAt(i) != null;
|
203 |
| } |
204 |
| |
205 |
0
| return topEntities.toArray(new TopEntity[]{});
|
206 |
| } |
207 |
| catch (InvalidModelException e) { |
208 |
0
| throw new WrappedInvalidModelException(e);
|
209 |
| } |
210 |
| catch (SAXException e) { |
211 |
0
| throw new ParserException(e.getMessage(), e);
|
212 |
| } |
213 |
| catch (ParserConfigurationException e) { |
214 |
0
| throw new ParserException(e.getMessage(), e);
|
215 |
| } |
216 |
| } |
217 |
| |
218 |
0
| public Set <String> listKeywords() {
|
219 |
0
| return new HashSet <String>();
|
220 |
| } |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
0
| public List <Object> getWarnings() {
|
227 |
0
| throw new UnsupportedOperationException("This method is not implemented for XML parsing");
|
228 |
| } |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
0
| public List <Object> getErrors() {
|
235 |
0
| throw new UnsupportedOperationException("This method is not implemented for XML parsing");
|
236 |
| } |
237 |
| } |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
| |
277 |
| |