uk.ac.manchester.cs.owl.owlapi.turtle.parser.TurtleParser.jj Maven / Gradle / Ivy
options {
JAVA_UNICODE_ESCAPE=true;
STATIC=false;
LOOKAHEAD=2;
//DEBUG_TOKEN_MANAGER=true;
//DEBUG_PARSER=true;
}
PARSER_BEGIN(TurtleParser)
package uk.ac.manchester.cs.owl.owlapi.turtle.parser;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
import org.semanticweb.owlapi.vocab.XSDVocabulary;
import org.semanticweb.owlapi.model.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.Reader;
import java.io.InputStream;
import org.coode.owlapi.rdfxml.parser.AnonymousNodeChecker;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.util.EscapeUtils;
@SuppressWarnings("javadoc")
public class TurtleParser implements AnonymousNodeChecker {
private Map string2IRI;
private String base;
private AtomicInteger blankNodeId;
private TripleHandler handler;
private DefaultPrefixManager pm = new DefaultPrefixManager();
public TurtleParser(Reader reader, TripleHandler handler, String base) {
this(reader);
this.handler = handler;
this.base = base;
string2IRI = new HashMap();
blankNodeId = new AtomicInteger(0);
pm.setDefaultPrefix("http://www.semanticweb.org/owl/owlapi/turtle#");
}
public TurtleParser(InputStream is, TripleHandler handler, String base) {
this(is);
this.handler = handler;
this.base = base;
string2IRI = new HashMap();
blankNodeId = new AtomicInteger(0);
pm.setDefaultPrefix("http://www.semanticweb.org/owl/owlapi/turtle#");
}
public DefaultPrefixManager getPrefixManager() {
return pm;
}
public void setTripleHandler(TripleHandler handler) {
this.handler = handler;
}
public boolean isAnonymousNode(String iri) {
return iri.indexOf("genid") != -1;
}
public boolean isAnonymousNode(IRI iri) {
return iri.toString().indexOf("genid") != -1;
}
public boolean isAnonymousSharedNode(String iri) {
return iri.indexOf("genid-nodeid") != -1;
}
protected IRI getNextBlankNode(String id) {
IRI iri;
if(id == null) {
iri = getIRI("genid" + blankNodeId.getAndIncrement());
}
else {
iri = getIRI("genid-nodeid-" + id);
}
return iri;
}
protected IRI getIRIFromQName(String qname) throws ParseException {
int colonIndex = qname.indexOf(':');
if(colonIndex == -1) {
throw new ParseException("Not a valid qname (missing ':') " + qname);
}
String prefix = qname.substring(0, colonIndex + 1);
if(prefix.equals("_:")) {
return getIRI("genid" + qname.substring(colonIndex + 1));
}
if(!pm.containsPrefixMapping(prefix)) {
throw new ParseException("Prefix not declared: " + prefix);
}
return pm.getIRI(qname);
}
public IRI getIRI(String s) {
if(s.charAt(0) == '<') {
s = s.substring(1, s.length() - 1);
}
IRI iri = string2IRI.get(s);
if(iri == null) {
iri = IRI.create(s);
if (!iri.isAbsolute()) {
iri = IRI.create(base.substring(0, base.lastIndexOf('/') + 1) + s);
}
string2IRI.put(s, iri);
}
return iri;
}
}
PARSER_END(TurtleParser)
SKIP: {" " | "\n" | "\t" | "\r"}
SKIP:
{
}
/////////////////////////////////////////////////////////////////////////////////////////////
MORE : {
: IN_STRING
}
MORE :
{
}
MORE :
{
}
TOKEN:
{
: DEFAULT
}
MORE : {
"\"\"\"" : IN_LONG_STRING
}
MORE :
{
< ~[] >
}
TOKEN :
{
: DEFAULT
}
/////////////////////////////////////////////////////////////////////////////////////////////
TOKEN:
{
}
TOKEN:
{
)+ >
}
TOKEN:
{
)+ ("." ()*)? | "." ()+ ) >
}
TOKEN:
{
)+ "." ()* | "." ()+ | ()+ )>
}
TOKEN:
{
)+>
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
>
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
TOKEN:
{
}
////////////////////////////////////////////////////////////////////////////////////////////
//
// IRIs
TOKEN:
{
", " "])*">">
}
TOKEN:
{
)? ":">
}
TOKEN:
{
| ["0"-"9"] ) ((|".")* )?>
}
TOKEN:
{
>
}
TOKEN:
{
((|".")* )?>
}
TOKEN:
{
}
TOKEN:
{
| "-"
| ["0"-"9"]
| "\u00B7"
| ["\u0300"-"\u036F"]
| ["\u203F"-"\u2040"]>
}
TOKEN:
{
| "_">
}
TOKEN:
{
>
}
TOKEN:
{
}
/////////////////////////////////////////////////////////////////////////////////////////////
//TOKEN:
//{
// ~["\""])>
//}
void parseDocument() :
{
}
{
(parseDirective() | parseStatement())+ {handler.handleEnd();}
}
void parseDirective() :
{
}
{
parsePrefixDirective() | parseBaseDirective()
}
void parsePrefixDirective() :
{
Token t;
String prefix = "";
IRI ns;
}
{
t={prefix=t.image;} ns=parseIRI() {
pm.setPrefix(prefix, ns.toString());
handler.handlePrefixDirective(prefix, ns.toString());
}
}
void parseBaseDirective() :
{
Token t;
}
{
t= {base = t.image.substring(1, t.image.length() - 1);} {
handler.handleBaseDirective(base);
}
}
void parseStatement() :
{
}
{
parseTriples()
}
void parseTriples() :
{
IRI subject;
}
{
subject = parseSubject() (parsePredicateObjectList(subject))?
}
IRI parseSubject() :
{
IRI iri;
}
{
(iri=parseResource() | iri=parseBlankNode()) {
return iri;
}
}
IRI parseLoneNS() :
{
Token t;
}
{
t= {
return getIRIFromQName(t.image);
}
}
IRI parseAbbreviatedIRI() :
{
Token t;
}
{
t= {
return getIRIFromQName(t.image);
}
}
IRI parseIRI() :
{
Token t;
}
{
t= {return getIRI(t.image);}
}
IRI parseBlankNode() :
{
IRI iri = null;
Token t;
}
{
(iri=parseNodeID()
|
{if(iri==null){iri = getNextBlankNode(null);}}
|
({if(iri==null){iri = getNextBlankNode(null);}} parsePredicateObjectList(iri) ()?)? {if (iri == null) {iri = getNextBlankNode(null); }}
|
iri = parseCollection()) {
return iri;
}
}
IRI parseNodeID() :
{
Token t;
}
{
t= {
return getNextBlankNode(t.image);
}
}
void parsePredicateObjectList(IRI subject) :
{
IRI predicate;
}
{
(predicate=parseVerb() parseObjectList(subject, predicate) ( predicate=parseVerb() parseObjectList(subject, predicate) )*) ()?
}
IRI parseVerb() :
{
IRI iri;
}
{
({iri = OWLRDFVocabulary.RDF_TYPE.getIRI();} | iri=parsePredicate()) {
return iri;
}
}
IRI parsePredicate() :
{
IRI iri;
}
{
iri=parseResource() {
return iri;
}
}
IRI parseResource() :
{
IRI iri;
}
{
(iri=parseIRI() | LOOKAHEAD(2)iri=parseAbbreviatedIRI()| LOOKAHEAD(2)iri=parseLoneNS()) {
return iri;
}
}
void parseObjectList(IRI subject, IRI predicate) :
{
}
{
parseObject(subject, predicate) ( parseObject(subject, predicate) )*
}
void parseObject(IRI subject, IRI predicate) :
{
IRI resObject;
}
{
(parseLiteral(subject, predicate)
|
((resObject=parseResource()
|
resObject=parseBlankNode())) {
handler.handleTriple(subject, predicate, resObject);
})
}
IRI parseCollection() :
{
IRI iri;
}
{
iri=parseItemList() {
return iri;
}
}
IRI parseItemList() :
{
// _x rdf:type rdf:List
// _x rdf:first
// _x rdf:next
IRI firstSubject = OWLRDFVocabulary.RDF_NIL.getIRI();
IRI subject = null;
IRI type = OWLRDFVocabulary.RDF_TYPE.getIRI();
IRI first = OWLRDFVocabulary.RDF_FIRST.getIRI();
IRI rest = OWLRDFVocabulary.RDF_REST.getIRI();
IRI list = OWLRDFVocabulary.RDF_LIST.getIRI();
IRI nil = OWLRDFVocabulary.RDF_NIL.getIRI();
}
{
(
{
IRI prevSubject = subject;
subject=getNextBlankNode(null);
if(prevSubject != null) {
handler.handleTriple(prevSubject, rest, subject);
}
else {
firstSubject = subject;
}
if(subject!=null) {
handler.handleTriple(subject, type, list);
}
}
parseObject(subject, first))* {
// Terminate list
if(subject!=null) {
handler.handleTriple(subject, rest, nil);
}
return firstSubject;
}
}
void parseLiteral(IRI subject, IRI predicate) :
{
String literal;
String lang = null;
IRI datatype = null;
Token t;
}
{
(literal=parseQuotedString() (( datatype=parseResource()) | (("@" (t=)){lang=t.image;}))?
{
if(datatype != null) {
handler.handleTriple(subject, predicate, literal, datatype);
}
else if(lang != null) {
handler.handleTriple(subject, predicate, literal, lang);
}
else {
handler.handleTriple(subject, predicate, literal);
}
}
)
|
(literal=parseInteger(){handler.handleTriple(subject, predicate, literal, XSDVocabulary.INTEGER.getIRI());})
|
(literal=parseDouble(){handler.handleTriple(subject, predicate, literal, XSDVocabulary.DOUBLE.getIRI());})
|
(literal=parseDecimal(){handler.handleTriple(subject, predicate, literal, XSDVocabulary.DECIMAL.getIRI());})
|
(literal=parseBoolean(){handler.handleTriple(subject, predicate, literal, XSDVocabulary.BOOLEAN.getIRI());})
}
String parseInteger() :
{
Token t;
}
{
t= {
return t.image;
}
|
t= {
return t.image;
}
}
String parseDouble() :
{
Token t;
}
{
t= {
return t.image;
}
}
String parseDecimal() :
{
Token t;
}
{
t= {
return t.image;
}
}
String parseBoolean() :
{
Token t;
}
{
(t= | t=) {
return t.image;
}
}
String parseQuotedString() :
{
String s;
}
{
s=parseString() {
return s;
}
}
String parseString() :
{
Token t;
String rawString = "";
}
{
(t= {
rawString = t.image.substring(1, t.image.length() - 1);
}
| t= {
rawString = t.image.substring(3, t.image.length() - 3);
}) {
return EscapeUtils.unescapeString(rawString);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy