Java Code Examples for org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtil#isStringLiteral()

The following examples show how to use org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtil#isStringLiteral() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EncodeForUri.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("ENCODE_FOR_URI requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexValue = literal.getLabel();

			return valueFactory.createLiteral(encodeUri(lexValue));
		} else {
			throw new ValueExprEvaluationException("Invalid argument for ENCODE_FOR_URI: " + literal);
		}
	} else {
		throw new ValueExprEvaluationException("Invalid argument for ENCODE_FOR_URI: " + args[0]);
	}
}
 
Example 2
Source File: CastFunction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException(
				getXsdName() + " cast requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];
		IRI datatype = literal.getDatatype();

		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexicalValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
			if (isValidForDatatype(lexicalValue)) {
				return valueFactory.createLiteral(lexicalValue, getXsdDatatype());
			}
		} else if (datatype != null) {
			if (datatype.equals(getXsdDatatype())) {
				return literal;
			}
		}
		return convert(valueFactory, literal);
	} else {
		return convert(valueFactory, args[0]);
	}
}
 
Example 3
Source File: LowerCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("LCASE requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		// LowerCase function accepts only string literals.
		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexicalValue = literal.getLabel().toLowerCase();
			Optional<String> language = literal.getLanguage();

			if (language.isPresent()) {
				return valueFactory.createLiteral(lexicalValue, language.get());
			} else if (XMLSchema.STRING.equals(literal.getDatatype())) {
				return valueFactory.createLiteral(lexicalValue, XMLSchema.STRING);
			} else {
				return valueFactory.createLiteral(lexicalValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}

}
 
Example 4
Source File: UpperCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("UCASE requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		// UpperCase function accepts only string literal
		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexicalValue = literal.getLabel().toUpperCase();
			Optional<String> language = literal.getLanguage();

			if (language.isPresent()) {
				return valueFactory.createLiteral(lexicalValue, language.get());
			} else if (XMLSchema.STRING.equals(literal.getDatatype())) {
				return valueFactory.createLiteral(lexicalValue, XMLSchema.STRING);
			} else {
				return valueFactory.createLiteral(lexicalValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}

}
 
Example 5
Source File: Concat.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length == 0) {
		throw new ValueExprEvaluationException("CONCAT requires at least 1 argument, got " + args.length);
	}

	StringBuilder concatBuilder = new StringBuilder();
	String commonLanguageTag = null;
	boolean useLanguageTag = true;

	for (Value arg : args) {
		if (arg instanceof Literal) {
			Literal lit = (Literal) arg;

			if (!QueryEvaluationUtil.isStringLiteral(lit)) {
				throw new ValueExprEvaluationException("unexpected datatype for CONCAT operand: " + lit);
			}

			// verify that every literal argument has the same language tag. If
			// not, the operator result should not use a language tag.
			if (useLanguageTag && Literals.isLanguageLiteral(lit)) {
				if (commonLanguageTag == null) {
					commonLanguageTag = lit.getLanguage().get();
				} else if (!commonLanguageTag.equals(lit.getLanguage().orElse(null))) {
					commonLanguageTag = null;
					useLanguageTag = false;
				}
			} else {
				useLanguageTag = false;
			}

			concatBuilder.append(lit.getLabel());
		} else {
			throw new ValueExprEvaluationException("unexpected argument type for CONCAT operator: " + arg);
		}
	}

	Literal result = null;

	if (useLanguageTag) {
		result = valueFactory.createLiteral(concatBuilder.toString(), commonLanguageTag);
	} else {
		result = valueFactory.createLiteral(concatBuilder.toString());
	}

	return result;

}
 
Example 6
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the two operands match according to the <code>regex</code> operator.
 *
 * @return <tt>true</tt> if the operands match according to the <tt>regex</tt> operator, <tt>false</tt> otherwise.
 */
public Value evaluate(Regex node, BindingSet bindings)
		throws QueryEvaluationException {
	Value arg = evaluate(node.getArg(), bindings);
	Value parg = evaluate(node.getPatternArg(), bindings);
	Value farg = null;
	ValueExpr flagsArg = node.getFlagsArg();
	if (flagsArg != null) {
		farg = evaluate(flagsArg, bindings);
	}

	if (QueryEvaluationUtil.isStringLiteral(arg) && QueryEvaluationUtil.isSimpleLiteral(parg)
			&& (farg == null || QueryEvaluationUtil.isSimpleLiteral(farg))) {
		String text = ((Literal) arg).getLabel();
		String ptn = ((Literal) parg).getLabel();
		String flags = "";
		if (farg != null) {
			flags = ((Literal) farg).getLabel();
		}
		// TODO should this Pattern be cached?
		int f = 0;
		for (char c : flags.toCharArray()) {
			switch (c) {
			case 's':
				f |= Pattern.DOTALL;
				break;
			case 'm':
				f |= Pattern.MULTILINE;
				break;
			case 'i':
				f |= Pattern.CASE_INSENSITIVE;
				f |= Pattern.UNICODE_CASE;
				break;
			case 'x':
				f |= Pattern.COMMENTS;
				break;
			case 'd':
				f |= Pattern.UNIX_LINES;
				break;
			case 'u':
				f |= Pattern.UNICODE_CASE;
				break;
			case 'q':
				f |= Pattern.LITERAL;
				break;
			default:
				throw new ValueExprEvaluationException(flags);
			}
		}
		Pattern pattern = Pattern.compile(ptn, f);
		boolean result = pattern.matcher(text).find();
		return BooleanLiteral.valueOf(result);
	}

	throw new ValueExprEvaluationException();
}
 
Example 7
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether the two operands match according to the <code>regex</code> operator.
 *
 * @return <tt>true</tt> if the operands match according to the
 * <tt>regex</tt> operator, <tt>false</tt> otherwise.
 */
private Value evaluate(Regex node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value arg = evaluate(node.getArg(), bindings);
    Value parg = evaluate(node.getPatternArg(), bindings);
    Value farg = null;
    ValueExpr flagsArg = node.getFlagsArg();
    if (flagsArg != null) {
        farg = evaluate(flagsArg, bindings);
    }
    if (QueryEvaluationUtil.isStringLiteral(arg) && QueryEvaluationUtil.isSimpleLiteral(parg)
            && (farg == null || QueryEvaluationUtil.isSimpleLiteral(farg))) {
        String text = ((Literal) arg).getLabel();
        String ptn = ((Literal) parg).getLabel();
        String flags = "";
        if (farg != null) {
            flags = ((Literal) farg).getLabel();
        }
        // TODO should this Pattern be cached?
        int f = 0;
        for (char c : flags.toCharArray()) {
            switch (c) {
                case 's':
                    f |= Pattern.DOTALL;
                    break;
                case 'm':
                    f |= Pattern.MULTILINE;
                    break;
                case 'i':
                    f |= Pattern.CASE_INSENSITIVE;
                    f |= Pattern.UNICODE_CASE;
                    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(flags);
            }
        }
        Pattern pattern = Pattern.compile(ptn, f);
        boolean result = pattern.matcher(text).find();
        return BooleanLiteral.valueOf(result);
    }
    throw new ValueExprEvaluationException();
}