Author: rhauch
Date: 2008-10-28 19:47:02 -0400 (Tue, 28 Oct 2008)
New Revision: 595
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/Name.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/Path.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicName.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicPath.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicPathSegment.java
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicNameTest.java
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicPathSegmentTest.java
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicPathTest.java
Log:
DNA-245 - Add methods to Name, Path.Segment, and Path to allow using custom delimiters
http://jira.jboss.com/jira/browse/DNA-245
Added to Name, Path.Segment, and Path the "getString(NamespaceRegistry, TextEncoder,
TextEncoder)" method, which uses the 2nd encoder to preprocess the various
delimiters. These methods can be used, for example, to escape (or convert) the delimiters
in a Path (for example, to escape the ":" as needed by a file system, or to
convert "/" to a different delimiter.
Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/Name.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/Name.java 2008-10-28
19:21:10 UTC (rev 594)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/Name.java 2008-10-28
23:47:02 UTC (rev 595)
@@ -24,9 +24,11 @@
import java.io.Serializable;
import net.jcip.annotations.Immutable;
import org.jboss.dna.common.text.TextEncoder;
+import org.jboss.dna.graph.properties.Path.Segment;
/**
* A qualified name consisting of a namespace and a local name.
+ *
* @author Randall Hauch
*/
@Immutable
@@ -34,12 +36,14 @@
/**
* Get the local name part of this qualified name.
+ *
* @return the local name; never null
*/
String getLocalName();
/**
* Get the URI for the namespace used in this qualified name.
+ *
* @return the URI; never null but possibly empty
*/
String getNamespaceUri();
@@ -47,6 +51,7 @@
/**
* Get the string form of the name. The {@link Path#DEFAULT_ENCODER default encoder}
is used to encode characters in the local
* name and namespace.
+ *
* @return the encoded string
* @see #getString(TextEncoder)
*/
@@ -54,6 +59,7 @@
/**
* Get the encoded string form of the name, using the supplied encoder to encode
characters in the local name and namespace.
+ *
* @param encoder the encoder to use, or null if the {@link Path#DEFAULT_ENCODER
default encoder} should be used
* @return the encoded string
* @see #getString()
@@ -61,11 +67,12 @@
public String getString( TextEncoder encoder );
/**
- * Get the string form of the name, using the supplied namespace registry to convert
the
- * {@link #getNamespaceUri() namespace URI} to a prefix. The {@link
Path#DEFAULT_ENCODER default encoder} is used to encode
- * characters in each of the path segments.
+ * Get the string form of the name, using the supplied namespace registry to convert
the {@link #getNamespaceUri() namespace
+ * URI} to a prefix. The {@link Path#DEFAULT_ENCODER default encoder} is used to
encode characters in each of the path
+ * segments.
+ *
* @param namespaceRegistry the namespace registry that should be used to obtain the
prefix for the
- * {@link Name#getNamespaceUri() namespace URI}
+ * {@link Name#getNamespaceUri() namespace URI}
* @return the encoded string
* @throws IllegalArgumentException if the namespace registry is null
* @see #getString(NamespaceRegistry,TextEncoder)
@@ -73,14 +80,36 @@
public String getString( NamespaceRegistry namespaceRegistry );
/**
- * Get the encoded string form of the name, using the supplied namespace registry to
convert the
- * {@link #getNamespaceUri() namespace URI} to a prefix.
+ * Get the encoded string form of the name, using the supplied namespace registry to
convert the {@link #getNamespaceUri()
+ * namespace URI} to a prefix.
+ *
* @param namespaceRegistry the namespace registry that should be used to obtain the
prefix for the
- * {@link Name#getNamespaceUri() namespace URI}
+ * {@link Name#getNamespaceUri() namespace URI}
* @param encoder the encoder to use, or null if the {@link Path#DEFAULT_ENCODER
default encoder} should be used
* @return the encoded string
* @throws IllegalArgumentException if the namespace registry is null
* @see #getString(NamespaceRegistry)
*/
- public String getString( NamespaceRegistry namespaceRegistry, TextEncoder encoder );
+ public String getString( NamespaceRegistry namespaceRegistry,
+ TextEncoder encoder );
+
+ /**
+ * Get the encoded string form of the name, using the supplied namespace registry to
convert the names' namespace URIs to
+ * prefixes and the supplied encoder to encode characters in each of the path
segments, and using the second delimiter to
+ * encode (or convert) the delimiter used between the namespace prefix and the local
part.
+ *
+ * @param namespaceRegistry the namespace registry that should be used to obtain the
prefix for the
+ * {@link Name#getNamespaceUri() namespace URIs} in the segment {@link
Segment#getName() names}
+ * @param encoder the encoder to use for encoding the {@link Name#getLocalName()
local part} and
+ * {@link Name#getNamespaceUri() namespace prefix}, or null if the {@link
Path#DEFAULT_ENCODER default encoder} should
+ * be used
+ * @param delimiterEncoder the encoder to use for encoding the delimiter between the
{@link Name#getLocalName() local part}
+ * and {@link Name#getNamespaceUri() namespace prefix}, or null if the
standard delimiter should be used
+ * @return the encoded string
+ * @see #getString(NamespaceRegistry)
+ * @see #getString(NamespaceRegistry, TextEncoder)
+ */
+ public String getString( NamespaceRegistry namespaceRegistry,
+ TextEncoder encoder,
+ TextEncoder delimiterEncoder );
}
Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/Path.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/Path.java 2008-10-28
19:21:10 UTC (rev 594)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/Path.java 2008-10-28
23:47:02 UTC (rev 595)
@@ -237,6 +237,28 @@
*/
public String getString( NamespaceRegistry namespaceRegistry,
TextEncoder encoder );
+
+ /**
+ * Get the encoded string form of the segment, using the supplied namespace
registry to convert the names' namespace URIs
+ * to prefixes and the supplied encoder to encode characters in each of the path
segments. The second encoder is used to
+ * encode (or convert) the delimiter between the {@link Name#getNamespaceUri()
namespace prefix} and the
+ * {@link Name#getLocalName() local part}.
+ *
+ * @param namespaceRegistry the namespace registry that should be used to obtain
the prefix for the
+ * {@link Name#getNamespaceUri() namespace URIs} in the segment {@link
Segment#getName() names}
+ * @param encoder the encoder to use for encoding the {@link Name#getLocalName()
local part} and
+ * {@link Name#getNamespaceUri() namespace prefix} in the segment's
{@link #getName() name}, or null if the
+ * {@link #DEFAULT_ENCODER default encoder} should be used
+ * @param delimiterEncoder the encoder to use for encoding the delimiter between
the {@link Name#getLocalName() local
+ * part} and {@link Name#getNamespaceUri() namespace prefix} of each
{@link Path#getSegmentsList() segment}, or
+ * null if the standard delimiters should be used
+ * @return the encoded string
+ * @see #getString(NamespaceRegistry)
+ * @see #getString(NamespaceRegistry, TextEncoder)
+ */
+ public String getString( NamespaceRegistry namespaceRegistry,
+ TextEncoder encoder,
+ TextEncoder delimiterEncoder );
}
/**
@@ -503,13 +525,16 @@
/**
* Get the string form of the path, using the supplied namespace registry to convert
the names' namespace URIs to prefixes.
- * The {@link #DEFAULT_ENCODER default encoder} is used to encode characters in each
of the path segments.
+ * The {@link #DEFAULT_ENCODER default encoder} is used to encode characters in each
of the path segments. The second encoder
+ * is used to encode (or convert) the delimiter between the {@link
Name#getNamespaceUri() namespace prefix} and the
+ * {@link Name#getLocalName() local part}.
*
* @param namespaceRegistry the namespace registry that should be used to obtain the
prefix for the
* {@link Name#getNamespaceUri() namespace URIs} in the segment {@link
Segment#getName() names}
* @return the encoded string
* @throws IllegalArgumentException if the namespace registry is null
* @see #getString(NamespaceRegistry,TextEncoder)
+ * @see #getString(NamespaceRegistry, TextEncoder, TextEncoder)
*/
public String getString( NamespaceRegistry namespaceRegistry );
@@ -519,12 +544,35 @@
*
* @param namespaceRegistry the namespace registry that should be used to obtain the
prefix for the
* {@link Name#getNamespaceUri() namespace URIs} in the segment {@link
Segment#getName() names}
- * @param encoder the encoder to use, or null if the {@link #DEFAULT_ENCODER default
encoder} should be used
+ * @param encoder the encoder to use for encoding the {@link Name#getLocalName()
local part} and
+ * {@link Name#getNamespaceUri() namespace prefix} of each {@link
Path#getSegmentsList() segment}, or null if the
+ * {@link #DEFAULT_ENCODER default encoder} should be used
* @return the encoded string
* @throws IllegalArgumentException if the namespace registry is null
* @see #getString(NamespaceRegistry)
+ * @see #getString(NamespaceRegistry, TextEncoder, TextEncoder)
*/
public String getString( NamespaceRegistry namespaceRegistry,
TextEncoder encoder );
+ /**
+ * Get the encoded string form of the path, using the supplied namespace registry to
convert the names' namespace URIs to
+ * prefixes and the supplied encoder to encode characters in each of the path
segments.
+ *
+ * @param namespaceRegistry the namespace registry that should be used to obtain the
prefix for the
+ * {@link Name#getNamespaceUri() namespace URIs} in the segment {@link
Segment#getName() names}
+ * @param encoder the encoder to use for encoding the {@link Name#getLocalName()
local part} and
+ * {@link Name#getNamespaceUri() namespace prefix} of each {@link
Path#getSegmentsList() segment}, or null if the
+ * {@link #DEFAULT_ENCODER default encoder} should be used
+ * @param delimiterEncoder the encoder to use for encoding the delimiter between the
{@link Name#getLocalName() local part}
+ * and {@link Name#getNamespaceUri() namespace prefix} of each {@link
Path#getSegmentsList() segment}, and for encoding
+ * the path delimiter, or null if the standard delimiters should be used
+ * @return the encoded string
+ * @see #getString(NamespaceRegistry)
+ * @see #getString(NamespaceRegistry, TextEncoder)
+ */
+ public String getString( NamespaceRegistry namespaceRegistry,
+ TextEncoder encoder,
+ TextEncoder delimiterEncoder );
+
}
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicName.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicName.java 2008-10-28
19:21:10 UTC (rev 594)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicName.java 2008-10-28
23:47:02 UTC (rev 595)
@@ -103,6 +103,7 @@
*/
public String getString( NamespaceRegistry namespaceRegistry,
TextEncoder encoder ) {
+ // This is the most-often used method, so implement it directly
CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri,
true);
if (prefix != null && prefix.length() != 0) {
@@ -113,7 +114,37 @@
/**
* {@inheritDoc}
+ *
+ * @see
org.jboss.dna.graph.properties.Name#getString(org.jboss.dna.graph.properties.NamespaceRegistry,
+ * org.jboss.dna.common.text.TextEncoder,
org.jboss.dna.common.text.TextEncoder)
*/
+ public String getString( NamespaceRegistry namespaceRegistry,
+ TextEncoder encoder,
+ TextEncoder delimiterEncoder ) {
+ if (namespaceRegistry == null) {
+ if (this.getNamespaceUri().length() == 0) {
+ if (this.getLocalName().equals(Path.SELF)) return Path.SELF;
+ if (this.getLocalName().equals(Path.PARENT)) return Path.PARENT;
+ }
+ if (encoder == null) encoder = Path.DEFAULT_ENCODER;
+ if (delimiterEncoder != null) {
+ return delimiterEncoder.encode("{") +
encoder.encode(this.namespaceUri) + delimiterEncoder.encode("}")
+ + encoder.encode(this.localName);
+ }
+ return "{" + encoder.encode(this.namespaceUri) + "}" +
encoder.encode(this.localName);
+
+ }
+ String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri,
true);
+ if (prefix != null && prefix.length() != 0) {
+ String delim = delimiterEncoder != null ?
delimiterEncoder.encode(":") : ":";
+ return encoder.encode(prefix) + delim + encoder.encode(this.localName);
+ }
+ return this.localName;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public int compareTo( Name that ) {
if (that == this) return 0;
int diff = this.getNamespaceUri().compareTo(that.getNamespaceUri());
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicPath.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicPath.java 2008-10-28
19:21:10 UTC (rev 594)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicPath.java 2008-10-28
23:47:02 UTC (rev 595)
@@ -199,14 +199,14 @@
* {@inheritDoc}
*/
public String getString() {
- return doGetString(null, DEFAULT_ENCODER);
+ return doGetString(null, DEFAULT_ENCODER, null);
}
/**
* {@inheritDoc}
*/
public String getString( TextEncoder encoder ) {
- return doGetString(null, encoder);
+ return doGetString(null, encoder, null);
}
/**
@@ -214,7 +214,7 @@
*/
public String getString( NamespaceRegistry namespaceRegistry ) {
CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
- return doGetString(namespaceRegistry, null);
+ return doGetString(namespaceRegistry, null, null);
}
/**
@@ -223,44 +223,55 @@
public String getString( NamespaceRegistry namespaceRegistry,
TextEncoder encoder ) {
CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
- return doGetString(namespaceRegistry, encoder);
+ return doGetString(namespaceRegistry, encoder, null);
}
/**
+ * {@inheritDoc}
+ *
+ * @see
org.jboss.dna.graph.properties.Path#getString(org.jboss.dna.graph.properties.NamespaceRegistry,
+ * org.jboss.dna.common.text.TextEncoder,
org.jboss.dna.common.text.TextEncoder)
+ */
+ public String getString( NamespaceRegistry namespaceRegistry,
+ TextEncoder encoder,
+ TextEncoder delimiterEncoder ) {
+ return doGetString(namespaceRegistry, encoder, delimiterEncoder);
+ }
+
+ /**
* Method that creates the string representation. This method works two different
ways depending upon whether the namespace
* registry is provided.
*
* @param namespaceRegistry
* @param encoder
+ * @param delimiterEncoder
* @return this path as a string
*/
protected String doGetString( NamespaceRegistry namespaceRegistry,
- TextEncoder encoder ) {
+ TextEncoder encoder,
+ TextEncoder delimiterEncoder ) {
if (encoder == null) encoder = DEFAULT_ENCODER;
- if (encoder == DEFAULT_ENCODER && this.path != null) return this.path;
+ if (encoder == DEFAULT_ENCODER && this.path != null &&
delimiterEncoder == null) return this.path;
+ final String delimiter = delimiterEncoder != null ?
delimiterEncoder.encode(DELIMITER_STR) : DELIMITER_STR;
// Since the segments are immutable, this code need not be synchronized because
concurrent threads
// may just compute the same value (with no harm done)
StringBuilder sb = new StringBuilder();
- if (this.isAbsolute()) sb.append(DELIMITER);
+ if (this.isAbsolute()) sb.append(delimiter);
boolean first = true;
for (Segment segment : this.segments) {
if (first) {
first = false;
} else {
- sb.append(DELIMITER);
+ sb.append(delimiter);
}
assert segment != null;
- if (namespaceRegistry != null) {
- sb.append(segment.getString(namespaceRegistry, encoder));
- } else {
- sb.append(segment.getString(encoder));
- }
+ sb.append(segment.getString(namespaceRegistry, encoder, delimiterEncoder));
}
String result = sb.toString();
// Save the result to the internal string if this the default encoder is used.
// This is not synchronized, but it's okay
- if (encoder == DEFAULT_ENCODER && this.path == null) this.path = result;
+ if (encoder == DEFAULT_ENCODER && this.path == null &&
delimiterEncoder == null) this.path = result;
return result;
}
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicPathSegment.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicPathSegment.java 2008-10-28
19:21:10 UTC (rev 594)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicPathSegment.java 2008-10-28
23:47:02 UTC (rev 595)
@@ -179,13 +179,23 @@
*/
public String getString( NamespaceRegistry namespaceRegistry,
TextEncoder encoder ) {
- CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
+ return getString(namespaceRegistry, encoder, null);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see
org.jboss.dna.graph.properties.Path.Segment#getString(org.jboss.dna.graph.properties.NamespaceRegistry,
+ * org.jboss.dna.common.text.TextEncoder,
org.jboss.dna.common.text.TextEncoder)
+ */
+ public String getString( NamespaceRegistry namespaceRegistry,
+ TextEncoder encoder,
+ TextEncoder delimiterEncoder ) {
if (encoder == null) encoder = Path.DEFAULT_ENCODER;
- String encodedName = this.getName().getString(namespaceRegistry, encoder);
+ String encodedName = this.getName().getString(namespaceRegistry, encoder,
delimiterEncoder);
if (this.hasIndex()) {
return encodedName + "[" + this.getIndex() + "]";
}
return encodedName;
}
-
}
Modified:
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicNameTest.java
===================================================================
---
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicNameTest.java 2008-10-28
19:21:10 UTC (rev 594)
+++
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicNameTest.java 2008-10-28
23:47:02 UTC (rev 595)
@@ -24,12 +24,11 @@
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.containsString;
+import org.jboss.dna.common.text.Jsr283Encoder;
import org.jboss.dna.common.text.TextEncoder;
import org.jboss.dna.graph.DnaLexicon;
import org.jboss.dna.graph.properties.Name;
import org.jboss.dna.graph.properties.Path;
-import org.jboss.dna.graph.properties.basic.BasicName;
-import org.jboss.dna.graph.properties.basic.BasicNamespaceRegistry;
import org.junit.Before;
import org.junit.Test;
@@ -44,6 +43,7 @@
private String validNamespaceUri;
private String validLocalName;
private TextEncoder encoder;
+ private TextEncoder delimiterEncoder;
private String validNamespacePrefix;
@Before
@@ -55,6 +55,14 @@
this.encoder = Path.URL_ENCODER;
this.namespaceRegistry = new BasicNamespaceRegistry();
this.namespaceRegistry.register(validNamespacePrefix, validNamespaceUri);
+ this.delimiterEncoder = new TextEncoder() {
+ public String encode( String text ) {
+ if (":".equals(text)) return "\\:";
+ if ("{".equals(text)) return "\\{";
+ if ("}".equals(text)) return "\\}";
+ return text;
+ }
+ };
}
@Test
@@ -171,4 +179,13 @@
result = name.getString(namespaceRegistry, encoder);
assertThat(result, is("some:name:with:colons"));
}
+
+ @Test
+ public void shouldUseDelimiterEncoderToEncodeDelimiterBetweenPrefixAndLocalPart() {
+ encoder = new Jsr283Encoder();
+ name = new BasicName(DnaLexicon.Namespace.URI,
"some:name:with:colons");
+ assertThat(name.getString(namespaceRegistry, encoder, delimiterEncoder),
is("dna\\:some\uf03aname\uf03awith\uf03acolons"));
+ assertThat(name.getString(null, encoder, delimiterEncoder), is("\\{" +
encoder.encode(DnaLexicon.Namespace.URI)
+ +
"\\}some\uf03aname\uf03awith\uf03acolons"));
+ }
}
Modified:
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicPathSegmentTest.java
===================================================================
---
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicPathSegmentTest.java 2008-10-28
19:21:10 UTC (rev 594)
+++
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicPathSegmentTest.java 2008-10-28
23:47:02 UTC (rev 595)
@@ -24,16 +24,12 @@
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
+import org.jboss.dna.common.text.Jsr283Encoder;
import org.jboss.dna.common.text.TextEncoder;
import org.jboss.dna.graph.DnaLexicon;
import org.jboss.dna.graph.properties.Name;
import org.jboss.dna.graph.properties.Path;
import org.jboss.dna.graph.properties.ValueFactory;
-import org.jboss.dna.graph.properties.basic.BasicNamespaceRegistry;
-import org.jboss.dna.graph.properties.basic.BasicPathSegment;
-import org.jboss.dna.graph.properties.basic.NameValueFactory;
-import org.jboss.dna.graph.properties.basic.PathValueFactory;
-import org.jboss.dna.graph.properties.basic.StringValueFactory;
import org.junit.Before;
import org.junit.Test;
@@ -163,4 +159,21 @@
assertThat(segment, is(not(segment2)));
}
+ @Test
+ public void shouldUseDelimiterEncoderToEncodeDelimiterBetweenPrefixAndLocalPart() {
+ TextEncoder encoder = new Jsr283Encoder();
+ validName = new BasicName(DnaLexicon.Namespace.URI,
"some:name:with:colons");
+ segment = new BasicPathSegment(validName, Path.NO_INDEX);
+ TextEncoder delimiterEncoder = new TextEncoder() {
+ public String encode( String text ) {
+ if (":".equals(text)) return "\\:";
+ if ("{".equals(text)) return "\\{";
+ if ("}".equals(text)) return "\\}";
+ return text;
+ }
+ };
+ assertThat(segment.getString(registry, encoder, delimiterEncoder),
is("dna\\:some\uf03aname\uf03awith\uf03acolons"));
+ assertThat(segment.getString(null, encoder, delimiterEncoder), is("\\{"
+ encoder.encode(DnaLexicon.Namespace.URI)
+ +
"\\}some\uf03aname\uf03awith\uf03acolons"));
+ }
}
Modified:
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicPathTest.java
===================================================================
---
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicPathTest.java 2008-10-28
19:21:10 UTC (rev 594)
+++
trunk/dna-graph/src/test/java/org/jboss/dna/graph/properties/basic/BasicPathTest.java 2008-10-28
23:47:02 UTC (rev 595)
@@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
+import org.jboss.dna.common.text.Jsr283Encoder;
import org.jboss.dna.common.text.TextEncoder;
import org.jboss.dna.graph.DnaLexicon;
import org.jboss.dna.graph.properties.InvalidPathException;
@@ -898,4 +899,35 @@
path = pathFactory.create("a/b/c");
assertThat(path, hasSegments(pathFactory, "a", "b",
"c"));
}
+
+ @Test
+ public void shouldConvertPathToString() {
+ TextEncoder encoder = new Jsr283Encoder();
+ TextEncoder delimEncoder = new TextEncoder() {
+ public String encode( String text ) {
+ if ("/".equals(text)) return "\\/";
+ if (":".equals(text)) return "\\:";
+ if ("{".equals(text)) return "\\{";
+ if ("}".equals(text)) return "\\}";
+ return text;
+ }
+ };
+ Path path = pathFactory.create("a/b/c");
+ assertThat(path.getString(namespaceRegistry), is("a/b/c"));
+ assertThat(path.getString(namespaceRegistry, encoder), is("a/b/c"));
+ assertThat(path.getString(namespaceRegistry, encoder, delimEncoder),
is("a\\/b\\/c"));
+
+ path = pathFactory.create("/a/b/c");
+ assertThat(path.getString(namespaceRegistry), is("/a/b/c"));
+ assertThat(path.getString(namespaceRegistry, encoder), is("/a/b/c"));
+ assertThat(path.getString(namespaceRegistry, encoder, delimEncoder),
is("\\/a\\/b\\/c"));
+
+ path = pathFactory.create("/dna:a/b/c");
+ assertThat(path.getString(encoder), is("/{" +
encoder.encode(DnaLexicon.Namespace.URI) + "}a/{}b/{}c"));
+ assertThat(path.getString(null, encoder, delimEncoder), is("\\/\\{" +
encoder.encode(DnaLexicon.Namespace.URI)
+ +
"\\}a\\/\\{\\}b\\/\\{\\}c"));
+ assertThat(path.getString(namespaceRegistry), is("/dna:a/b/c"));
+ assertThat(path.getString(namespaceRegistry, encoder),
is("/dna:a/b/c"));
+ assertThat(path.getString(namespaceRegistry, encoder, delimEncoder),
is("\\/dna\\:a\\/b\\/c"));
+ }
}