View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3    Copyright (c) 2005, University of Innsbruck, Austria
4    This library is free software; you can redistribute it and/or modify it under
5    the terms of the GNU Lesser General Public License as published by the Free
6    Software Foundation; either version 2.1 of the License, or (at your option)
7    any later version.
8    This library is distributed in the hope that it will be useful, but WITHOUT
9    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11   details.
12   You should have received a copy of the GNU Lesser General Public License along
13   with this library; if not, write to the Free Software Foundation, Inc.,
14   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15   */
16  package org.deri.wsmo4j.locator;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.InputStreamReader;
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.omwg.ontology.Ontology;
29  import org.wsmo.common.Entity;
30  import org.wsmo.common.IRI;
31  import org.wsmo.common.Identifier;
32  import org.wsmo.common.exception.InvalidModelException;
33  import org.wsmo.common.exception.SynchronisationException;
34  import org.wsmo.factory.Factory;
35  import org.wsmo.locator.Locator;
36  import org.wsmo.wsml.Parser;
37  import org.wsmo.wsml.ParserException;
38  
39  /**
40   * This locator is meant to be used for importing wsml files. He looks for a 
41   * file given its IRI, and parses it into an WSMO object model.
42   * 
43   * By default, the locator only imports ontologies from logical URIs. 
44   * Given a mapping from logical URIs to physical locations, he can also 
45   * locate ontologies from physical locations. This feature is enabled by 
46   * submitting a HashMap containing such a mapping in the preferences map.
47   *   HashMap preferences = new HashMap();
48   *   HashMap mapping = new HashMap();
49   *   mapping.put(...,...);
50   *	 preferences.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.locator.LocatorImpl");
51   *   preferences.put(Locator.URI_MAPPING, mapping);
52   *
53   * <pre>
54   *  Created on Aug 22, 2006
55   *  Committed by $Author: morcen $
56   *  $Source$,
57   * </pre>
58   *
59   * @author nathalie.steinmetz@deri.org
60   * @version $Revision: 1946 $ $Date: 2007-04-02 15:13:28 +0300 (Mon, 02 Apr 2007) $
61   */
62  public class LocatorImpl implements Locator {
63  	
64  	private Parser wsmlParser = null;
65  	
66  	private HashMap mapping = new HashMap();
67  	
68  	public LocatorImpl(Map prefs) {
69  		if (prefs != null) {
70  			// this preference indicates a given wsmo parser to use
71  			Object o = prefs.get(Factory.WSMO_PARSER);
72  			if (o == null || !(o instanceof Parser)) {
73  				o = Factory.createParser(new HashMap <String, Object>());
74  			}
75  			wsmlParser = (Parser) o;
76  			
77  			// this preference submits a mapping from logical URIs to physical file 
78  			// locations, e.g. the file identified by "http://example.org/ex" is 
79  			// located on "file:/C:/exampleFiles/example.wsml"
80  			o = prefs.get(Locator.URI_MAPPING);
81  			if (o != null && o instanceof HashMap) {
82  				mapping = (HashMap) o;
83  			}
84  		}
85  	}
86  	
87  	public Set <Entity> lookup(Identifier id) throws SynchronisationException {
88  		Set <Entity> set = new HashSet <Entity>();
89  		Ontology ontology = (Ontology) lookup(id, null);
90  		if (ontology != null) {
91  			set.add(ontology);
92  		}
93  		return set;
94  	}
95  
96  	public Entity lookup(Identifier id, Class clazz) {
97  		if (id instanceof IRI) {
98  			URL urlId = null;
99  			String idString = null;
100 			if (!mapping.isEmpty() && mapping.containsKey(id.toString())) {
101 				idString = (String) mapping.get(id.toString());
102 			}
103 			try {
104 				if (idString != null) {
105 					urlId = new URL(idString);
106 				}
107 				else {
108 					urlId = new URL(id.toString());
109 				}
110 				InputStream is = urlId.openStream();
111 				if (is != null) {
112 					// assuming first topentity in file is an ontology  
113 					return (Ontology) wsmlParser.parse(new InputStreamReader(is))[0]; 
114 				}
115 			} catch (MalformedURLException e) {
116 				System.out.println(e.toString() + " at: " + urlId.toString());
117 				return null;
118 			} catch (IOException e) {
119 				System.out.println(e.toString() + " at: " + urlId.toString());
120 				return null;
121 			} catch (ParserException e) {
122 				System.out.println(e.toString() + " at: " + urlId.toString());
123 				return null;
124 			} catch (InvalidModelException e) {
125 				System.out.println(e.toString() + " at: " + urlId.toString());
126 				return null;
127 			}
128 		}
129 		return null;
130 	}
131 
132 }
133 /*
134  * $Log$
135  * Revision 1.4  2007/04/02 12:13:28  morcen
136  * Generics support added to wsmo-api, wsmo4j and wsmo-test
137  *
138  * Revision 1.3  2006/11/27 11:36:08  nathaliest
139  * changed the validator to not import ontologies on its own and added documentation how to use the default locator to import ontologies
140  *
141  * Revision 1.2  2006/08/29 15:19:48  nathaliest
142  * changed the validator to call the memory based locator manager and fixed a problem causing loops at the location of ontologies in the validator
143  *
144  * Revision 1.1  2006/08/22 16:25:49  nathaliest
145  * added locator implementation and set it as default locator at the locator manager
146  *
147  * 
148  */