Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
*/
public class IRIUtils {

private static final Pattern IRI_PATTERN = Pattern.compile("^(([\\w\\-]+:\\/\\/([\\w\\-_:]+\\.)*[\\w\\-_:]*)(\\/([\\w\\-\\._\\:]+\\/)*))([\\w\\-\\._\\:]+)?(\\?[\\w\\-_\\:\\?\\=]+)?((\\#)?([\\w\\-_]+))?$");
private static final Pattern IRI_PATTERN = Pattern.compile("^(?<namespace>(?<protocol>[\\w\\-]+):(?<dblSlashes>\\/\\/)?(?<domain>([\\w\\-_:@]+\\.)*[\\w\\-_:]*))((?<path>\\/([\\w\\-\\._\\:]+\\/)*)(?<finalPath>[\\w\\-\\._\\:]+)?(?<query>\\?[\\w\\-_\\:\\?\\=]+)?(\\#)?(?<fragment>([\\w\\-_]+))?)?$");
private static final Pattern STANDARD_IRI_PATTERN = Pattern.compile("^(([^:/?#\\s]+):)(\\/\\/([^/?#\\s]*))?([^?#\\s]*)(\\?([^#\\s]*))?(#(.*))?");


/**
* Prevent instantiation of the utility class.
*/
Expand All @@ -29,15 +30,24 @@ public static String guessNamespace(String iri) {
Matcher matcher = IRI_PATTERN.matcher(iri);

if(matcher.matches()) {
if((matcher.group(8) == null) || (matcher.group(6) == null && matcher.group(9) == null) ) { // If the IRI has no fragment or ends with a slash

return matcher.group(1);
} else {
// 1: Domain and path ending with a slash, 6: final path element without slash, 9: final # if there is a fragment
return matcher.group(1) + matcher.group(6) + matcher.group(9);
if(matcher.group("protocol") != null && matcher.group("protocol").equals("_")) {
return "";
}
StringBuilder namespace = new StringBuilder();
namespace.append(matcher.group("protocol")).append(":");
if(matcher.group("dblSlashes") != null) {
namespace.append(matcher.group("dblSlashes"));
}
namespace.append(matcher.group("domain"));
if(matcher.group("path") != null) {
namespace.append(matcher.group("path"));
}
if(matcher.group("fragment") != null && matcher.group("finalPath") != null) {
namespace.append(matcher.group("finalPath")).append("#");
}
return namespace.toString();
} else {
return "";
throw new IllegalStateException("No namespace found for the given IRI: " + iri + ".");
}
} catch (IllegalStateException e) {
return "";
Expand All @@ -54,10 +64,10 @@ public static String guessLocalName(String iri) {
Matcher matcher = IRI_PATTERN.matcher(iri);

if(matcher.matches()) {
if(matcher.group(10) != null){ // If the IRI has a fragment
return matcher.group(10);
} else if(matcher.group(6) != null ) { // If the IRI has no fragment but do not ends with a slash
return matcher.group(6);
if(matcher.group("fragment") != null){ // If the IRI has a fragment
return matcher.group("fragment");
} else if(matcher.group("finalPath") != null ) { // If the IRI has no fragment but do not ends with a slash
return matcher.group("finalPath");
} else { // If the URI ends with a slash
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public void constructorStringTest() {
assertEquals("test", coreseIRI.getLocalName());
}

@Test
public void constructorStringTest_otherURIS() {
CoreseIRI coreseIRI_noSlash = new CoreseIRI("http://www.monicamurphy.org");
assertEquals("http://www.monicamurphy.org", coreseIRI_noSlash.stringValue());
assertEquals("http://www.monicamurphy.org", coreseIRI_noSlash.getCoreseNode().getLabel());
assertEquals("http://www.monicamurphy.org", coreseIRI_noSlash.getNamespace());
assertEquals("", coreseIRI_noSlash.getLocalName());

CoreseIRI coreseIRI_email = new CoreseIRI("mailto:monica@monicamurphy.org");
assertEquals("mailto:monica@monicamurphy.org", coreseIRI_email.stringValue());
assertEquals("mailto:monica@monicamurphy.org", coreseIRI_email.getCoreseNode().getLabel());
assertEquals("mailto:monica@monicamurphy.org", coreseIRI_email.getNamespace());
assertEquals("", coreseIRI_email.getLocalName());
}

@Test
public void constructorIriTest() {
CoreseIRI coreseIRI = new CoreseIRI("http://example.org/test");
Expand Down