org.semanticweb.owlapi.rdf.syntax
Class RDFParser

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by org.semanticweb.owlapi.rdf.syntax.RDFParser
All Implemented Interfaces:
RDFConstants, org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class RDFParser
extends org.xml.sax.helpers.DefaultHandler
implements RDFConstants

This class parses the RDF according to the syntax specified in http://www.w3.org/TR/rdf-syntax-grammar/.


Nested Class Summary
protected  class RDFParser.EmptyPropertyElement
          Parses emptyPropertyElt production.
protected  class RDFParser.NodeElement
          Parses the nodeElement production.
protected  class RDFParser.NodeElementList
          Parses the nodeElementList production.
protected  class RDFParser.ParseTypeCollectionPropertyElement
          Parses parseTypeCollectionPropertyElt production.
protected  class RDFParser.ParseTypeLiteralPropertyElement
          Parses parseTypeLiteralPropertyElt production.
protected  class RDFParser.ParseTypeResourcePropertyElement
          Parses parseTypeResourcePropertyElt production.
protected  class RDFParser.PropertyElementList
          Parses the propertyEltList production.
protected static class RDFParser.ReificationManager
           
protected  class RDFParser.ReifiedStatementBag
           
protected  class RDFParser.ResourceOrLiteralPropertyElement
          Parses resourcePropertyElt or literalPropertyElt productions.
protected  class RDFParser.StartRDF
          State expecting start of RDF text.
protected static class RDFParser.State
          Base class for all parser states.
 
Field Summary
protected  IRI m_baseIRI
          IRI of the document being parsed.
protected  java.util.LinkedList<IRI> m_baseIRIs
          Stack of base IRIs.
protected  RDFConsumer m_consumer
          Consumer receiving notifications about parsing events.
protected  org.xml.sax.Locator m_documentLocator
          Document locator.
protected  org.xml.sax.ErrorHandler m_errorHandler
          Registered error handler.
protected  int m_generatedIRIIndex
          Number of the last generated IRI.
protected  java.lang.String m_language
          The current language.
protected  java.util.LinkedList<java.lang.String> m_languages
          The stack of languages.
protected  RDFParser.State m_state
          Current parser's state.
protected  java.util.List<RDFParser.State> m_states
          Stack of parser states.
protected static org.xml.sax.Locator s_nullDocumentLocator
           
protected static javax.xml.parsers.SAXParserFactory s_parserFactory
           
protected  java.util.Map<java.lang.String,IRI> uriCache
           
 
Fields inherited from interface org.semanticweb.owlapi.rdf.util.RDFConstants
ATTR_ABOUT, ATTR_ABOUT_EACH, ATTR_ABOUT_EACH_PREFIX, ATTR_BAG_ID, ATTR_DATATYPE, ATTR_ID, ATTR_NODE_ID, ATTR_PARSE_TYPE, ATTR_RESOURCE, ELT_ALT, ELT_BAG, ELT_DESCRIPTION, ELT_LI, ELT_RDF, ELT_SEQ, ELT_TYPE, KAONNS, PARSE_TYPE_COLLECTION, PARSE_TYPE_LITERAL, PARSE_TYPE_RESOURCE, RDF_ABOUT, RDF_BAG, RDF_DATATYPE, RDF_DESCRIPTION, RDF_FIRST, RDF_ID, RDF_LI, RDF_LIST, RDF_NIL, RDF_NODE_ID, RDF_OBJECT, RDF_PARSE_TYPE, RDF_PREDICATE, RDF_PROPERTY, RDF_RDF, RDF_RESOURCE, RDF_REST, RDF_STATEMENT, RDF_SUBJECT, RDF_TYPE, RDF_XMLLITERAL, RDFNS, RDFS_CLASS, RDFS_COMMENT, RDFS_DOMAIN, RDFS_LABEL, RDFS_RANGE, RDFS_SUBCLASSOF, RDFS_SUBPROPERTYOF, RDFSNS, XMLLANG, XMLNS
 
Constructor Summary
RDFParser()
          Creates a RDF parser.
 
Method Summary
 void characters(char[] data, int start, int length)
          Called when character content is parsed.
protected  void checkUnsupportedAttributes(org.xml.sax.Attributes atts)
          Checks if attribute list contains some of the unsupported attributes.
 void endDocument()
          Called when document parsing is ended.
 void endElement(java.lang.String namespaceIRI, java.lang.String localName, java.lang.String qName)
          Called when element parsing is ended.
 void error(org.xml.sax.SAXParseException e)
          Called when error is encountered.
 void fatalError(org.xml.sax.SAXParseException e)
          Called when a fatal error is encountered.
