final String fullRequestedUri = getFullRequestedUri(juri);
final String path = juri.getPath();
if ( path == null ) {
throw new URISyntaxException(str, "not path");
}
if ( !path.startsWith("/") ) {
throw new URISyntaxException(str, "not absolute path");
}
int idx = path.indexOf('/', 1);
if ( idx < 0 ) {
throw new URISyntaxException(str, "No root");
}
String root = path.substring(0, idx); // include leading slash
String reqUri = path;
String contextPath = root;
String requestedUri = reqUri;
// parsing described with an example:
// afterRoot = /mmi/someVocab.owl/someTerm
String afterRoot = requestedUri.substring(contextPath.length());
if ( afterRoot.startsWith("/") ) {
afterRoot = afterRoot.substring(1);
}
int rootIdx = fullRequestedUri.indexOf(afterRoot);
untilRoot = fullRequestedUri.substring(0, rootIdx);
assert untilRoot.endsWith("/");
String[] parts = afterRoot.split("/");
// Either: 1 part = { mmi } if allowUntilAuthority
// or: 2 parts = { mmi, someVocab.owl }
// or: 3 parts = { mmi, someVocab.owl, someTerm }
// or = { mmi, someVersion, someVocab.owl}
// or: 4 parts = { mmi, someVersion, someVocab.owl, someTerm }
if ( parts.length < 2 || parts.length > 4 ) {
// if only one part, then accepted only if allowUntilAuthority
if ( parts.length == 1 && allowUntilAuthority ) {
// OK
}
else {
throw new URISyntaxException(fullRequestedUri, "2, 3, or 4 parts expected: "
+Arrays.asList(parts));
}
}
String _version = null; // will remain null if not given.
String _topic = "";
String _term = ""; // will remain "" if not given
if ( parts.length == 1 ) {
// OK
assert allowUntilAuthority;
}
else if ( parts.length == 2 ) {
_topic = parts[1];
}
else if ( parts.length == 4 ) {
_version = parts[1];
_topic = parts[2];
_term = parts[3];
}
else {
assert parts.length == 3 ;
// Determine which of the two cases (a) or (b) we are dealing with:
// (a) { mmi, someVocab.owl, someTerm }
// (b) { mmi, someVersion, someVocab.owl}
// if parts[1] starts with a digit or is LATEST_VERSION_INDICATOR, take that part as the version:
if ( parts[1].length() > 0
&& ( Character.isDigit(parts[1].charAt(0))
|| parts[1].equals(LATEST_VERSION_INDICATOR) )
) {
// case (b):
_version = parts[1];
_topic = parts[2];
}
else {
// case (a)
_topic = parts[1];
_term = parts[2];
}
}
String _authority = parts[0];
// remove any extension from _authority, _topic and _term, but remember them to assign this.extension below
Set<String> extensions = new HashSet<String>();
String _authorityExt = "";
int dotIdx = _authority.lastIndexOf('.');
if ( dotIdx >= 0) {
_authorityExt = _authority.substring(dotIdx).toLowerCase();
_authority = _authority.substring(0, dotIdx);
extensions.add(_authorityExt);
}
String _topicExt = "";
dotIdx = _topic.lastIndexOf('.');
if ( dotIdx >= 0) {
_topicExt = _topic.substring(dotIdx).toLowerCase();
_topic = _topic.substring(0, dotIdx);
extensions.add(_topicExt);
}
String _termExt = "";
dotIdx = _term.lastIndexOf('.');
if ( dotIdx >= 0) {
_termExt = _term.substring(dotIdx).toLowerCase();
_term = _term.substring(0, dotIdx);
extensions.add(_termExt);
}
if ( extensions.size() > 1 ) {
// there are different extensions:
throw new URISyntaxException(fullRequestedUri, "Explicit extensions given but different: " +extensions);
}
////////////////////////////////////////////////////////////////////////////////
// now, assign to my final fields and do remaining checks:
authority = _authority;
version = _version;
topic = _topic;
term = _term;
extension = extensions.size() == 1 ? extensions.iterator().next() : "";
if ( authority.length() == 0 ) {
throw new URISyntaxException(fullRequestedUri, "Missing authority in URI");
}
if ( authority.startsWith("-") ) {
throw new URISyntaxException(fullRequestedUri, "Authority cannot start with hyphen");
}
if ( Character.isDigit(authority.charAt(0)) ) {
throw new URISyntaxException(fullRequestedUri, "Authority cannot start with digit");
}
if ( ! allowUntilAuthority && topic.length() == 0 ) {
throw new URISyntaxException(fullRequestedUri, "Missing topic in URI");
}
if ( topic.length() > 0 && Character.isDigit(topic.charAt(0)) ) {
throw new URISyntaxException(fullRequestedUri, "Topic cannot start with digit");
}
// check version, if given:
if ( version != null ) {
checkVersion(version);