001: /*
002: * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: * 1. Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * 3. The name of the author may not be used to endorse or promote products
014: * derived from this software without specific prior written permission.
015:
016: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: *
027: * $Id: RDFReaderFImpl.java,v 1.21 2008/01/02 12:05:03 andy_seaborne Exp $
028: */
029:
030: package com.hp.hpl.jena.rdf.model.impl;
031:
032: import com.hp.hpl.jena.*;
033: import com.hp.hpl.jena.rdf.model.*;
034: import com.hp.hpl.jena.shared.*;
035: import java.util.Properties;
036: import com.hp.hpl.jena.JenaRuntime;
037:
038: /**
039: *
040: * @author bwm
041: * @version $Revision: 1.21 $ $Date: 2008/01/02 12:05:03 $
042: */
043: public class RDFReaderFImpl extends Object implements RDFReaderF {
044:
045: private static final String GRDDLREADER = "com.hp.hpl.jena.grddl.GRDDLReader";
046: private static final String TURTLEREADER = "com.hp.hpl.jena.n3.turtle.TurtleReader";
047:
048: // Old reader (N3 based)
049: //private static final String TURTLEREADER = "com.hp.hpl.jena.n3.N3TurtleJenaReader" ;
050:
051: protected static Properties langToClassName = null;
052:
053: // predefined languages - these should probably go in a properties file
054:
055: protected static final String LANGS[] = { "RDF/XML",
056: "RDF/XML-ABBREV", "N-TRIPLE", "N-TRIPLES", "N3", "TURTLE",
057: "Turtle", "TTL", "GRDDL" };
058: // default readers for each language
059:
060: protected static final String DEFAULTREADERS[] = {
061: "com.hp.hpl.jena.rdf.arp.JenaReader",
062: "com.hp.hpl.jena.rdf.arp.JenaReader",
063: Jena.PATH + ".rdf.model.impl.NTripleReader",
064: Jena.PATH + ".rdf.model.impl.NTripleReader",
065: "com.hp.hpl.jena.n3.N3JenaReader", TURTLEREADER,
066: TURTLEREADER, TURTLEREADER, GRDDLREADER };
067:
068: protected static final String DEFAULTLANG = LANGS[0];
069:
070: protected static final String PROPNAMEBASE = Jena.PATH + ".reader.";
071:
072: static { // static initializer - set default readers
073: langToClassName = new Properties();
074: for (int i = 0; i < LANGS.length; i++) {
075: langToClassName.setProperty(LANGS[i], JenaRuntime
076: .getSystemProperty(PROPNAMEBASE + LANGS[i],
077: DEFAULTREADERS[i]));
078: }
079: }
080:
081: /** Creates new RDFReaderFImpl */
082: public RDFReaderFImpl() {
083: }
084:
085: public RDFReader getReader() {
086: return getReader(DEFAULTLANG);
087: }
088:
089: public RDFReader getReader(String lang) {
090:
091: // setup default language
092: if (lang == null || lang.equals("")) {
093: lang = LANGS[0];
094: }
095:
096: String className = langToClassName.getProperty(lang);
097: if (className == null || className.equals("")) {
098: throw new NoReaderForLangException(lang);
099: }
100: try {
101: return (RDFReader) Class.forName(className).newInstance();
102: } catch (ClassNotFoundException e) {
103: if (className.equals(GRDDLREADER))
104: throw new ConfigException(
105: "The GRDDL reader must be downloaded separately from Sourceforge, and included on the classpath.",
106: e);
107: throw new ConfigException("Reader not found on classpath",
108: e);
109: } catch (Exception e) {
110: throw new JenaException(e);
111: }
112: }
113:
114: public String setReaderClassName(String lang, String className) {
115: return setBaseReaderClassName(lang, className);
116: }
117:
118: public static String setBaseReaderClassName(String lang,
119: String className) {
120: String oldClassName = langToClassName.getProperty(lang);
121: langToClassName.setProperty(lang, className);
122: return oldClassName;
123: }
124: }
|