📄 rdqlparser.java
字号:
/* Generated By:JavaCC: Do not edit this line. RdqlParser.java */package org.openrdf.sesame.query.rdql.parser;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.openrdf.util.xml.XmlDatatypeUtil;import org.openrdf.vocabulary.OWL;import org.openrdf.vocabulary.RDF;import org.openrdf.vocabulary.RDFS;import org.openrdf.vocabulary.XmlSchema;import org.openrdf.model.Literal;import org.openrdf.model.URI;import org.openrdf.model.impl.LiteralImpl;import org.openrdf.model.impl.URIImpl;import org.openrdf.rio.ntriples.NTriplesUtil;import org.openrdf.sesame.query.TableQuery;import org.openrdf.sesame.sail.query.And;import org.openrdf.sesame.sail.query.BooleanConstant;import org.openrdf.sesame.sail.query.BooleanExpr;import org.openrdf.sesame.sail.query.GraphPattern;import org.openrdf.sesame.sail.query.Like;import org.openrdf.sesame.sail.query.LiteralExpr;import org.openrdf.sesame.sail.query.MathExpr;import org.openrdf.sesame.sail.query.Not;import org.openrdf.sesame.sail.query.Null;import org.openrdf.sesame.sail.query.Or;import org.openrdf.sesame.sail.query.ProjectionElem;import org.openrdf.sesame.sail.query.Query;import org.openrdf.sesame.sail.query.ResourceExpr;import org.openrdf.sesame.sail.query.SelectQuery;import org.openrdf.sesame.sail.query.StringCompare;import org.openrdf.sesame.sail.query.StringConstant;import org.openrdf.sesame.sail.query.TriplePattern;import org.openrdf.sesame.sail.query.ValueCompare;import org.openrdf.sesame.sail.query.ValueExpr;import org.openrdf.sesame.sail.query.Var;public class RdqlParser implements RdqlParserConstants {/*----------+| Constants |+----------*/ public static final URI XSD_INTEGER = new URIImpl(XmlSchema.INTEGER); public static final URI XSD_FLOAT = new URIImpl(XmlSchema.FLOAT);/*----------+| Variables |+----------*/ /** * Mapping of prefix to namespace. **/ protected Map _namespaces = new HashMap(16); /** * List of shared variables. Named variables are shared; multiple * occurrences of the name of one variable in a query represent * the same variable. **/ protected List _sharedVars = new ArrayList(16); /** * Anonymous variable identifier. **/ protected int _varNo = 0;/*-------------------------------+| Methods |+-------------------------------*/ /** * Adds a mapping from the specified prefix to the specified * namespace. The specified prefix must already be mapped to some * (other) namespace. * * @return true if the mapping has been added, false otherwise. * @exception ParseException If the specified prefix is already * mapped to some (other) namespace. **/ protected void _setNamespacePrefix(String prefix, String namespace) throws ParseException { if (_namespaces.containsKey(prefix)) { // Prefix already defined, report this error. throw new ParseException("Prefix \"" + prefix + "\" already in use."); } _namespaces.put(prefix, namespace); } /** * Gets the namespace that is mapped to the supplied prefix. **/ protected String _getNamespace(String prefix) { String result = (String)_namespaces.get(prefix); // rdf, rdfs, xsd and owl are default // namespace when not defined explicitly. if (result == null) { if ("rdf".equals(prefix)) { result = RDF.NAMESPACE; } else if ("rdfs".equals(prefix)) { result = RDFS.NAMESPACE; } else if ("xsd".equals(prefix)) { result = XmlSchema.NAMESPACE; } else if ("owl".equals(prefix)) { result = OWL.NAMESPACE; } } return result; } /** * Returns the shared instance of Var that is associated with the * specified variable name. A new shared variable will be created * whenever this method is called with a new variable name. **/ protected Var _getSharedVar(String varName) { Var var; // Search for a variable with the supplied name in _sharedVars Iterator i = _sharedVars.iterator(); while (i.hasNext()) { var = (Var)i.next(); if (var.getName().equals(varName)) { // Variable found return var; } } // Variable does not exist; create one and add it to a number of lists var = new Var(varName); _sharedVars.add(var); return var; } /** * Creates a new anonymous variable and returns it. **/ protected Var _createAnonymousVar() { Var result = new Var("_" + _varNo++); result.setAnonymous(true); return result; } /** * Parses a string representating of full or abbreviated URI and * creates a URI object from it. **/ protected URI _parseURI(String uri) throws ParseException { if (uri.startsWith("<")) { return _parseQuotedURI(uri); } else { return _parseQName(uri); } } /** * Parses a quoted URI (i.e. <tt><http://foo/bar></tt>) and creates a * URI object for it. **/ protected URI _parseQuotedURI(String uri) throws ParseException { // Full URI starts with "<" and ends with ">", remove these characters. try { uri = uri.substring(1, uri.length() - 1); // Substitute any prefixes with their associated namespace int colonIdx = uri.indexOf(':'); if (colonIdx > 0) { String prefix = uri.substring(0, colonIdx); String localName = uri.substring(colonIdx + 1); String namespace = _getNamespace(prefix); if (namespace != null) { // Rebuild URI using namespace and localname uri = namespace + localName; } } return new URIImpl(uri); } catch (IllegalArgumentException e) { // Not a valid, fully qualified URI throw new ParseException(e.getMessage()); } } /** * Parses a string representing an abbreviated URI (i.e. * <tt>rdf:type</tt>) and creates a URI object for it. **/ protected URI _parseQName(String uri) throws ParseException { // QNames have the form PREFIX:LNAME, substitute the PREFIX // with an actual namespace. int colonIdx = uri.indexOf(':'); String prefix = uri.substring(0, colonIdx); String localName = uri.substring(colonIdx + 1); String namespace = _getNamespace(prefix); if (namespace == null) { throw new ParseException("Prefix \"" + prefix + "\" not defined."); } return new URIImpl(namespace + localName); } /** * Parses a new Literal from the supplied String. **/ protected Literal _parseLiteral(String literal) throws ParseException { // Find string separation points int endLabelIdx = _findEndOfLabel(literal); int startLangIdx = literal.indexOf("@", endLabelIdx); int startDtIdx = literal.indexOf("^^", Math.max(endLabelIdx, startLangIdx)); // Get label String label = literal.substring(1, endLabelIdx); label = NTriplesUtil.unescapeString(label); if (startLangIdx != -1) { // Get language String language = literal.substring(startLangIdx + 1); return new LiteralImpl(label, language); } else if (startDtIdx != -1) { // Get datatype String dtString = literal.substring(startDtIdx + 2); URI datatype = _parseURI(dtString); // Normalize label when datatype is XML Schema built-in if (XmlDatatypeUtil.isBuiltInDatatype(datatype.getURI())) { try { label = XmlDatatypeUtil.normalize(label, datatype.getURI()); } catch (IllegalArgumentException e) { throw new ParseException(e.getMessage()); } } return new LiteralImpl(label, datatype); } else { return new LiteralImpl(label); } } /** * Finds the end of the label in a literal string. This method * takes into account that characters can be escaped using * backslashes. * * @return The index of the double quote ending the label. **/ private int _findEndOfLabel(String literal) throws ParseException { // First character of literal is guaranteed to be a double // quote, start search at second character. boolean previousWasBackslash = false; for (int i = 1; i < literal.length(); i++) { char c = literal.charAt(i); if (c == '"' && !previousWasBackslash) { return i; } else if (c == '\\' && !previousWasBackslash) { // start of escape previousWasBackslash = true; } else if (previousWasBackslash) { // c was escaped previousWasBackslash = false; } } throw new ParseException("Could not find end of literal label"); } /** * Creates a new ParseException with the supplied current and * expected tokens. See ParseException for details. **/ protected ParseException _createParseException( Token currentTokenVal, String[] tokenImage) { // Append "<EOF>" in front of the supplied String array. // ParseException.getMessage expects "<EOF>" as the first String // in the String array. int length = tokenImage.length; // New String array has one element extra, therefore length of // new String array is increased by one. String[] newTokenImage = new String[length + 1]; // First element is "<EOF>" newTokenImage[0] = "<EOF>"; // Copy Strings of the supplied String array to the new String array. for (int i = 0; i < length; i++) { newTokenImage[i +1] = tokenImage[i]; } int[][] expectedTokenSequences = new int[1][length]; for (int i = 0; i < length; i++) { expectedTokenSequences [0][i] = i +1; } return new ParseException(currentTokenVal, expectedTokenSequences, newTokenImage); } final public TableQuery parseQuery() throws ParseException { Query qc; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case PREFIXES: prefixes_clause(); break; default: jj_la1[0] = jj_gen; ; } qc = select_query(); jj_consume_token(0); {if (true) return new TableQuery(qc);} throw new Error("Missing return statement in function"); } final public void comma_opt() throws ParseException { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case COMMA: jj_consume_token(COMMA); break; default: jj_la1[1] = jj_gen; ; } }/*-------------------------------+| NAMESPACE DECLARATIONS |+-------------------------------*/ final public void prefixes_clause() throws ParseException { jj_consume_token(PREFIXES); prefix_decl(); label_1: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case COMMA: case NCNAME: ; break; default: jj_la1[2] = jj_gen; break label_1; } comma_opt(); prefix_decl(); } } final public void prefix_decl() throws ParseException { Token prefixToken; ResourceExpr uri; prefixToken = jj_consume_token(NCNAME); jj_consume_token(FOR); uri = quoted_uri(); String prefix = prefixToken.image; _setNamespacePrefix(prefix, uri.getString()); } final public Query select_query() throws ParseException { List projection; GraphPattern graphPattern = new GraphPattern(); jj_consume_token(SELECT); projection = projection(); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case SOURCE: case FROM: switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case SOURCE: jj_consume_token(SOURCE); break; case FROM: jj_consume_token(FROM); break; default: jj_la1[3] = jj_gen; jj_consume_token(-1); throw new ParseException(); } jj_consume_token(QUOTED_URI); label_2: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case COMMA: case QUOTED_URI: ; break; default: jj_la1[4] = jj_gen; break label_2; } comma_opt(); jj_consume_token(QUOTED_URI); } break; default: jj_la1[5] = jj_gen; ; } jj_consume_token(WHERE); triple_pattern_clause(graphPattern); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case SUCHTHAT: jj_consume_token(SUCHTHAT); constraint_clause(graphPattern); break; default: jj_la1[6] = jj_gen; ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -