Replace implements Function { @Override public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { if (args.length < 3 || args.length > 4) { throw new ValueExprEvaluationException("Incorrect number of arguments for REPLACE: " + args.length); } try { Literal arg = (Literal) args[0]; Literal pattern = (Literal) args[1]; Literal replacement = (Literal) args[2]; Literal flags = null; if (args.length == 4) { flags = (Literal) args[3]; } if (!QueryEvaluationUtil.isStringLiteral(arg)) { throw new ValueExprEvaluationException("incompatible operand for REPLACE: " + arg); } if (!QueryEvaluationUtil.isSimpleLiteral(pattern)) { throw new ValueExprEvaluationException("incompatible operand for REPLACE: " + pattern); } if (!QueryEvaluationUtil.isSimpleLiteral(replacement)) { throw new ValueExprEvaluationException("incompatible operand for REPLACE: " + replacement); } String flagString = null; if (flags != null) { if (!QueryEvaluationUtil.isSimpleLiteral(flags)) { throw new ValueExprEvaluationException("incompatible operand for REPLACE: " + flags); } flagString = flags.getLabel(); } String argString = arg.getLabel(); String patternString = pattern.getLabel(); String replacementString = replacement.getLabel(); int f = 0; if (flagString != null) { for (char c : flagString.toCharArray()) { switch (c) { case 's': f |= Pattern.DOTALL; break; case 'm': f |= Pattern.MULTILINE; break; case 'i': f |= Pattern.CASE_INSENSITIVE; break; case 'x': f |= Pattern.COMMENTS; break; case 'd': f |= Pattern.UNIX_LINES; break; case 'u': f |= Pattern.UNICODE_CASE; break; default: throw new ValueExprEvaluationException(flagString); } } } Pattern p = Pattern.compile(patternString, f); String result = p.matcher(argString).replaceAll(replacementString); Optional<String> lang = arg.getLanguage(); IRI dt = arg.getDatatype(); if (lang.isPresent()) { return valueFactory.createLiteral(result, lang.get()); } else if (dt != null) { return valueFactory.createLiteral(result, dt); } else { return valueFactory.createLiteral(result); } } catch (ClassCastException e) { throw new ValueExprEvaluationException("literal operands expected", e); } }  @Override String getURI(); @Override Literal evaluate(ValueFactory valueFactory, Value... args);  }
@Test public void testEvaluate1() { Literal arg = f.createLiteral("foobar"); Literal pattern = f.createLiteral("ba"); Literal replacement = f.createLiteral("Z"); try { Literal result = replaceFunc.evaluate(f, arg, pattern, replacement); assertEquals("fooZr", result.getLabel()); } catch (ValueExprEvaluationException e) { fail(e.getMessage()); } }
@Test public void testEvaluate2() { Literal arg = f.createLiteral("foobar"); Literal pattern = f.createLiteral("BA"); Literal replacement = f.createLiteral("Z"); try { Literal result = replaceFunc.evaluate(f, arg, pattern, replacement); assertEquals("foobar", result.getLabel()); } catch (ValueExprEvaluationException e) { fail(e.getMessage()); } }
@Test public void testEvaluate3() { Literal arg = f.createLiteral("foobar"); Literal pattern = f.createLiteral("BA"); Literal replacement = f.createLiteral("Z"); Literal flags = f.createLiteral("i"); try { Literal result = replaceFunc.evaluate(f, arg, pattern, replacement, flags); assertEquals("fooZr", result.getLabel()); } catch (ValueExprEvaluationException e) { fail(e.getMessage()); } }
@Test public void testEvaluate4() { Literal arg = f.createLiteral(10); Literal pattern = f.createLiteral("BA"); Literal replacement = f.createLiteral("Z"); try { Literal result = replaceFunc.evaluate(f, arg, pattern, replacement); fail("error expected on incompatible operand"); } catch (ValueExprEvaluationException e) { } }
@Test public void testEvaluate5() { Literal arg = f.createLiteral("foobarfoobarbarfoo"); Literal pattern = f.createLiteral("ba"); Literal replacement = f.createLiteral("Z"); try { Literal result = replaceFunc.evaluate(f, arg, pattern, replacement); assertEquals("fooZrfooZrZrfoo", result.getLabel()); } catch (ValueExprEvaluationException e) { fail(e.getMessage()); } }
@Test public void testEvaluate6() { Literal arg = f.createLiteral("foobarfoobarbarfooba"); Literal pattern = f.createLiteral("ba."); Literal replacement = f.createLiteral("Z"); try { Literal result = replaceFunc.evaluate(f, arg, pattern, replacement); assertEquals("fooZfooZZfooba", result.getLabel()); } catch (ValueExprEvaluationException e) { fail(e.getMessage()); } }
@Test public void testEvaluate7() { Literal arg = f.createLiteral("日本語", "ja"); Literal pattern = f.createLiteral("[^a-zA-Z0-9]"); Literal replacement = f.createLiteral("-"); try { Literal result = replaceFunc.evaluate(f, arg, pattern, replacement); assertEquals("---", result.getLabel()); assertEquals("ja", result.getLanguage().orElse(null)); } catch (ValueExprEvaluationException e) { fail(e.getMessage()); } }