Skip to content

Commit

Permalink
apacheGH-2936: Update IRI commands for jena-iri3986
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Jan 13, 2025
1 parent 5f904cc commit 7834af3
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 26 deletions.
138 changes: 126 additions & 12 deletions jena-cmds/src/main/java/arq/iri.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,30 @@

package arq;

import java.io.PrintStream;
import java.util.Iterator ;

import org.apache.jena.atlas.lib.Lib;
import org.apache.jena.iri.IRI ;
import org.apache.jena.iri.IRIFactory ;
import org.apache.jena.iri.Violation ;
import org.apache.jena.irix.SetupJenaIRI;
import org.apache.jena.iri3986.provider.IRIProvider3986;
import org.apache.jena.iri3986.provider.IRIProvider3986.IRIx3986;
import org.apache.jena.irix.*;
import org.apache.jena.irix.IRIProviderJenaIRI.IRIxJena;
import org.apache.jena.rfc3986.IRI3986;
import org.apache.jena.rfc3986.IRIParseException;

/**
* Parse and print IRIs
*/
public class iri
{
private static PrintStream out = System.out;
// Errors and warnings.
private static PrintStream err = System.err;

public static void main(String... args)
{
IRIFactory iriFactory = SetupJenaIRI.iriCheckerFactory() ;

boolean first = true ;
for ( String iriStr : args )
{
Expand All @@ -42,16 +52,120 @@ public static void main(String... args)
System.out.println() ;
first = false ;

IRI iri = iriFactory.create(iriStr) ;
System.out.println(iriStr + " ==> "+iri) ;
if ( iri.isRelative() )
System.out.println("Relative: "+iri.isRelative()) ;
String setting = Lib.getenv(SystemIRIx.sysPropertyProvider, SystemIRIx.envVariableProvider);

IRIProvider provider = null;
if ( setting != null ) {
provider = switch(setting) {
case "IRI3986" -> new IRIProvider3986();
case "IRI0" -> new IRIProviderJenaIRI();
default -> {
System.err.println("Unknown IRI Provider: "+setting);
System.exit(1);
yield null;
}
};
}

if ( provider == null )
provider = SystemIRIx.getProvider();

IRIx irix;
try {
irix = provider.create(iriStr);
} catch (IRIException ex) {
System.err.println(ex.getMessage());
continue;
}

// jena-iri3986
if ( irix instanceof IRIx3986 iri3986 ) {
print(iri3986, iriStr);
continue;
}
// jena-iri: The original Jena IRI subsystem
if ( irix instanceof IRIxJena iriJena ) {
print(iriJena, iriStr);
continue;
}
print(irix, iriStr);
}
}

private static void print(IRIx3986 irix, String iriStr) {
try {
IRI3986 iri = irix.getImpl();
IRI3986 iri1 = iri.normalize();

Iterator<Violation> vIter = iri.violations(true) ;
for ( ; vIter.hasNext() ; )
{
System.out.println(vIter.next().getShortMessage()) ;
out.printf("Input: <%s>\n", iriStr);
out.printf(" Parsed: %s\n", iri.rebuild()) ;
out.printf(" Absolute: %s\n", iri.isAbsolute());
out.printf(" Relative: %s\n", iri.isRelative());
out.printf(" Hierarchical: %s\n", iri.isHierarchical());
out.printf(" Rootless: %s\n", iri.isRootless());
if ( ! iri.equals(iri1) )
out.printf(" Normalized: %s\n", iri1) ;
out.printf("\n");
out.printf("%s|%s| ", "Scheme", iri.scheme());
out.printf("%s|%s| ", "Authority", iri.authority());
out.printf("%s|%s| ", "Host", iri.host());
if ( iri.hasPort() )
out.printf("%s|%s| ", "Port", iri.port());
out.printf("%s|%s| ", "Path", iri.path());
out.printf("%s|%s| ", "Query", iri.query());
out.printf("%s|%s|", "Fragment", iri.fragment());
out.println();
if ( iri.hasViolations() ) {
out.println();
out.println("Scheme specific warnings:");
iri.forEachViolation(v->{
out.print(" ");
err.printf("%s\n", v.message());
});
}
} catch (IRIParseException ex) {
System.err.printf("Error: %s\n", ex.getMessage());
}
}

private static void print(IRIxJena jenaIRI, String iriStr) {
IRI iri = jenaIRI.getImpl();
System.out.println(iriStr + " ==> " + iri);
if ( jenaIRI.isRelative() )
System.out.println("Relative: " + iri.isRelative());

Iterator<Violation> vIter = iri.violations(true);
for ( ; vIter.hasNext() ; ) {
System.out.println(vIter.next().getShortMessage());
}
}

private static void print(IRIx irix, String iriStr) {
try {
IRIx iri1 = irix.normalize();

out.printf("Input: <%s>\n", iriStr);
out.printf(" Absolute: %s\n", irix.isAbsolute());
out.printf(" Relative: %s\n", irix.isRelative());
if ( ! irix.equals(iri1) )
out.printf(" Normalized: %s\n", iri1) ;
out.printf("\n");
out.printf("%s|%s| ", "Scheme", irix.scheme());
out.println();
if ( irix.hasViolations() ) {
out.println();
out.println("Scheme specific warnings:");
irix.handleViolations((error, msg)->{
String type = (error? "Error:" : "Warn:");
out.print(" ");
err.printf("%-6s\n", type, msg);
});
}
} catch (IRIParseException ex) {
System.err.printf("Error: %s\n", ex.getMessage());
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public String scheme() {

@Override
public IRIx resolve(String other) {
// create3986() - checks syntax, and errors if in strict mode.
IRI3986 iriOther = create3986(other);
// Does not complain if iriOther is bad but create3986 did checking.
IRI3986 iri2 = this.iri.resolve(iriOther);
return newIRIx(iri2);
}
Expand All @@ -87,7 +87,6 @@ public IRIx resolve(String other) {
public IRIx resolve(IRIx other) {
IRIx3986 iriOther = (IRIx3986)other;
IRI3986 iri2 = this.iri.resolve(iriOther.iri);
//violations(iri2);
return newIRIx(iri2);
}

Expand Down
12 changes: 6 additions & 6 deletions jena-iri3986/src/main/java/org/apache/jena/rfc3986/IRI3986.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ public String fragment() {
}

/**
* <a href="https://tools.ietf.org/html/rfc3986#section-4.3">RFC 3986, Section
* 4.3</a>
* <a href="https://tools.ietf.org/html/rfc3986#section-4.3">RFC 3986, Section 4.3</a>
*/
@Override
public boolean isAbsolute() {
Expand Down Expand Up @@ -1622,8 +1621,7 @@ private void checkHTTPx(URIScheme scheme) {

if ( !hasHost() )
schemeReport(this, Issue.http_no_host, scheme, "http and https URI schemes require //host/");

if ( /* hasHost() && */ (host0 == host1) )
else if ( /* hasHost() && */ (host0 == host1) )
schemeReport(this, Issue.http_empty_host, scheme, "http and https URI schemes do not allow the host to be empty");

// https://tools.ietf.org/html/rfc3986#section-3.2.3
Expand Down Expand Up @@ -1951,8 +1949,10 @@ private static void addReport(IRI3986 iri, String iriStr, URIScheme uriScheme, I

private static void addReportParseError(IRI3986 iri, String iriStr, String message) {
// The iri object is probably only partial populated.
// Use iriStr for the message.
String msg = "'"+iriStr+"' : "+message;
// Exception message already has the IRI string. But check.
String msg = message;
if ( ! message.startsWith("<"+iriStr+">") )
msg = "'"+iriStr+"' : "+message;
Violation v = new Violation(iriStr, null, Issue.ParseError, msg);
addReport(iri, v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private static int findValidNamespace(String string, int startNamespace, BiConsu

// RFC 8141 section 5.1 (described in RFC 3406)
if ( LibParseIRI.caseInsensitiveRegion(string, startNamespace, "X-") ) {
String start = string.substring(0,2);
String start = string.substring(startNamespace,2+startNamespace);
handler.accept(Issue.urn_x_namespace, "Namespace id starts with '"+start+"'");
return -1;
}
Expand All @@ -270,7 +270,7 @@ private static int findValidNamespace(String string, int startNamespace, BiConsu
char chx = charAt(string, i);
if ( !seenNonZero ) {
if ( chx == '0' ) {
handler.accept(Issue.urn_bad_nid, "Leading zero in an informal namepsace");
handler.accept(Issue.urn_bad_nid, "Leading zero in an informal namespace");
return -1;
} else
seenNonZero = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void main(String... args) {
String relStr = fixup(args[1]);

IRI3986 base = createOrExit(baseStr, "Bad base");
IRI3986 target = createOrExit(relStr, "Bad");
IRI3986 target = createOrExit(relStr, "Bad IRI");

if ( ! base.isAbsolute() ) {
System.err.println("Base must be an absolute IRI: '" +base+"'");
Expand All @@ -45,7 +45,9 @@ public static void main(String... args) {
IRI3986 result = base.relativize(target);
System.out.println("Base: "+base);
System.out.println("IRI: "+target);
System.out.println();
System.out.println("Relative: "+result);
System.out.println();

print(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void main(String... args) {
String relStr = fixup(args[1]);

IRI3986 base = createOrExit(baseStr, "Bad base");
IRI3986 rel = createOrExit(baseStr, "Bad ");
IRI3986 rel = createOrExit(relStr, "Bad relatuve IRI");

if ( ! base.isAbsolute() ) {
System.err.println("Base should be an absolute IRI: '" +base+"'");
Expand All @@ -44,9 +44,9 @@ public static void main(String... args) {
IRI3986 result = base.resolve(rel);
System.out.println("Base: "+base);
System.out.println("IRI: "+rel);
System.out.println();
System.out.println("Resolved: "+result);

System.out.println();
print(result);
}
}

0 comments on commit 7834af3

Please sign in to comment.