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