Java Code Examples for org.apache.jena.sparql.expr.NodeValue#isString()

The following examples show how to use org.apache.jena.sparql.expr.NodeValue#isString() . 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: FUN_JSONPath.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(NodeValue json, NodeValue jsonpath) {
    if (json.getDatatypeURI() != null
            && !json.getDatatypeURI().equals(datatypeUri)
            && !json.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) {
        LOG.debug("The datatype of the first argument should be <" + datatypeUri + ">"
                + " or <http://www.w3.org/2001/XMLSchema#string>. Got "
                + json.getDatatypeURI());
    }
    if (!jsonpath.isString()) {
        LOG.debug("The second argument should be a string. Got " + json);
    }

    try {
        Object value = JsonPath.parse(json.asNode().getLiteralLexicalForm())
                .limit(1).read(jsonpath.getString());
        return nodeForObject(value);
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(json.asNode());
            LOG.debug("No evaluation of " + compressed + ", " + jsonpath);
        }
        throw new ExprEvalException("No evaluation of " + jsonpath);
    }
}
 
Example 2
Source File: FUN_MixedCase.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(NodeValue node) {
    if (!node.isString()) {
        LOG.debug("The input should be a string. Got " + node);
        throw new ExprEvalException("The input should be a string. Got " + node);
    }
    String string = node.getString();
    StringBuilder converted = new StringBuilder();
    boolean convertNext = false;
    for (char ch : string.toCharArray()) {
        if (Character.isWhitespace(ch)) {
            convertNext = true;
        } else if (!Character.isLetterOrDigit(ch)) {
        } else if (convertNext) {
            converted.append(Character.toUpperCase(ch));
            convertNext = false;
        } else {
            converted.append(ch);
        }
    }
    return new NodeValueString(converted.toString());
}
 
Example 3
Source File: FUN_Property.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
    if (args == null) {
        throw new ARQInternalErrorException("FunctionBase: Null args list");
    }
    if (args.size() != 2) {
        throw new ExprEvalException("Expecting two argument");
    }
    NodeValue file = args.get(0).eval(binding, env);
    NodeValue propertyNode = args.get(1).eval(binding, env);
    if (!propertyNode.isString()) {
        throw new ExprEvalException("Second argument must be a string. Got " + propertyNode);
    }
    Properties properties;
    try {
        properties = getProperties(file, env);
    } catch (IOException ex) {
        throw new ExprEvalException("IOException while extracting properties document " + file, ex);
    }
    String prop = properties.getProperty(propertyNode.getString());
    if (prop == null) {
        throw new ExprEvalException("Property " + prop + " not found in properties document " + file);
    }
    return new NodeValueString(prop);
}
 
Example 4
Source File: ST_Format.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
public NodeValue exec(List<NodeValue> args, FunctionEnv env) {
    if (args.size() < 1) {
        LOG.debug("Expecting at least one arguments.");
        throw new ExprEvalException("Expecting at least one arguments.");
    }
    final NodeValue format = args.get(0);
    if (!format.isIRI() && !format.isString() && !format.asNode().isLiteral()) {
        LOG.debug("First argument must be a URI or a String.");
        throw new ExprEvalException("First argument must be a URI or a String.");
    }

    Object[] params = new String[args.size() - 1];
    for (int i = 0; i < args.size() - 1; i++) {
        params[i] = args.get(i + 1).asUnquotedString();
    }
    try {
        String output = String.format(getString(format, env), params);
        return new NodeValueString(output);
    } catch (IllegalFormatException ex) {
        throw new ExprEvalException("Exception while executing st:format(" + args + ")");
    }
}
 
Example 5
Source File: FUN_CamelCase.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(NodeValue node) {
    if (!node.isString()) {
        LOG.debug("The input should be a string. Got " + node);
        throw new ExprEvalException("The input should be a string. Got " + node);
    }
    String string = node.getString();
    StringBuilder converted = new StringBuilder();
    boolean convertNext = true;
    for (char ch : string.toCharArray()) {
        if (Character.isWhitespace(ch)) {
            convertNext = true;
        } else if (!Character.isLetterOrDigit(ch)) {
        } else if (convertNext) {
            converted.append(Character.toUpperCase(ch));
            convertNext = false;
        } else {
            converted.append(ch);
        }
    }
    return new NodeValueString(converted.toString());
}
 
Example 6
Source File: ExpandPrefixFunction.java    From tarql with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public NodeValue exec(NodeValue prefix, Context context) {
	if (prefix == null) {
		return null;
	}
	if (!prefix.isString()) {
		throw new ExprEvalException(NAME + ": not a string: " + prefix);
	}
	PrefixMapping prefixes = context.get(PREFIX_MAPPING);
	if (prefixes == null) {
		throw new ExprEvalException(NAME + ": no prefix mapping registered");
	}
	String iri = prefixes.getNsPrefixURI(prefix.asString());
	if (iri == null) {
		throw new ExprEvalException(NAME + ": prefix not defined: " + prefix);
	}
	return NodeValue.makeString(iri);
}
 
