001 /*
002 * (c) Copyright 2009 University of Bristol
003 * All rights reserved.
004 * [See end of file]
005 */
006 package net.rootdev.javardfa;
007
008 import com.hp.hpl.jena.rdf.model.Model;
009 import com.hp.hpl.jena.rdf.model.RDFErrorHandler;
010 import com.hp.hpl.jena.rdf.model.RDFReader;
011 import com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl;
012 import java.io.IOException;
013 import java.io.InputStream;
014 import java.io.Reader;
015 import org.xml.sax.InputSource;
016 import org.xml.sax.SAXException;
017 import org.xml.sax.XMLReader;
018
019 /**
020 * @author Damian Steer <pldms@mac.com>
021 */
022
023 public class RDFaReader implements RDFReader {
024
025 static {
026 RDFReaderFImpl.setBaseReaderClassName("HTML", HTMLRDFaReader.class.getName());
027 RDFReaderFImpl.setBaseReaderClassName("XHTML", XHTMLRDFaReader.class.getName());
028 }
029
030 public static class HTMLRDFaReader extends RDFaReader {
031 @Override public XMLReader getReader() {
032 return ParserFactory.createHTML5Reader();
033 }
034
035 @Override public void initParser(Parser parser) {
036 parser.enable(Setting.ManualNamespaces);
037 }
038 }
039
040 public static class XHTMLRDFaReader extends RDFaReader {
041 @Override public XMLReader getReader() throws SAXException {
042 return ParserFactory.createNonvalidatingReader();
043 }
044 }
045
046 private XMLReader xmlReader;
047
048 public void read(Model arg0, Reader arg1, String arg2) {
049 this.runParser(arg0, arg2, new InputSource(arg1));
050 }
051
052 public void read(Model arg0, InputStream arg1, String arg2) {
053 this.runParser(arg0, arg2, new InputSource(arg1));
054 }
055
056 public void read(Model arg0, String arg1) {
057 this.runParser(arg0, arg1, new InputSource(arg1));
058 }
059
060 public Object setProperty(String arg0, Object arg1) {
061 throw new UnsupportedOperationException("Not supported yet.");
062 }
063
064 public RDFErrorHandler setErrorHandler(RDFErrorHandler arg0) {
065 throw new UnsupportedOperationException("Not supported yet.");
066 }
067
068 public void setReader(XMLReader reader) { this.xmlReader = reader; }
069 public XMLReader getReader() throws SAXException { return xmlReader; }
070 public void initParser(Parser parser) { }
071
072 private StatementSink getSink(Model arg0) {
073 return new JenaStatementSink(arg0);
074 }
075
076 private void runParser(Model arg0, String arg2, InputSource source) {
077 StatementSink sink = getSink(arg0);
078 Parser parser = new Parser(sink);
079 parser.setBase(arg2);
080 initParser(parser);
081 try {
082 XMLReader xreader = getReader();
083 xreader.setContentHandler(parser);
084 xreader.parse(source);
085 } catch (IOException ex) {
086 throw new RuntimeException("IO Error when parsing", ex);
087 } catch (SAXException ex) {
088 throw new RuntimeException("SAX Error when parsing", ex);
089 }
090 }
091
092 }
093
094 /*
095 * (c) Copyright 2009 University of Bristol
096 * All rights reserved.
097 *
098 * Redistribution and use in source and binary forms, with or without
099 * modification, are permitted provided that the following conditions
100 * are met:
101 * 1. Redistributions of source code must retain the above copyright
102 * notice, this list of conditions and the following disclaimer.
103 * 2. Redistributions in binary form must reproduce the above copyright
104 * notice, this list of conditions and the following disclaimer in the
105 * documentation and/or other materials provided with the distribution.
106 * 3. The name of the author may not be used to endorse or promote products
107 * derived from this software without specific prior written permission.
108 *
109 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
110 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
111 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
112 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
113 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
114 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
115 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
116 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
117 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
118 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
119 */