001 /*
002 * URI.java
003 *
004 * Niraj Aswani, 09/March/07
005 *
006 * $Id: URI.html,v 1.0 2007/03/09 16:13:01 niraj Exp $
007 */
008 package gate.creole.ontology;
009
010
011 /**
012 * Each resource has a unique URI in the Ontology. Each URI has a
013 * namespace and a name which all together makes a qualified URI. e.g.
014 * namespace: http://gate.ac.uk, aResourceName: Person , URI =
015 * http://gate.ac.uk#Person. It is possible to have a URI for an
016 * anonymouse resource, in which case the namespace is set to an empty
017 * string and a flag (isAnonymouseResource) is set to true.
018 *
019 * @author Niraj Aswani
020 *
021 * @deprecated Use OURI objects and the {@link Ontology} factory methods
022 * for creating them instead.
023 */
024 @Deprecated
025 public class URI implements OURI, ONodeID, OBNodeID {
026
027 /**
028 * Namespace for this URI (in current version - a value before the
029 * last occurance of '#' or '/')
030 */
031 protected String namespace;
032
033 /**
034 * A Resource name (in current version - a value after the last
035 * occurance of '#' or '/')
036 */
037 protected String aResourceName;
038
039 /**
040 * String representation of the URI
041 */
042 protected String uri;
043
044 /**
045 * Denotes whether the OResource this URI belongs to is an anonymous
046 * or not.
047 */
048 protected boolean isAnonymousResource;
049
050 /**
051 * Constructor
052 *
053 * @param uri
054 * @param isAnonymousResource
055 * @throws InvalidURIException
056 */
057 public URI(String uri, boolean isAnonymousResource)
058 throws InvalidURIException {
059 this.isAnonymousResource = isAnonymousResource;
060 if(!this.isAnonymousResource) {
061 int index = uri.lastIndexOf('#');
062 if(index < 0) {
063 index = uri.lastIndexOf('/');
064 if(index < 0) throw new InvalidURIException("Invalid URI :" + uri);
065 if(index + 2 > uri.length())
066 throw new InvalidURIException("Invalid URI :" + uri);
067 this.uri = uri;
068 this.namespace = "";
069 this.aResourceName = uri.substring(index + 1, uri.length());
070 }
071 else {
072 this.uri = uri;
073 this.namespace = uri.substring(0, index + 1);
074 this.aResourceName = uri.substring(index + 1, uri.length());
075 }
076 }
077 else {
078 this.uri = uri;
079 this.namespace = "";
080 this.aResourceName = "[" + uri + "]";
081 }
082 }
083
084 /**
085 * Retrieves the name space part from the URI. In this implementation
086 * it retrieves the string that appears before the last occurance of
087 * '#' or '/'.
088 *
089 * @return
090 */
091 public String getNameSpace() {
092 return this.namespace;
093 }
094
095 /**
096 * Retrieves the resource name from the given URI. In this
097 * implementation it retrieves the string that appears after the last
098 * occurance of '#' or '/'.
099 *
100 * @return
101 */
102 public String getResourceName() {
103 return this.aResourceName;
104 }
105
106 /**
107 * Returns the string representation of the uri. In case of anonymous
108 * class, it returns the '[' + resourcename + ']'.
109 */
110 public String toString() {
111 return this.uri;
112 }
113
114 /**
115 * Indicates whether the URI refers to an anonymous resource
116 *
117 * @return
118 */
119 public boolean isAnonymousResource() {
120 return this.isAnonymousResource;
121 }
122
123 public int compareTo(ONodeID other) {
124 return this.toString().compareTo(other.toString());
125 }
126
127 public boolean equals(Object other) {
128 if(other instanceof URI) {
129 return uri.equals(((URI)other).uri);
130 } else {
131 return false;
132 }
133 }
134
135 public int hashCode() {
136 return uri.hashCode();
137 }
138
139 public String toTurtle() {
140 if(isAnonymousResource()) {
141 if(uri.startsWith("_:")) {
142 return uri;
143 } else {
144 return "_:"+uri;
145 }
146 } else {
147 return "<"+uri+">";
148 }
149 }
150
151 public void validate() {
152 throw new UnsupportedOperationException("Method not implemented");
153 }
154
155 public String toDisplayString() {
156 throw new UnsupportedOperationException("Method not implemented");
157 }
158
159 public String toASCIIString() {
160 throw new UnsupportedOperationException("Method not implemented");
161 }
162
163 }
|