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

📄 arpfilter.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: ARPFilter.java,v 1.12 2003/01/15 16:35:44 jeremy_carroll Exp $
   
   AUTHOR:  Jeremy J. Carroll
*/
/*
 * ARPFilter.java
 *
 * Created on June 21, 2001, 10:01 PM
 */

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



import java.util.Map;
import java.util.HashMap;
import java.util.BitSet;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

import org.xml.sax.ErrorHandler;
import org.xml.sax.XMLReader;
//import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXNotRecognizedException;

import org.xml.sax.helpers.XMLFilterImpl;
import org.xml.sax.ext.LexicalHandler;
import java.io.IOException;

/**
 *
 * @author  jjc
 */
class ARPFilter extends XMLFilterImpl 
                       implements RDFParserConstants, ARPErrorNumbers, LexicalHandler{
   static {
      org.apache.xerces.utils.XMLCharacterProperties.initCharFlags();
      CharacterModel.isFullyNormalizedConstruct("make the linkage error happen early");
   }
  
    ARPFilter ()  
    {
        super(saxDriver());
        setErrorHandler(new DefaultErrorHandler());
    }

    
    static private XMLReader saxDriver()  {
        /*
        String parserClassName = System.getProperty("org.xml.sax.driver",
                                  "org.apache.xerces.parsers.SAXParser");    
        return XMLReaderFactory.createXMLReader(parserClassName);
         */
        return new org.apache.xerces.parsers.SAXParser();
    }
 
    
    private Throwable parsersLastException;
    
    private Map nodeIdUserData;
    
    private boolean embedding = true;
    boolean setEmbedding(boolean x) {
        boolean old = embedding;
        embedding = x;
        return old;
    }
    
    synchronized public void parse (InputSource input)
    throws IOException, SAXException
    {
        // Make sure we have a sane state for
        // Namespace processing.
        getParent().setFeature("http://xml.org/sax/features/namespaces", true);
        getParent().setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        getParent().setProperty("http://xml.org/sax/properties/lexical-handler",this);
        
        nodeIdUserData = new HashMap();
        String base = input.getSystemId();
        if (base==null)
            warning(WARN_XMLBASE_MISSING,"Base URI not specified for input file; local references will expand incorrectly.");
        else 
            base = ParserSupport.truncateXMLBase(base);
            
        documentContext = new XMLContext(base,"");
        // Start the RDFParser
        pipe = new TokenPipe(this);
        parsersLastException = null;
        final Thread xmlParser = Thread.currentThread();
        
       // Flag to denote when a parse has been terminated unnaturally.  It
       // must be declared final to be accessible in an anonymous inner class.
        final boolean[] unnaturalTermination = new boolean[1];

        Thread rdfParser = new Thread(){
            public void run() {
                try {
                    RDFParser p = new RDFParser(pipe,ARPFilter.this);
                    unnaturalTermination[0] = false;
                    if ( embedding )
                       p.embeddedFile( documentContext );
                    else 
                        p.rdfFile( documentContext );
                }
                catch (WrappedException wrapped) {
                    xmlParser.interrupt();
                    parsersLastException = wrapped;
                }
                catch (ParseException parse) {
                    parsersLastException = parse;
                    xmlParser.interrupt();
                }
                catch ( CoRoutineDeathException death ) {
                }
                catch (Exception e) {
                    // Unnatural termiantion.
                    System.err.println("ARP - Internal Error: " + ParseException.formatMessage(e));
                    e.printStackTrace();
                    parsersLastException = e;
                    
                    // Set a flag to indicate that unnatural termination has occurred.
                    unnaturalTermination[0] = true;

                    try {
                       getErrorHandler().fatalError(new SAXParseException("ARP internal error",null,e));
                    }
                    catch (Exception saxe) {
                    }
                    // Patch from Colin Britton.
                    // If the RDFParser is still going - interrupt it.
                    // ******************************************************
                    // Current thread should be interrupted only after other
                    // processing (e.g. the fatalError() handler method) has
                    // been completed.
                    // ******************************************************
                    xmlParser.interrupt();
                }
                catch (Throwable th) {
                    parsersLastException = th;
                    xmlParser.interrupt();
                }
            }
        };
        try {
            rdfParser.start();
            super.parse(input);
            pipe.putNextToken(new Token(EOF,null));
            rdfParser.join();
            // Wait for the RDFParser
        }
        catch ( InterruptedException e ) {
            // Might happen,
        }
        catch (CoRoutineDeathException e) {
            // Might happen,
        }
        catch ( WrappedException we ) {
            we.throwMe();
        }
        catch ( SAXException saxe ) {
            // From the XML parser
            warning( WARN_BAD_XML, "Failed to parse XML.");
            throw saxe;  // I think the handler will have seen this before.
        } 
        catch ( IOException ioe ) {
            throw ioe;
        }
        catch ( Throwable ee ) {
            System.err.println("ARP - Internal Error: " + ee.getMessage());
            ee.printStackTrace();
            parsersLastException = ee;
        }
        finally {
            // If the RDFParser is still going - interrupt it.
            rdfParser.interrupt();
// Patch thanks to David Makepeace and Andrew Newman
            try {
              // Do not add an EOF token to the pipe if the RDF parser has been
              // unnaturally terminated because tokens are no longer being
              // consumed from the TokenPipe, so an infinite loop will result.
              if (unnaturalTermination[0] == false) {
                pipe.putNextToken(new Token(EOF,null));
              }
              rdfParser.join();
            } catch (InterruptedException ex) {
              // Ignore
            }
// End Patch

            locator = null;
        }
        if ( parsersLastException != null ) {
            if ( parsersLastException instanceof WrappedException ) {
                ((WrappedException)parsersLastException).throwMe();
            }
            if ( parsersLastException instanceof RuntimeException )
                throw (RuntimeException)parsersLastException;
            if ( parsersLastException instanceof Error )
                throw (Error)parsersLastException;
            if ( parsersLastException instanceof Exception ) {
                if ( !( parsersLastException instanceof SAXException ) )
                    parsersLastException = new SAXException((Exception)parsersLastException);
                if ( parsersLastException instanceof SAXParseException )
                    getErrorHandler().fatalError((SAXParseException)parsersLastException);
                else
                    throw (SAXException)parsersLastException;
            }
            //  throw parsersLastException;
            return;
        }
        
    //    System.out.println("Parse completed");
        
    }
    
    StatementHandler setStatementHandler(StatementHandler sh) {
        StatementHandler old = statementHandler;
        statementHandler = sh;
        return old;
    }
    StatementHandler statementHandler =
    new StatementHandler() {
        public void statement(AResource s,AResource p,AResource o){
        }
        public void statement(AResource s,AResource p,ALiteral o){
        }
    };
    // accessed in ARPQname.
    XMLContext documentContext;
    //String documentURI;
    private TokenPipe pipe;
    private Locator locator;
    static final String rdfns = "http://www.w3.org/1999/02/22-rdf-syntax-ns#".intern();
    static final String xmlns = "http://www.w3.org/XML/1998/namespace".intern();
    static final Map rdfnames = new HashMap();
    static {
        rdfnames.put("Description",new Integer(E_DESCRIPTION));
        rdfnames.put("RDF",new Integer(E_RDF));
        rdfnames.put("li",new Integer(E_LI));
    }
    static final Set
    knownRDFProperties = new HashSet();
    static final Set
    knownRDFTypes = knownRDFProperties; // WG decision makes this distinction spurious.
    //new HashSet();
    static {
        knownRDFTypes.add("Bag");
        knownRDFTypes.add("Seq");
        knownRDFTypes.add("Alt");
        knownRDFTypes.add("Property");
        knownRDFProperties.add("type");
        knownRDFTypes.add("Statement");
        knownRDFProperties.add("subject");
        knownRDFProperties.add("predicate");
        knownRDFProperties.add("object");
        knownRDFProperties.add("value");
    }
    static final Set
    knownBadRDFNames = new HashSet(); 
    static {
        knownBadRDFNames.add("ID");
        knownBadRDFNames.add("about");
        knownBadRDFNames.add("aboutEach");
        knownBadRDFNames.add("aboutEachPrefix");
        knownBadRDFNames.add("resource");
        knownBadRDFNames.add("bagID");
        knownBadRDFNames.add("parseType");
        knownBadRDFNames.add("li");
        knownBadRDFNames.add("type");
        knownBadRDFNames.add("Description");
        knownBadRDFNames.add("nodeID");
    }
    // The order of these must match their occurrence in grammar rules.
    static private String specialAtts[] = {
        "base",
        "lang",
        "space",
        "ID",
        "about",
        "nodeID",
        "resource",
        "bagID",
        "datatype",
        "parseType",
        "type"
    };
    static private String specialNameSpaces[] = {
        xmlns,
        xmlns,
        xmlns,
        rdfns,
        rdfns,
        rdfns,
        rdfns,
        rdfns,
        rdfns,
        rdfns,
        rdfns
    };
    static private int A_XMLSPACE=-1;
    static private int specialAttValues[] = {
        A_XMLBASE,
        A_XMLLANG,
        A_XMLSPACE,
        A_ID,
        A_ABOUT,
        A_NODEID,
        A_RESOURCE,
        A_BAGID,
        A_DATATYPE,

⌨️ 快捷键说明

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