protected  java.lang.String getIDNodeIDAboutResourceIRI(org.xml.sax.Attributes atts)
          Extracts the IRI of the resource from rdf:ID, rdf:nodeID or rdf:about attribute.
 IRI getIRI(java.lang.String s)
           
protected  java.lang.String getIRIFromAbout(java.lang.String about)
          Returns an absolute IRI from an about attribute.
protected  java.lang.String getIRIFromID(java.lang.String id)
          Returns an absolute IRI from an ID.
protected  java.lang.String getIRIFromNodeID(java.lang.String nodeID)
          Returns an abolute IRI from a nodeID attribute.
protected  java.lang.String getIRIFromResource(java.lang.String resource)
          Returns an absolute IRI from a resource attribute.
protected  java.lang.String getNodeIDResourceResourceIRI(org.xml.sax.Attributes atts)
          Extracts the IRI of the resource from rdf:resource or rdf:nodeID attribute.
protected  RDFParser.ReificationManager getReificationManager(org.xml.sax.Attributes atts)
          Returns the reification manager for given attributes.
 boolean isAnonymousNodeID(java.lang.String iri)
           
 boolean isAnonymousNodeIRI(java.lang.String uri)
          Tests whether supplied IRI was generated by this parser in order to label an anonymous node.
protected  boolean isWhitespaceOnly(char[] data, int start, int length)
          Checks whether given characters contain only whitespace.
protected  boolean isWhitespaceOnly(java.lang.StringBuilder buffer)
          Checks whether given characters contain only whitespace.
protected  java.lang.String nextAnonymousIRI()
          Generates next anonymous IRI.
 void parse(org.xml.sax.InputSource source, RDFConsumer consumer)
          Parses RDF from given input source.
protected  java.util.Map parseStringArguments(java.lang.String string)
          Parses the string into a map of name-value pairs.
protected  void popState()
          Pops a state from the stack.
 void processingInstruction(java.lang.String target, java.lang.String data)
          Called when processing instruction is parsed.
protected  void processXMLBase(org.xml.sax.Attributes atts)
          Processes xml:base reference if there is one.
protected  void processXMLLanguage(org.xml.sax.Attributes atts)
          Processes xml:language reference is there is one.
protected  void propertyAttributes(java.lang.String subjectIRI, org.xml.sax.Attributes atts, RDFParser.ReificationManager reificationManager)
          Parses the propertyAttributes production.
protected  void pushState(RDFParser.State state)
          Pushes a new state on the state stack.
