org.apache.jena.shared.impl.PrefixMappingImpl Java Examples

The following examples show how to use org.apache.jena.shared.impl.PrefixMappingImpl. 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: SPARQLSubstitutions.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a parsable SPARQL string based on a fragment and prefix declarations.
 * Depending on the setting of the flag useGraphPrefixes, this either uses the
 * prefixes from the Jena graph of the given executable, or strictly uses sh:prefixes.
 * @param str  the query fragment (e.g. starting with SELECT)
 * @param executable  the sh:SPARQLExecutable potentially holding the sh:prefixes
 * @return the parsable SPARQL string
 */
public static String withPrefixes(String str, Resource executable) {
	if(useGraphPrefixes) {
		return ARQFactory.get().createPrefixDeclarations(executable.getModel()) + str;
	}
	else {
		StringBuffer sb = new StringBuffer();
		PrefixMapping pm = new PrefixMappingImpl();
		Set<Resource> reached = new HashSet<Resource>();
		for(Resource ontology : JenaUtil.getResourceProperties(executable, SH.prefixes)) {
			String duplicate = collectPrefixes(ontology, pm, reached);
			if(duplicate != null) {
				throw new SHACLException("Duplicate prefix declaration for prefix " + duplicate);
			}
		}
		for(String prefix : pm.getNsPrefixMap().keySet()) {
			sb.append("PREFIX ");
			sb.append(prefix);
			sb.append(": <");
			sb.append(pm.getNsPrefixURI(prefix));
			sb.append(">\n");
		}
		sb.append(str);
		return sb.toString();
	}
}
 
Example #3
Source File: DiffGraph.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public PrefixMapping getPrefixMapping() {
	if (pm == null) {
		// copy delegate's prefix mapping.
		pm = new PrefixMappingImpl().setNsPrefixes(base.getPrefixMapping());
	}
	return pm;
}
 
Example #4
Source File: BdbTypeNameStore.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public BdbTypeNameStore(DataStorage dataStore, String dataStoreRdfPrefix) throws IOException {
  this.dataStoreRdfPrefix = dataStoreRdfPrefix;
  prefixMapping = new PrefixMappingImpl();
  final String storedValue = dataStore.getValue();
  if (storedValue == null) {
    data = new TypeNames();
    addStandardPrefixes();
  } else {
    data = objectMapper.readValue(storedValue, new TypeReference<TypeNames>() {});
  }
  prefixMapping.setNsPrefixes(data.prefixes);
  this.dataStore = dataStore;
}
 
Example #5
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 #6
Source File: TarqlQueryExecution.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private QueryExecution createQueryExecution(Query query, Model model) {
	QueryExecution result = QueryExecutionFactory.create(query, model);
	PrefixMappingImpl prefixes = new PrefixMappingImpl();
	prefixes.setNsPrefixes(tq.getPrologue().getPrefixMapping());
	prefixes.setNsPrefix("tarql", tarql.NS);
	result.getContext().set(ExpandPrefixFunction.PREFIX_MAPPING, prefixes);
	return result;
}
 
Example #7
Source File: FunctionTest.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Before
public void setUp() {
	prefixes = new PrefixMappingImpl();
	prefixes.setNsPrefix("tarql", tarql.NS);
	env = new FunctionEnvBase();
	env.getContext().set(ExpandPrefixFunction.PREFIX_MAPPING, prefixes);
}
 
Example #8
Source File: TextOutputStarTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void withoutPrefixMapping() {
	final MyTextOutput o = new MyTextOutput( new PrefixMappingImpl() );
	assertEquals("<http://example.com/i> <http://example.com/i> <http://example.com/i> ", getResult(o));
}
 
Example #9
Source File: TextOutputStarTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
static public PrefixMapping getPrefixMappingForTests() {
	return new PrefixMappingImpl().setNsPrefix("ex", "http://example.com/");
}
 
Example #10
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 + "#");
}