Java Code Examples for org.apache.jena.shared.PrefixMapping#getNsPrefixURI()

The following examples show how to use org.apache.jena.shared.PrefixMapping#getNsPrefixURI() . 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: SPARQLPrefixResolver.java    From NLIWOD with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public String getNsPrefixURI(final String prefix) {
	String s = super.getLocalPrefixMapping().getNsPrefixURI(prefix);
	if (s != null) {
		return s;
	}

	PrefixMapping pmapGlobal = super.getGlobalPrefixMapping();
	s = pmapGlobal.getNsPrefixURI(prefix);
	if (s != null) {
		super.getLocalPrefixMapping().setNsPrefix(prefix, s);
		return s;
	}
	return null;

}
 
Example 2
Source File: SPARQLPrefixResolver.java    From NLIWOD with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public String expandPrefix(final String prefixed) {
	String s = super.getLocalPrefixMapping().expandPrefix(prefixed);
	PrefixMapping pmapGlobal = super.getGlobalPrefixMapping();
	if (pmapGlobal == null) {
		return s;
	}

	if (s == null || s.equals(prefixed)) {
		if (pmapGlobal != null) {
			s = pmapGlobal.expandPrefix(prefixed);
		}
		if (s != null) {
			int colon = prefixed.indexOf(':');
			String prefix = prefixed.substring(0, colon);
			String uri = pmapGlobal.getNsPrefixURI(prefix);
			super.getLocalPrefixMapping().setNsPrefix(prefix, uri);
		}
	}
	return s;
}
 
Example 3
Source File: ARQFactory.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new Query from a partial query (possibly lacking
 * PREFIX declarations), using the ARQ syntax specified by <code>getSyntax</code>.
 * @param model  the Model to operate on
 * @param partialQuery  the (partial) query string
 * @return the Query
 */
public Query createQuery(Model model, String partialQuery) {
	PrefixMapping pm = new PrefixMappingImpl();
    String defaultNamespace = JenaUtil.getNsPrefixURI(model, "");
    if(defaultNamespace != null) {
        pm.setNsPrefix("", defaultNamespace);
    }
    Map<String,String> extraPrefixes = ExtraPrefixes.getExtraPrefixes();
    for(String prefix : extraPrefixes.keySet()) {
    	String ns = extraPrefixes.get(prefix);
    	if(ns != null && pm.getNsPrefixURI(prefix) == null) {
    		pm.setNsPrefix(prefix, ns);
    	}
    }

    // Get all the prefixes from the model at once.
    Map<String, String> map = model.getNsPrefixMap();
    map.remove("");
    pm.setNsPrefixes(map);
    
	return doCreateQuery(partialQuery, pm);
}
 
Example 4
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 5
Source File: PrefixUtils.java    From shacl with Apache License 2.0 5 votes vote down vote up
/** 
 * Remove prefix mappings from {@code dstGraph} that do not exist in {@code srcGraph}.
 * @param dstGraph  the destination graph
 * @param srcGraph  the source graph
 * @return false if no changes where made.
 */
public static boolean removeMissingPrefixes(Graph dstGraph, Graph srcGraph) {
    boolean changeMade = false;
    PrefixMapping dstPM = dstGraph.getPrefixMapping();
    PrefixMapping srcPM = srcGraph.getPrefixMapping();
    // Delete those from dstPM that are not in srcPM
    for(String prefix : dstPM.getNsPrefixMap().keySet()) {
        if(srcPM.getNsPrefixURI(prefix) == null) {
            dstPM.removeNsPrefix(prefix);
            changeMade = true;
        }
    }
    return changeMade;
}
 
Example 6
Source File: SPARQLSubstitutions.java    From shacl with Apache License 2.0 5 votes vote down vote up
private static String collectPrefixes(Resource ontology, PrefixMapping pm, Set<Resource> reached) {
	
	reached.add(ontology);
	
	for(Resource decl : JenaUtil.getResourceProperties(ontology, SH.declare)) {
		String prefix = JenaUtil.getStringProperty(decl, SH.prefix);
		String ns = JenaUtil.getStringProperty(decl, SH.namespace);
		if(prefix != null && ns != null) {
			String oldNS = pm.getNsPrefixURI(prefix);
			if(oldNS != null && !oldNS.equals(ns)) {
				return prefix;
			}
			pm.setNsPrefix(prefix, ns);
		}
	}
	
	for(Resource imp : JenaUtil.getResourceProperties(ontology, OWL.imports)) {
		if(!reached.contains(imp)) {
			String duplicate = collectPrefixes(imp, pm, reached);
			if(duplicate != null) {
				return duplicate;
			}
		}
	}
	
	return null;
}
 
Example 7
Source File: StreamingRDFWriter.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private PrefixMapping ensureRDFPrefix(PrefixMapping prefixes) {
	// Some prefix already registered for the RDF namespace -- good enough
	if (prefixes.getNsURIPrefix(RDF.getURI()) != null) return prefixes;
	// rdf: is registered to something else -- give up
	if (prefixes.getNsPrefixURI("rdf") != null) return prefixes;
	// Register rdf:
	PrefixMapping newPrefixes = new PrefixMappingImpl();
	newPrefixes.setNsPrefixes(prefixes);
	newPrefixes.setNsPrefix("rdf", RDF.getURI());
	return newPrefixes;
}
 
Example 8
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)));
}