Java Code Examples for org.apache.jena.sparql.util.Context#get()

The following examples show how to use org.apache.jena.sparql.util.Context#get() . 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: 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 2
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
public static void loadGraph(Context context, String sourceURI, String baseURI, StreamRDF dest) {
	if(getDataset(context).containsNamedModel(sourceURI)) {
		final Model model = getDataset(context).getNamedModel(sourceURI);
		StreamRDFOps.sendGraphToStream(model.getGraph(), dest);
		return;
	}
	if(!isRootContext(context)) {
		Context parentContext = (Context) context.get(PARENT_CONTEXT);
		loadGraph(parentContext, sourceURI, baseURI, dest);
		return;
	}
	final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
	final String acceptHeader = "text/turtle;q=1.0,application/rdf+xml;q=0.9,*/*;q=0.1";
	final LookUpRequest request = new LookUpRequest(sourceURI, acceptHeader);
	try (TypedInputStream tin = sm.open(request);) {
		if(tin == null) {
			LOG.warn("Could not locate graph " + request);
			return;
		}
		Lang lang = RDFLanguages.contentTypeToLang(tin.getMediaType());
		RDFParser.create().source(tin).base(baseURI).context(context).lang(lang).parse(dest);
	} catch (RiotException ex) {
		LOG.warn("Error while loading graph " + sourceURI, ex);
	}
}
 
Example 3
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static void close(Context context) {
	Commons commons = context.get(COMMONS);
	try {
		LOG.trace("Closing context");
		commons.closingTasks.forEach(Runnable::run);
	} catch (Exception ex) {
		LOG.warn("Exception while closing context:", ex);
	}
}
 
Example 4
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 5
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static StreamRDF getGenerateOutput(Context context) {
	StreamRDF output = context.get(OUTPUT_GENERATE);
	if(output != null) {
		return output;
	}
	if(!isRootContext(context)) {
		Context parentContext = (Context) context.get(PARENT_CONTEXT);
		return getGenerateOutput(parentContext);
	}
	return null;
}
 
Example 6
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static Consumer<ResultSet> getSelectOutput(Context context) {
	Consumer<ResultSet> output = context.get(OUTPUT_SELECT);
	if(output != null) {
		return output;
	}
	if(!isRootContext(context)) {
		Context parentContext = (Context) context.get(PARENT_CONTEXT);
		return getSelectOutput(parentContext);
	}
	return null;
}
 
Example 7
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static IndentedWriter getTemplateOutput(Context context) {
	IndentedWriter writer = context.get(OUTPUT_TEMPLATE);
	if(writer != null) {
		return writer;
	}
	if(!isRootContext(context)) {
		Context parentContext = (Context) context.get(PARENT_CONTEXT);
		return getTemplateOutput(parentContext);
	}
	return null;
}
 
Example 8
Source File: SourcePlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
final protected Binding exec(
        final Binding binding,
        final Context context) {

    LOG.debug("Start " + this);
    Objects.nonNull(binding);
    // generate the source URI.
    final String sourceUri = getActualSource(binding);
    final String acceptHeader = getAcceptHeader(binding);
    LOG.trace("... resolved to SOURCE <" + sourceUri + "> ACCEPT " + acceptHeader + " AS " + var);
    final LookUpRequest request = new LookUpRequest(sourceUri, acceptHeader);
    final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
    Objects.requireNonNull(sm);
    final TypedInputStream stream = sm.open(request);
    if (stream == null) {
        LOG.info("Exec SOURCE <" + sourceUri + "> ACCEPT " + acceptHeader + " AS " + var + " returned nothing.");
        return BindingFactory.binding(binding);
    }
    try (InputStream in = stream.getInputStream()) {
        final String literal = IOUtils.toString(in, "UTF-8");
        final RDFDatatype dt;
        if (stream.getMediaType() != null && stream.getMediaType().getContentType() != null) {
            dt = tm.getSafeTypeByName("http://www.iana.org/assignments/media-types/" + stream.getMediaType().getContentType());
        } else {
            dt = tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#string");
        }
        final Node n = NodeFactory.createLiteral(literal, dt);
        LOG.debug("Exec " + this + " returned. "
                + "Enable TRACE level for more.");
        if (LOG.isTraceEnabled()) {
            LOG.trace("Exec " + this + " returned\n" + LogUtils.compress(n));
        }
        return BindingFactory.binding(binding, var, n);
    } catch (IOException | DatatypeFormatException ex) {
        LOG.warn("Exception while looking up " + sourceUri + ":", ex);
        return BindingFactory.binding(binding);
    }
}
 
Example 9
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static synchronized Node[] getInfo(final Context context, final Node_List list) {
	final int size = (Integer) context.get(SIZE);
	if (size == 0) {
		return NIL;
	}
	if (context.isUndef(LIST_NODES)) {
		context.set(LIST_NODES, new HashMap<>());
	}
	final Map<Node_List, Node[]> listNodes = context.get(LIST_NODES);
	if (!listNodes.containsKey(list)) {
		createListNodes(listNodes, list, size);
	}
	return listNodes.get(list);
}
 
Example 10
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static PrefixMapping getPrefixMapping(Context context) {
	PrefixMapping pm = context.get(PREFIX_MANAGER);
	Objects.nonNull(pm);
	return pm;
}
 
Example 11
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static void addTaskOnClose(Context context, final Runnable task) {
	Commons commons = context.get(COMMONS);
	commons.closingTasks.add(task);
}
 
Example 12
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static String getBase(Context context) {
	String base = context.get(BASE);
	return base;
}
 
Example 13
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static boolean isDebugStConcat(Context context) {
	Commons commons = context.get(COMMONS);
	return commons.debugTemplate;
}
 
Example 14
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static boolean isRootContext(Context context) {
	return context.get(PARENT_CONTEXT) == null;
}
 
Example 15
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static QueryExecutor getQueryExecutor(Context context) {
	Commons commons = context.get(COMMONS);
	return commons.queryExecutor;
}
 
Example 16
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static ExecutorService getExecutor(Context context) {
	Commons commons = context.get(COMMONS);
	return commons.executor;
}
 
Example 17
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static TypedInputStream openStream(Context context, String sourceUri, String acceptHeader) {
	final LookUpRequest request = new LookUpRequest(sourceUri, acceptHeader);
	final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
	Objects.requireNonNull(sm);
	return sm.open(request);
}
 
Example 18
Source File: IteratorFunctionRegistry.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static IteratorFunctionRegistry get(Context context) {
	if (context == null) {
		return null;
	}
	return (IteratorFunctionRegistry) context.get(SPARQLExt.REGISTRY_ITERATORS);
}
 
Example 19
Source File: DB2DescribeHandler.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public void start(Model accumulateResultModel, Context cxt) {
	acc = accumulateResultModel;
	this.dataset = (Dataset) cxt.get(ARQConstants.sysCurrentDataset);
	resources = new HashSet<String>();
}
 
Example 20
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static Dataset getDataset(Context context) {
	return context.get(DATASET);
}