You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2024/03/29 09:25:13 UTC
(jena) 05/07: Improve SplitIRI code
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
commit 3ea08e2e7cf77ba6892d4d549ce5b1a042e65c11
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Thu Mar 28 18:06:47 2024 +0000
Improve SplitIRI code
---
.../java/org/apache/jena/rdf/model/impl/Util.java | 93 +----------
.../apache/jena/shared/impl/PrefixMappingImpl.java | 100 ++++++------
.../main/java/org/apache/jena/util/SplitIRI.java | 175 +++++++++++++++++++--
.../org/apache/jena/util/TestPackage_util.java | 31 ++--
...SplitIRI_TTL.java => TestSplitIRI_Display.java} | 87 ++--------
.../org/apache/jena/util/TestSplitIRI_TTL.java | 139 +++++-----------
6 files changed, 290 insertions(+), 335 deletions(-)
diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/Util.java b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/Util.java
index 0f06f9c455..f039c622e5 100644
--- a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/Util.java
+++ b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/Util.java
@@ -29,104 +29,23 @@ import org.apache.jena.graph.TextDirection;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.shared.CannotEncodeCharacterException;
import org.apache.jena.util.SplitIRI;
-import org.apache.jena.util.XMLChar;
/** Some utility functions.
*/
public class Util extends Object {
+ protected static Pattern standardEntities =
+ Pattern.compile( "&|<|>|\t|\n|\r|\'|\"" );
+
/**
* Given an absolute URI, determine the split point between the namespace
- * part and the localname part. If there is no valid localname part then the
- * length of the string is returned. The algorithm tries to find the longest
- * NCName at the end of the uri, not immediately preceeded by the first
- * colon in the string.
- * <p>
- * This operation follows XML QName rules which are more complicated than
- * needed for Turtle and TriG. For example, QName can't start with a digit.
- *
- * @param uri
- * @return the index of the first character of the localname
- * @see SplitIRI
+ * part and the localname part. See {@link SplitIRI#splitNamespaceXML} for details.
*/
public static int splitNamespaceXML(String uri) {
-
- // XML Namespaces 1.0:
- // A qname name is NCName ':' NCName
- // NCName ::= NCNameStartChar NCNameChar*
- // NCNameChar ::= NameChar - ':'
- // NCNameStartChar ::= Letter | '_'
- //
- // XML 1.0
- // NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] |
- // [#xD8-#xF6] | [#xF8-#x2FF] |
- // [#x370-#x37D] | [#x37F-#x1FFF] |
- // [#x200C-#x200D] | [#x2070-#x218F] |
- // [#x2C00-#x2FEF] | [#x3001-#xD7FF] |
- // [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
- // NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 |
- // [#x0300-#x036F] | [#x203F-#x2040]
- // Name ::= NameStartChar (NameChar)*
-
- char ch;
- int lg = uri.length();
- if (lg == 0)
- return 0;
- int i = lg-1;
- for (; i >= 1; i--) {
- ch = uri.charAt(i);
- if (notNameChar(ch)) break;
- }
-
- int j = i + 1;
-
- if ( j >= lg )
- return lg;
-
- // Check we haven't split up a %-encoding.
- if ( j >= 2 && uri.charAt(j-2) == '%' )
- j = j+1;
- if ( j >= 1 && uri.charAt(j-1) == '%' ) {
- j = j+2;
- if ( j > lg )
- // JENA-1941: Protect against overshoot in the case of "%x"
- // at end of a (bad) URI.
- return lg;
- }
-
- // Have found the leftmost NCNameChar from the
- // end of the URI string.
- // Now scan forward for an NCNameStartChar
- // The split must start with NCNameStart.
- for (; j < lg; j++) {
- ch = uri.charAt(j);
-// if (XMLChar.isNCNameStart(ch))
-// break;
- if (XMLChar.isNCNameStart(ch))
- {
- // "mailto:" is special.
- // split "mailto:me" as "mailto:m" and "e" !
- // Keep part after mailto: with at least one character.
- if ( j == 7 && uri.startsWith("mailto:"))
- // Don't split at "mailto:"
- continue;
- else
- break;
- }
- }
- return j;
+ // Legacy. Call the moved code.
+ return SplitIRI.splitNamespaceXML(uri);
}
- /**
- answer true iff this is not a legal NCName character, ie, is
- a possible split-point start.
- */
- public static boolean notNameChar( char ch )
- { return !XMLChar.isNCName( ch ); }
-
- protected static Pattern standardEntities =
- Pattern.compile( "&|<|>|\t|\n|\r|\'|\"" );
-
public static String substituteStandardEntities( String s )
{
if (standardEntities.matcher( s ).find())
diff --git a/jena-core/src/main/java/org/apache/jena/shared/impl/PrefixMappingImpl.java b/jena-core/src/main/java/org/apache/jena/shared/impl/PrefixMappingImpl.java
index 9c64fe6050..6582c7232e 100644
--- a/jena-core/src/main/java/org/apache/jena/shared/impl/PrefixMappingImpl.java
+++ b/jena-core/src/main/java/org/apache/jena/shared/impl/PrefixMappingImpl.java
@@ -37,22 +37,22 @@ import org.apache.jena.util.XMLChar;
public class PrefixMappingImpl implements PrefixMapping
// See also PrefixMappingBase, PrefixMappingMem
// in org.apache.jena.sparql.graph
- // for a different implementation
+ // for a different implementation
{
private Map<String, String> prefixToURI;
private Map<String, String> URItoPrefix;
private boolean locked;
-
+
public PrefixMappingImpl()
- {
+ {
// ConcurrentHashMaps protects against breaking each datastructure
// but does not protect against inconsistency.
prefixToURI = new ConcurrentHashMap<>();
- URItoPrefix = new ConcurrentHashMap<>();
+ URItoPrefix = new ConcurrentHashMap<>();
}
-
+
protected void set(String prefix, String uri) {
prefixToURI.put(prefix, uri) ;
URItoPrefix.put(uri, prefix) ;
@@ -68,13 +68,13 @@ public class PrefixMappingImpl implements PrefixMapping
@Override
public PrefixMapping lock()
- {
- locked = true;
+ {
+ locked = true;
return this;
}
-
+
@Override
- public PrefixMapping setNsPrefix( String prefix, String uri )
+ public PrefixMapping setNsPrefix( String prefix, String uri )
{
checkUnlocked();
checkLegal( prefix );
@@ -83,7 +83,7 @@ public class PrefixMappingImpl implements PrefixMapping
set( prefix, uri );
return this;
}
-
+
@Override
public PrefixMapping removeNsPrefix( String prefix )
{
@@ -92,7 +92,7 @@ public class PrefixMappingImpl implements PrefixMapping
regenerateReverseMapping() ;
return this;
}
-
+
@Override
public PrefixMapping clearNsPrefixMap() {
checkUnlocked();
@@ -103,42 +103,42 @@ public class PrefixMappingImpl implements PrefixMapping
regenerateReverseMapping() ;
return this ;
}
-
+
protected void regenerateReverseMapping()
{
URItoPrefix.clear();
for (Map.Entry<String, String> e: prefixToURI.entrySet())
URItoPrefix.put( e.getValue(), e.getKey() );
}
-
+
protected void checkUnlocked()
{ if (locked) throw new JenaLockedException( this ); }
-
+
private void checkProper( String uri )
- {
+ {
// suppressed by popular demand. TODO consider optionality
// if (!isNiceURI( uri )) throw new NamespaceEndsWithNameCharException( uri );
}
-
+
public static boolean isNiceURI( String uri )
{
if (uri.equals( "" )) return false;
char last = uri.charAt( uri.length() - 1 );
- return Util.notNameChar( last );
+ return !XMLChar.isNCName(last);
}
-
+
/**
- Add the bindings of other to our own. We defer to the general case
+ Add the bindings of other to our own. We defer to the general case
because we have to ensure the URIs are checked.
-
+
@param other the PrefixMapping whose bindings we are to add to this.
*/
@Override
public PrefixMapping setNsPrefixes( PrefixMapping other )
{ return setNsPrefixes( other.getNsPrefixMap() ); }
-
+
/**
- Answer this PrefixMapping after updating it with the <code>(p, u)</code>
+ Answer this PrefixMapping after updating it with the <code>(p, u)</code>
mappings in <code>other</code> where neither <code>p</code> nor
<code>u</code> appear in this mapping.
*/
@@ -154,13 +154,13 @@ public class PrefixMappingImpl implements PrefixMapping
}
return this;
}
-
+
/**
Add the bindings in the prefixToURI to our own. This will fail with a ClassCastException
if any key or value is not a String; we make no guarantees about order or
completeness if this happens. It will fail with an IllegalPrefixException if
any prefix is illegal; similar provisos apply.
-
+
@param other the Map whose bindings we are to add to this.
*/
@Override
@@ -171,28 +171,28 @@ public class PrefixMappingImpl implements PrefixMapping
setNsPrefix( e.getKey(), e.getValue() );
return this;
}
-
+
/**
Checks that a prefix is "legal" - it must be a valid XML NCName.
*/
private void checkLegal( String prefix )
{
if (prefix.length() > 0 && !XMLChar.isValidNCName( prefix ))
- throw new PrefixMapping.IllegalPrefixException( prefix );
+ throw new PrefixMapping.IllegalPrefixException( prefix );
}
-
+
@Override
- public String getNsPrefixURI( String prefix )
+ public String getNsPrefixURI( String prefix )
{ return get( prefix ); }
-
+
@Override
public Map<String, String> getNsPrefixMap()
{ return CollectionFactory.createHashedMap( prefixToURI ); }
-
+
@Override
public String getNsURIPrefix( String uri )
{ return URItoPrefix.get( uri ); }
-
+
/**
Expand a prefixed URI. There's an assumption that any URI of the form
Head:Tail is subject to mapping if Head is in the prefix mapping. So, if
@@ -202,22 +202,22 @@ public class PrefixMappingImpl implements PrefixMapping
public String expandPrefix( String prefixed )
{
int colon = prefixed.indexOf( ':' );
- if (colon < 0)
+ if (colon < 0)
return prefixed;
else
{
String uri = get( prefixed.substring( 0, colon ) );
return uri == null ? prefixed : uri + prefixed.substring( colon + 1 );
- }
+ }
}
-
+
/**
Answer a readable (we hope) representation of this prefix mapping.
*/
@Override
public String toString()
{ return "pm:" + prefixToURI; }
-
+
/**
Answer the qname for <code>uri</code> which uses a prefix from this
mapping, or null if there isn't one.
@@ -225,19 +225,19 @@ public class PrefixMappingImpl implements PrefixMapping
Relies on <code>splitNamespace</code> to carve uri into namespace and
localname components; this ensures that the localname is legal and we just
have to (reverse-)lookup the namespace in the prefix table.
-
+
@see org.apache.jena.shared.PrefixMapping#qnameFor(java.lang.String)
*/
@Override
public String qnameFor( String uri )
- {
+ {
int split = Util.splitNamespaceXML( uri );
String ns = uri.substring( 0, split ), local = uri.substring( split );
if (local.equals( "" )) return null;
String prefix = URItoPrefix.get( ns );
return prefix == null ? null : prefix + ":" + local;
}
-
+
/**
Compress the URI using the prefix mapping. This version of the code looks
through all the maplets and checks each candidate prefix URI for being a
@@ -251,16 +251,16 @@ public class PrefixMappingImpl implements PrefixMapping
Entry<String, String> e = findMapping( uri, true );
return e == null ? uri : e.getKey() + ":" + uri.substring( (e.getValue()).length() );
}
-
+
@Override
public boolean samePrefixMappingAs( PrefixMapping other )
{
- return other instanceof PrefixMappingImpl
+ return other instanceof PrefixMappingImpl
? equals( (PrefixMappingImpl) other )
: equalsByMap( other )
;
}
-
+
@Override
public boolean hasNoMappings()
{ return prefixToURI.isEmpty(); }
@@ -268,22 +268,22 @@ public class PrefixMappingImpl implements PrefixMapping
@Override
public int numPrefixes()
{ return prefixToURI.size(); }
-
+
protected boolean equals( PrefixMappingImpl other )
{ return other.sameAs( this ); }
-
+
protected boolean sameAs( PrefixMappingImpl other )
{ return prefixToURI.equals( other.prefixToURI ); }
-
+
protected final boolean equalsByMap( PrefixMapping other )
{ return getNsPrefixMap().equals( other.getNsPrefixMap() ); }
-
+
/**
Answer a prefixToURI entry in which the value is an initial substring of <code>uri</code>.
If <code>partial</code> is false, then the value must equal <code>uri</code>.
-
+
Does a linear search of the entire prefixToURI, so not terribly efficient for large maps.
-
+
@param uri the value to search for
@param partial true if the match can be any leading substring, false for exact match
@return some entry (k, v) such that uri starts with v [equal for partial=false]
@@ -294,8 +294,8 @@ public class PrefixMappingImpl implements PrefixMapping
{
String ss = e.getValue();
if (uri.startsWith( ss ) && (partial || ss.length() == uri.length())) return e;
- }
- return null;
- }
+ }
+ return null;
+ }
}
diff --git a/jena-core/src/main/java/org/apache/jena/util/SplitIRI.java b/jena-core/src/main/java/org/apache/jena/util/SplitIRI.java
index 17fc730c2e..77cf2725cb 100644
--- a/jena-core/src/main/java/org/apache/jena/util/SplitIRI.java
+++ b/jena-core/src/main/java/org/apache/jena/util/SplitIRI.java
@@ -18,28 +18,43 @@
package org.apache.jena.util;
+import java.util.Objects;
+
+import org.apache.jena.atlas.lib.StrUtils;
import org.apache.jena.graph.Node;
-import org.apache.jena.rdf.model.impl.Util;
-//import org.apache.jena.riot.system.RiotChars;
+//Has copies of import org.apache.jena.riot.system.RiotChars;
/**
* Code to split an URI or IRI into prefix and local part.
+ * This code does <em>not</em> consider any prefix mapping.
+ * The split is based on finding the last {@code /} or {@code #}
+ * character.
+ * <p>
* Historically, 'prefix' is referred to as 'namespace'
* reflecting RDF/XML history.
* <p>
* For display, use {@link #localname} and {@link #namespace}.
- * This follows Turtle, adds some pragmatic rules but does not escape
- * any characters. A URI is split never split before the last {@code /}
- * or last {@code #}, if present.
+ * These pragmatic follow Turtle (e.g. localname allows a plain trailing dot)
+ * but does not escape any characters.
+ *
+ * These operations are for display (c.f. {@link Object#toString} and do not guarantee a round-trip;
+ * namespace+localname may not be the exact input IRI.
+ * A URI is split never split before the last {@code /} or {@code #}, if present.
* See {@link #splitpoint} for more details.
* <p>
- * This code forms the machinery behind {@link Node#getLocalName}
- * {@link Node#getNameSpace} for URI Nodes following the XML rules.
- * <p>
- * {@link #namespaceTTL} and {@link #localnameTTL} is strict Turtle; the local name is escaped if necessary.
+ * The functions
+ * {@link #namespaceTTL} and {@link #localnameTTL}
+ * provide a strict Turtle split, if possible;
+ * the local name is escaped if necessary.
+ * {@link #namespaceTTL} can be used to build a set of prefix mappings.
+ * {@link #localnameTTLNoEsc} is the same as {@link #localnameTTL}
+ * without the escaping applied.
* <p>
* The functions {@link #namespaceXML} and {@link #localnameXML}
* apply the rules for XML qnames.
+ * <p>
+ * This code forms the machinery behind {@link Node#getLocalName}
+ * {@link Node#getNameSpace} for URI Nodes following the XML rules.
*/
public class SplitIRI
{
@@ -48,6 +63,7 @@ public class SplitIRI
* Return the input string if there is no splitpoint.
*/
public static String namespace(String string) {
+ Objects.requireNonNull(string, "string argument is null");
int i = splitpoint(string);
if ( i < 0 )
return string;
@@ -57,9 +73,10 @@ public class SplitIRI
/** Calculate a localname - do not escape PN_LOCAL_ESC.
* This is not guaranteed to be legal Turtle.
* Use with {@link #namespace}
- * Return an empty string if there is no splitpoint.
+ * Return an empty string if there is no split point.
*/
public static String localname(String string) {
+ Objects.requireNonNull(string, "string argument is null");
int i = splitpoint(string);
if ( i < 0 )
return "";
@@ -68,24 +85,49 @@ public class SplitIRI
/**
* Return the 'namespace' (prefix) for a URI string,
- * legal for Turtle and goes with {@link #localnameTTL}
+ * legal for Turtle and goes with {@link #localnameTTL}.
+ * This operation does not guaratee that the argument has a legal localname.
*/
public static String namespaceTTL(String string) {
- return namespace(string);
+ int i = splitpoint(string);
+ if ( i < 0 )
+ return string;
+ String ns = string.substring(0, i);
+ return ns;
}
/**
* Calculate a localname - enforce legal Turtle
- * escape PN_LOCAL_ESC, check for final '.'
+ * escapes for localnames (rule PN_LOCAL_ESC),
+ * A final '.' is escaped.
+ * Return "" for "no split".
* Use with {@link #namespaceTTL}
*/
public static String localnameTTL(String string) {
String x = localname(string);
if ( x.isEmpty())
return x;
+ // This will escape the final DOT but leave internal dots alone.
return escape_PN_LOCAL_ESC(x);
}
+ /**
+ * Calculate a localname - enforce legal Turtle
+ * without applying the escape PN_LOCAL_ESC.
+ * Return "" for 'no split'
+ * Check for final '.' - if present, return "".
+ */
+ public static String localnameTTLNoEsc(String string) {
+ String x = localname(string);
+ if ( x.isEmpty())
+ return x;
+ char lastChar = StrUtils.lastChar(x);
+ if ( lastChar == '.' )
+ // No legal localname.
+ return "";
+ return x;
+ }
+
private static String escape_PN_LOCAL_ESC(String x) {
// Assume that escapes are rare so scan once to make sure there
// is work to do then scan again doing the work.
@@ -113,8 +155,26 @@ public class SplitIRI
return sb.toString();
}
+ /**
+ * Scan for chars needing escape.
+ * Return the index of the first.
+ * Return -1 for no escape needed.
+ *
+ */
+ private static int needsEscape(String x) {
+ int N = x.length();
+ for ( int i = 0; i < N; i++ ) {
+ char ch = x.charAt(i);
+ if ( needsEscape(ch, (i==N-1)) ) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
private static boolean needsEscape(char ch, boolean finalChar) {
if ( ch == '.' )
+ // Only needed at the end.
return finalChar;
return isPN_LOCAL_ESC(ch);
}
@@ -146,6 +206,8 @@ public class SplitIRI
* and {@code path/abc} .
* <p>
* Split URN's after ':'.
+ * <p>
+ * This function does not enforce the Turtle rule that the final character can not be '.'.
*
* @param uri URI string
* @return The split point, or -1 for "not found".
@@ -245,9 +307,8 @@ public class SplitIRI
*/
/** Split point, according to XML qname rules.
* This is the longest NCName at the end of the uri.
- * See {@link Util#splitNamespaceXML}.
*/
- public static int splitXML(String string) { return Util.splitNamespaceXML(string); }
+ public static int splitXML(String string) { return splitNamespaceXML(string); }
/** Namespace, according to XML qname rules.
* Use with {@link #localnameXML}.
@@ -332,5 +393,89 @@ public class SplitIRI
private static boolean range(int ch, char a, char b) {
return (ch >= a && ch <= b);
}
+
+ // -------- --------
+ /**
+ * Given an absolute URI, determine the split point between the namespace
+ * part and the localname part. If there is no valid localname part then the
+ * length of the string is returned. The algorithm tries to find the longest
+ * NCName at the end of the uri, not immediately preceeded by the first
+ * colon in the string.
+ * <p>
+ * This operation follows XML QName rules which are more complicated than
+ * needed for Turtle and TriG. For example, QName can't start with a digit.
+ *
+ * @param uri
+ * @return the index of the first character of the localname
+ * @see SplitIRI
+ */
+ public static int splitNamespaceXML(String uri) {
+
+ // XML Namespaces 1.0:
+ // A qname name is NCName ':' NCName
+ // NCName ::= NCNameStartChar NCNameChar*
+ // NCNameChar ::= NameChar - ':'
+ // NCNameStartChar ::= Letter | '_'
+ //
+ // XML 1.0
+ // NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] |
+ // [#xD8-#xF6] | [#xF8-#x2FF] |
+ // [#x370-#x37D] | [#x37F-#x1FFF] |
+ // [#x200C-#x200D] | [#x2070-#x218F] |
+ // [#x2C00-#x2FEF] | [#x3001-#xD7FF] |
+ // [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
+ // NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 |
+ // [#x0300-#x036F] | [#x203F-#x2040]
+ // Name ::= NameStartChar (NameChar)*
+
+ char ch;
+ int lg = uri.length();
+ if (lg == 0)
+ return 0;
+ int i = lg-1;
+ for (; i >= 1; i--) {
+ ch = uri.charAt(i);
+ if ( !XMLChar.isNCName(ch) )
+ break;
+ }
+
+ int j = i + 1;
+
+ if ( j >= lg )
+ return lg;
+
+ // Check we haven't split up a %-encoding.
+ if ( j >= 2 && uri.charAt(j-2) == '%' )
+ j = j+1;
+ if ( j >= 1 && uri.charAt(j-1) == '%' ) {
+ j = j+2;
+ if ( j > lg )
+ // JENA-1941: Protect against overshoot in the case of "%x"
+ // at end of a (bad) URI.
+ return lg;
+ }
+
+ // Have found the leftmost NCNameChar from the
+ // end of the URI string.
+ // Now scan forward for an NCNameStartChar
+ // The split must start with NCNameStart.
+ for (; j < lg; j++) {
+ ch = uri.charAt(j);
+// if (XMLChar.isNCNameStart(ch))
+// break;
+ if (XMLChar.isNCNameStart(ch))
+ {
+ // "mailto:" is special.
+ // split "mailto:me" as "mailto:m" and "e" !
+ // Keep part after mailto: with at least one character.
+ if ( j == 7 && uri.startsWith("mailto:"))
+ // Don't split at "mailto:"
+ continue;
+ else
+ break;
+ }
+ }
+ return j;
+ }
}
diff --git a/jena-core/src/test/java/org/apache/jena/util/TestPackage_util.java b/jena-core/src/test/java/org/apache/jena/util/TestPackage_util.java
index ac7fc370d3..b25bef47c6 100644
--- a/jena-core/src/test/java/org/apache/jena/util/TestPackage_util.java
+++ b/jena-core/src/test/java/org/apache/jena/util/TestPackage_util.java
@@ -31,23 +31,26 @@ public class TestPackage_util extends TestSuite {
static public TestSuite suite() {
return new TestPackage_util();
}
-
+
/** Creates new TestPackage */
private TestPackage_util() {
super( "util" );
- addTest( "TestTokenzier", TestTokenizer.suite());
- addTest( "TestFileUtils", TestFileUtils.suite() );
- addTest( "TestHashUtils", TestCollectionFactory.suite() );
- addTest( "TestLocationMapper", TestLocationMapper.suite() ) ;
- addTest( "TestFileManager", TestFileManager.suite()) ;
- addTest( "TestMonitors", TestMonitors.suite()) ;
- addTest( "TestPrintUtil", TestPrintUtil.suite()) ;
+ addTest( "TestTokenzier", TestTokenizer.suite());
+ addTest( "TestFileUtils", TestFileUtils.suite() );
+ addTest( "TestHashUtils", TestCollectionFactory.suite() );
+ addTest( "TestLocationMapper", TestLocationMapper.suite() ) ;
+ addTest( "TestFileManager", TestFileManager.suite()) ;
+ addTest( "TestMonitors", TestMonitors.suite()) ;
+ addTest( "TestPrintUtil", TestPrintUtil.suite()) ;
addTest( "TestPrefixMappingUtils", TestPrefixMappingUtils.suite() );
- addTest( TestIteratorCollection.suite() );
- addTest( "TestSplitIRI_XML", TestSplitIRI_XML.suite()) ;
- addTest( "TestSplitIRI_TTL", TestSplitIRI_TTL.suite()) ;
- addTest( "TestModelCollector", TestModelCollector.suite()) ;
-
+ addTest( "TestIteratorCollection", TestIteratorCollection.suite() );
+ addTest( "TestModelCollector", TestModelCollector.suite()) ;
+
+ addTest( "TestSplitIRI_Display", TestSplitIRI_Display.suite()) ;
+ addTest( "TestSplitIRI_XML", TestSplitIRI_XML.suite()) ;
+ addTest( "TestSplitIRI_TTL", TestSplitIRI_TTL.suite()) ;
+
+ // Needs the legacy N3 reader loaded.
addTestSuite( TestLocators.class );
addTestSuite( TestOneToManyMap.class );
}
@@ -55,7 +58,7 @@ public class TestPackage_util extends TestSuite {
private void addTest(String name, TestSuite tc) {
tc.setName(name);
addTest(tc);
- }
+ }
private void addTest(String name, Test tc) {
addTest(tc);
}
diff --git a/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java b/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_Display.java
similarity index 60%
copy from jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java
copy to jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_Display.java
index 0d302ec6d2..4a32b802d8 100644
--- a/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java
+++ b/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_Display.java
@@ -20,20 +20,18 @@ package org.apache.jena.util;
import static org.apache.jena.util.SplitIRI.* ;
-import java.util.Objects ;
-
import junit.framework.JUnit4TestAdapter ;
import org.junit.Assert ;
import org.junit.Test ;
/**
- * Test splitting IRI strings using Turtle rules.
- * Includes the generally splitting for display - the
- * difference is that for Turtle, some characters need escaping.
+ * Test splitting IRI strings using the display rules.
+ * See {SplitIRI} for details.
+ * Display is convenience - namespace+localname may not reconstruct the IRI.
*/
-public class TestSplitIRI_TTL {
+public class TestSplitIRI_Display {
public static junit.framework.Test suite() {
- return new JUnit4TestAdapter(TestSplitIRI_TTL.class) ;
+ return new JUnit4TestAdapter(TestSplitIRI_Display.class) ;
}
// Basics
@@ -54,68 +52,32 @@ public class TestSplitIRI_TTL {
@Test public void split_rel_3() { testPrefixLocalname("", "", "" ) ; }
// Bizarre but legal URIs
- @Test public void split_weird_1() { testPrefixLocalname("abc:def", "abc:", "def" ) ; }
+ @Test public void split_weird_1() { testPrefixLocalname("abc:def", "abc:", "def" ) ; }
+ @Test public void split_weird_2() { testPrefixLocalname("", "", "" ) ; }
// Turtle details.
// "." leading dot is not legal.
- @Test public void split_ttl_01() { testPrefixLocalname("http://example/foo#bar:baz", "http://example/foo#", "bar:baz" ) ; }
- @Test public void split_ttl_02() { testPrefixLocalname("http://example/a:b:c", "http://example/", "a:b:c" ) ; }
- @Test public void split_ttl_03() { testPrefixLocalname("http://example/.2.3.4", "http://example/.", "2.3.4" ) ; }
+ @Test public void split_ttl_01() { testPrefixLocalname("http://example/foo#bar:baz", "http://example/foo#", "bar:baz" ) ; }
+ @Test public void split_ttl_02() { testPrefixLocalname("http://example/a:b:c", "http://example/", "a:b:c" ) ; }
+ @Test public void split_ttl_03() { testPrefixLocalname("http://example/.2.3.4", "http://example/.", "2.3.4" ) ; }
// "." leading dot is not legal.
- @Test public void split_ttl_04() { testPrefixLocalname("abc:xyz/.def", "abc:xyz/.", "def" ) ; }
+ @Test public void split_ttl_04() { testPrefixLocalname("abc:xyz/.def", "abc:xyz/.", "def" ) ; }
// "-" leading dash is not legal.
- @Test public void split_ttl_05() { testPrefixLocalname("abc:xyz/-def", "abc:xyz/-", "def" ) ; }
- @Test public void split_ttl_06() { testPrefixLocalname("abc:xyz/-.-.-def", "abc:xyz/-.-.-", "def" ) ; }
- @Test public void split_ttl_07() { testPrefixLocalname("http://example/id=89", "http://example/", "id=89" ) ; }
- // Trailing '.'
- @Test public void split_ttl_08() { testPrefixLocalname("http://example/2.3.", "http://example/", "2.3." ) ; }
-
- // Turtle details, including escaping.
- // Test for PrefixLocalnameEsc
- @Test public void split_ttl_esc_01() { testPrefixLocalnameEsc("http://example/id=89", "http://example/", "id\\=89" ) ; }
- @Test public void split_ttl_esc_02() { testPrefixLocalnameEsc("http://example/a,b", "http://example/", "a\\,b" ) ; }
- // Trailing '.'
- @Test public void split_ttl_esc_03() { testPrefixLocalnameEsc("http://example/2.3.", "http://example/", "2.3\\." ) ; }
+ @Test public void split_ttl_05() { testPrefixLocalname("abc:xyz/-def", "abc:xyz/-", "def" ) ; }
+ @Test public void split_ttl_06() { testPrefixLocalname("abc:xyz/-.-.-def", "abc:xyz/-.-.-", "def" ) ; }
+ @Test public void split_ttl_07() { testPrefixLocalname("http://example/id=89", "http://example/", "id=89" ) ; }
// URNs split differently.
@Test public void split_urn_01() { testPrefixLocalname("urn:foo:bar", "urn:foo:", "bar") ; }
@Test public void split_urn_02() { testPrefixLocalname("urn:example:bar/b", "urn:example:", "bar/b"); }
@Test public void split_urn_03() { testPrefixLocalname("urn:example:bar#frag", "urn:example:bar#", "frag"); }
-
- // Fragments, including Turtle escapes.
- @Test public void split_frag_01() { testPrefixLocalname("http://example/foo#bar:baz", "http://example/foo#", "bar:baz" ) ; }
- @Test public void split_frag_02() { testPrefixLocalnameEsc("http://example/abc/def#ghi/jkl", "http://example/abc/def#", "ghi\\/jkl"); }
- @Test public void split_frag_03() { testPrefixLocalnameEsc("urn:example:abc#ghi:jkl", "urn:example:abc#", "ghi:jkl"); }
- @Test public void split_frag_04() { testPrefixLocalnameEsc("urn:example:abc#ghi/jkl", "urn:example:abc#", "ghi\\/jkl"); }
- @Test public void split_frag_05() { testPrefixLocalnameEsc("urn:example:abc#ghi:jkl", "urn:example:abc#", "ghi:jkl"); }
-
private void testSplit(String string, int expected) {
int i = splitpoint(string) ;
Assert.assertEquals(expected, i) ;
}
- // ??
- private void testTurtle(String string, String expectedPrefix, String expectedLocalname) {
- int i = splitpoint(string) ;
- String ns = string ;
- String ln = "" ;
- if ( i > 0 ) {
- ns = string.substring(0, i) ;
- ln = string.substring(i) ;
- }
-
- if ( expectedPrefix != null )
- Assert.assertEquals(expectedPrefix, ns);
- if ( expectedLocalname != null )
- Assert.assertEquals(expectedLocalname, ln);
- if ( expectedPrefix != null && expectedLocalname != null ) {
- String x = ns+ln ;
- Assert.assertEquals(string, x) ;
- }
- }
-
private void testPrefixLocalnameNoSplit(String string) {
int i = splitpoint(string) ;
String msg = string ;
@@ -141,13 +103,6 @@ public class TestSplitIRI_TTL {
}
}
- // Do worry about local name escaping.
- private void testPrefixLocalnameEsc(String string, String expectedNamespace, String expectedLocalname) {
- checkPrefixLocalname(string,
- expectedNamespace, namespaceTTL(string),
- expectedLocalname, localnameTTL(string)) ;
- }
-
private void checkPrefixLocalname(String string,
String expectedNamespace, String actualNamespace,
String expectedLocalname, String actualLocalName) {
@@ -156,18 +111,4 @@ public class TestSplitIRI_TTL {
if ( expectedLocalname != null )
Assert.assertEquals(expectedLocalname, actualLocalName);
}
-
- private void testPrefixLocalnameNot(String string, String expectedPrefix, String expectedLocalname) {
- String ns = namespace(string) ;
- String ln = localname(string) ;
-
- boolean b1 = Objects.equals(expectedPrefix, ns) ;
- boolean b2 = Objects.equals(expectedLocalname, ln) ;
-
- // Test not both true.
- Assert.assertFalse("Wrong: ("+ns+","+ln+")", b1&&b2);
- // But it still combines.
- String x = ns+ln ;
- Assert.assertEquals(string, x) ;
- }
}
diff --git a/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java b/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java
index 0d302ec6d2..45eb95fe6f 100644
--- a/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java
+++ b/jena-core/src/test/java/org/apache/jena/util/TestSplitIRI_TTL.java
@@ -20,8 +20,6 @@ package org.apache.jena.util;
import static org.apache.jena.util.SplitIRI.* ;
-import java.util.Objects ;
-
import junit.framework.JUnit4TestAdapter ;
import org.junit.Assert ;
import org.junit.Test ;
@@ -38,114 +36,77 @@ public class TestSplitIRI_TTL {
// Basics
@Test public void split_basic_00() { testSplit("http://example/foo", "http://example/".length()) ; }
- @Test public void split_basic_01() { testPrefixLocalname("http://example/foo", "http://example/", "foo" ) ; }
- @Test public void split_basic_02() { testPrefixLocalname("http://example/foo#bar", "http://example/foo#", "bar" ) ; }
- @Test public void split_basic_03() { testPrefixLocalname("http://example/foo#", "http://example/foo#", "" ) ; }
- @Test public void split_basic_04() { testPrefixLocalname("http://example/", "http://example/", "" ) ; }
- @Test public void split_basic_05() { testPrefixLocalname("http://example/1abc", "http://example/", "1abc" ) ; }
- @Test public void split_basic_06() { testPrefixLocalname("http://example/1.2.3.4", "http://example/", "1.2.3.4" ) ; }
- @Test public void split_basic_07() { testPrefixLocalname("http://example/xyz#1.2.3.4", "http://example/xyz#", "1.2.3.4" ) ; }
- @Test public void split_basic_08() { testPrefixLocalname("http://example/xyz#_abc", "http://example/xyz#", "_abc" ) ; }
- @Test public void split_basic_09() { testPrefixLocalname("http://example/xyz/_1.2.3.4", "http://example/xyz/", "_1.2.3.4" ) ; }
+ @Test public void split_basic_01() { testPrefixLocalnameTTL("http://example/foo", "http://example/", "foo" ) ; }
+ @Test public void split_basic_02() { testPrefixLocalnameTTL("http://example/foo#bar", "http://example/foo#", "bar" ) ; }
+ @Test public void split_basic_03() { testPrefixLocalnameTTL("http://example/foo#", "http://example/foo#", "" ) ; }
+ @Test public void split_basic_04() { testPrefixLocalnameTTL("http://example/", "http://example/", "" ) ; }
+ @Test public void split_basic_05() { testPrefixLocalnameTTL("http://example/1abc", "http://example/", "1abc" ) ; }
+ @Test public void split_basic_06() { testPrefixLocalnameTTL("http://example/1.2.3.4", "http://example/", "1.2.3.4" ) ; }
+ @Test public void split_basic_07() { testPrefixLocalnameTTL("http://example/xyz#1.2.3.4", "http://example/xyz#", "1.2.3.4" ) ; }
+ @Test public void split_basic_08() { testPrefixLocalnameTTL("http://example/xyz#_abc", "http://example/xyz#", "\\_abc" ) ; }
+ @Test public void split_basic_09() { testPrefixLocalnameTTL("http://example/xyz/_1.2.3.4", "http://example/xyz/", "\\_1.2.3.4" ) ; }
// Relative URIs
- @Test public void split_rel_1() { testPrefixLocalname("xyz/_1.2.3.4", "xyz/", "_1.2.3.4" ) ; }
- @Test public void split_rel_2() { testPrefixLocalname("xyz", "", "xyz" ) ; }
- @Test public void split_rel_3() { testPrefixLocalname("", "", "" ) ; }
+ @Test public void split_rel_1() { testPrefixLocalnameTTL("xyz/_1.2.3.4", "xyz/", "\\_1.2.3.4" ) ; }
+ @Test public void split_rel_2() { testPrefixLocalnameTTL("xyz", "", "xyz" ) ; }
+ @Test public void split_rel_3() { testPrefixLocalnameTTL("", "", "" ) ; }
// Bizarre but legal URIs
- @Test public void split_weird_1() { testPrefixLocalname("abc:def", "abc:", "def" ) ; }
+ @Test public void split_weird_1() { testPrefixLocalnameTTL("abc:def", "abc:", "def" ) ; }
+ @Test public void split_weird_2() { testPrefixLocalnameTTL("", "", "" ) ; }
+ // Trailing '.' - split with escape.
+ @Test public void split_weird_3() { testPrefixLocalnameTTL("http://example/abc#x.", "http://example/abc#", "x\\." ) ; }
+ @Test public void split_weird_4() { testPrefixLocalnameTTL("http://example/", "http://example/", "" ) ; }
// Turtle details.
// "." leading dot is not legal.
- @Test public void split_ttl_01() { testPrefixLocalname("http://example/foo#bar:baz", "http://example/foo#", "bar:baz" ) ; }
- @Test public void split_ttl_02() { testPrefixLocalname("http://example/a:b:c", "http://example/", "a:b:c" ) ; }
- @Test public void split_ttl_03() { testPrefixLocalname("http://example/.2.3.4", "http://example/.", "2.3.4" ) ; }
+ @Test public void split_ttl_01() { testPrefixLocalnameTTL("http://example/foo#bar:baz", "http://example/foo#", "bar:baz" ) ; }
+ @Test public void split_ttl_02() { testPrefixLocalnameTTL("http://example/a:b:c", "http://example/", "a:b:c" ) ; }
+ @Test public void split_ttl_03() { testPrefixLocalnameTTL("http://example/.2.3.4", "http://example/.", "2.3.4" ) ; }
// "." leading dot is not legal.
- @Test public void split_ttl_04() { testPrefixLocalname("abc:xyz/.def", "abc:xyz/.", "def" ) ; }
+ @Test public void split_ttl_04() { testPrefixLocalnameTTL("abc:xyz/.def", "abc:xyz/.", "def" ) ; }
// "-" leading dash is not legal.
- @Test public void split_ttl_05() { testPrefixLocalname("abc:xyz/-def", "abc:xyz/-", "def" ) ; }
- @Test public void split_ttl_06() { testPrefixLocalname("abc:xyz/-.-.-def", "abc:xyz/-.-.-", "def" ) ; }
- @Test public void split_ttl_07() { testPrefixLocalname("http://example/id=89", "http://example/", "id=89" ) ; }
- // Trailing '.'
- @Test public void split_ttl_08() { testPrefixLocalname("http://example/2.3.", "http://example/", "2.3." ) ; }
+ @Test public void split_ttl_05() { testPrefixLocalnameTTL("abc:xyz/-def", "abc:xyz/-", "def" ) ; }
+ @Test public void split_ttl_06() { testPrefixLocalnameTTL("abc:xyz/-.-.-def", "abc:xyz/-.-.-", "def" ) ; }
+ // Turtle-escape.
+ @Test public void split_ttl_07() { testPrefixLocalnameTTL("http://example/id=89", "http://example/", "id\\=89" ) ; }
// Turtle details, including escaping.
// Test for PrefixLocalnameEsc
- @Test public void split_ttl_esc_01() { testPrefixLocalnameEsc("http://example/id=89", "http://example/", "id\\=89" ) ; }
- @Test public void split_ttl_esc_02() { testPrefixLocalnameEsc("http://example/a,b", "http://example/", "a\\,b" ) ; }
- // Trailing '.'
- @Test public void split_ttl_esc_03() { testPrefixLocalnameEsc("http://example/2.3.", "http://example/", "2.3\\." ) ; }
+ @Test public void split_ttl_esc_01() { testPrefixLocalnameTTL("http://example/id=89", "http://example/", "id\\=89" ) ; }
+ @Test public void split_ttl_esc_02() { testPrefixLocalnameTTL("http://example/a,b", "http://example/", "a\\,b" ) ; }
+ // Trailing '.' Legal if escaped.
+ @Test public void split_ttl_esc_03() { testPrefixLocalnameTTL("http://example/2.3.", "http://example/", "2.3\\." ) ; }
+ @Test public void split_ttl_esc_04() { testPrefixLocalnameTTL("http://example/abc#x.", "http://example/abc#", "x\\." ) ; }
// URNs split differently.
- @Test public void split_urn_01() { testPrefixLocalname("urn:foo:bar", "urn:foo:", "bar") ; }
- @Test public void split_urn_02() { testPrefixLocalname("urn:example:bar/b", "urn:example:", "bar/b"); }
- @Test public void split_urn_03() { testPrefixLocalname("urn:example:bar#frag", "urn:example:bar#", "frag"); }
-
+ @Test public void split_urn_01() { testPrefixLocalnameTTL("urn:foo:bar", "urn:foo:", "bar") ; }
+ @Test public void split_urn_02() { testPrefixLocalnameTTL("urn:example:bar/b", "urn:example:", "bar\\/b"); }
+ @Test public void split_urn_03() { testPrefixLocalnameTTL("urn:example:bar#frag", "urn:example:bar#", "frag"); }
// Fragments, including Turtle escapes.
- @Test public void split_frag_01() { testPrefixLocalname("http://example/foo#bar:baz", "http://example/foo#", "bar:baz" ) ; }
- @Test public void split_frag_02() { testPrefixLocalnameEsc("http://example/abc/def#ghi/jkl", "http://example/abc/def#", "ghi\\/jkl"); }
- @Test public void split_frag_03() { testPrefixLocalnameEsc("urn:example:abc#ghi:jkl", "urn:example:abc#", "ghi:jkl"); }
- @Test public void split_frag_04() { testPrefixLocalnameEsc("urn:example:abc#ghi/jkl", "urn:example:abc#", "ghi\\/jkl"); }
- @Test public void split_frag_05() { testPrefixLocalnameEsc("urn:example:abc#ghi:jkl", "urn:example:abc#", "ghi:jkl"); }
+ @Test public void split_frag_01() { testPrefixLocalnameTTL("http://example/foo#bar:baz", "http://example/foo#", "bar:baz" ) ; }
+ @Test public void split_frag_02() { testPrefixLocalnameTTL("http://example/abc/def#ghi/jkl", "http://example/abc/def#", "ghi\\/jkl"); }
+ @Test public void split_frag_03() { testPrefixLocalnameTTL("urn:example:abc#ghi:jkl", "urn:example:abc#", "ghi:jkl"); }
+ @Test public void split_frag_04() { testPrefixLocalnameTTL("urn:example:abc#ghi/jkl", "urn:example:abc#", "ghi\\/jkl"); }
+ @Test public void split_frag_05() { testPrefixLocalnameTTL("urn:example:abc#ghi:jkl", "urn:example:abc#", "ghi:jkl"); }
private void testSplit(String string, int expected) {
int i = splitpoint(string) ;
Assert.assertEquals(expected, i) ;
}
- // ??
- private void testTurtle(String string, String expectedPrefix, String expectedLocalname) {
- int i = splitpoint(string) ;
- String ns = string ;
- String ln = "" ;
- if ( i > 0 ) {
- ns = string.substring(0, i) ;
- ln = string.substring(i) ;
- }
-
- if ( expectedPrefix != null )
- Assert.assertEquals(expectedPrefix, ns);
- if ( expectedLocalname != null )
- Assert.assertEquals(expectedLocalname, ln);
- if ( expectedPrefix != null && expectedLocalname != null ) {
- String x = ns+ln ;
- Assert.assertEquals(string, x) ;
- }
- }
-
- private void testPrefixLocalnameNoSplit(String string) {
- int i = splitpoint(string) ;
- String msg = string ;
- if ( i != -1 ) {
- // Better error message.
- String ns = namespaceTTL(string) ;
- String ln = localnameTTL(string) ;
- msg = "Unexpected split of '"+string+"' into ("+ns+", "+ln+") [index="+i+"]" ;
- }
- Assert.assertEquals(msg, -1, i) ;
- }
-
- // Don't worry about local name escaping.
- private void testPrefixLocalname(String string, String expectedNamespace, String expectedLocalname) {
- String actualNamespace = namespace(string) ;
- String actualLocalName = localname(string) ;
+ private void testPrefixLocalnameTTL(String string, String expectedNamespace, String expectedLocalname) {
checkPrefixLocalname(string,
- expectedNamespace, actualNamespace,
- expectedLocalname, actualLocalName) ;
- if ( expectedNamespace != null && expectedLocalname != null ) {
- String x = actualNamespace+actualLocalName ;
- Assert.assertEquals(string, x) ;
- }
+ expectedNamespace, namespaceTTL(string),
+ expectedLocalname, localnameTTL(string)) ;
}
- // Do worry about local name escaping.
- private void testPrefixLocalnameEsc(String string, String expectedNamespace, String expectedLocalname) {
+ private void testPrefixLocalnameTTLnoESC(String string, String expectedNamespace, String expectedLocalname) {
checkPrefixLocalname(string,
expectedNamespace, namespaceTTL(string),
- expectedLocalname, localnameTTL(string)) ;
+ expectedLocalname, localnameTTLNoEsc(string)) ;
}
private void checkPrefixLocalname(String string,
@@ -156,18 +117,4 @@ public class TestSplitIRI_TTL {
if ( expectedLocalname != null )
Assert.assertEquals(expectedLocalname, actualLocalName);
}
-
- private void testPrefixLocalnameNot(String string, String expectedPrefix, String expectedLocalname) {
- String ns = namespace(string) ;
- String ln = localname(string) ;
-
- boolean b1 = Objects.equals(expectedPrefix, ns) ;
- boolean b2 = Objects.equals(expectedLocalname, ln) ;
-
- // Test not both true.
- Assert.assertFalse("Wrong: ("+ns+","+ln+")", b1&&b2);
- // But it still combines.
- String x = ns+ln ;
- Assert.assertEquals(string, x) ;
- }
}