Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/com/twcable/jackalope/impl/common/Paths.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,24 @@ public static String resolve(String first, String second) {
private static String stripRoot(String path) {
return isAbsolute(path) ? path.substring(1) : path;
}

/**
* Strip a trailing '/' on a path.
*
* Items saved in the JCR can be accessed as both /some/path and
* /some/path/ but the standard output is w/o the trailing '/'.
*
* Note that the root path '/' is preserved as '/'.
*
* @param path The path to be modified
* @return The path without a trailing '/' if one is found.
*/
public static String stripTrailingSeparator(String path) {
if (path != null && !path.equals(SEPARATOR)) {
if (path.lastIndexOf(SEPARATOR) == (path.length() - 1)) {
return path.substring(0, path.length() - 1);
}
}
return path;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/twcable/jackalope/impl/jcr/ItemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public abstract class ItemImpl implements Item {
*/
ItemImpl(@Nonnull SessionImpl session, @Nonnull String path) throws ItemNotFoundException, ItemExistsException {
this.session = session;
this.path = path;
this.path = Paths.stripTrailingSeparator(path);
session.addItem(this);
}

Expand All @@ -67,7 +67,7 @@ public String getPath() {


void setPath(String path) {
this.path = path;
this.path = Paths.stripTrailingSeparator(path);
}


Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/twcable/jackalope/impl/jcr/SessionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,19 @@ public Property getProperty(String absPath) throws PathNotFoundException {

@Override
public boolean itemExists(String absPath) {
return itemStore.containsKey(absPath);
return itemStore.containsKey(Paths.stripTrailingSeparator(absPath));
}


@Override
public boolean nodeExists(String absPath) {
return itemExists(absPath) && itemStore.get(absPath).isNode();
return itemExists(absPath) && itemStore.get(Paths.stripTrailingSeparator(absPath)).isNode();
}


@Override
public boolean propertyExists(String absPath) {
return itemExists(absPath) && !itemStore.get(absPath).isNode();
return itemExists(absPath) && !itemStore.get(Paths.stripTrailingSeparator(absPath)).isNode();
}


Expand Down Expand Up @@ -353,7 +353,7 @@ Item removeItem(@Nonnull ItemImpl item) {


private ItemImpl getItemImpl(String absPath) {
return itemStore.get(absPath);
return itemStore.get(Paths.stripTrailingSeparator(absPath));
}


Expand All @@ -364,6 +364,9 @@ private Item storeItem(@Nonnull ItemImpl item) throws ItemNotFoundException {


private void moveItem(String src, String dest) {
src = Paths.stripTrailingSeparator(src);
dest = Paths.stripTrailingSeparator(dest);

ItemImpl item = itemStore.get(src);
item.setPath(dest);
itemStore.put(dest, item);
Expand Down
16 changes: 16 additions & 0 deletions src/test/groovy/com/twcable/jackalope/impl/common/PathsSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,20 @@ class PathsSpec extends Specification {
"/segment" | "next" | "/segment/next"
"/segment" | "/next" | "/next"
}

def "strip trailing separator"() {
expect:
Paths.stripTrailingSeparator(path) == expected

where:
path | expected
null | null
"/" | "/"
"/segment" | "/segment"
"/segment/" | "/segment"
"/segment/next" | "/segment/next"
"/segment/next/" | "/segment/next"


}
}