Skip to content

Commit

Permalink
apacheGH-2060: Fix for IRI isAbsolute (RFC 3986 vs RFC 2396)
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Nov 2, 2023
1 parent efb85bd commit c47e4c2
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 206 deletions.
355 changes: 178 additions & 177 deletions jena-core/src/main/java/org/apache/jena/ext/xerces/util/URI.java

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions jena-core/src/main/java/org/apache/jena/irix/IRIx.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ protected IRIx(String string) {
* <p>
* Note that a URI can be both "not absolute" and "not relative", e.g. {@code http://example/path#fragment}.
* <p>
* Beware that <a href="https://datatracker.ietf.org/doc/html/rfc2396#section-3.1">RFC 2396 section 3.1</a> has a
* different definition, where the scheme is required but a fragment may be present.
* <p>
* See {@linkplain #isReference()} for testing whether a URI is suitable for use in RDF.
*/
public abstract boolean isAbsolute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ void baseUsed(XMLHandler forErrors, Taint taintMe, String relUri,
void checkBaseUse(XMLHandler forErrors, Taint taintMe, String relUri, IRI rslt) throws SAXParseException {

String resolvedURI = rslt.toString();
if (relUri.equals(resolvedURI) && rslt.isAbsolute())
// "absolute" is not the opposite of "relative" in RFC 3986 (it is in RFC 2396).
if (relUri.equals(resolvedURI) && !rslt.isRelative())
return;

forErrors.warning(taintMe, errno, errmsg + ": <" + relUri + ">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private void checkNamespaceURI(String uri) throws SAXParseException {
if (uri.length() != 0)
{
IRI u = iriFactory().create(uri);
if (!u.isAbsolute()) {
if (u.isRelative()) {
warning(null,
WARN_RELATIVE_NAMESPACE_URI_DEPRECATED,
"The namespace URI: <"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void baseUsed(XMLHandler forErrors, Taint taintMe, String relUri,
void checkBaseUse(XMLHandler forErrors, Taint taintMe, String relUri, IRIx rslt) throws SAXParseException {

String resolvedURI = rslt.toString();
if (relUri.equals(resolvedURI) && rslt.isAbsolute())
// "absolute" is not the opposite of "relative" in RFC 3986 (it is in RFC 2396).
if (relUri.equals(resolvedURI) && !rslt.isRelative())
return;

forErrors.warning(taintMe, errno, errmsg + ": <" + relUri + ">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private void checkNamespaceURI(String uri) throws SAXParseException {
} catch (IRIException ex) {
u = IRIx.createAny(uri);
}
if (!u.isAbsolute()) {
if (u.isRelative()) {
warning(null,
WARN_RELATIVE_NAMESPACE_URI_DEPRECATED,
"The namespace URI: <"
Expand Down
22 changes: 21 additions & 1 deletion jena-core/src/test/java/org/apache/jena/irix/TestIRIx.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public TestIRIx(String name, IRIProvider provider) {
super(name, provider);
}

// ---- RFC 3986 Grammar
// ---- RFC 3986 Grammar : misc parsing.

@Test public void uri_01() { test("http://example/abc"); }

Expand All @@ -49,6 +49,7 @@ public TestIRIx(String name, IRIProvider provider) {
@Test public void uri_06() { test("http://1.2.3.4/abc"); }

// ---- Compliance with HTTP RFC7230. https://tools.ietf.org/html/rfc7230#section-2.7

@Test(expected=IRIException.class)
public void http_01() { parse("http:"); }

Expand Down Expand Up @@ -135,6 +136,18 @@ public TestIRIx(String name, IRIProvider provider) {

@Test public void reference_08() { reference("file:///a:/~jena/file", true); }

@Test public void reference_09() { reference("http://example/abcd#frag", true); }

// -- isAbsolute, isRelative : These are not opposites in RFC 3986. (String, isAbsolute, isRelative)

@Test public void abs_rel_01() { test_abs_rel("http://example/abc", true, false); }

@Test public void abs_rel_02() { test_abs_rel("abc", false, true); }

@Test public void abs_rel_03() { test_abs_rel("http://example/abc#def", false, false); }

@Test public void abs_rel_04() { test_abs_rel("abc#def", false, true); }

// -- Resolving
@Test public void resolve_http_01() { resolve("http://example/", "path", "http://example/path"); }

Expand Down Expand Up @@ -202,6 +215,13 @@ private void resolve(String baseUriStr, String otherStr, String expected) {
assertEquals("Base=<"+baseUriStr+"> Rel=<"+otherStr+">", expected, iriStr);
}

// Create - is it suitable for an RDF reference?
private void test_abs_rel(String uriStr, boolean isAbsolute, boolean isRelative) {
IRIx iri = IRIx.create(uriStr);
assertEquals("Absolute test: IRI = "+uriStr, isAbsolute, iri.isAbsolute());
assertEquals("Relative test: IRI = "+uriStr, isRelative, iri.isRelative());
}

// Create - is it suitable for an RDF reference?
private void reference(String uriStr) {
IRIx iri = IRIx.create(uriStr);
Expand Down
28 changes: 4 additions & 24 deletions jena-iri/src/main/java/org/apache/jena/iri/impl/AbsIRIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ private void createExceptions(long m) {

@Override
public boolean isAbsolute() {
return has(SCHEME);
// Definition from RFC3986 section 4.3
return has(SCHEME) && ! has(FRAGMENT);
// Definition from RFC2396 section 3.1
//return has(SCHEME);
}

abstract boolean has(int component);
Expand All @@ -218,29 +221,6 @@ public boolean isRelative() {
return !has(SCHEME);
}

/*
* public boolean isRDFURIReference() { return !hasException(RDF); }
*
* public boolean isIRI() { return !hasException(IRI); }
*
* public boolean isURIinASCII() { return !hasException(URI); }
*/
// public boolean isVeryBad() {
// return false;
// }
// public boolean isXSanyURI() {
// return !hasException(XMLSchema);
// }
/*
public boolean hasException(int conformance) {
return hasExceptionMask(getFactory().recsToMask(conformance));
}
public Iterator exceptions(int conformance) {
return exceptionsMask(getFactory().recsToMask(conformance));
}
*/

@Override
public boolean hasViolation(boolean includeWarnings) {
return hasExceptionMask(getSchemeSpec().getMask(includeWarnings));
Expand Down

0 comments on commit c47e4c2

Please sign in to comment.