Example 7
Source File: FUN_TitleCase.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public NodeValue exec(NodeValue node) {
    if (!node.isString()) {
        LOG.debug("The input should be a string. Got " + node);
        throw new ExprEvalException("The input should be a string. Got " + node);
    }
    String string = node.getString();
    StringBuilder converted = new StringBuilder();
    boolean firstSpace = false;
    boolean convertNext = true;
    for (char ch : string.toCharArray()) {
        if (Character.isWhitespace(ch)) {
            if(firstSpace) {
                converted.append(' ');
                firstSpace = false;
                convertNext = true;
            }
        } else if (!Character.isLetterOrDigit(ch)) {
        } else if (convertNext) {
            converted.append(Character.toUpperCase(ch));
            convertNext = false;
            firstSpace = true;
        } else {
            converted.append(ch);
            firstSpace = true;
        }
    }
    return new NodeValueString(converted.toString());
}
 
Example 8
Source File: FUN_regex.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public NodeValue exec(NodeValue stringValue, NodeValue regex, NodeValue locationV) {
    if (!stringValue.isLiteral()) {
        LOG.debug("First argument must be a literal, got: " + stringValue);
        throw new ExprEvalException("First argument must be a literal, got: " + stringValue);
    }
    String string = stringValue.asNode().getLiteralLexicalForm();

    if (!regex.isString()) {
        LOG.debug("Second argument must be a string, got: " + regex);
        throw new ExprEvalException("Second argument must be a string, got: " + regex);
    }
    String regexString = regex.asString();
    Pattern pattern;
    try {
        pattern = Pattern.compile(regexString, Pattern.MULTILINE);
    } catch(Exception ex) {
        LOG.debug("Exception while compiling regex string " + regexString, ex);
        throw new ExprEvalException("Exception while compiling regex string " + regexString, ex);
    }

    if (!locationV.isInteger()) {
        LOG.debug("Third argument must be an integer, got: " + locationV);
        throw new ExprEvalException("Third argument must be an integer, got: " + locationV);
    }

    int location = locationV.getInteger().intValue();

    Matcher matcher = pattern.matcher(string);

    if (matcher.find()) {
    	return new NodeValueString(matcher.group(location));
    }
    throw new ExprEvalException("The regex did not match on " +stringValue + " ( regex was " + regex);
}
 
Example 9
Source File: classify.java    From xcurator with Apache License 2.0 5 votes vote down vote up
@Override
public NodeValue exec(NodeValue v)
{ 
    if ( v.isNumber() ) return NodeValue.makeString("number") ;
    if ( v.isDateTime() ) return NodeValue.makeString("dateTime") ;
    if ( v.isString() ) return NodeValue.makeString("string") ;
    
    return NodeValue.makeString("unknown") ;
}
 
Example 10
Source File: ExpandPrefixedNameFunction.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public NodeValue exec(NodeValue name, Context context) {
	if (name == null) return null;
	if (!name.isString()) throw new ExprEvalException("Not a string: " + name);
	PrefixMapping prefixes = context.get(ExpandPrefixFunction.PREFIX_MAPPING);
	if (prefixes == null) throw new ExprEvalException("No prefix mapping registered");
	String pname = name.asString();
	int idx = pname.indexOf(':');
	if (idx == -1) throw new ExprEvalException("Not a prefixed name: " + name);
	String prefix = pname.substring(0, idx);
	String iri = prefixes.getNsPrefixURI(prefix);
	if (iri == null) throw new ExprEvalException("Prefix not defined: " + prefix);
	return NodeValue.makeNode(NodeFactory.createURI(iri + pname.substring(idx + 1)));
}
 
Example 11
Source File: FUN_XPath.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public NodeValue exec(NodeValue xml, NodeValue xpath) {
    if (xml.getDatatypeURI() != null
            && !xml.getDatatypeURI().equals(XML_URI)
            && !xml.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) {
        LOG.debug("The URI of NodeValue1 MUST be <" + XML_URI + ">"
                + " or <http://www.w3.org/2001/XMLSchema#string>. Got "
                + xml.getDatatypeURI());
    }
    if (!xpath.isString()) {
        LOG.debug("The second argument should be a string. Got " + xpath);
    }
    DocumentBuilderFactory builderFactory
            = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
        // THIS IS A HACK !! FIND A BETTER WAY TO MANAGE NAMESPACES
        String xmlstring = xml.asNode().getLiteralLexicalForm().replaceAll("xmlns=\"[^\"]*\"", "");
        
        builder = builderFactory.newDocumentBuilder();
        InputStream in = new ByteArrayInputStream(xmlstring.getBytes("UTF-8"));
        Document document = builder.parse(in);

        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new UniversalNamespaceResolver(document));
        //Node node = (Node) xPath.compile(xpath.getString()).evaluate(document, XPathConstants.NODE);

        org.w3c.dom.Node xmlNode = (org.w3c.dom.Node) xPath
                .compile(xpath.getString())
                .evaluate(document, XPathConstants.NODE);
        if (xmlNode == null) {
            LOG.debug("No evaluation of " + xpath);
            throw new ExprEvalException("No evaluation of " + xpath);
        }
        return nodeForNode(xmlNode);
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(xml.asNode());
            LOG.debug("No evaluation of " + compressed + ", " + xpath, ex);
        }
        throw new ExprEvalException("No evaluation of " + xpath, ex);
    }
}