From 41c629cd585a0664c8e84569b68457cb8586c31c Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Sun, 31 Jan 2021 04:00:49 +0100 Subject: [PATCH] Fix issues with Windows Path handling (#178) --- .../verify.groovy | 4 +-- .../AbstractJavaGeneratorMojo.java | 10 ++++--- .../jaxb2/shared/FileSystemUtilities.java | 28 +++++++++++++------ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/it/xjc-handles-spaces-in-filenames/verify.groovy b/src/it/xjc-handles-spaces-in-filenames/verify.groovy index 21706f68..b42861cb 100644 --- a/src/it/xjc-handles-spaces-in-filenames/verify.groovy +++ b/src/it/xjc-handles-spaces-in-filenames/verify.groovy @@ -57,7 +57,7 @@ File expectedObjectFactoryFile = new File(basedir, pathToLeafPackage + "ObjectFa | [7]: E:\Mojohaus\jaxb2-maven-plugin\target\it\xjc-handles-spaces-in-filenames\target\generated-sources\jaxb\META-INF\sun-jaxb.episode | [8]: -b | [9]: E:\Mojohaus\jaxb2-maven-plugin\target\it\xjc-handles-spaces-in-filenames\src\main\xjb\spaced filename.xjb -| [10]: /E:/Mojohaus/jaxb2-maven-plugin/target/it/xjc-handles-spaces-in-filenames/src/main/xsd/address.xsd +| [10]: E:\Mojohaus\jaxb2-maven-plugin\target\it\xjc-handles-spaces-in-filenames\src\main\xsd\address.xsd | +=================== [End 11 XJC Arguments] */ @@ -66,7 +66,7 @@ def xjcArgumentPatternPrefix = "\\| \\[\\p{Digit}+\\]: "; Pattern expectedBArgumentPattern = Pattern.compile(xjcArgumentPatternPrefix + "\\-b"); Pattern expectedXjbArgumentPattern = Pattern.compile(xjcArgumentPatternPrefix + ".*src/main/xjb/spaced filename.xjb".replace("/", sep)); -Pattern expectedSourceArgumentPattern = Pattern.compile(xjcArgumentPatternPrefix + ".*src/main/xsd/address.xsd"); +Pattern expectedSourceArgumentPattern = Pattern.compile(xjcArgumentPatternPrefix + ".*src(/|\\\\)main(/|\\\\)xsd(/|\\\\)address.xsd"); boolean foundBArgument = false; boolean foundXjbArgument = false; diff --git a/src/main/java/org/codehaus/mojo/jaxb2/javageneration/AbstractJavaGeneratorMojo.java b/src/main/java/org/codehaus/mojo/jaxb2/javageneration/AbstractJavaGeneratorMojo.java index f3a45452..632a6d40 100644 --- a/src/main/java/org/codehaus/mojo/jaxb2/javageneration/AbstractJavaGeneratorMojo.java +++ b/src/main/java/org/codehaus/mojo/jaxb2/javageneration/AbstractJavaGeneratorMojo.java @@ -42,6 +42,7 @@ import java.io.File; import java.io.FileWriter; import java.net.HttpURLConnection; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; @@ -661,10 +662,11 @@ private String[] getXjcArguments(final String classPath, final String episodeFil // Shorten the argument if possible. if ("file".equalsIgnoreCase(current.getProtocol())) { - unwrappedSourceXSDs.add(FileSystemUtilities.relativize( - current.getPath(), - new File(System.getProperty("user.dir")), - true)); + try { + unwrappedSourceXSDs.add(new File(current.toURI()).getPath()); + } catch (final URISyntaxException e) { + throw new MojoExecutionException(e.getMessage(), e); + } } else { unwrappedSourceXSDs.add(current.toString()); } diff --git a/src/main/java/org/codehaus/mojo/jaxb2/shared/FileSystemUtilities.java b/src/main/java/org/codehaus/mojo/jaxb2/shared/FileSystemUtilities.java index 4ee12c91..e58333b9 100644 --- a/src/main/java/org/codehaus/mojo/jaxb2/shared/FileSystemUtilities.java +++ b/src/main/java/org/codehaus/mojo/jaxb2/shared/FileSystemUtilities.java @@ -34,6 +34,8 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -535,18 +537,26 @@ public static String relativize(final String path, Validate.notNull(path, "path"); Validate.notNull(parentDir, "parentDir"); - final String basedirPath = FileSystemUtilities.getCanonicalPath(parentDir); - String toReturn = path; + final Path p = Paths.get(path); + final Path pd = parentDir.toPath(); - // Compare case insensitive - if (path.toLowerCase().startsWith(basedirPath.toLowerCase())) { - toReturn = path.substring(basedirPath.length()); + String platformSpecificPath; + if (p.normalize().startsWith(pd.normalize().toString())) { + platformSpecificPath = pd.relativize(p).toString(); + } else { + platformSpecificPath = p.toString(); } - // Handle whitespace in the argument. - return removeInitialFileSep && toReturn.startsWith(File.separator) - ? toReturn.substring(File.separator.length()) - : toReturn; + if (removeInitialFileSep && platformSpecificPath.startsWith(File.separator)) { + platformSpecificPath = platformSpecificPath.substring(File.separator.length()); + } + + // NOTE: it appears this function is meant to preserve the file separator that was passed in the path + if (path.indexOf('\\') == -1) { + platformSpecificPath = platformSpecificPath.replace('\\', '/'); + } + + return platformSpecificPath; } /**