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

The following examples show how to use org.apache.jena.shared.PrefixMapping#setNsPrefix() . 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: 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 2
Source File: SPARQLPrefixResolver.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
/** @see org.apache.jena.shared.PrefixMapping#shortForm(java.lang.String) */
@Override
public String shortForm(final String uri) {

	PrefixMapping pmapLocal = super.getLocalPrefixMapping();
	PrefixMapping pmapGlobal = super.getGlobalPrefixMapping();
	String s = pmapLocal.shortForm(uri);
	if (pmapGlobal == null) {
		return s;
	}

	if (s == null || s.equals(uri)) {
		s = pmapGlobal.shortForm(uri);
		if (s != null && !s.equals(uri)) {
			boolean b = false;
			try {
				b = (boolean) checkValidPrefixMethod.invoke(null, s);
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (b) {
				String prefix = s.substring(0, s.indexOf(":"));
				pmapLocal.setNsPrefix(prefix, pmapGlobal.getNsPrefixURI(prefix));
			}
		}
	}

	return s;
}
 
Example 3
Source File: SPARQLPrefixResolver.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
/** @see org.apache.jena.shared.PrefixMapping#qnameFor(java.lang.String) */
@Override
public String qnameFor(final String uri) {
	PrefixMapping pmapLocal = super.getLocalPrefixMapping();
	PrefixMapping pmapGlobal = super.getGlobalPrefixMapping();
	String s = pmapLocal.qnameFor(uri);

	if (s != null) {
		return s;
	}

	if (pmapGlobal != null) {
		s = pmapGlobal.qnameFor(uri);
		if (s != null) {
			boolean b = false;
			try {
				b = (boolean) checkValidPrefixMethod.invoke(null, s);
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (b) {
				String prefix = s.substring(0, s.indexOf(":"));
				pmapLocal.setNsPrefix(prefix, pmapGlobal.getNsPrefixURI(prefix));
			}
		}
	}
	return s;
}
 
Example 4
Source File: PrefixUtils.java    From shacl with Apache License 2.0 5 votes vote down vote up
/** 
 * Copy prefix mappings into {@code dstGraph} from {@code srcGraph},
 * checking whether the prefix mapping is already set.
 * @param dstGraph  the destination graph
 * @param srcGraph  the source graph
 * @return false if no changes where made.
 */
public static boolean copyPrefixMap(Graph dstGraph, Graph srcGraph) {
    boolean changeMade = false;
    PrefixMapping dstPM = dstGraph.getPrefixMapping();
    PrefixMapping srcPM = srcGraph.getPrefixMapping();
    // Copy different prefix mappings.
    for (Map.Entry<String, String> e : srcPM.getNsPrefixMap().entrySet()) {
        if (!e.getValue().equals(dstPM.getNsPrefixURI(e.getKey()))) {
            dstPM.setNsPrefix(e.getKey(), e.getValue());
            changeMade = true;
        }
    }
    return changeMade;
}
 
Example 5
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 6
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 7
Source File: JenaUtil.java    From shacl with Apache License 2.0 4 votes vote down vote up
private static void ensurePrefix(PrefixMapping prefixMapping, String prefix, String uristr) {
    // set if not present, or if different
    if (!uristr.equals(prefixMapping.getNsPrefixURI(prefix))) {
        prefixMapping.setNsPrefix(prefix, uristr);
    }
}
 
Example 8
Source File: LargeInputTest.java    From tarql with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Before
public void setUp() {
	PrefixMapping prefixes = new PrefixMappingImpl();
	prefixes.setNsPrefix("ex", EX + "#");
}