001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.model.util;
007:
008: import org.openrdf.model.URI;
009:
010: /**
011: * @author Arjohn Kampman
012: */
013: public class URIUtil {
014:
015: /**
016: * Finds the index of the first local name character in an (non-relative)
017: * URI. This index is determined by the following the following steps:
018: * <ul>
019: * <li>Find the <em>first</em> occurrence of the '#' character,
020: * <li>If this fails, find the <em>last</em> occurrence of the '/'
021: * character,
022: * <li>If this fails, find the <em>last</em> occurrence of the ':'
023: * character.
024: * <li>Add <tt>1<tt> to the found index and return this value.
025: * </ul>
026: * Note that the third step should never fail as every legal (non-relative)
027: * URI contains at least one ':' character to seperate the scheme from the
028: * rest of the URI. If this fails anyway, the method will throw an
029: * {@link IllegalArgumentException}.
030: *
031: * @param uri
032: * A URI string.
033: * @return The index of the first local name character in the URI string. Note that
034: * this index does not reference an actual character if the algorithm determines
035: * that there is not local name. In that case, the return index is equal to the
036: * length of the URI string.
037: * @throws IllegalArgumentException
038: * If the supplied URI string doesn't contain any of the separator
039: * characters. Every legal (non-relative) URI contains at least one
040: * ':' character to seperate the scheme from the rest of the URI.
041: */
042: public static int getLocalNameIndex(String uri) {
043: int separatorIdx = uri.indexOf('#');
044:
045: if (separatorIdx < 0) {
046: separatorIdx = uri.lastIndexOf('/');
047: }
048:
049: if (separatorIdx < 0) {
050: separatorIdx = uri.lastIndexOf(':');
051: }
052:
053: if (separatorIdx < 0) {
054: throw new IllegalArgumentException(
055: "No separator character founds in URI: " + uri);
056: }
057:
058: return separatorIdx + 1;
059: }
060:
061: /**
062: * Checks whether the URI consisting of the specified namespace and local
063: * name has been split correctly according to the URI splitting rules
064: * specified in {@link URI}.
065: *
066: * @param namespace
067: * The URI's namespace, must not be <tt>null</tt>.
068: * @param localName
069: * The URI's local name, must not be <tt>null</tt>.
070: * @return <tt>true</tt> if the specified URI has been correctly split into
071: * a namespace and local name, <tt>false</tt> otherwise.
072: * @see URI
073: * @see #getLocalNameIndex(String)
074: */
075: public static boolean isCorrectURISplit(String namespace,
076: String localName) {
077: assert namespace != null : "namespace must not be null";
078: assert localName != null : "localName must not be null";
079:
080: if (namespace.length() == 0) {
081: return false;
082: }
083:
084: int nsLength = namespace.length();
085: char lastNsChar = namespace.charAt(nsLength - 1);
086:
087: if (lastNsChar == '#'
088: && namespace.lastIndexOf('#', nsLength - 2) == -1) {
089: // namespace ends with a '#' and does not contain any futher '#'
090: // characters
091: return true;
092: } else if (localName.indexOf('#') == -1
093: && localName.indexOf('/') == -1) {
094: if (lastNsChar == '/') {
095: // URI does not contain any '#' characters and the namespace ends
096: // with the last '/' character
097: return true;
098: } else if (lastNsChar == ':'
099: && localName.indexOf(':') == -1) {
100: // URI does not contain any '#' or '/' characters and the namespace
101: // ends with the last ':' character
102: return true;
103: }
104: }
105:
106: return false;
107: }
108: }
|