2ronwalf/*
2ronwalf * Created on May 13, 2004
2ronwalf */
2ronwalfpackage org.mindswap.utils;
2ronwalf
2ronwalfimport java.net.URI;
2ronwalfimport java.util.Hashtable;
2ronwalfimport java.util.Map;
2ronwalfimport java.util.Set;
2ronwalf
2ronwalfimport org.mindswap.owl.vocabulary.OWL;
2ronwalfimport org.mindswap.owl.vocabulary.RDF;
2ronwalfimport org.mindswap.owl.vocabulary.RDFS;
2ronwalfimport org.mindswap.owl.vocabulary.XSD;
2ronwalf
2ronwalf/**
2ronwalf * A class to convert URI's to QNames. Borrowed from SWOOP code.
2ronwalf *
2ronwalf * @author Evren Sirin
2ronwalf */
2ronwalfpublic class QNameProvider {
2ronwalf
2ronwalf // stores a map of uri -> prefix
2ronwalf Map uriToPrefix;
2ronwalf
2ronwalf Map prefixToUri;
2ronwalf
2ronwalf /**
2ronwalf *
2ronwalf */
2ronwalf public QNameProvider() {
2ronwalf uriToPrefix = new Hashtable();
2ronwalf prefixToUri = new Hashtable();
2ronwalf
2ronwalf // initialize it with standard stuff
2ronwalf setMapping("owl", OWL.ns);
2ronwalf setMapping("rdf", RDF.ns);
2ronwalf setMapping("rdfs", RDFS.ns);
2ronwalf setMapping("xsd", XSD.ns);
2ronwalf }
2ronwalf
2ronwalf public static boolean isNameStartChar(char ch) {
2ronwalf return (Character.isLetter(ch) || ch == '_');
2ronwalf }
2ronwalf
2ronwalf public static boolean isNameChar(char ch) {
2ronwalf return (isNameStartChar(ch) || Character.isDigit(ch) || ch == '.' || ch == '-');
2ronwalf }
2ronwalf
2ronwalf public static int findNameStartIndex(String str) {
2ronwalf char[] strChars = str.toCharArray();
2ronwalf int nameStartIndex = -1;
2ronwalf boolean foundNameChar = false;
2ronwalf
2ronwalf for(int strIndex = strChars.length - 1; strIndex >= 0; strIndex--) {
2ronwalf char letter = strChars[strIndex];
2ronwalf
2ronwalf if(isNameStartChar(letter)) {
2ronwalf nameStartIndex = strIndex;
2ronwalf foundNameChar = true;
2ronwalf }
2ronwalf else if(foundNameChar && !isNameChar(letter)) {
2ronwalf break;
2ronwalf }
2ronwalf }
2ronwalf return nameStartIndex;
2ronwalf }
2ronwalf
2ronwalf public static int findLastNameIndex(String str) {
2ronwalf char[] strChars = str.toCharArray();
2ronwalf int nameIndex = -1;
2ronwalf
2ronwalf for(int strIndex = strChars.length - 1; strIndex >= 0; strIndex--) {
2ronwalf char letter = strChars[strIndex];
2ronwalf if(isNameChar(letter)) {
2ronwalf nameIndex = strIndex;
2ronwalf }
2ronwalf else {
2ronwalf break;
2ronwalf }
2ronwalf }
2ronwalf return nameIndex;
2ronwalf }
2ronwalf
2ronwalf public static int findNextNonNameIndex(String str, int startIndex) {
2ronwalf char[] strChars = str.toCharArray();
2ronwalf int nameIndex = startIndex;
2ronwalf for(nameIndex = startIndex; nameIndex < strChars.length; nameIndex++) {
2ronwalf char letter = strChars[nameIndex];
2ronwalf if(!isNameChar(letter)) {
2ronwalf break;
2ronwalf }
2ronwalf }
2ronwalf return nameIndex;
2ronwalf }
2ronwalf
2ronwalf protected static String[] splitURI(String uriString) {
2ronwalf int nameStart, prefixStart, prefixEnd;
2ronwalf String base, prefix, name;
2ronwalf String[] bpn = new String[3];
2ronwalf
2ronwalf nameStart = findLastNameIndex(uriString);
2ronwalf if(nameStart < 0) {
2ronwalf //System.out.println("Couldn't find name for "+uriString);
2ronwalf return null;
2ronwalf }
2ronwalf name = uriString.substring(nameStart);
2ronwalf if(nameStart == 0) {
2ronwalf //System.out.println("Name starts at beginning");
2ronwalf base = "";
2ronwalf prefix = "a"; // Pick a unique prefix later
2ronwalf }
2ronwalf else {
2ronwalf base = uriString.substring(0, nameStart);
2ronwalf //System.out.println("Uri: "+ uri + " Base: " +base);
2ronwalf prefixStart = findNameStartIndex(base);
2ronwalf if(prefixStart < 0) {
2ronwalf //System.out.println("Prefix < 0");
2ronwalf prefix = "b"; // Pick a uniqe prefix later
2ronwalf }
2ronwalf else {
2ronwalf prefixEnd = findNextNonNameIndex(base, prefixStart + 1);
2ronwalf prefix = uriString.substring(prefixStart, prefixEnd);
2ronwalf }
2ronwalf }
2ronwalf bpn[0] = base;
2ronwalf bpn[1] = prefix;
2ronwalf bpn[2] = name;
2ronwalf
2ronwalf return bpn;
2ronwalf }
2ronwalf
2ronwalf public String getPrefix(String uri) {
2ronwalf return (String) uriToPrefix.get(uri);
2ronwalf }
2ronwalf
2ronwalf public String getURI(String prefix) {
2ronwalf return (String) prefixToUri.get(prefix);
2ronwalf }
2ronwalf
2ronwalf public void removePrefix(String prefix) {
2ronwalf String uri = (String) prefixToUri.remove(prefix);
2ronwalf if(uri != null)
2ronwalf uriToPrefix.remove(uri);
2ronwalf }
2ronwalf
2ronwalf public boolean setMapping(String prefix, String uri) {
2ronwalf String currentUri = getURI(prefix);
2ronwalf if(currentUri == null) {
2ronwalf //System.out.println("Setting prefix "+prefix+": "+uri);
2ronwalf prefixToUri.put(prefix, uri);
2ronwalf uriToPrefix.put(uri, prefix);
2ronwalf return true;
2ronwalf }
2ronwalf else if(currentUri == uri) {
2ronwalf return true;
2ronwalf }
2ronwalf else {
2ronwalf return false;
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf public Set getPrefixSet() {
2ronwalf return prefixToUri.keySet();
2ronwalf }
2ronwalf
2ronwalf public Set getURISet() {
2ronwalf return uriToPrefix.keySet();
2ronwalf }
2ronwalf
2ronwalf public String shortForm(URI uri) {
2ronwalf return shortForm(uri.toString());
2ronwalf }
2ronwalf
2ronwalf public String shortForm(String uri) {
2ronwalf //System.out.println("Shortform for " + uri);
2ronwalf return shortForm(uri, true);
2ronwalf }
2ronwalf
2ronwalf public String shortForm(String uri, boolean default_to_uri) {
2ronwalf String[] bpn = splitURI(uri);
2ronwalf String base, possible_prefix, prefix, name;
2ronwalf String qname;
2ronwalf
2ronwalf if(bpn == null) {
2ronwalf if(default_to_uri) {
2ronwalf return uri;
2ronwalf }
2ronwalf else {
2ronwalf return null;
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf base = bpn[0];
2ronwalf possible_prefix = bpn[1];
2ronwalf name = bpn[2];
2ronwalf
2ronwalf if(possible_prefix.endsWith(".owl") || possible_prefix.endsWith(".rdf")
2ronwalf || possible_prefix.endsWith(".xml"))
2ronwalf possible_prefix = possible_prefix.substring(0, possible_prefix
2ronwalf .length() - 4);
2ronwalf
2ronwalf if(Character.isLowerCase(possible_prefix.charAt(1)))
2ronwalf possible_prefix = Character.toLowerCase(possible_prefix.charAt(0))
2ronwalf + possible_prefix.substring(1, possible_prefix.length());
2ronwalf
2ronwalf prefix = getPrefix(base);
2ronwalf if(prefix == null) {
2ronwalf // Check prefix for uniqueness
2ronwalf prefix = possible_prefix;
2ronwalf int mod = 0;
2ronwalf while(!setMapping(prefix, base)) {
2ronwalf prefix = possible_prefix + mod;
2ronwalf mod++;
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf qname = prefix.length() == 0 ? name : prefix + ":" + name;
2ronwalf return qname;
2ronwalf }
2ronwalf
2ronwalf public String longForm(String qname) {
2ronwalf String[] str = qname.split(":");
2ronwalf String prefix = str[0];
2ronwalf String localName = str[1];
2ronwalf String uri = getURI(prefix);
2ronwalf
2ronwalf if(uri == null)
2ronwalf throw new IllegalArgumentException("Prefix " + prefix + " is not known!");
2ronwalf
2ronwalf return uri + localName;
2ronwalf }
2ronwalf}