001: /*
002: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.util;
007:
008: import org.apache.commons.logging.*;
009:
010: import com.hp.hpl.jena.rdf.model.*;
011: import com.hp.hpl.jena.shared.*;
012:
013: import com.hp.hpl.jena.db.*;
014:
015: /** A set of static convenience methods for getting models
016: * The loader will guess the language/type of the model using
017: * {@link #guessLang(String) guessLang}
018: *
019: * @author Andy Seaborne
020: * @version $Id: ModelLoader.java,v 1.28 2008/01/02 12:07:44 andy_seaborne Exp $
021: */
022:
023: public class ModelLoader {
024: static Log log = LogFactory.getLog(ModelLoader.class);
025:
026: /** @deprecated Use FileUtils.FileUtils.langXML */
027: public static final String langXML = FileUtils.langXML;
028:
029: /** @deprecated Use FileUtils.langXMLAbbrev */
030: public static final String langXMLAbbrev = FileUtils.langXMLAbbrev;
031:
032: /** @deprecated Use FileUtils.langNTriple */
033: public static final String langNTriple = FileUtils.langNTriple;
034:
035: /** @deprecated Use FileUtils.langN3 */
036: public static final String langN3 = FileUtils.langN3;
037: // Non-standard
038: /** @deprecated Use FileUtils.langBDB */
039: public static final String langBDB = FileUtils.langBDB;
040: /** @deprecated Use FileUtils.langSQL */
041: public static final String langSQL = FileUtils.langSQL;
042:
043: /** Load a model
044: * @deprecated Use FileManager.get().loadModel(urlStr)
045: * @param urlStr The URL or file name of the model
046: */
047:
048: public static Model loadModel(String urlStr) {
049: return loadModel(urlStr, null);
050: }
051:
052: /** Load a model or attached a persistent store (but not a database).
053: * @deprecated Use FileManager.get().loadModel(urlStr, lang)
054: * @param urlStr The URL or file name of the model
055: * @param lang The language of the data - if null, the system guesses
056: */
057:
058: public static Model loadModel(String urlStr, String lang) {
059: try {
060: return FileManager.get().loadModel(urlStr, lang);
061: } catch (JenaException ex) {
062: return null;
063: }
064: }
065:
066: /** Load a model or attached a persistent store.
067: * Tries to guess syntax type.
068: * Database paramters only needed if its a database.
069: *
070: * @param urlStr The URL or file name of the model
071: * @param lang The language of the data - if null, the system guesses
072: * @param dbUser Database user name (for RDB/JDBC)
073: * @param dbPassword Database password (for RDB/JDBC)
074: * @param modelName The name of the model
075: * @param dbType Database type (e.g. MySQL)
076: * @param driver JDBC driver to load.
077: * @return Model
078: */
079:
080: public static Model loadModel(String urlStr, String lang,
081: String dbUser, String dbPassword, String modelName,
082: String dbType, String driver) {
083: // Wild guess at the language!
084: if (lang == null)
085: lang = guessLang(urlStr);
086:
087: if (lang == null)
088: lang = FileUtils.langXML;
089:
090: if (lang.equals(FileUtils.langBDB)) {
091: // @@ temporarily not supported
092: LogFactory.getLog(ModelLoader.class).fatal(
093: "Failed to open Berkeley database");
094: return null;
095: /*
096: // URL had better be a file!
097: if ( basename != null )
098: urlStr = basename+File.separator+urlStr ;
099:
100: String dirBDB = getDirname(urlStr) ;
101: if ( dirBDB == null || dirBDB.length() == 0)
102: dirBDB = "." ;
103: urlStr = getBasename(urlStr) ;
104:
105: log.debug("BDB: file="+urlStr+", dir="+dirBDB+", basename="+basename) ;
106:
107: try {
108: Model model = new ModelBdb(new StoreBdbF(dirBDB, urlStr)) ;
109: return model ;
110: } catch (JenaException rdfEx)
111: {
112: log.severe("Failed to open Berkeley database", rdfEx) ;
113: System.exit(1) ;
114: }
115: */
116: }
117:
118: if (lang.equals(FileUtils.langSQL))
119: return connectToDB(urlStr, dbUser, dbPassword, modelName,
120: 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,
127: com.hp.hpl.jena.rdf.arp.JenaReader.class.getName());
128: m.setReaderClassName(FileUtils.langXMLAbbrev,
129: com.hp.hpl.jena.rdf.arp.JenaReader.class.getName());
130:
131: // Default.
132: //m.setReaderClassName(langNTriple, com.hp.hpl.jena.rdf.arp.NTriple.class.getName()) ;
133:
134: try {
135: FileManager.get().readModel(m, urlStr, null, lang);
136: } catch (JenaException rdfEx) {
137: log.warn("Error loading data source", rdfEx);
138: return null;
139: }
140: return m;
141: }
142:
143: /** Load a model from a file into a model.
144: * @deprecated Use FileManager.get().readModel(model, urlStr) instead
145: * @param model Model to read into
146: * @param urlStr URL (or filename) to read from
147: * @return Returns the model passed in.
148: */
149:
150: public static Model loadModel(Model model, String urlStr) {
151: return loadModel(model, urlStr, null);
152: }
153:
154: /** Load a model from a file into a model.
155: * @deprecated Use FileManager.get().readModel(model, urlStr, lang) instead
156: * @param model Model to read into
157: * @param urlStr URL (or filename) to read from
158: * @param lang Null mean guess based on the URI String
159: * @return Returns the model passed in.
160: */
161:
162: public static Model loadModel(Model model, String urlStr,
163: String lang) {
164: try {
165: return FileManager.get().readModel(model, urlStr, null,
166: lang);
167: } catch (Exception e) {
168: LogFactory.getLog(ModelLoader.class).warn(
169: "No such data source: " + urlStr);
170: return null;
171: }
172: }
173:
174: /**
175: * Connect to a database.
176: * @param urlStr
177: * @param dbUser
178: * @param dbPassword
179: * @param dbType
180: * @param driverName Load this driver (if not null)
181: * @return Model
182: */
183:
184: public static Model connectToDB(String urlStr, String dbUser,
185: String dbPassword, String modelName, String dbType,
186: String driverName) {
187: // Fragment ID is the model name.
188: try {
189: if (driverName != null)
190: Class.forName(driverName).newInstance();
191: } catch (Exception ex) {
192: }
193:
194: try {
195: IDBConnection conn = ModelFactory
196: .createSimpleRDBConnection(urlStr, dbUser,
197: dbPassword, dbType);
198: return ModelRDB.open(conn, modelName);
199: } catch (JenaException rdfEx) {
200: LogFactory
201: .getLog(ModelLoader.class)
202: .error(
203: "Failed to open SQL database: ModelLoader.connectToDB",
204: rdfEx);
205: throw rdfEx;
206: }
207: }
208:
209: /** Guess the language/type of model data. Updated by Chris, hived off the
210: * model-suffix part to FileUtils as part of unifying it with similar code in FileGraph.
211: *
212: * <ul>
213: * <li> If the URI of the model starts jdbc: it is assumed to be an RDB model</li>
214: * <li> If the URI ends ".rdf", it is assumed to be RDF/XML</li>
215: * <li> If the URI end .nt, it is assumed to be N-Triples</li>
216: * <li> If the URI end .bdb, it is assumed to be BerkeleyDB model [suppressed at present]</li>
217: * </ul>
218: * @deprecated Use FileUtils.guessLang
219: * @param urlStr URL to base the guess on
220: * @param defaultLang Default guess
221: * @return String Guessed syntax - or the default supplied
222: */
223:
224: public static String guessLang(String urlStr, String defaultLang) {
225: if (urlStr.startsWith("jdbc:") || urlStr.startsWith("JDBC:"))
226: return FileUtils.langSQL;
227: else
228: return FileUtils.guessLang(urlStr, defaultLang);
229: }
230:
231: /** Guess the language/type of model data
232: *
233: *
234: * <ul>
235: * <li> If the URI of the model starts jdbc: it is assumed to be an RDB model</li>
236: * <li> If the URI ends ".rdf", it is assumed to be RDF/XML</li>
237: * <li> If the URI end .nt, it is assumed to be N-Triples</li>
238: * <li> If the URI end .bdb, it is assumed to be BerkeleyDB model</li>
239: * </ul>
240: * @deprecated Use FileUtils.guessLang
241: * @param urlStr URL to base the guess on
242: * @return String Guessed syntax - null for no guess.
243: */
244:
245: public static String guessLang(String urlStr) {
246: return FileUtils.guessLang(urlStr);
247: }
248: }
249:
250: /*
251: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
252: * All rights reserved.
253: *
254: * Redistribution and use in source and binary forms, with or without
255: * modification, are permitted provided that the following conditions
256: * are met:
257: * 1. Redistributions of source code must retain the above copyright
258: * notice, this list of conditions and the following disclaimer.
259: * 2. Redistributions in binary form must reproduce the above copyright
260: * notice, this list of conditions and the following disclaimer in the
261: * documentation and/or other materials provided with the distribution.
262: * 3. The name of the author may not be used to endorse or promote products
263: * derived from this software without specific prior written permission.
264: *
265: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
266: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
267: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
268: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
269: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
270: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
271: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
272: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
273: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
274: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275: */
|