⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jenareader.java

📁 semantic web framework
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  (c) Copyright Hewlett-Packard Company 2001
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 * * $Id: JenaReader.java,v 1.8 2002/09/18 11:55:31 jeremy_carroll Exp $
 
   AUTHOR:  Jeremy J. Carroll
 */
/*
 * JenaReader.java
 *
 * Created on July 10, 2001, 6:29 AM
 */

package com.hp.hpl.jena.rdf.arp;

import com.hp.hpl.mesa.rdf.jena.model.Model;
import com.hp.hpl.mesa.rdf.jena.model.RDFReader;
import com.hp.hpl.mesa.rdf.jena.model.RDFErrorHandler;
import com.hp.hpl.mesa.rdf.jena.model.RDFException;
import com.hp.hpl.mesa.rdf.jena.model.Resource;
import com.hp.hpl.mesa.rdf.jena.model.Literal;
import com.hp.hpl.mesa.rdf.jena.model.Property;
import com.hp.hpl.mesa.rdf.jena.mem.ModelMem;
import com.hp.hpl.mesa.rdf.jena.common.ResourceImpl;
import com.hp.hpl.mesa.rdf.jena.common.PropertyImpl;
import com.hp.hpl.mesa.rdf.jena.common.LiteralImpl;
import com.hp.hpl.mesa.rdf.jena.common.RDFDefaultErrorHandler;
import java.io.*;
import java.net.*;


import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;

import org.xml.sax.XMLFilter;


/** Interface between Jena and ARP.
 *
 * @author jjc
 */
public class JenaReader implements RDFReader, ARPErrorNumbers {
    
/** Sets the reader for the languages RDF/XML and RDF/XML-ABBREV to be JenaReader.
 * @param m The Model on which to set the reader properties.
 */
    static public void useMe(Model m) {
        m.setReaderClassName("RDF/XML",JenaReader.class.getName());
        m.setReaderClassName("RDF/XML-ABBREV",JenaReader.class.getName());
    }
    
    static private final String saxFeaturesURL = "http://xml.org/sax/features/";
    static private final String saxPropertiesURL = "http://xml.org/sax/properties/";
    static private final String apacheFeaturesURL = "http://apache.org/xml/features/";
    static private final String apachePropertiesURL = "http://apache.org/xml/properties/";
    static private final String arpPropertiesURL = "http://jena.hpl.hp.com/arp/properties/";
    static private final int arpPropertiesURLLength = arpPropertiesURL.length();
    
    
    
