001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.n3;
007:
008: import com.hp.hpl.jena.graph.GraphEvents;
009: import com.hp.hpl.jena.rdf.model.*;
010: import java.net.*;
011: import java.io.*;
012:
013: import org.apache.commons.logging.LogFactory;
014:
015: import com.hp.hpl.jena.shared.*;
016: import com.hp.hpl.jena.util.FileUtils;
017:
018: /** Abstract class that sorts out input sterams, readers and base URIs, to call a
019: * single worker function with model, UTF8 reader and visated base
020: *
021: * @author Andy Seaborne
022: * @version $Id: JenaReaderBase.java,v 1.3 2008/01/02 12:04:50 andy_seaborne Exp $
023: */
024:
025: public abstract class JenaReaderBase implements RDFReader {
026: RDFErrorHandler errorHandler = null;
027:
028: public JenaReaderBase() {
029: }
030:
031: final public void read(Model model, Reader r, String base) {
032: checkReader(r);
033: readImpl(model, r, base);
034: }
035:
036: final public void read(Model model, java.lang.String url) {
037: try {
038: URLConnection conn = new URL(url).openConnection();
039: String encoding = conn.getContentEncoding();
040: if (encoding == null)
041: read(model, new InputStreamReader(
042: conn.getInputStream(), FileUtils.encodingUTF8),
043: url);
044: else {
045: LogFactory.getLog(this .getClass()).warn(
046: "URL content is not UTF-8");
047: read(model, new InputStreamReader(
048: conn.getInputStream(), encoding), url);
049: }
050: } catch (JenaException e) {
051: if (errorHandler == null)
052: throw e;
053: errorHandler.error(e);
054: } catch (Exception ex) {
055: if (errorHandler == null)
056: throw new JenaException(ex);
057: errorHandler.error(ex);
058: }
059: }
060:
061: final public void read(Model model, InputStream in, String base) {
062: readImpl(model, FileUtils.asBufferedUTF8(in), base);
063: }
064:
065: final public RDFErrorHandler setErrorHandler(
066: RDFErrorHandler errHandler) {
067: RDFErrorHandler old = errorHandler;
068: errorHandler = errHandler;
069: return old;
070: }
071:
072: final public Object setProperty(String propName, Object propValue) {
073: return null;
074: }
075:
076: protected void checkReader(Reader r) {
077: if (r instanceof FileReader) {
078: FileReader f = (FileReader) r;
079: if (f.getEncoding()
080: .equalsIgnoreCase(FileUtils.encodingUTF8))
081: LogFactory.getLog(this .getClass()).warn(
082: "FileReader is not UTF-8");
083: }
084: }
085:
086: private void readImpl(Model model, Reader reader, String base) {
087: // The reader has been checked, if possible, by now or
088: // constructed correctly by code here.
089: if (base != null)
090: base = IRIResolver.resolveGlobal(base);
091: try {
092: model.notifyEvent(GraphEvents.startRead);
093: readWorker(model, reader, base);
094: } catch (JenaException e) {
095: if (errorHandler == null)
096: throw e;
097: errorHandler.error(e);
098: } catch (Exception ex) {
099: if (errorHandler == null)
100: throw new JenaException(ex);
101: errorHandler.error(ex);
102: } finally {
103: model.notifyEvent(GraphEvents.finishRead);
104: }
105: }
106:
107: protected abstract void readWorker(Model model, Reader reader,
108: String base) throws Exception;
109: }
110:
111: /*
112: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
113: * All rights reserved.
114: *
115: * Redistribution and use in source and binary forms, with or without
116: * modification, are permitted provided that the following conditions
117: * are met:
118: * 1. Redistributions of source code must retain the above copyright
119: * notice, this list of conditions and the following disclaimer.
120: * 2. Redistributions in binary form must reproduce the above copyright
121: * notice, this list of conditions and the following disclaimer in the
122: * documentation and/or other materials provided with the distribution.
123: * 3. The name of the author may not be used to endorse or promote products
124: * derived from this software without specific prior written permission.
125: *
126: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
127: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
128: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
129: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
130: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
131: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
132: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
133: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
134: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
135: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
136: */
|