Reformat Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/6dedadc5 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/6dedadc5 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/6dedadc5
Branch: refs/heads/master Commit: 6dedadc5d0900bb9b1e01fbdecba9c50d91d9234 Parents: 199fcf5 Author: Andy Seaborne <[email protected]> Authored: Tue Apr 19 12:27:20 2016 +0100 Committer: Andy Seaborne <[email protected]> Committed: Tue Apr 19 12:27:20 2016 +0100 ---------------------------------------------------------------------- .../org/apache/jena/sparql/lang/ParserBase.java | 367 ++++++++----------- 1 file changed, 158 insertions(+), 209 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/6dedadc5/jena-arq/src/main/java/org/apache/jena/sparql/lang/ParserBase.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/lang/ParserBase.java b/jena-arq/src/main/java/org/apache/jena/sparql/lang/ParserBase.java index 953549c..90d5164 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/lang/ParserBase.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/lang/ParserBase.java @@ -106,157 +106,139 @@ public class ParserBase public void setPrologue(Prologue prologue) { this.prologue = prologue ; } public Prologue getPrologue() { return prologue ; } - protected void setInConstructTemplate(boolean b) - { + protected void setInConstructTemplate(boolean b) { setBNodesAreVariables(!b) ; } - + protected boolean getBNodesAreVariables() { return bNodesAreVariables ; } - protected void setBNodesAreVariables(boolean bNodesAreVariables) - { + + protected void setBNodesAreVariables(boolean bNodesAreVariables) { this.bNodesAreVariables = bNodesAreVariables ; if ( bNodesAreVariables ) activeLabelMap = anonVarLabels ; - else - activeLabelMap = bNodeLabels ; + else + activeLabelMap = bNodeLabels ; } protected boolean getBNodesAreAllowed() { return bNodesAreAllowed ; } - protected void setBNodesAreAllowed(boolean bNodesAreAllowed) - { + + protected void setBNodesAreAllowed(boolean bNodesAreAllowed) { this.bNodesAreAllowed = bNodesAreAllowed ; } protected boolean getAllowAggregatesInExpressions() { - return allowAggregatesInExpressions; + return allowAggregatesInExpressions ; } protected void setAllowAggregatesInExpressions(boolean allowAggregatesInExpressions) { this.allowAggregatesInExpressions = allowAggregatesInExpressions; } - protected Element compressGroupOfOneGroup(ElementGroup elg) - { + protected Element compressGroupOfOneGroup(ElementGroup elg) { // remove group of one group. - if ( elg.size() == 1 ) - { + if ( elg.size() == 1 ) { Element e1 = elg.get(0) ; if ( e1 instanceof ElementGroup ) return e1 ; } return elg ; } - - protected Node createLiteralInteger(String lexicalForm) - { + + protected Node createLiteralInteger(String lexicalForm) { return NodeFactory.createLiteral(lexicalForm, XSDDatatype.XSDinteger) ; } - - protected Node createLiteralDouble(String lexicalForm) - { + + protected Node createLiteralDouble(String lexicalForm) { return NodeFactory.createLiteral(lexicalForm, XSDDatatype.XSDdouble) ; } - - protected Node createLiteralDecimal(String lexicalForm) - { + + protected Node createLiteralDecimal(String lexicalForm) { return NodeFactory.createLiteral(lexicalForm, XSDDatatype.XSDdecimal) ; } - protected Node stripSign(Node node) - { - if ( ! node.isLiteral() ) return node ; + protected Node stripSign(Node node) { + if ( !node.isLiteral() ) + return node ; String lex = node.getLiteralLexicalForm() ; String lang = node.getLiteralLanguage() ; RDFDatatype dt = node.getLiteralDatatype() ; - - if ( ! lex.startsWith("-") && ! lex.startsWith("+") ) - throw new ARQInternalErrorException("Literal does not start with a sign: "+lex) ; - + + if ( !lex.startsWith("-") && !lex.startsWith("+") ) + throw new ARQInternalErrorException("Literal does not start with a sign: " + lex) ; + lex = lex.substring(1) ; return NodeFactory.createLiteral(lex, lang, dt) ; } - - protected Node createLiteral(String lexicalForm, String langTag, String datatypeURI) - { + + protected Node createLiteral(String lexicalForm, String langTag, String datatypeURI) { Node n = null ; // Can't have type and lang tag in parsing. - if ( datatypeURI != null) - { + if ( datatypeURI != null ) { RDFDatatype dType = TypeMapper.getInstance().getSafeTypeByName(datatypeURI) ; n = NodeFactory.createLiteral(lexicalForm, dType) ; - } - else if ( langTag != null && ! langTag.isEmpty() ) + } else if ( langTag != null && !langTag.isEmpty() ) n = NodeFactory.createLiteral(lexicalForm, langTag) ; else n = NodeFactory.createLiteral(lexicalForm) ; return n ; } - protected long integerValue(String s) - { + protected long integerValue(String s) { try { if ( s.startsWith("+") ) s = s.substring(1) ; - if ( s.startsWith("0x") ) - { + if ( s.startsWith("0x") ) { // Hex s = s.substring(2) ; return Long.parseLong(s, 16) ; } return Long.parseLong(s) ; - } catch (NumberFormatException ex) - { + } + catch (NumberFormatException ex) { try { // Possible too large for a long. BigInteger integer = new BigInteger(s) ; - throwParseException("Number '"+s+"' is a valid number but can't not be stored in a long") ; - } catch (NumberFormatException ex2) {} + throwParseException("Number '" + s + "' is a valid number but can't not be stored in a long") ; + } + catch (NumberFormatException ex2) {} throw new QueryParseException(ex, -1, -1) ; } } - - protected double doubleValue(String s) - { + + protected double doubleValue(String s) { if ( s.startsWith("+") ) s = s.substring(1) ; double valDouble = Double.parseDouble(s) ; - return valDouble ; + return valDouble ; } - + /** Remove first and last characters (e.g. ' or "") from a string */ - protected static String stripQuotes(String s) - { - return s.substring(1,s.length()-1) ; + protected static String stripQuotes(String s) { + return s.substring(1, s.length() - 1) ; } - - /** Remove first 3 and last 3 characters (e.g. ''' or """) from a string */ - protected static String stripQuotes3(String s) - { - return s.substring(3,s.length()-3) ; + + /** Remove first 3 and last 3 characters (e.g. ''' or """) from a string */ + protected static String stripQuotes3(String s) { + return s.substring(3, s.length() - 3) ; } - /** remove the first n charcacters from the string*/ - public static String stripChars(String s, int n) - { - return s.substring(n, s.length()) ; + /** remove the first n charcacters from the string */ + public static String stripChars(String s, int n) { + return s.substring(n, s.length()) ; } - - protected Var createVariable(String s, int line, int column) - { + + protected Var createVariable(String s, int line, int column) { s = s.substring(1) ; // Drop the marker - + // This is done by the parser input stream nowadays. - //s = unescapeCodePoint(s, line, column) ; - // Check \ u did not put in any illegals. + // s = unescapeCodePoint(s, line, column) ; + // Check \ u did not put in any illegals. return Var.alloc(s) ; } // ---- IRIs and Nodes - // See RiotLib re bNode IRIs. - // Merge sometime. - protected String resolveQuotedIRI(String iriStr, int line, int column) - { + protected String resolveQuotedIRI(String iriStr, int line, int column) { iriStr = stripQuotes(iriStr) ; return resolveIRI(iriStr, line, column) ; } @@ -265,23 +247,23 @@ public class ParserBase private static Logger parserLog = LoggerFactory.getLogger(ParserLoggerName) ; private static ErrorHandler errorHandler = ErrorHandlerFactory.errorHandlerStd(parserLog) ; - protected String resolveIRI(String iriStr, int line, int column) - { + protected String resolveIRI(String iriStr, int line, int column) { if ( isBNodeIRI(iriStr) ) return iriStr ; - - if ( getPrologue() != null ) - { + + if ( getPrologue() != null ) { if ( getPrologue().getResolver() != null ) try { // Used to be errors (pre Jena 2.12.0) // .resolve(iriStr) IRI iri = getPrologue().getResolver().resolveSilent(iriStr) ; - if ( true ) + if ( true ) CheckerIRI.iriViolations(iri, errorHandler, line, column) ; iriStr = iri.toString() ; - } catch (JenaURIException ex) - { throwParseException(ex.getMessage(), line, column) ; } + } + catch (JenaURIException ex) { + throwParseException(ex.getMessage(), line, column) ; + } } return iriStr ; } @@ -289,38 +271,36 @@ public class ParserBase // Pragmatic. private final boolean fixupUndefinedPrefixedNames = ARQ.isTrue(ARQ.fixupUndefinedPrefixes) ; - protected String resolvePName(String prefixedName, int line, int column) - { + protected String resolvePName(String prefixedName, int line, int column) { // It's legal. int idx = prefixedName.indexOf(':') ; - + // -- Escapes in local name String prefix = prefixedName.substring(0, idx) ; - String local = prefixedName.substring(idx+1) ; + String local = prefixedName.substring(idx + 1) ; local = unescapePName(local, line, column) ; - prefixedName = prefix+":"+local ; + prefixedName = prefix + ":" + local ; // -- - + String s = getPrologue().expandPrefixedName(prefixedName) ; if ( s == null ) { if ( fixupUndefinedPrefixedNames ) return ARQ.fixupPrefixes.apply(prefixedName) ; - throwParseException("Unresolved prefixed name: "+prefixedName, line, column) ; + throwParseException("Unresolved prefixed name: " + prefixedName, line, column) ; } return s ; } private boolean skolomizedBNodes = ARQ.isTrue(ARQ.constantBNodeLabels) ; - protected Node createNode(String iri) - { + + protected Node createNode(String iri) { if ( skolomizedBNodes ) return RiotLib.createIRIorBNode(iri) ; else return NodeFactory.createURI(iri) ; } - - protected boolean isBNodeIRI(String iri) - { + + protected boolean isBNodeIRI(String iri) { return skolomizedBNodes && RiotLib.isBNodeIRI(iri) ; } @@ -342,14 +322,12 @@ public class ParserBase { } // On entry to a new group, the current BGP is ended. - protected void startGroup(ElementGroup elg) - { + protected void startGroup(ElementGroup elg) { endBasicGraphPattern() ; startBasicGraphPattern() ; } - - protected void endGroup(ElementGroup elg) - { + + protected void endGroup(ElementGroup elg) { endBasicGraphPattern() ; } @@ -362,98 +340,80 @@ public class ParserBase protected Node createListNode(int line, int column) { return createBNode(line, column) ; } // Unlabelled bNode. - protected Node createBNode(int line, int column) - { - if ( ! bNodesAreAllowed ) + protected Node createBNode(int line, int column) { + if ( !bNodesAreAllowed ) throwParseException("Blank nodes not allowed in DELETE templates", line, column) ; return activeLabelMap.allocNode() ; } - + // Labelled bNode. - protected Node createBNode(String label, int line, int column) - { - if ( ! bNodesAreAllowed ) - throwParseException("Blank nodes not allowed in DELETE templates: "+label, line, column) ; + protected Node createBNode(String label, int line, int column) { + if ( !bNodesAreAllowed ) + throwParseException("Blank nodes not allowed in DELETE templates: " + label, line, column) ; if ( previousLabels.contains(label) ) - throwParseException("Blank node label reuse not allowed at this point: "+label, - line, column) ; - - //label = unescapeCodePoint(label, line, column) ; + throwParseException("Blank node label reuse not allowed at this point: " + label, line, column) ; + + // label = unescapeCodePoint(label, line, column) ; return activeLabelMap.asNode(label) ; } - - protected Expr createExprExists(Element element) - { + + protected Expr createExprExists(Element element) { return new E_Exists(element) ; } - - protected Expr createExprNotExists(Element element) - { + + protected Expr createExprNotExists(Element element) { // Could negate here. return new E_NotExists(element) ; } - - protected String fixupPrefix(String prefix, int line, int column) - { + + protected String fixupPrefix(String prefix, int line, int column) { // \ u processing! if ( prefix.endsWith(":") ) - prefix = prefix.substring(0, prefix.length()-1) ; - return prefix ; + prefix = prefix.substring(0, prefix.length() - 1) ; + return prefix ; } - - protected void setAccGraph(QuadAccSink acc, Node gn) - { + + protected void setAccGraph(QuadAccSink acc, Node gn) { acc.setGraph(gn) ; } - - protected void insert(TripleCollector acc, Node s, Node p, Node o) - { + + protected void insert(TripleCollector acc, Node s, Node p, Node o) { acc.addTriple(new Triple(s, p, o)) ; } - - protected void insert(TripleCollectorMark acc, int index, Node s, Node p, Node o) - { + + protected void insert(TripleCollectorMark acc, int index, Node s, Node p, Node o) { acc.addTriple(index, new Triple(s, p, o)) ; } - - protected void insert(TripleCollector acc, Node s, Node p, Path path, Node o) - { + + protected void insert(TripleCollector acc, Node s, Node p, Path path, Node o) { if ( p == null ) acc.addTriplePath(new TriplePath(s, path, o)) ; else acc.addTriple(new Triple(s, p, o)) ; } - - protected void insert(TripleCollectorMark acc, int index, Node s, Node p, Path path, Node o) - { + + protected void insert(TripleCollectorMark acc, int index, Node s, Node p, Path path, Node o) { if ( p == null ) acc.addTriplePath(index, new TriplePath(s, path, o)) ; else acc.addTriple(index, new Triple(s, p, o)) ; } - - protected void insert(TripleCollector target, ElementPathBlock source) - { - for (TriplePath path : source.getPattern()) - { - if (path.isTriple()) - { - target.addTriple(path.asTriple()); - } - else - { - target.addTriplePath(path); + + protected void insert(TripleCollector target, ElementPathBlock source) { + for ( TriplePath path : source.getPattern() ) { + if ( path.isTriple() ) { + target.addTriple(path.asTriple()) ; + } else { + target.addTriplePath(path) ; } } } - protected Expr asExpr(Node n) - { + protected Expr asExpr(Node n) { return ExprUtils.nodeToExpr(n) ; } - protected Expr asExprNoSign(Node n) - { + protected Expr asExprNoSign(Node n) { String lex = n.getLiteralLexicalForm() ; String lang = n.getLiteralLanguage() ; String dtURI = n.getLiteralDatatypeURI() ; @@ -489,93 +449,82 @@ public class ParserBase } } - public static String unescapePName(String s, int line, int column) - { + public static String unescapePName(String s, int line, int column) { char escape = '\\' ; int idx = s.indexOf(escape) ; - + if ( idx == -1 ) return s ; - + int len = s.length() ; StringBuilder sb = new StringBuilder() ; - - for ( int i = 0 ; i < len ; i++ ) - { + + for ( int i = 0 ; i < len ; i++ ) { char ch = s.charAt(i) ; // Keep line and column numbers. - switch (ch) - { - case '\n': - case '\r': + switch (ch) { + case '\n' : + case '\r' : line++ ; column = 1 ; break ; - default: + default : column++ ; break ; } - if ( ch != escape ) - { + if ( ch != escape ) { sb.append(ch) ; continue ; } // Escape - if ( i >= s.length()-1 ) + if ( i >= s.length() - 1 ) throwParseException("Illegal escape at end of string", line, column) ; - char ch2 = s.charAt(i+1) ; - column = column+1 ; + char ch2 = s.charAt(i + 1) ; + column = column + 1 ; i = i + 1 ; - switch (ch2) - { // PN_LOCAL_ESC - case '_' : - case '~' : - case '.' : - case '-' : - case '!' : - case '$' : - case '&' : - case '\'' : - case '(' : - case ')' : - case '*' : - case '+' : - case ',' : - case ';' : - case '=' : - case ':' : - case '/' : - case '?' : - case '#' : - case '@' : - case '%' : - sb.append(ch2) ; - break ; - default: - throwParseException("Illegal prefix name escape: "+ch2, line, column) ; - } + switch (ch2) { // PN_LOCAL_ESC + case '_' : + case '~' : + case '.' : + case '-' : + case '!' : + case '$' : + case '&' : + case '\'' : + case '(' : + case ')' : + case '*' : + case '+' : + case ',' : + case ';' : + case '=' : + case ':' : + case '/' : + case '?' : + case '#' : + case '@' : + case '%' : + sb.append(ch2) ; + break ; + default : + throwParseException("Illegal prefix name escape: " + ch2, line, column) ; + } } return sb.toString() ; } - - - protected void warnDeprecation(String msg) - { + + protected void warnDeprecation(String msg) { Log.warn(this, msg) ; } - - public static void throwParseException(String msg, int line, int column) - { - throw new QueryParseException("Line " + line + ", column " + column + ": " + msg, - line, column) ; + + public static void throwParseException(String msg, int line, int column) { + throw new QueryParseException("Line " + line + ", column " + column + ": " + msg, line, column) ; } - - public static void throwParseException(String msg) - { + + public static void throwParseException(String msg) { throw new QueryParseException(msg, -1, -1) ; } - }
