001: /*
002: * LICENSE INFORMATION
003: * Copyright 2005-2007 by FZI (http://www.fzi.de).
004: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
005: * <OWNER> = Max Völkel
006: * <ORGANIZATION> = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe, Germany
007: * <YEAR> = 2007
008: *
009: * Project information at http://semweb4j.org/rdf2go
010: */
011: package org.ontoware.rdf2go.model.node.impl;
012:
013: import java.net.URISyntaxException;
014:
015: import org.ontoware.rdf2go.exception.ModelRuntimeException;
016: import org.ontoware.rdf2go.model.node.BlankNode;
017: import org.ontoware.rdf2go.model.node.Node;
018: import org.ontoware.rdf2go.model.node.URI;
019: import org.slf4j.Logger;
020: import org.slf4j.LoggerFactory;
021:
022: public class URIImpl extends ResourceImpl implements URI {
023:
024: private static final Logger log = LoggerFactory
025: .getLogger(URIImpl.class);
026:
027: private String uriString;
028:
029: /**
030: * Checks uri for validity and creates
031: *
032: * @param uriString
033: * @throws IllegalArgumentException
034: * if the uri is not valid and createURIWithChecking is true
035: */
036: public URIImpl(String uriString) {
037: this (uriString, true);
038: }
039:
040: /**
041: * @param uriString
042: * @param createURIWithChecking
043: * if true, checks for valid uri
044: * @throws IllegalArgumentException
045: * if the uri is not valid and createURIWithChecking is true
046: */
047: public URIImpl(String uriString, boolean createURIWithChecking) {
048: if (createURIWithChecking) {
049: try {
050: new java.net.URI(uriString);
051: } catch (URISyntaxException e) {
052: throw new IllegalArgumentException(e);
053: }
054: }
055: this .uriString = uriString;
056: }
057:
058: /**
059: * This method is deprecated. Just use new URIImpl(uriString) instead.
060: * @deprecated use the constructors instead
061: * @param uriString
062: * @return a URI
063: */
064: public static URI create(String uriString) {
065: return new URIImpl(uriString);
066: }
067:
068: /**
069: * This method is deprecated. Just use new URIImpl(uriString,false) instead.
070: * @deprecated use the constructors instead
071: * @param uriString
072: * @return a URI
073: */
074: public static URI createURIWithoutChecking(String uriString) {
075: return new URIImpl(uriString, false);
076: }
077:
078: public String toString() {
079: return this .uriString;
080: }
081:
082: public URI asURI() throws ClassCastException {
083: return this ;
084: }
085:
086: public BlankNode asBlankNode() throws ClassCastException {
087: throw new ClassCastException("Cannot cast a URI to a BlankNode");
088: }
089:
090: public boolean equals(Object other) {
091:
092: if (other == null)
093: return false;
094:
095: log.debug("comparing for equal: " + this + " and " + other
096: + " of type " + other.getClass());
097:
098: if (other instanceof URI) {
099: boolean equal = ((URI) other).toString().equals(
100: this .toString());
101:
102: log.debug("they are equal? " + equal);
103:
104: return equal;
105: } else
106: log.debug("URIImpl cannot compare with type "
107: + other.getClass() + " so this is false");
108:
109: return false;
110: }
111:
112: public java.net.URI toJavaURI() throws URISyntaxException {
113: return new java.net.URI(this .uriString);
114: }
115:
116: public int hashCode() {
117: return this .uriString.hashCode();
118: }
119:
120: public int compareTo(Node other) {
121: if (other instanceof URI) {
122: return this .uriString.compareTo(((URI) other).toString());
123: } else {
124: // sort by type
125: return NodeUtils.compareByType(this , other);
126: }
127: }
128:
129: public String toSPARQL() {
130: return "<" + uriString + ">";
131: }
132:
133: public java.net.URI asJavaURI() {
134: try {
135: return new java.net.URI(toString());
136: } catch (URISyntaxException e) {
137: throw new ModelRuntimeException(e);
138: }
139: }
140:
141: }
|