Skip to content

Commit 1f4c411

Browse files
author
James Leigh
committed
Fix #74: Validate xsd:anyURI to be a valid IRI
Signed-off-by: James Leigh <james.leigh@ontotext.com>
1 parent 73b0065 commit 1f4c411

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

core/model/src/main/java/org/eclipse/rdf4j/model/datatypes/XMLDatatypeUtil.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.math.BigDecimal;
1111
import java.math.BigInteger;
12+
import java.net.URISyntaxException;
1213
import java.util.StringTokenizer;
1314

1415
import javax.xml.datatype.DatatypeConfigurationException;
@@ -18,6 +19,7 @@
1819
import javax.xml.datatype.XMLGregorianCalendar;
1920
import javax.xml.namespace.QName;
2021

22+
import org.eclipse.rdf4j.common.net.ParsedIRI;
2123
import org.eclipse.rdf4j.common.text.ASCIIUtil;
2224
import org.eclipse.rdf4j.model.IRI;
2325
import org.eclipse.rdf4j.model.vocabulary.XMLSchema;
@@ -269,6 +271,9 @@ else if (datatype.equals(XMLSchema.YEARMONTHDURATION)) {
269271
else if (datatype.equals(XMLSchema.QNAME)) {
270272
result = isValidQName(value);
271273
}
274+
else if (datatype.equals(XMLSchema.ANYURI)) {
275+
result = isValidAnyURI(value);
276+
}
272277

273278
return result;
274279
}
@@ -652,6 +657,25 @@ public static boolean isValidQName(String value) {
652657
return true;
653658
}
654659

660+
/**
661+
* Determines if the supplied value is an Internationalized Resource Identifier Reference (IRI). An anyURI
662+
* value can be absolute or relative, and may have an optional fragment identifier (i.e., it may be an IRI
663+
* Reference). This type should be used when the value fulfills the role of an IRI, as defined in
664+
* [RFC&nbsp;3987] or its successor(s) in the IETF Standards Track.
665+
*
666+
* @param value
667+
* @return <tt>true</tt> if a valid IRI, <tt>false</tt> otherwise
668+
*/
669+
public static boolean isValidAnyURI(String value) {
670+
try {
671+
new ParsedIRI(value.trim());
672+
return true;
673+
}
674+
catch (URISyntaxException e) {
675+
return false;
676+
}
677+
}
678+
655679
private static boolean isPrefixStartChar(int c) {
656680
return ASCIIUtil.isLetter(c) || c >= 0x00C0 && c <= 0x00D6 || c >= 0x00D8 && c <= 0x00F6
657681
|| c >= 0x00F8 && c <= 0x02FF || c >= 0x0370 && c <= 0x037D || c >= 0x037F && c <= 0x1FFF
@@ -758,6 +782,9 @@ else if (datatype.equals(XMLSchema.BOOLEAN)) {
758782
else if (datatype.equals(XMLSchema.DATETIME)) {
759783
result = normalizeDateTime(value);
760784
}
785+
else if (datatype.equals(XMLSchema.ANYURI)) {
786+
result = collapseWhiteSpace(value);
787+
}
761788

762789
return result;
763790
}

core/model/src/test/java/org/eclipse/rdf4j/model/datatypes/XMLDatatypeUtilTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,25 @@ public class XMLDatatypeUtilTest {
198198
"⻐:⻘⻨" // random chinese chars, if this sequence happens to mean something, this is unintended
199199
};
200200

201-
/** ivalid xsd:QName values */
201+
/** invalid xsd:QName values */
202202
private static final String[] INVALID_QNAMES = { "1:bar", "foo:1bar", "foo:bar:baz", "foo", "_:bar" };
203203

204+
/** http://www.datypic.com/sc/xsd/t-xsd_anyURI.html */
205+
private static final String[] VALID_URI = {
206+
"http://datypic.com",
207+
"mailto:info@datypic.com",
208+
"../%C3%A9dition.html",
209+
"../édition.html",
210+
"http://datypic.com/prod.html#shirt",
211+
" http://datypic.com/prod.html#shirt ",
212+
"../prod.html#shirt",
213+
"urn:example:org",
214+
"" };
215+
216+
private static final String[] INVALID_URI = {
217+
"http://datypic.com#frag1#frag2",
218+
"http://datypic.com#f% rag" };
219+
204220
@Test
205221
public void testNormalize() {
206222
assertEquals("-1.0E-1", XMLDatatypeUtil.normalize("-0.1", XMLSchema.DOUBLE));
@@ -251,6 +267,8 @@ public void testIsValidValue() {
251267
testValidation(VALID_QNAMES, XMLSchema.QNAME, true);
252268
testValidation(INVALID_QNAMES, XMLSchema.QNAME, false);
253269

270+
testValidation(VALID_URI, XMLSchema.ANYURI, true);
271+
testValidation(INVALID_URI, XMLSchema.ANYURI, false);
254272
}
255273

256274
private void testValidation(String[] values, IRI datatype, boolean validValues) {

0 commit comments

Comments
 (0)