@Test public void testEvaluateStartBefore1() { Literal pattern = f.createLiteral("ABC"); Literal startIndex = f.createLiteral(0); Literal length = f.createLiteral(1); try { Literal result = substrFunc.evaluate(f, pattern, startIndex, length); assertTrue(result.getLabel().equals("")); } catch (ValueExprEvaluationException e) { fail(e.getMessage()); } }
@Override public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { if (args.length < 2 || args.length > 3) { throw new ValueExprEvaluationException("Incorrect number of arguments for SUBSTR: " + args.length); } Value argValue = args[0]; Value startIndexValue = args[1]; Value lengthValue = null; if (args.length > 2) { lengthValue = args[2]; } if (argValue instanceof Literal) { Literal literal = (Literal) argValue; if (QueryEvaluationUtil.isStringLiteral(literal)) { String lexicalValue = literal.getLabel(); int startIndex = 0; if (startIndexValue instanceof Literal) { int startLiteral = intFromLiteral((Literal) startIndexValue); try { startIndex = startLiteral - 1; } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal start index value (expected int value): " + startIndexValue); } } else if (startIndexValue != null) { throw new ValueExprEvaluationException( "illegal start index value (expected literal value): " + startIndexValue); } int endIndex = lexicalValue.length(); if (lengthValue instanceof Literal) { try { int length = intFromLiteral((Literal) lengthValue); if (length < 1) { return convert("", literal, valueFactory); } endIndex = Math.min(startIndex + length, endIndex); if (endIndex < 0) { return convert("", literal, valueFactory); } } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal length value (expected int value): " + lengthValue); } } else if (lengthValue != null) { throw new ValueExprEvaluationException( "illegal length value (expected literal value): " + lengthValue); } try { startIndex = Math.max(startIndex, 0); lexicalValue = lexicalValue.substring(startIndex, endIndex); return convert(lexicalValue, literal, valueFactory); } catch (IndexOutOfBoundsException e) { throw new ValueExprEvaluationException( "could not determine substring, index out of bounds " + startIndex + "length:" + endIndex, e); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } }
Substring implements Function { @Override public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { if (args.length < 2 || args.length > 3) { throw new ValueExprEvaluationException("Incorrect number of arguments for SUBSTR: " + args.length); } Value argValue = args[0]; Value startIndexValue = args[1]; Value lengthValue = null; if (args.length > 2) { lengthValue = args[2]; } if (argValue instanceof Literal) { Literal literal = (Literal) argValue; if (QueryEvaluationUtil.isStringLiteral(literal)) { String lexicalValue = literal.getLabel(); int startIndex = 0; if (startIndexValue instanceof Literal) { int startLiteral = intFromLiteral((Literal) startIndexValue); try { startIndex = startLiteral - 1; } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal start index value (expected int value): " + startIndexValue); } } else if (startIndexValue != null) { throw new ValueExprEvaluationException( "illegal start index value (expected literal value): " + startIndexValue); } int endIndex = lexicalValue.length(); if (lengthValue instanceof Literal) { try { int length = intFromLiteral((Literal) lengthValue); if (length < 1) { return convert("", literal, valueFactory); } endIndex = Math.min(startIndex + length, endIndex); if (endIndex < 0) { return convert("", literal, valueFactory); } } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal length value (expected int value): " + lengthValue); } } else if (lengthValue != null) { throw new ValueExprEvaluationException( "illegal length value (expected literal value): " + lengthValue); } try { startIndex = Math.max(startIndex, 0); lexicalValue = lexicalValue.substring(startIndex, endIndex); return convert(lexicalValue, literal, valueFactory); } catch (IndexOutOfBoundsException e) { throw new ValueExprEvaluationException( "could not determine substring, index out of bounds " + startIndex + "length:" + endIndex, e); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } } }
Substring implements Function { @Override public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { if (args.length < 2 || args.length > 3) { throw new ValueExprEvaluationException("Incorrect number of arguments for SUBSTR: " + args.length); } Value argValue = args[0]; Value startIndexValue = args[1]; Value lengthValue = null; if (args.length > 2) { lengthValue = args[2]; } if (argValue instanceof Literal) { Literal literal = (Literal) argValue; if (QueryEvaluationUtil.isStringLiteral(literal)) { String lexicalValue = literal.getLabel(); int startIndex = 0; if (startIndexValue instanceof Literal) { int startLiteral = intFromLiteral((Literal) startIndexValue); try { startIndex = startLiteral - 1; } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal start index value (expected int value): " + startIndexValue); } } else if (startIndexValue != null) { throw new ValueExprEvaluationException( "illegal start index value (expected literal value): " + startIndexValue); } int endIndex = lexicalValue.length(); if (lengthValue instanceof Literal) { try { int length = intFromLiteral((Literal) lengthValue); if (length < 1) { return convert("", literal, valueFactory); } endIndex = Math.min(startIndex + length, endIndex); if (endIndex < 0) { return convert("", literal, valueFactory); } } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal length value (expected int value): " + lengthValue); } } else if (lengthValue != null) { throw new ValueExprEvaluationException( "illegal length value (expected literal value): " + lengthValue); } try { startIndex = Math.max(startIndex, 0); lexicalValue = lexicalValue.substring(startIndex, endIndex); return convert(lexicalValue, literal, valueFactory); } catch (IndexOutOfBoundsException e) { throw new ValueExprEvaluationException( "could not determine substring, index out of bounds " + startIndex + "length:" + endIndex, e); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } }  }
Substring implements Function { @Override public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { if (args.length < 2 || args.length > 3) { throw new ValueExprEvaluationException("Incorrect number of arguments for SUBSTR: " + args.length); } Value argValue = args[0]; Value startIndexValue = args[1]; Value lengthValue = null; if (args.length > 2) { lengthValue = args[2]; } if (argValue instanceof Literal) { Literal literal = (Literal) argValue; if (QueryEvaluationUtil.isStringLiteral(literal)) { String lexicalValue = literal.getLabel(); int startIndex = 0; if (startIndexValue instanceof Literal) { int startLiteral = intFromLiteral((Literal) startIndexValue); try { startIndex = startLiteral - 1; } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal start index value (expected int value): " + startIndexValue); } } else if (startIndexValue != null) { throw new ValueExprEvaluationException( "illegal start index value (expected literal value): " + startIndexValue); } int endIndex = lexicalValue.length(); if (lengthValue instanceof Literal) { try { int length = intFromLiteral((Literal) lengthValue); if (length < 1) { return convert("", literal, valueFactory); } endIndex = Math.min(startIndex + length, endIndex); if (endIndex < 0) { return convert("", literal, valueFactory); } } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal length value (expected int value): " + lengthValue); } } else if (lengthValue != null) { throw new ValueExprEvaluationException( "illegal length value (expected literal value): " + lengthValue); } try { startIndex = Math.max(startIndex, 0); lexicalValue = lexicalValue.substring(startIndex, endIndex); return convert(lexicalValue, literal, valueFactory); } catch (IndexOutOfBoundsException e) { throw new ValueExprEvaluationException( "could not determine substring, index out of bounds " + startIndex + "length:" + endIndex, e); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } }  @Override String getURI(); @Override Literal evaluate(ValueFactory valueFactory, Value... args); static int intFromLiteral(Literal literal); }
Substring implements Function { @Override public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { if (args.length < 2 || args.length > 3) { throw new ValueExprEvaluationException("Incorrect number of arguments for SUBSTR: " + args.length); } Value argValue = args[0]; Value startIndexValue = args[1]; Value lengthValue = null; if (args.length > 2) { lengthValue = args[2]; } if (argValue instanceof Literal) { Literal literal = (Literal) argValue; if (QueryEvaluationUtil.isStringLiteral(literal)) { String lexicalValue = literal.getLabel(); int startIndex = 0; if (startIndexValue instanceof Literal) { int startLiteral = intFromLiteral((Literal) startIndexValue); try { startIndex = startLiteral - 1; } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal start index value (expected int value): " + startIndexValue); } } else if (startIndexValue != null) { throw new ValueExprEvaluationException( "illegal start index value (expected literal value): " + startIndexValue); } int endIndex = lexicalValue.length(); if (lengthValue instanceof Literal) { try { int length = intFromLiteral((Literal) lengthValue); if (length < 1) { return convert("", literal, valueFactory); } endIndex = Math.min(startIndex + length, endIndex); if (endIndex < 0) { return convert("", literal, valueFactory); } } catch (NumberFormatException e) { throw new ValueExprEvaluationException( "illegal length value (expected int value): " + lengthValue); } } else if (lengthValue != null) { throw new ValueExprEvaluationException( "illegal length value (expected literal value): " + lengthValue); } try { startIndex = Math.max(startIndex, 0); lexicalValue = lexicalValue.substring(startIndex, endIndex); return convert(lexicalValue, literal, valueFactory); } catch (IndexOutOfBoundsException e) { throw new ValueExprEvaluationException( "could not determine substring, index out of bounds " + startIndex + "length:" + endIndex, e); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } } else { throw new ValueExprEvaluationException("unexpected input value for function substring: " + argValue); } }  @Override String getURI(); @Override Literal evaluate(ValueFactory valueFactory, Value... args); static int intFromLiteral(Literal literal);  }