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  package com.ontotext.wsmo4j.parser.xml;
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   * @author not attributable
26   * @version 1.0
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   * Parse XML file and creates WSMO obkjects (check wsml-xml-syntax.xsd)
62   *
63   * @author not attributable
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       *  Map properties = new TreeMap();
79       *  properties.put(Factory.PROVIDER_CLASS, "com.ontotext.wsmo4j.xmlparser.WsmlXmlParser");
80       *  properties.put(Parser.PARSER_WSMO_FACTORY, factory);
81       *  properties.put(Parser.PARSER_LE_FACTORY, leFactory);
82       *  parser = Factory.createParser(properties);
83       *
84       * @param map Initialization properties, requires Parser.PARSER_LE_FACTORY
85       *  and Parser.PARSER_WSMO_FACTORY
86       */
87      public WsmlXmlParser(Map map) {
88          wsmoFactory = (WsmoFactory) map.get(Factory.WSMO_FACTORY);
89          leFactory = (LogicalExpressionFactory) map.get(Factory.LE_FACTORY);
90          dataFactory = (DataFactory) map.get(Factory.DATA_FACTORY);
91          if (wsmoFactory == null) {
92              wsmoFactory = Factory.createWsmoFactory(new HashMap <String, Object> ());
93          }
94          if (leFactory == null) {
95              leFactory = Factory.createLogicalExpressionFactory(new HashMap <String, Object> ());
96          }
97          if (dataFactory == null) {
98              dataFactory = Factory.createDataFactory(new HashMap <String, Object> ());
99          }
100 
101         assert wsmoFactory != null;
102         assert leFactory != null;
103         assert dataFactory != null;
104 
105         leParser = new LogExprParserImpl(map);
106     }
107 
108     WsmoFactory getFactory() {
109         return wsmoFactory;
110     }
111 
112     LogicalExpressionFactory getLEFactory() {
113         return leFactory;
114     }
115 
116     DataFactory getDataFactory() {
117         return dataFactory;
118     }
119 
120     LogExprParserImpl getLEParser() {
121         return leParser;
122     }
123 
124     /**
125      * Parses a XML source
126      *
127      * @param src Reader to the xml file
128      */
129     public TopEntity[] parse(Reader src) throws IOException, ParserException {
130         return parse(new InputSource(src));
131     }
132 
133     /**
134      * Parses a XML source
135      *
136      * @param src Reader to the xml file
137      * @param options Ignored
138      */
139     public TopEntity[] parse(Reader src, Map options) throws IOException, ParserException {
140         return parse(src);
141     }
142 
143     /**
144      * Parses a XML source
145      *
146      * @param src Source xml
147      */
148     public TopEntity[] parse(StringBuffer src) throws ParserException {
149         try {
150             return parse(new InputSource(new StringReader(src.toString())));
151         }
152         catch (IOException e) {
153             throw new RuntimeException("IOException occured! " + e.getMessage(), e);
154         }
155     }
156 
157     /**
158      * Parses a XML source
159      *
160      * @param src Source xml
161      * @param options Ignored
162      */
163     public TopEntity[] parse(StringBuffer src, Map options) throws ParserException {
164         return parse(src);
165     }
166 
167     private TopEntity[] parse(InputSource source) throws IOException, ParserException {
168         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
169 
170         try {
171             factory.setNamespaceAware(true);
172 
173             if (isValidating) {
174                 try {
175                     /*factory.setValidating(true);
176                     factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
177                             "http://www.w3.org/2001/XMLSchema");
178                     factory
179                             .setAttribute(
180                                     "http://java.sun.com/xml/jaxp/properties/schemaSource",
181                                     new File("URL TO THE SCHEMA"));
182                                             */
183                 }
184                 catch (IllegalArgumentException e) {
185                     System.out.println("Schema validation is disabled!");
186                     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             DocumentBuilder docBuilder = factory.newDocumentBuilder();
195             Document document = docBuilder.parse(source);
196             NodeList nodes = document.getElementsByTagName("wsml");
197             Vector <TopEntity> topEntities = new Vector <TopEntity> ();
198 
199             for (int i = 0; i < nodes.getLength(); i++) {
200                 Node node = nodes.item(i);
201                 topEntities.add(NodeWsml.deserialzieTopEntity(node, this));
202                 assert topEntities.elementAt(i) != null;
203             }
204 
205             return topEntities.toArray(new TopEntity[]{});
206         }
207         catch (InvalidModelException e) {
208             throw new WrappedInvalidModelException(e);
209         }
210         catch (SAXException e) {
211             throw new ParserException(e.getMessage(), e);
212         }
213         catch (ParserConfigurationException e) {
214             throw new ParserException(e.getMessage(), e);
215         }
216     }
217 
218     public Set <String> listKeywords() {
219         return new HashSet <String>();
220     }
221     
222     /*
223      *  (non-Javadoc)
224      * @see org.wsmo.wsml.Parser#getWarnings()
225      */
226     public List <Object> getWarnings() {
227 		throw new UnsupportedOperationException("This method is not implemented for XML parsing");
228 	}
229     
230     /*
231      *  (non-Javadoc)
232      * @see org.wsmo.wsml.Parser#getErrors()
233      */
234 	public List <Object> getErrors() {
235 		throw new UnsupportedOperationException("This method is not implemented for XML parsing");
236 	}
237 }
238 
239 /*
240  * $Log$
241  * Revision 1.8  2007/04/02 12:19:08  morcen
242  * Generics support added to wsmo-api, wsmo4j and wsmo-test
243  *
244  * Revision 1.7  2007/04/02 12:13:22  morcen
245  * Generics support added to wsmo-api, wsmo4j and wsmo-test
246  *
247  * Revision 1.6  2006/11/10 11:08:53  nathaliest
248  * added getWarnings() and getErrors() methods to Parser interface, implemented them in the rdf parser implementation and added UnsupportedOperationException to the other parser implementations
249  *
250  * Revision 1.5  2006/01/13 10:06:56  vassil_momtchev
251  * removed hardcoded schema url. schema validation is still not used.
252  *
253  * Revision 1.4  2006/01/13 09:59:10  vassil_momtchev
254  * exceptions handling refactored; isValidation const added
255  *
256  * Revision 1.3  2006/01/11 13:03:30  marin_dimitrov
257  * common constants moved to Factory
258  *
259  * Revision 1.2  2005/12/09 11:29:32  vassil_momtchev
260  * listkeywords method added; implemented to return empty set
261  *
262  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
263  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
264  *
265  * Revision 1.6  2005/09/21 06:28:56  holgerlausen
266  * removing explicit factory creations and introducing parameter maps for parsers
267  *
268  * Revision 1.5  2005/09/17 09:02:51  vassil_momtchev
269  * datafactory is passed with the map in constructor
270  *
271  * Revision 1.4  2005/09/02 09:43:32  ohamano
272  * integrate wsmo-api and le-api on api and reference implementation level; full parser, serializer and validator integration will be next step
273  *
274  * Revision 1.3  2005/08/08 08:15:00  vassil_momtchev
275  * javadoc added, implemented all parse methods, optimize xml transformation
276  *
277  */