Skip to content

Commit

Permalink
Support paths of non-default file systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
joehni committed Feb 13, 2016
1 parent 914c07e commit b43dfe8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
*/
package com.thoughtworks.xstream.converters.extended;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;


/**
* Converts a {@link Path} to string.
*
* @author Aaron Johnson
* @author Jörg Schaible
*/
public class PathConverter extends AbstractSingleValueConverter {

Expand All @@ -30,12 +35,25 @@ public boolean canConvert(final Class<?> type) {

@Override
public Object fromString(final String str) {
return Paths.get(str);
try {
final URI uri = new URI(str);
if (uri.getScheme() == null) {
return Paths.get(str);
} else {
return Paths.get(uri);
}
} catch (final URISyntaxException e) {
throw new ConversionException(e);
}
}

/** The Path.toString() method returns the path as a string already. */
@Override
public String toString(final Object obj) {
return obj.toString();
final Path path = (Path)obj;
if (path.getFileSystem() == FileSystems.getDefault()) {
return path.toString();
} else {
return path.toUri().toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
*/
package com.thoughtworks.acceptance;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import com.thoughtworks.xstream.XStream;

Expand All @@ -23,16 +29,36 @@
public class Extended17TypesTest extends AbstractAcceptanceTest {

@Override
protected void setupSecurity(XStream xstream) {
protected void setupSecurity(final XStream xstream) {
super.setupSecurity(xstream);
xstream.allowTypeHierarchy(Path.class);
}

/**
* @author Aaron Johnson
*/
public void testPath() {
public void testPathOfDefaultFileSystem() {
assertBothWays(Paths.get("../a/relative/path"), "<path>../a/relative/path</path>");
assertBothWays(Paths.get("/an/absolute/path"), "<path>/an/absolute/path</path>");

final String absolutePathName = Paths.get("target").toAbsolutePath().toString();
final URI uri = URI.create("file://" + absolutePathName);
assertBothWays(Paths.get(uri), "<path>" + absolutePathName + "</path>");
}

public void testPathOfNonDefaultFileSystem() throws IOException {
final Map<String, String> env = new HashMap<>();
env.put("create", "true");
final URI uri = URI.create("jar:file://"
+ Paths.get("target/lib/proxytoys-0.2.1.jar").toAbsolutePath().toString());

FileSystem zipfs = null;
try {
zipfs = FileSystems.newFileSystem(uri, env);
final String entry = "/com/thoughtworks/proxy/kit/SimpleReference.class";
final Path path = zipfs.getPath(entry);
assertBothWays(path, "<path>" + uri.toString() + "!" + entry + "</path>");
} finally {
if (zipfs != null) {
zipfs.close();
}
}
}
}

0 comments on commit b43dfe8

Please sign in to comment.