diff --git a/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java b/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java index 8dd2bb0d729..12ed3327fad 100644 --- a/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java +++ b/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java @@ -21,9 +21,9 @@ import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -79,9 +79,8 @@ public void compile(ToolContext context) throws ToolException { String dirName = fileName.substring(0, fileName.lastIndexOf(File.separator) + 1); String path = outPutDir + File.separator + dirName; - if (!dirSet.contains(path)) { + if (dirSet.add(path)) { - dirSet.add(path); File file = new File(path); if (file.isDirectory() && file.list() != null) { for (String str : file.list()) { @@ -89,17 +88,16 @@ public void compile(ToolContext context) throws ToolException { fileList.add(path + str); } else { // copy generated xml file or others to class directory - File otherFile = new File(path + File.separator + str); + Path otherFile = Paths.get(path, str); String suffix = "xml"; - if (otherFile.isFile() + if (Files.isReadable(otherFile) && str.regionMatches(true, str.length() - suffix.length(), suffix, 0, suffix.length()) && context.get(ToolConstants.CFG_CLASSDIR) != null) { String targetDir = (String)context.get(ToolConstants.CFG_CLASSDIR); - File targetFile = new File(targetDir + File.separator + dirName - + File.separator + str); try { - copyXmlFile(otherFile, targetFile); + Files.copy(otherFile, + Files.createDirectories(Paths.get(targetDir, dirName)).resolve(str)); } catch (IOException e) { Message msg = new Message("FAIL_TO_COPY_GENERATED_RESOURCE_FILE", LOG); @@ -126,26 +124,4 @@ public void compile(ToolContext context) throws ToolException { } } - private void copyXmlFile(File from, File to) throws ToolException, IOException { - - String dir = to.getCanonicalPath() - .substring(0, to.getCanonicalPath().lastIndexOf(File.separator)); - File dirFile = new File(dir); - dirFile.mkdirs(); - try (InputStream input = Files.newInputStream(from.toPath()); - OutputStream output = Files.newOutputStream(to.toPath())) { - byte[] b = new byte[1024 * 3]; - int len = 0; - while (len != -1) { - len = input.read(b); - if (len != -1) { - output.write(b, 0, len); - } - } - output.flush(); - } catch (Exception e) { - Message msg = new Message("FAIL_TO_COPY_GENERATED_RESOURCE_FILE", LOG); - throw new ToolException(msg, e); - } - } } diff --git a/tools/common/src/test/java/org/apache/cxf/tools/common/ProcessorTestBase.java b/tools/common/src/test/java/org/apache/cxf/tools/common/ProcessorTestBase.java index a8e4a7e896e..77d8b6b645f 100644 --- a/tools/common/src/test/java/org/apache/cxf/tools/common/ProcessorTestBase.java +++ b/tools/common/src/test/java/org/apache/cxf/tools/common/ProcessorTestBase.java @@ -58,12 +58,12 @@ public class ProcessorTestBase { - public static final List DEFAULT_IGNORE_ATTR = Arrays.asList(new String[]{"attributeFormDefault", - "elementFormDefault", - "form", - "version", - "part@name"}); - public static final List DEFAULT_IGNORE_TAG = Arrays.asList(new String[]{"sequence"}); + public static final List DEFAULT_IGNORE_ATTR = Arrays.asList("attributeFormDefault", + "elementFormDefault", + "form", + "version", + "part@name"); + public static final List DEFAULT_IGNORE_TAG = Arrays.asList("sequence"); //CHECKSTYLE:OFF @Rule