protected  java.lang.String resolveIRI(java.lang.String uri)
          Resolves an IRI with the current base.
 void setDocumentLocator(org.xml.sax.Locator locator)
          Called to receive a document locator.
 void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
          Sets the error handler.
 void startDocument()
          Called when document parsing is started.
 void startElement(java.lang.String namespaceIRI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
          Called when an element is started.
protected  void statementWithLiteralValue(java.lang.String subject, java.lang.String predicate, java.lang.String object, java.lang.String dataType, java.lang.String reificationID)
          Called when a statement with literal value is added to the model.
protected  void statementWithResourceValue(java.lang.String subject, java.lang.String predicate, java.lang.String object, java.lang.String reificationID)
          Called when a statement with resource value is added to the model.
 void warning(org.xml.sax.SAXParseException e)
          Called when warning is encountered.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, ignorableWhitespace, notationDecl, resolveEntity, skippedEntity, startPrefixMapping, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

s_nullDocumentLocator

protected static final org.xml.sax.Locator s_nullDocumentLocator

s_parserFactory

protected static final javax.xml.parsers.SAXParserFactory s_parserFactory

uriCache

protected java.util.Map<java.lang.String,IRI> uriCache

m_errorHandler

protected org.xml.sax.ErrorHandler m_errorHandler
Registered error handler.


m_baseIRIs

protected java.util.LinkedList<IRI> m_baseIRIs
Stack of base IRIs.


m_baseIRI

protected IRI m_baseIRI
IRI of the document being parsed.


m_languages

protected java.util.LinkedList<java.lang.String> m_languages
The stack of languages.


m_language

protected java.lang.String m_language
The current language.


m_consumer

protected RDFConsumer m_consumer
Consumer receiving notifications about parsing events.


m_state

protected RDFParser.State m_state
Current parser's state.


m_states

protected java.util.List<RDFParser.State> m_states
Stack of parser states.


m_generatedIRIIndex

protected int m_generatedIRIIndex
Number of the last generated IRI.


m_documentLocator

protected org.xml.sax.Locator m_documentLocator
Document locator.

Constructor Detail

RDFParser

public RDFParser()
Creates a RDF parser.

Method Detail

parse

public void parse(org.xml.sax.InputSource source,
                  RDFConsumer consumer)
           throws org.xml.sax.SAXException,
                  java.io.IOException
Parses RDF from given input source.

Parameters:
source - specifies where RDF comes from
consumer - receives notifications about RDF parsing events
Throws:
org.xml.sax.SAXException
java.io.IOException

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Called to receive a document locator.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler
Overrides:
setDocumentLocator in class org.xml.sax.helpers.DefaultHandler
Parameters:
locator - the document locator

setErrorHandler

public void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
Sets the error handler.

Parameters:
errorHandler - the error handler

warning

public void warning(org.xml.sax.SAXParseException e)
             throws org.xml.sax.SAXException
Called when warning is encountered.

Specified by:
warning in interface org.xml.sax.ErrorHandler
Overrides:
warning in class org.xml.sax.helpers.DefaultHandler
Parameters:
e - the exception
Throws:
org.xml.sax.SAXException

error

public void error(org.xml.sax.SAXParseException e)
           throws org.xml.sax.SAXException
Called when error is encountered.

Specified by:
error in interface org.xml.sax.ErrorHandler
Overrides:
error in class org.xml.sax.helpers.DefaultHandler
Parameters:
e - the exception
Throws:
org.xml.sax.SAXException

fatalError

public void fatalError(org.xml.sax.SAXParseException e)
                throws org.xml.sax.SAXException
Called when a fatal error is encountered.

Specified by:
fatalError in interface org.xml.sax.ErrorHandler
Overrides:
fatalError in class org.xml.sax.helpers.DefaultHandler
Parameters:
e - the exception
Throws:
org.xml.sax.SAXException

startDocument

public void startDocument()
Called when document parsing is started.

Specified by:
startDocument in interface org.xml.sax.ContentHandler
Overrides:
startDocument in class org.xml.sax.helpers.DefaultHandler

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Called when document parsing is ended.

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

startElement

public void startElement(java.lang.String namespaceIRI,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException
Called when an element is started.

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
namespaceIRI - the IRI of the namespace
localName - the local name of the element
qName - the Q-name of the element
atts - the attributes
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String namespaceIRI,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Called when element parsing is ended.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
namespaceIRI - the IRI of the namespace
localName - the local name of the element
qName - the Q-name of the element
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] data,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Called when character content is parsed.

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Parameters:
data - the data buffer containing the characters
start - the start index of character text
length - the length of the character text
Throws:
org.xml.sax.SAXException

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Called when processing instruction is parsed.

Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Overrides:
processingInstruction in class org.xml.sax.helpers.DefaultHandler
Parameters:
target - the name of the processing instruction
data - the argument to the processing instruction
Throws:
org.xml.sax.SAXException

pushState

protected void pushState(RDFParser.State state)
Pushes a new state on the state stack.

Parameters:
state - new state

popState

protected void popState()
                 throws org.xml.sax.SAXException
Pops a state from the stack.

Throws:
org.xml.sax.SAXException

checkUnsupportedAttributes

protected void checkUnsupportedAttributes(org.xml.sax.Attributes atts)
                                   throws org.xml.sax.SAXException
Checks if attribute list contains some of the unsupported attributes.

Parameters:
atts - the attributes
Throws:
org.xml.sax.SAXException

processXMLBase

protected void processXMLBase(org.xml.sax.Attributes atts)
                       throws org.xml.sax.SAXException
Processes xml:base reference if there is one.

Parameters:
atts - the attributes potentially containing xml:base declaration
Throws:
org.xml.sax.SAXException

processXMLLanguage

protected void processXMLLanguage(org.xml.sax.Attributes atts)
Processes xml:language reference is there is one.

Parameters:
atts - the attributes potentially containing xml:language declaration

resolveIRI

protected java.lang.String resolveIRI(java.lang.String uri)
                               throws org.xml.sax.SAXException
Resolves an IRI with the current base.

Parameters:
uri - the IRI being resolved
Returns:
the resolved IRI
Throws:
org.xml.sax.SAXException

getIRIFromID

protected java.lang.String getIRIFromID(java.lang.String id)
                                 throws org.xml.sax.SAXException
Returns an absolute IRI from an ID.

Throws:
org.xml.sax.SAXException

getIRIFromAbout

protected java.lang.String getIRIFromAbout(java.lang.String about)
                                    throws org.xml.sax.SAXException
Returns an absolute IRI from an about attribute.

Throws:
org.xml.sax.SAXException

getIRIFromNodeID

protected java.lang.String getIRIFromNodeID(java.lang.String nodeID)
                                     throws org.xml.sax.SAXException
Returns an abolute IRI from a nodeID attribute.

Throws:
org.xml.sax.SAXException

getIRIFromResource

protected java.lang.String getIRIFromResource(java.lang.String resource)
                                       throws org.xml.sax.SAXException
Returns an absolute IRI from a resource attribute.

Throws:
org.xml.sax.SAXException

nextAnonymousIRI

protected java.lang.String nextAnonymousIRI()
                                     throws org.xml.sax.SAXException
Generates next anonymous IRI.

Throws:
org.xml.sax.SAXException

getIDNodeIDAboutResourceIRI

protected java.lang.String getIDNodeIDAboutResourceIRI(org.xml.sax.Attributes atts)
                                                throws org.xml.sax.SAXException
Extracts the IRI of the resource from rdf:ID, rdf:nodeID or rdf:about attribute. If no attribute is found, an IRI is generated.

Throws:
org.xml.sax.SAXException

getNodeIDResourceResourceIRI

protected java.lang.String getNodeIDResourceResourceIRI(org.xml.sax.Attributes atts)
                                                 throws org.xml.sax.SAXException
Extracts the IRI of the resource from rdf:resource or rdf:nodeID attribute. If no attribute is found, null is returned.

Parameters:
atts - the attributes
Returns:
the IRI of the resource or null
Throws:
org.xml.sax.SAXException

statementWithResourceValue

protected void statementWithResourceValue(java.lang.String subject,
                                          java.lang.String predicate,
                                          java.lang.String object,
                                          java.lang.String reificationID)
                                   throws org.xml.sax.SAXException
Called when a statement with resource value is added to the model.

Parameters:
subject - IRI of the subject resource
predicate - IRI of the predicate resource
object - IRI of the object resource
reificationID - if not null, contains IRI of the resource that will wold the reified statement
Throws:
org.xml.sax.SAXException

statementWithLiteralValue

protected void statementWithLiteralValue(java.lang.String subject,
                                         java.lang.String predicate,
                                         java.lang.String object,
                                         java.lang.String dataType,
                                         java.lang.String reificationID)
                                  throws org.xml.sax.SAXException
Called when a statement with literal value is added to the model.

Parameters:
subject - IRI of the subject resource
predicate - IRI of the predicate resource
object - literal object value
dataType - the IRI of the literal's datatype (may be null)
reificationID - if not null, contains IRI of the resource that will wold the reified statement
Throws:
org.xml.sax.SAXException

propertyAttributes

protected void propertyAttributes(java.lang.String subjectIRI,
                                  org.xml.sax.Attributes atts,
                                  RDFParser.ReificationManager reificationManager)
                           throws org.xml.sax.SAXException
Parses the propertyAttributes production.

Parameters:
subjectIRI - IRI of the resource whose properties are being parsed
atts - attributes
reificationManager - the reification manager
Throws:
org.xml.sax.SAXException

isWhitespaceOnly

protected boolean isWhitespaceOnly(char[] data,
                                   int start,
                                   int length)
Checks whether given characters contain only whitespace.

Parameters:
data - the data being checked
start - the start index (inclusive)
length - the end index (non-inclusive)
Returns:
true if characters contain whitespace

isWhitespaceOnly

protected boolean isWhitespaceOnly(java.lang.StringBuilder buffer)
Checks whether given characters contain only whitespace.

Parameters:
buffer - the data being checked
Returns:
true if characters contain whitespace

getReificationManager

protected RDFParser.ReificationManager getReificationManager(org.xml.sax.Attributes atts)
                                                      throws org.xml.sax.SAXException
Returns the reification manager for given attributes.

Parameters:
atts - the attributes
Returns:
the reification manager
Throws:
org.xml.sax.SAXException

parseStringArguments

protected java.util.Map parseStringArguments(java.lang.String string)
                                      throws org.xml.sax.SAXException
Parses the string into a map of name-value pairs.

Parameters:
string - string to be parsed
Returns:
map of name-value pairs
Throws:
org.xml.sax.SAXException - if there was an IOException this will be wrapped in a parse exception

isAnonymousNodeIRI

public boolean isAnonymousNodeIRI(java.lang.String uri)
Tests whether supplied IRI was generated by this parser in order to label an anonymous node.

Parameters:
uri - the IRI
Returns:
true if the IRI was generated by this parser to label an anonymous node

isAnonymousNodeID

public boolean isAnonymousNodeID(java.lang.String iri)

getIRI

public IRI getIRI(java.lang.String s)