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 483721dda..5bb966843 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 @@ -15,7 +15,7 @@ public class IRIUtils { private static final Logger logger = LoggerFactory.getLogger(IRIUtils.class); - 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]*))?(#(.*))?"); /** @@ -34,9 +34,11 @@ public static String guessNamespace(String iri) { Matcher matcher = IRI_PATTERN.matcher(iri); if(matcher.matches()) { - if(matcher.group(8) == null ) { // If the IRI has no fragment) + 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); } } else { @@ -57,10 +59,12 @@ public static String guessLocalName(String iri) { Matcher matcher = IRI_PATTERN.matcher(iri); if(matcher.matches()) { - if(matcher.group(8) == null ) { // If the IRI has no fragment) - return matcher.group(6); - } else { + 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); + } else { // If the URI ends with a slash + return ""; } } else { return ""; diff --git a/src/test/java/fr/inria/corese/core/next/impl/temp/CoreseAdaptedValueFactoryTest.java b/src/test/java/fr/inria/corese/core/next/impl/temp/CoreseAdaptedValueFactoryTest.java index 00f0a4298..4b3b5ced4 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/temp/CoreseAdaptedValueFactoryTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/temp/CoreseAdaptedValueFactoryTest.java @@ -125,4 +125,11 @@ public void testCreateStatementWithContext() { assertEquals(literal, statement.getObject()); assertEquals(context, statement.getContext()); } + + @Test + public void testCreateFOAFURI() { + IRI foaf = valueFactory.createIRI("http://xmlns.com/foaf/0.1/"); + assertNotNull(foaf); + assertEquals("http://xmlns.com/foaf/0.1/", foaf.stringValue()); + } }