org.bridgedb.uri.tools.GraphResolver Maven / Gradle / Ivy
The newest version!
// BridgeDb,
// An abstraction layer for identifier mapping services, both local and online.
//
// Copyright 2006-2009 BridgeDb developers
// Copyright 2012-2013 Christian Y. A. Brenninkmeijer
// Copyright 2012-2013 OpenPhacts
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.bridgedb.uri.tools;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.bridgedb.rdf.UriPattern;
import org.bridgedb.utils.BridgeDBException;
import org.bridgedb.utils.ConfigReader;
/**
*
* @author Christian
*/
public class GraphResolver {
private HashMap> allowedUriPattern;
private final static String PROPERTIES_FILE = "graph.properties";
private final static String PROPERTY_PREFIX = "context.";
private final static String PATTERN = "pattern";
private final static String GRAPH_POSTFIX = ".graph";
private static GraphResolver instance;
public static GraphResolver getInstance() throws BridgeDBException{
if (instance == null){
UriPattern.refreshUriPatterns();
instance = new GraphResolver();
}
return instance;
}
private GraphResolver() throws BridgeDBException{
readProperties();
}
private void readProperties() throws BridgeDBException{
allowedUriPattern = new HashMap>();
Properties properties = ConfigReader.getProperties(PROPERTIES_FILE);
Set keys = properties.stringPropertyNames();
for (String key:keys){
if (key.startsWith(PROPERTY_PREFIX)){
String[] parts = key.split("\\.");
if (parts[2].equals(PATTERN)){
String graphKey = PROPERTY_PREFIX + parts[1] + GRAPH_POSTFIX;
String graph = properties.getProperty(graphKey);
String pattern = properties.getProperty(key);
addPatterns(graph, pattern);
}
}
}
}
private void addPatterns(String graph, String pattern) throws BridgeDBException{
Set uriPatterns = RegexUriPattern.existingByPattern(pattern);
//unable to set default some example CHEBI have different URIs in different contexts
//UriPattern test = UriPattern.existingByPattern(pattern);
//test.setGraphDefault();
if (uriPatterns.isEmpty() ){
throw new BridgeDBException("no UriPattern known for " + pattern);
}
addPatterns(graph, uriPatterns);
}
private void addPatterns(String graph, Set uriPatterns) throws BridgeDBException{
Set patterns = allowedUriPattern.get(graph);
if (patterns == null){
patterns = new HashSet();
}
patterns.addAll(uriPatterns);
allowedUriPattern.put(graph, patterns);
}
private void addPattern(String graph, RegexUriPattern uriPattern) throws BridgeDBException{
Set patterns = allowedUriPattern.get(graph);
if (patterns == null){
patterns = new HashSet();
}
patterns.add(uriPattern);
allowedUriPattern.put(graph, patterns);
}
public static Set knownGraphs() throws BridgeDBException{
return getInstance().allowedUriPattern.keySet();
}
public static Set getUriPatternsForGraph(String graph) throws BridgeDBException {
if (graph == null || graph.isEmpty()){
return new HashSet();
}
GraphResolver resolver = getInstance();
Set results = resolver.getAllowedPatterns(graph);
if (results == null){
throw new BridgeDBException ("Unkown Graph " + graph);
}
return results;
}
public static void addMapping(String graph, String pattern) throws BridgeDBException{
GraphResolver gr = getInstance();
gr.addPatterns(graph, pattern);
}
public static void addMapping(String graph, UriPattern uriPattern) throws BridgeDBException{
GraphResolver gr = getInstance();
gr.addPatterns(graph, RegexUriPattern.byPattern(uriPattern));
}
public static void addMapping(String graph, RegexUriPattern uriPattern) throws BridgeDBException{
GraphResolver gr = getInstance();
gr.addPattern(graph, uriPattern);
}
public static void addTestMappings() throws BridgeDBException{
GraphResolver gr = getInstance();
gr.addPatterns("http://www.conceptwiki.org", "http://www.conceptwiki.org/concept/$id");
gr.addPatterns("http://www.chemspider.com", "http://rdf.chemspider.com/$id");
}
private Set getAllowedPatterns(String graph) {
return allowedUriPattern.get(graph);
}
public Map> getAllowedUriPatterns() {
return allowedUriPattern;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy