public static NamespacePrefixService getInstance(){ if(INSTANCE == null){ try { INSTANCE = new StanbolNamespacePrefixService(new File(PREFIX_MAPPINGS)); } catch (IOException e) { throw new IllegalStateException("Unable to read "+PREFIX_MAPPINGS,e); } } return INSTANCE; }
@Override public String getShortName(String uri) { String namespace = NamespaceMappingUtils.getNamespace(uri); if(namespace != null){ String prefix = getPrefix(namespace); if(prefix != null){ return prefix+uri.substring(namespace.length()); } //else no mapping -> return the full URI } //no namespace -> return the full URI return uri; }
@Override public String getPrefix(String namespace) { List<String> prefixes = getPrefixes(namespace); return prefixes.isEmpty() ? null : prefixes.get(0); }
@Override public String getNamespace(String prefix) { String namespace; mappingsLock.readLock().lock(); try { namespace = prefixMap.get(prefix); } finally { mappingsLock.readLock().unlock(); } if(namespace == null){ ServiceReference[] refs = getSortedProviderReferences(); for(int i=0;namespace == null && i<refs.length;i++){ NamespacePrefixProvider provider = getService(refs[i]); if(provider != null){ namespace = provider.getNamespace(prefix); } } } return namespace; }
/** * Imports tab separated prefix mappings from the parsed Stream * @param in * @throws IOException */ public void importPrefixMappings(InputStream in) throws IOException { readPrefixMappings(in); }
@Override public String setPrefix(String prefix, String namespace) { boolean validPrefix = NamespaceMappingUtils.checkPrefix(prefix); boolean validNamespace = NamespaceMappingUtils.checkNamespace(namespace); if(validPrefix && validNamespace){ try { return addMapping(prefix, namespace,true); } catch (IOException e) { throw new IllegalStateException("Unable to add Mapping because of an " + e.getClass().getSimpleName()+ "while setting the parsed mapping",e); } } else { StringBuilder sb = new StringBuilder("The parsed Mapping is not Valid: "); if(!validPrefix){ sb.append(String.format("The parsed prefix '%s' MUST only contain " + "alpha numeric chars including '_' and '-'",prefix)); } if(!validNamespace){ if(!validPrefix) { sb.append("| "); } sb.append(String.format("The parsed namespace '%s' MUST end with" + "'/' or '#' in case of an URI or ':' in case of an URN",namespace)); } throw new IllegalArgumentException(sb.toString()); } }
@Override public String getFullName(String shortNameOrUri) { String prefix = NamespaceMappingUtils.getPrefix(shortNameOrUri); if(prefix != null){ String namespace = getNamespace(prefix); if(namespace != null){ return namespace+shortNameOrUri.substring(prefix.length()+1); } else { //no mapping return null return null; } } else { //not a shortName ... return the parsed return shortNameOrUri; } }
synchronized (this) { if(providersTracker == null && bundleContext != null){ openTracker();
writePrefixMappings(new FileOutputStream(mappingsFile, false));
@Override public List<String> getPrefixes(String namespace) { List<String> prefixes; mappingsLock.readLock().lock(); try { prefixes = namespaceMap.get(namespace); } finally { mappingsLock.readLock().unlock(); } if(prefixes == null){ ServiceReference[] refs = getSortedProviderReferences(); for(int i=0;prefixes == null && i<refs.length;i++){ NamespacePrefixProvider provider = getService(refs[i]); if(provider != null){ prefixes = provider.getPrefixes(namespace); } } } return prefixes == null ? Collections.EMPTY_LIST:prefixes; }
/** * Imports tab separated prefix mappings from the parsed Stream * @param in * @throws IOException */ public void importPrefixMappings(InputStream in) throws IOException { readPrefixMappings(in); }
@Override public String setPrefix(String prefix, String namespace) { boolean validPrefix = NamespaceMappingUtils.checkPrefix(prefix); boolean validNamespace = NamespaceMappingUtils.checkNamespace(namespace); if(validPrefix && validNamespace){ try { return addMapping(prefix, namespace,true); } catch (IOException e) { throw new IllegalStateException("Unable to add Mapping because of an " + e.getClass().getSimpleName()+ "while setting the parsed mapping",e); } } else { StringBuilder sb = new StringBuilder("The parsed Mapping is not Valid: "); if(!validPrefix){ sb.append(String.format("The parsed prefix '%s' MUST only contain " + "alpha numeric chars including '_' and '-'",prefix)); } if(!validNamespace){ if(!validPrefix) { sb.append("| "); } sb.append(String.format("The parsed namespace '%s' MUST end with" + "'/' or '#' in case of an URI or ':' in case of an URN",namespace)); } throw new IllegalArgumentException(sb.toString()); } }
@Override public String getFullName(String shortNameOrUri) { String prefix = NamespaceMappingUtils.getPrefix(shortNameOrUri); if(prefix != null){ String namespace = getNamespace(prefix); if(namespace != null){ return namespace+shortNameOrUri.substring(prefix.length()+1); } else { //no mapping return null return null; } } else { //not a shortName ... return the parsed return shortNameOrUri; } }
synchronized (this) { if(providersTracker == null && bundleContext != null){ openTracker();
writePrefixMappings(new FileOutputStream(mappingsFile, false));
public static NamespacePrefixService getInstance(){ if(INSTANCE == null){ try { INSTANCE = new StanbolNamespacePrefixService(new File(PREFIX_MAPPINGS)); } catch (IOException e) { throw new IllegalStateException("Unable to read "+PREFIX_MAPPINGS,e); } } return INSTANCE; }
@Override public String getNamespace(String prefix) { String namespace; mappingsLock.readLock().lock(); try { namespace = prefixMap.get(prefix); } finally { mappingsLock.readLock().unlock(); } if(namespace == null){ ServiceReference[] refs = getSortedProviderReferences(); for(int i=0;namespace == null && i<refs.length;i++){ NamespacePrefixProvider provider = getService(refs[i]); if(provider != null){ namespace = provider.getNamespace(prefix); } } } return namespace; }
/** * Constructs an Stanbol NamespacePrefixService and initialises other * {@link NamespacePrefixProvider} implementations using the * Java {@link ServiceLoader} utility. * @param mappingFile the mapping file used to manage local mappings. If * <code>null</code> no president local mappings are supported. * @throws IOException */ public StanbolNamespacePrefixService(File mappingFile) throws IOException { this.mappingsFile = mappingFile; if(mappingsFile != null && mappingsFile.isFile()){ readPrefixMappings(new FileInputStream(mappingsFile)); } //else no mappings yet ... nothing todo loader = ServiceLoader.load(NamespacePrefixProvider.class); } /**
/** * Expected to be called only during activation * @param in * @throws IOException */ private void readPrefixMappings(InputStream in) throws IOException { LineIterator it = IOUtils.lineIterator(in, "UTF-8"); while(it.hasNext()){ String mapping = it.nextLine(); if(mapping.charAt(0) != '#'){ int sep = mapping.indexOf('\t'); if(sep < 0 || mapping.length() <= sep+1){ log.warn("Illegal prefix mapping '{}'",mapping); } else { String old = addMapping(mapping.substring(0, sep),mapping.substring(sep+1),false); if(old != null){ log.info("Duplicate mention of prefix {}. Override mapping from {} to {}", new Object[]{mapping.substring(0, sep), old, mapping.substring(sep+1)}); } } } else { //comment log.debug(mapping); } } } private void writePrefixMappings(OutputStream os) throws IOException {
@Override public String getPrefix(String namespace) { List<String> prefixes = getPrefixes(namespace); return prefixes.isEmpty() ? null : prefixes.get(0); }