KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > ModelLoader


1 /*
2  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.util;
7
8 import org.apache.commons.logging.*;
9
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.shared.*;
12
13 import com.hp.hpl.jena.db.*;
14
15 /** A set of static convenience methods for getting models
16  * The loader will guess the language/type of the model using
17  * {@link #guessLang(String) guessLang}
18  *
19  * @author Andy Seaborne
20  * @version $Id: ModelLoader.java,v 1.25 2005/03/13 12:01:22 andy_seaborne Exp $
21  */

22
23 public class ModelLoader
24 {
25     static Log log = LogFactory.getLog(ModelLoader.class) ;
26     
27     /** @deprecated Use FileUtils.FileUtils.langXML */
28     public static final String JavaDoc langXML = FileUtils.langXML ;
29     
30     /** @deprecated Use FileUtils.langXMLAbbrev */
31     public static final String JavaDoc langXMLAbbrev = FileUtils.langXMLAbbrev ;
32     
33     /** @deprecated Use FileUtils.langNTriple */
34     public static final String JavaDoc langNTriple = FileUtils.langNTriple ;
35     
36     /** @deprecated Use FileUtils.langN3 */
37     public static final String JavaDoc langN3 = FileUtils.langN3 ;
38     // Non-standard
39
/** @deprecated Use FileUtils.langBDB */
40     public static final String JavaDoc langBDB = FileUtils.langBDB ;
41     /** @deprecated Use FileUtils.langSQL */
42     public static final String JavaDoc langSQL = FileUtils.langSQL ;
43
44     
45     /** Load a model
46      * @deprecated Use FileManager.get().loadModel(urlStr)
47      * @param urlStr The URL or file name of the model
48      */

49
50     public static Model loadModel(String JavaDoc urlStr) { return loadModel(urlStr, null) ; }
51
52     /** Load a model or attached a persistent store (but not a database).
53      * @deprecated Use FileManager.get().loadModel(urlStr, lang)
54      * @param urlStr The URL or file name of the model
55      * @param lang The language of the data - if null, the system guesses
56      */

57
58     public static Model loadModel(String JavaDoc urlStr, String JavaDoc lang)
59     {
60         try {
61             return FileManager.get().loadModel(urlStr, lang) ;
62         } catch (JenaException ex) { return null ; }
63     }
64
65     /** Load a model or attached a persistent store.
66      * Tries to guess syntax type.
67      * Database paramters only needed if its a database.
68      *
69      * @param urlStr The URL or file name of the model
70      * @param lang The language of the data - if null, the system guesses
71      * @param dbUser Database user name (for RDB/JDBC)
72      * @param dbPassword Database password (for RDB/JDBC)
73      * @param modelName The name of the model
74      * @param dbType Database type (e.g. MySQL)
75      * @param driver JDBC driver to load.
76      * @return Model
77      */

78
79
80     public static Model loadModel(String JavaDoc urlStr, String JavaDoc lang,
81                                   String JavaDoc dbUser, String JavaDoc dbPassword,
82                                   String JavaDoc modelName, String JavaDoc dbType, String JavaDoc driver)
83     {
84         // Wild guess at the language!
85
if ( lang == null )
86             lang = guessLang(urlStr) ;
87
88         if ( lang == null )
89             lang = FileUtils.langXML ;
90
91         if ( lang.equals(FileUtils.langBDB) )
92         {
93             // @@ temporarily not supported
94
LogFactory.getLog(ModelLoader.class).fatal("Failed to open Berkeley database") ;
95             return null ;
96 /*
97             // URL had better be a file!
98             if ( basename != null )
99                 urlStr = basename+File.separator+urlStr ;
100
101             String dirBDB = getDirname(urlStr) ;
102             if ( dirBDB == null || dirBDB.length() == 0)
103                    dirBDB = "." ;
104             urlStr = getBasename(urlStr) ;
105
106             log.debug("BDB: file="+urlStr+", dir="+dirBDB+", basename="+basename) ;
107
108             try {
109                 Model model = new ModelBdb(new StoreBdbF(dirBDB, urlStr)) ;
110                 return model ;
111             } catch (JenaException rdfEx)
112             {
113                 log.severe("Failed to open Berkeley database", rdfEx) ;
114                 System.exit(1) ;
115             }
116             */

117         }
118
119         if ( lang.equals(FileUtils.langSQL) )
120             return connectToDB(urlStr, dbUser, dbPassword, modelName, dbType, driver) ;
121
122         // Its a files.
123
// Language is N3, RDF/XML or N-TRIPLE
124
Model m = ModelFactory.createDefaultModel() ;
125
126         m.setReaderClassName(FileUtils.langXML, com.hp.hpl.jena.rdf.arp.JenaReader.class.getName());
127         m.setReaderClassName(FileUtils.langXMLAbbrev, com.hp.hpl.jena.rdf.arp.JenaReader.class.getName());
128
129         // Default.
130
//m.setReaderClassName(langNTriple, com.hp.hpl.jena.rdf.arp.NTriple.class.getName()) ;
131

132         try {
133             FileManager.get().readModel(m, urlStr, null, lang) ;
134         } catch (JenaException rdfEx)
135         {
136             log.warn("Error loading data source", rdfEx);
137             return null ;
138         }
139         return m ;
140     }
141
142     /** Load a model from a file into a model.
143      * @deprecated Use FileManager.get().readModel(model, urlStr) instead
144      * @param model Model to read into
145      * @param urlStr URL (or filename) to read from
146      * @return Returns the model passed in.
147      */

148
149     public static Model loadModel(Model model, String JavaDoc urlStr)
150     {
151         return loadModel(model, urlStr, null) ;
152     }
153     /** Load a model from a file into a model.
154      * @deprecated Use FileManager.get().readModel(model, urlStr, lang) instead
155      * @param model Model to read into
156      * @param urlStr URL (or filename) to read from
157      * @param lang Null mean guess based on the URI String
158      * @return Returns the model passed in.
159      */

160
161     public static Model loadModel(Model model, String JavaDoc urlStr, String JavaDoc lang)
162     {
163         try {
164             return FileManager.get().readModel(model, urlStr, null, lang) ;
165         }
166         catch (Exception JavaDoc e)
167         {
168             LogFactory.getLog(ModelLoader.class).warn("No such data source: "+urlStr);
169             return null ;
170         }
171     }
172
173     /**
174      * Connect to a database.
175      * @param urlStr
176      * @param dbUser
177      * @param dbPassword
178      * @param dbType
179      * @param driverName Load this driver (if not null)
180      * @return Model
181      */

182
183     public static Model connectToDB(String JavaDoc urlStr,
184                                     String JavaDoc dbUser, String JavaDoc dbPassword,
185                                     String JavaDoc modelName,
186                                     String JavaDoc dbType, String JavaDoc driverName)
187     {
188         // Fragment ID is the model name.
189
try {
190             if ( driverName != null )
191                 Class.forName(driverName).newInstance();
192         } catch (Exception JavaDoc ex) {}
193
194         try {
195             IDBConnection conn =
196                 ModelFactory.createSimpleRDBConnection(urlStr, dbUser, dbPassword, dbType) ;
197             return ModelRDB.open(conn, modelName) ;
198         } catch (JenaException rdfEx)
199         {
200             LogFactory.getLog(ModelLoader.class).error("Failed to open SQL database: ModelLoader.connectToDB", rdfEx) ;
201             throw rdfEx ;
202         }
203     }
204     /** Guess the language/type of model data. Updated by Chris, hived off the
205      * model-suffix part to FileUtils as part of unifying it with similar code in FileGraph.
206      *
207      * <ul>
208      * <li> If the URI of the model starts jdbc: it is assumed to be an RDB model</li>
209      * <li> If the URI ends ".rdf", it is assumed to be RDF/XML</li>
210      * <li> If the URI end .nt, it is assumed to be N-Triples</li>
211      * <li> If the URI end .bdb, it is assumed to be BerkeleyDB model [suppressed at present]</li>
212      * </ul>
213      * @deprecated Use FileUtils.guessLang
214      * @param urlStr URL to base the guess on
215      * @param defaultLang Default guess
216      * @return String Guessed syntax - or the default supplied
217      */

218
219     public static String JavaDoc guessLang( String JavaDoc urlStr, String JavaDoc defaultLang )
220     {
221         if ( urlStr.startsWith("jdbc:") || urlStr.startsWith("JDBC:") )
222             return FileUtils.langSQL ;
223         else
224             return FileUtils.guessLang( urlStr, defaultLang );
225     }
226     
227     /** Guess the language/type of model data
228      *
229      *
230      * <ul>
231      * <li> If the URI of the model starts jdbc: it is assumed to be an RDB model</li>
232      * <li> If the URI ends ".rdf", it is assumed to be RDF/XML</li>
233      * <li> If the URI end .nt, it is assumed to be N-Triples</li>
234      * <li> If the URI end .bdb, it is assumed to be BerkeleyDB model</li>
235      * </ul>
236      * @deprecated Use FileUtils.guessLang
237      * @param urlStr URL to base the guess on
238      * @return String Guessed syntax - null for no guess.
239      */

240
241     public static String JavaDoc guessLang(String JavaDoc urlStr)
242     {
243         return FileUtils.guessLang(urlStr) ;
244     }
245 }
246
247
248 /*
249  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
250  * All rights reserved.
251  *
252  * Redistribution and use in source and binary forms, with or without
253  * modification, are permitted provided that the following conditions
254  * are met:
255  * 1. Redistributions of source code must retain the above copyright
256  * notice, this list of conditions and the following disclaimer.
257  * 2. Redistributions in binary form must reproduce the above copyright
258  * notice, this list of conditions and the following disclaimer in the
259  * documentation and/or other materials provided with the distribution.
260  * 3. The name of the author may not be used to endorse or promote products
261  * derived from this software without specific prior written permission.
262  *
263  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
264  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
265  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
266  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
267  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
268  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
269  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
270  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
272  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273  */

274
Popular Tags