
com.weaverplatform.sp4rql.model.token.TokenFactory Maven / Gradle / Ivy
package com.weaverplatform.sp4rql.model.token;
import com.weaverplatform.sp4rql.error.NotSupportedException;
import com.weaverplatform.sp4rql.error.ParseException;
import com.weaverplatform.sp4rql.model.restriction.QuadRestriction;
import com.weaverplatform.sp4rql.model.scope.ScopeQuery;
import com.weaverplatform.sp4rql.model.scope.Sp4rqlScope;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.TerminalNode;
import sp4rql.Sp4rql11Parser;
import java.util.List;
public class TokenFactory {
int blankNodeIndex = 0;
private ScopeQuery queryUnit;
public void setQueryUnit(ScopeQuery queryUnit) {
this.queryUnit = queryUnit;
}
public String expandBase(String iri) {
if(queryUnit.getBaseIri() == null && queryUnit.getPrefixMap() == null) {
return iri;
}
if(queryUnit.getBaseIri() != null && !isAbsolute(iri)) {
return queryUnit.getBaseIri() + iri;
} else {
return iri;
}
}
// Copied from https://stackoverflow.com/questions/10687099/how-to-test-if-a-url-string-is-absolute-or-relative
private boolean isAbsolute(String iri) {
if(iri.indexOf("//") == 0) { return true; } // URL is protocol-relative (= absolute)
if(iri.indexOf("://") == -1) { return false; } // URL has no protocol (= relative)
if(iri.indexOf(".") == -1) { return false; } // URL does not contain a dot, i.e. no TLD (= relative, possibly REST)
if(iri.indexOf("/") == -1) { return false; } // URL does not contain a single slash (= relative)
if(iri.indexOf(":") > iri.indexOf("/")) { return false; } // The first colon comes after the first slash (= relative)
if(iri.indexOf("://") < iri.indexOf(".")) { return true; } // Protocol is defined before first dot (= absolute)
return false; // Anything else must be relative
}
public BlankToken addBlank() {
BlankToken token = new BlankToken(String.format("b%d", blankNodeIndex++));
return token;
}
public BlankToken addBlank(String label) {
BlankToken token = new BlankToken(label);
return token;
}
public String getNamespace(String prefix) {
if(!queryUnit.getPrefixMap().containsKey(prefix)) {
throw new ParseException(String.format("Could not find prefix %s in prefix map", prefix));
}
return queryUnit.getPrefixMap().get(prefix);
}
public ReferenceToken abbreviatedToToken(String abbreviated) {
if(abbreviated == null || abbreviated.indexOf(":") < 0) {
throw new ParseException(String.format("Could not locate the colon in the abbreviated uri %s", abbreviated));
}
String[] parts = abbreviated.split(":");
String namespace = parts[0];
String localName = parts[1];
return new ReferenceToken(String.format("%s%s", getNamespace(namespace), localName));
}
public Sp4rqlToken getToken(Sp4rql11Parser.VerbContext context) throws ParseException {
if(context.varOrIri() != null) {
return getToken(context.varOrIri());
}
if(context.A() != null) {
return getA();
}
throw new ParseException("Failed parsing verb");
}
public Sp4rqlToken getToken(Sp4rql11Parser.VarOrIriContext context) throws ParseException {
if(context.var() != null) {
return getToken(context.var());
}
if(context.iri() != null) {
return getToken(context.iri());
}
throw new ParseException("Failed parsing varOrIri");
}
public ReferenceToken getA() {
return new ReferenceToken("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
}
public String rawIri(ParserRuleContext context) {
String value = null;
List list;
list = context.getTokens(Sp4rql11Parser.IRIREF);
if (!list.isEmpty()) {
String quotedValue = list.get(0).getText();
value = quotedValue.substring(1, quotedValue.length() - 1);
}
list = context.getTokens(Sp4rql11Parser.UNQUOTED_IRIREF);
if (!list.isEmpty()) {
value = list.get(0).getText();
}
if(value == null) {
throw new ParseException("No value found for IRI on context");
}
return value;
}
public ReferenceToken parseIri(ParserRuleContext context) {
String value = rawIri(context);
return new ReferenceToken(expandBase(value));
}
public ReferenceToken getToken(Sp4rql11Parser.PrefixedNameContext context) {
if(context.PNAME_NS() != null) {
String abbreviated = context.PNAME_NS().getText();
return abbreviatedToToken(abbreviated);
} else if (context.PNAME_LN() != null) {
String abbreviated = context.PNAME_LN().getText();
return abbreviatedToToken(abbreviated);
} else {
throw new ParseException("No value found for prefixed name");
}
}
public ReferenceToken getToken(Sp4rql11Parser.IriContext context) throws ParseException {
if(context.IRIREF() != null || context.UNQUOTED_IRIREF() != null) {
return parseIri(context);
}
if(context.prefixedName() != null) {
return getToken(context.prefixedName());
}
throw new ParseException("Failed parsing iri");
}
public BlankToken getToken(Sp4rql11Parser.BlankNodeContext context, Sp4rqlScope scope) {
if(context.BLANK_NODE_LABEL() != null) {
String label = context.BLANK_NODE_LABEL().getText();
return addBlank(label);
}
return addBlank();
}
public Sp4rqlToken getToken(Sp4rql11Parser.VerbSimpleContext context, Sp4rqlScope scope) {
if(context.var() != null) {
return getToken(context.var());
}
if(context.NIL() != null) {
return new NullToken();
}
throw new ParseException("Failed parsing verbSimple");
}
public VariableToken getToken(Sp4rql11Parser.VarContext context) {
String label = null;
if(context.VAR1() != null) {
label = context.VAR1().getText().substring(1);
}
if(context.VAR2() != null) {
label = context.VAR2().getText().substring(1);
}
if(label != null) {
return new VariableToken(label);
}
throw new ParseException("Failed parsing var");
}
public Sp4rqlToken getToken(Sp4rql11Parser.GraphTermContext context, Sp4rqlScope scope) {
if(context.iri() != null) {
return getToken(context.iri());
}
if(context.rDFLiteral() != null) {
return getToken(context.rDFLiteral());
}
if(context.numericLiteral() != null) {
return getToken(context.numericLiteral());
}
if(context.booleanLiteral() != null) {
return getToken(context.booleanLiteral());
}
if(context.blankNode() != null) {
return getToken(context.blankNode(), scope);
}
if(context.NIL() != null) {
return new NullToken();
}
throw new ParseException("Failed parsing graphTerm");
}
public Sp4rqlToken getToken(Sp4rql11Parser.VarOrTermContext context, Sp4rqlScope scope) {
if(context.var() != null) {
return getToken(context.var());
}
if(context.graphTerm() != null) {
return getToken(context.graphTerm(), scope);
}
throw new ParseException("Failed parsing varOrTerm");
}
public Sp4rqlToken getToken(Sp4rql11Parser.VarOrTermOrEmbTPContext context, Sp4rqlScope scope) {
if(context.var() != null) {
return getToken(context.var());
}
if(context.graphTerm() != null) {
return getToken(context.graphTerm(), scope);
}
if(context.embTP() != null) {
return getToken(context.embTP(), scope);
}
throw new ParseException("Failed parsing varOrTermOrEmbTP");
}
public BlankToken getToken(Sp4rql11Parser.EmbTPContext embTPCtx, Sp4rqlScope scope) {
BlankToken statement = addBlank();
Sp4rqlToken subject = getToken(embTPCtx.varOrTermOrEmbTP(0), scope);
Sp4rqlToken predicate = getToken(embTPCtx.verb());
Sp4rqlToken object = getToken(embTPCtx.varOrTermOrEmbTP(1), scope);
scope.addRestriction(new QuadRestriction(statement, subject, predicate, object));
return statement;
}
public Sp4rqlToken getToken(Sp4rql11Parser.ObjectContext context, Sp4rqlScope scope) {
if(context.embTP() != null) {
return getToken(context.embTP(), scope);
}
if(context.graphNode() != null) {
return getToken(context.graphNode(), scope);
}
throw new ParseException("Failed parsing object");
}
public Sp4rqlToken getToken(Sp4rql11Parser.GraphNodeContext context, Sp4rqlScope scope) {
if(context.varOrTerm() != null) {
return getToken(context.varOrTerm(), scope);
}
if(context.triplesNode() != null) {
throw new NotSupportedException("The triplesNode is not supported");
}
throw new ParseException("Failed parsing graphNode");
}
public Sp4rqlToken getToken(Sp4rql11Parser.DataBlockValueContext context) {
if(context.iri() != null) {
return getToken(context.iri());
}
if(context.rDFLiteral() != null) {
return getToken(context.rDFLiteral());
}
if(context.numericLiteral() != null) {
return getToken(context.numericLiteral());
}
if(context.booleanLiteral() != null) {
return getToken(context.booleanLiteral());
}
if(context.UNDEF() != null) {
return new NullToken();
}
throw new ParseException("Failed parsing dataBlockValue");
}
public LiteralToken getToken(Sp4rql11Parser.RDFLiteralContext context) {
if(context.string() == null) {
throw new ParseException("Could not read content of literal");
}
LiteralToken token = getToken(context.string());
if (context.iri() != null) {
ReferenceToken dataType = getToken(context.iri());
token.setDataType(dataType);
}
if (context.LANGTAG() != null) {
token.setLanguageTag(context.LANGTAG().getText());
}
return token;
}
public LiteralToken getToken(Sp4rql11Parser.StringContext context) {
String value;
if (context.STRING_LITERAL1() != null || context.STRING_LITERAL2() != null) {
value = context.getText().substring(1, context.getText().length() - 1);
} else if (context.STRING_LITERAL_LONG1() != null || context.STRING_LITERAL_LONG2() != null) {
value = context.getText().substring(3, context.getText().length() - 3);
} else {
throw new ParseException("Could not read content of string literal");
}
return new LiteralToken(value);
}
public LiteralToken getToken(Sp4rql11Parser.NumericLiteralContext context) {
String text = context.getText();
if(text.indexOf(".") > -1) {
return new LiteralToken(Double.parseDouble(text));
} else {
return new LiteralToken(Integer.parseInt(text));
}
}
public LiteralToken getToken(Sp4rql11Parser.NumericLiteralPositiveContext context) {
String text = context.getText();
if(text.indexOf(".") > -1) {
return new LiteralToken(Double.parseDouble(text));
} else {
return new LiteralToken(Integer.parseInt(text));
}
}
public LiteralToken getToken(Sp4rql11Parser.NumericLiteralNegativeContext context) {
String text = context.getText();
if(text.indexOf(".") > -1) {
return new LiteralToken(Double.parseDouble(text));
} else {
return new LiteralToken(Integer.parseInt(text));
}
}
public LiteralToken getToken(Sp4rql11Parser.BooleanLiteralContext context) {
return new LiteralToken(Boolean.parseBoolean(context.getText()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy