Skip to content

Commit 41c629c

Browse files
authored
Fix issues with Windows Path handling (#178)
1 parent 72a0f9d commit 41c629c

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/it/xjc-handles-spaces-in-filenames/verify.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ File expectedObjectFactoryFile = new File(basedir, pathToLeafPackage + "ObjectFa
5757
| [7]: E:\Mojohaus\jaxb2-maven-plugin\target\it\xjc-handles-spaces-in-filenames\target\generated-sources\jaxb\META-INF\sun-jaxb.episode
5858
| [8]: -b
5959
| [9]: E:\Mojohaus\jaxb2-maven-plugin\target\it\xjc-handles-spaces-in-filenames\src\main\xjb\spaced filename.xjb
60-
| [10]: /E:/Mojohaus/jaxb2-maven-plugin/target/it/xjc-handles-spaces-in-filenames/src/main/xsd/address.xsd
60+
| [10]: E:\Mojohaus\jaxb2-maven-plugin\target\it\xjc-handles-spaces-in-filenames\src\main\xsd\address.xsd
6161
|
6262
+=================== [End 11 XJC Arguments]
6363
*/
@@ -66,7 +66,7 @@ def xjcArgumentPatternPrefix = "\\| \\[\\p{Digit}+\\]: ";
6666
Pattern expectedBArgumentPattern = Pattern.compile(xjcArgumentPatternPrefix + "\\-b");
6767
Pattern expectedXjbArgumentPattern = Pattern.compile(xjcArgumentPatternPrefix
6868
+ ".*src/main/xjb/spaced filename.xjb".replace("/", sep));
69-
Pattern expectedSourceArgumentPattern = Pattern.compile(xjcArgumentPatternPrefix + ".*src/main/xsd/address.xsd");
69+
Pattern expectedSourceArgumentPattern = Pattern.compile(xjcArgumentPatternPrefix + ".*src(/|\\\\)main(/|\\\\)xsd(/|\\\\)address.xsd");
7070

7171
boolean foundBArgument = false;
7272
boolean foundXjbArgument = false;

src/main/java/org/codehaus/mojo/jaxb2/javageneration/AbstractJavaGeneratorMojo.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.File;
4343
import java.io.FileWriter;
4444
import java.net.HttpURLConnection;
45+
import java.net.URISyntaxException;
4546
import java.net.URL;
4647
import java.net.URLConnection;
4748
import java.util.ArrayList;
@@ -661,10 +662,11 @@ private String[] getXjcArguments(final String classPath, final String episodeFil
661662

662663
// Shorten the argument if possible.
663664
if ("file".equalsIgnoreCase(current.getProtocol())) {
664-
unwrappedSourceXSDs.add(FileSystemUtilities.relativize(
665-
current.getPath(),
666-
new File(System.getProperty("user.dir")),
667-
true));
665+
try {
666+
unwrappedSourceXSDs.add(new File(current.toURI()).getPath());
667+
} catch (final URISyntaxException e) {
668+
throw new MojoExecutionException(e.getMessage(), e);
669+
}
668670
} else {
669671
unwrappedSourceXSDs.add(current.toString());
670672
}

src/main/java/org/codehaus/mojo/jaxb2/shared/FileSystemUtilities.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.net.MalformedURLException;
3535
import java.net.URL;
3636
import java.net.URLDecoder;
37+
import java.nio.file.Path;
38+
import java.nio.file.Paths;
3739
import java.util.ArrayList;
3840
import java.util.Collections;
3941
import java.util.List;
@@ -535,18 +537,26 @@ public static String relativize(final String path,
535537
Validate.notNull(path, "path");
536538
Validate.notNull(parentDir, "parentDir");
537539

538-
final String basedirPath = FileSystemUtilities.getCanonicalPath(parentDir);
539-
String toReturn = path;
540+
final Path p = Paths.get(path);
541+
final Path pd = parentDir.toPath();
540542

541-
// Compare case insensitive
542-
if (path.toLowerCase().startsWith(basedirPath.toLowerCase())) {
543-
toReturn = path.substring(basedirPath.length());
543+
String platformSpecificPath;
544+
if (p.normalize().startsWith(pd.normalize().toString())) {
545+
platformSpecificPath = pd.relativize(p).toString();
546+
} else {
547+
platformSpecificPath = p.toString();
544548
}
545549

546-
// Handle whitespace in the argument.
547-
return removeInitialFileSep && toReturn.startsWith(File.separator)
548-
? toReturn.substring(File.separator.length())
549-
: toReturn;
550+
if (removeInitialFileSep && platformSpecificPath.startsWith(File.separator)) {
551+
platformSpecificPath = platformSpecificPath.substring(File.separator.length());
552+
}
553+
554+
// NOTE: it appears this function is meant to preserve the file separator that was passed in the path
555+
if (path.indexOf('\\') == -1) {
556+
platformSpecificPath = platformSpecificPath.replace('\\', '/');
557+
}
558+
559+
return platformSpecificPath;
550560
}
551561

552562
/**

0 commit comments

Comments
 (0)