    /** Creates new JenaReader
     */
    public JenaReader() {
        arpf = new ARPFilter();
    }
    private ARPFilter arpf;
    private Model model;
    public void read(Model model,String url) throws RDFException {
        
        try {
        	URLConnection conn = new URL(url).openConnection();
        	String encoding = conn.getContentEncoding();
        	if ( encoding == null )
               read(model,conn.getInputStream(), url);
        	else 
               read(model,new InputStreamReader(conn.getInputStream(),encoding), url);
        }
        catch (RDFException e) {
            throw e;
        }
        catch (IOException e) {
            throw new RDFException(e);
        }
    }
    

/** Converts an ARP literal into a Jena Literal.
 * @param lit The ARP literal.
 * @return The Jena Literal.
 * @deprecated Should never have been public.
 */
    static public Literal translate(ALiteral lit) {
        return new LiteralImpl(lit.toString(),lit.getLang(),lit.isWellFormedXML());
    }
    Literal convert(ALiteral lit)  throws RDFException {
        return model.createLiteral(lit.toString(),lit.getLang(),lit.isWellFormedXML());
    }
/** Converts an ARP resource into a Jena resource.
 * @param r The ARP resource.
 * @return The Jena resource.
 * @deprecated Should never have been public.
 */
    static public Resource translate(AResource r) {
        if ( r.isAnonymous() ) {
            String id = r.getAnonymousID();
            Resource rr = (Resource)r.getUserData();
            if ( rr == null ) {
                rr = new ResourceImpl();
                r.setUserData(rr);
            }
            return rr;
        } else {
            return new ResourceImpl(r.getURI());
        }
    }
    Resource convert(AResource r) throws RDFException {
        if ( r.isAnonymous() ) {
            String id = r.getAnonymousID();
            Resource rr = (Resource)r.getUserData();
            if ( rr == null ) {
                rr = model.createResource();
                r.setUserData(rr);
            }
            return rr;
        } else {
            return model.createResource(r.getURI());
        }
    }
/** Converts an ARP resource into a Jena property.
 * @param r The ARP resource.
 * @throws RDFException If r is anonymous, or similarly ill-formed.
 * @return The Jena property.
 * @deprecated Should never have been public.
 */
    static public Property translatePred(AResource r) throws RDFException {
        return new PropertyImpl(r.getURI());
    }
    Property convertPred(AResource r) throws RDFException {
        return model.createProperty(r.getURI());
    }
    /**
     *  Reads from reader, using base URI xmlbase, adding triples to model.
     * If xmlbase is "" then relative URIs may be added to model.
     * @param model A model to add triples to.
     * @param reader The RDF/XML document.
     * @param xmlBase The base URI of the document or "".
     */
    synchronized private void read(Model m,InputSource inputS,String xmlBase) throws RDFException {
        try {
        	model = m;
            if ( xmlBase == null  ) {
                xmlBase = ARPResource.dummy;
                errorHandler.error(new NullPointerException("A document base URI must be specified."));
            }
            if ( ! xmlBase.equals("") ) {
                try {
                    URI uri = new URI(xmlBase);
                }
                catch (MalformedURIException e) {
                    errorHandler.error(e);
                }
            } else {
                xmlBase = ARPResource.dummy;
            }
            inputS.setSystemId(xmlBase);
            arpf.setStatementHandler( new LocationStatementHandler() {
                public void statement(AResource subj, AResource pred, AResource obj ) {
                    try {
                        model.add(convert(subj),convertPred(pred),convert(obj));
                    }
                    catch (RDFException e) {
                        errorHandler.error(e);
                    }
                }
                public void statement(AResource subj, AResource pred, ALiteral lit ) {
                    try {
                        model.add(convert(subj),convertPred(pred),convert(lit));
                    }
                    catch (RDFException e) {
                        errorHandler.error(e);
                    }
                }
                public void statement(AResource subj, AResource pred, AResource obj, Location loc ) {
                    try {
                        model.add(convert(subj),convertPred(pred),convert(obj),loc);
                    }
                    catch (RDFException e) {
                        errorHandler.error(e);
                    }
		}
                public void statement(AResource subj, AResource pred, ALiteral lit, Location loc ) {
                    try {
                        model.add(convert(subj),convertPred(pred),convert(lit), loc);
                    }
                    catch (RDFException e) {
                        errorHandler.error(e);
                    }
                }
            } );

            
            arpf.setErrorHandler( new ARPSaxErrorHandler(errorHandler));
            arpf.parse(inputS);
        }
        catch (IOException e) {
            new RDFException(e);
        }
        catch (SAXException e) {
            new RDFException(e);
        }
    }
    
    /**
     *  Reads from reader, using base URI xmlbase, adding triples to model.
     * If xmlbase is "" then relative URIs may be added to model.
     * @param model A model to add triples to.
     * @param reader The RDF/XML document.
     * @param xmlBase The base URI of the document or "".
     */
    public void read(final Model model,Reader reader,String xmlBase) throws RDFException {
        read(model,new InputSource(reader),xmlBase);
    }
    /**
     *  Reads from inputStream, using base URI xmlbase, adding triples to model.
     * If xmlbase is "" then relative URIs may be added to model.
     * @param model A model to add triples to.
     * @param reader The RDF/XML document.
     * @param xmlBase The base URI of the document or "".
     */
    public void read(final Model model,InputStream in,String xmlBase) throws RDFException {
        read(model,new InputSource(in),xmlBase);
    }
    
    RDFErrorHandler errorHandler = new RDFDefaultErrorHandler();
    
    
/** 
   Change the error handler.
 *<p>
 * Note that errors of class {@link ParseException}
 * can be promoted using the {@link ParseException#promote}
 * method.
 * See ARP documentation for {@link org.xml.sax.ErrorHandler} for the
 * details of error promotion.
 * @param errHandler The new error handler.
 * @return The old error handler.
 */    
    public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler) {
        RDFErrorHandler old = this.errorHandler;
        this.errorHandler = errHandler;
        return old;
    }
/**
 *
 * Change a property of the RDF or XML parser.
 * <p>
 * This method is untested.
 * <p>
 * I do not believe that many of the XML features or properties are in fact
 * useful for ARP users.
 * The ARP properties allow fine-grained control over error reporting.
 * <p>
 * This interface can be used to set and get:
 * <dl>
 * <dt>
 * SAX2 features</dt>
 * <dd>
 * See <a href="http://xml.apache.org/xerces-j/features.html">Xerces features</a>.
 * Value should be given as a String "true" or "false" or a Boolean.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -