Skip to content

Package: FileUtil$1

FileUtil$1

nameinstructionbranchcomplexitylinemethod
accept(File)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.ws.test.util;
12:
13: import com.sun.xml.ws.test.World;
14: import org.apache.tools.ant.taskdefs.Copy;
15: import org.apache.tools.ant.taskdefs.Delete;
16: import org.apache.tools.ant.types.FileSet;
17:
18: import java.io.File;
19: import java.io.FileFilter;
20: import java.io.IOException;
21: import java.util.ArrayList;
22: import java.util.List;
23: import java.util.Stack;
24:
25: /**
26: * Class for utility methods for finding files and
27: * information about them.
28: */
29: public class FileUtil {
30:
31: public static final FileFilter DIRECTORY_FILTER = new FileFilter() {
32: public boolean accept(File path) {
33: return path.isDirectory();
34: }
35: };
36:
37: public static final FileFilter JAR_FILE_FILTER = new FileFilter() {
38: public boolean accept(File path) {
39: return path.getName().endsWith(".jar");
40: }
41: };
42:
43: /**
44: * This method returns the fully qualified names of
45: * class files below the given directory.
46: */
47: public static String [] getClassFileNames(File dir) {
48: List<String> names = new ArrayList<String>();
49: Stack<String> pathStack = new Stack<String>();
50: addClassNames(dir, pathStack, names);
51: return names.toArray(new String [names.size()]);
52: }
53:
54: /**
55: * Recursive method for adding class names under
56: * a given top directory.
57: */
58: private static void addClassNames(File current, Stack<String> stack,
59: List<String> names) {
60:
61: File[] children = current.listFiles();
62: if (children == null) {
63: return;
64: }
65: for (File child : children) {
66: if (child.isDirectory()) {
67: stack.push(child.getName());
68: addClassNames(child, stack, names);
69: stack.pop();
70: } else {
71: if (child.getName().endsWith(".class")) {
72: names.add(createFullName(stack, child));
73: }
74: }
75: }
76: }
77:
78: /*
79: * Create a fully-qualified path name.
80: */
81: private static String createFullName(Stack<String> dirs, File classFile) {
82: String className = classFile.getName().substring(
83: 0, classFile.getName().indexOf(".class"));
84: if (dirs.empty()) {
85: return className;
86: }
87: StringBuilder fullName = new StringBuilder();
88: for (String dir : dirs) {
89: fullName.append(dir);
90: fullName.append(".");
91: }
92: fullName.append(className);
93: return fullName.toString();
94: }
95:
96: /**
97: * Recursively delete a directory and all its descendants.
98: */
99: public static void deleteRecursive(File dir) {
100: Delete d = new Delete();
101: d.setProject(World.project);
102: d.setDir(dir);
103: d.execute();
104: }
105:
106: /**
107: * Copies a single file.
108: */
109: public static void copyFile(File src, File dest) {
110: Copy cp = new Copy();
111: cp.setOverwrite(true);
112: cp.setProject(World.project);
113: cp.setFile(src);
114: cp.setTofile(dest);
115: cp.execute();
116: }
117:
118: /**
119: * Copies a whole directory recursively.
120: */
121: public static void copyDir(File src, File dest, String excludes) {
122: Copy cp = new Copy();
123: cp.setProject(World.project);
124: cp.setTodir(dest);
125: FileSet fs = new FileSet();
126: if (excludes != null) {
127: fs.setExcludes(excludes);
128: }
129: fs.setDir(src);
130: cp.addFileset(fs);
131: cp.execute();
132: }
133:
134: public static File createTmpDir( boolean scheduleDeleteOnVmExit ) throws IOException {
135: // create a temporary directory
136: File dir = new File(".");
137: File targetFolder = new File(dir, "target");
138: if (targetFolder.exists()) {
139: dir = targetFolder;
140: }
141: File tmpFile = File.createTempFile("wstest", "tmp", dir);
142: tmpFile.delete();
143: tmpFile.mkdir();
144: if(scheduleDeleteOnVmExit) {
145: tmpFile.deleteOnExit();
146: }
147: return tmpFile;
148: }
149: }