diff --git a/src/main/java/fr/inria/corese/core/next/impl/common/util/IRIUtils.java b/src/main/java/fr/inria/corese/core/next/impl/common/util/IRIUtils.java index bb1f83146..7c4a819b3 100644 --- a/src/main/java/fr/inria/corese/core/next/impl/common/util/IRIUtils.java +++ b/src/main/java/fr/inria/corese/core/next/impl/common/util/IRIUtils.java @@ -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("^(?(?[\\w\\-]+):(?\\/\\/)?(?([\\w\\-_:@]+\\.)*[\\w\\-_:]*))((?\\/([\\w\\-\\._\\:]+\\/)*)(?[\\w\\-\\._\\:]+)?(?\\?[\\w\\-_\\:\\?\\=]+)?(\\#)?(?([\\w\\-_]+))?)?$"); private static final Pattern STANDARD_IRI_PATTERN = Pattern.compile("^(([^:/?#\\s]+):)(\\/\\/([^/?#\\s]*))?([^?#\\s]*)(\\?([^#\\s]*))?(#(.*))?"); + /** * Prevent instantiation of the utility class. */ @@ -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 ""; @@ -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 ""; } diff --git a/src/test/java/fr/inria/corese/core/next/impl/temp/CoreseIRITest.java b/src/test/java/fr/inria/corese/core/next/impl/temp/CoreseIRITest.java index 9519cd4f0..77bfc928c 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/temp/CoreseIRITest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/temp/CoreseIRITest.java @@ -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");