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