1 package de.matthias_burbach.mosaique4eclipse.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.core.resources.IResource;
10 import org.eclipse.core.resources.IWorkspace;
11 import org.eclipse.core.resources.ResourcesPlugin;
12 import org.eclipse.core.runtime.IPath;
13 import org.eclipse.core.runtime.Path;
14
15 /***
16 * Holds various low level helper methods.
17 *
18 * @author Matthias Burbach
19 */
20 public final class Util {
21 /***
22 * Private default constructor to enforce static usage of this utility
23 * class.
24 */
25 private Util() {
26
27 }
28
29 /***
30 * @param file The file to convert.
31 * @return The converted IFile object.
32 */
33 public static IFile fileToIFile(final File file) {
34 IFile result = null;
35 if (file != null) {
36 IWorkspace workspace = ResourcesPlugin.getWorkspace();
37 IPath path = new Path(file.getAbsolutePath());
38 result = workspace.getRoot().getFileForLocation(path);
39 }
40 return result;
41 }
42
43 /***
44 * @param iFile The IFile object to convert.
45 * @return The converted file.
46 */
47 public static File iFiletoFile(final IFile iFile) {
48 File result = null;
49 if (iFile != null) {
50 result = iFile.getLocation().toFile();
51 }
52 return result;
53 }
54
55 /***
56 * @param path The filesystem path of a file to test for existence in any of
57 * the currently open Eclipse projects in the user's workspace.
58 * @return <code>true</code> if the file was found in the workspace.
59 */
60 public static boolean existsInOpenProject(final String path) {
61 boolean result = false;
62 if (path != null) {
63 File file = new File(path);
64 if (file != null) {
65 IFile iFile = fileToIFile(file);
66 if (iFile != null) {
67 result = iFile.exists();
68 }
69 }
70 }
71 return result;
72 }
73
74 /***
75 * @param iPath The IPath path of a file to test for existence in any of
76 * the currently open Eclipse projects in the user's workspace.
77 * @return <code>true</code> if the file was found in the workspace.
78 */
79 public static boolean existsInOpenProject(final IPath iPath) {
80 boolean result = false;
81 if (iPath != null) {
82 String path = iPathToAbsolutePath(iPath);
83 result = existsInOpenProject(path);
84 }
85 return result;
86 }
87
88 /***
89 * @param iPath The IPath path to convert to an absolute filesystem path.
90 * @return The operating system specific absolute filesystem path or
91 * <code>null</code> if the path was not found in the workspace.
92 */
93 public static String iPathToAbsolutePath(final IPath iPath) {
94 IWorkspace workspace = ResourcesPlugin.getWorkspace();
95 IResource iResource = workspace.getRoot().findMember(iPath);
96 String result = null;
97 if (iResource != null) {
98 IPath location = iResource.getLocation();
99 if (location != null) {
100 result = location.toOSString();
101 }
102 }
103 return result;
104 }
105
106 /***
107 * @param obj An object to test whether it represents an IFile which is
108 * a Struts configuration file
109 * (name starts with 'struts-config').
110 * @return <code>true</code> if the object is recognized to be a Struts
111 * config.
112 */
113 public static boolean isStrutsConfigFile(final Object obj) {
114 boolean result = false;
115 if (obj instanceof IFile) {
116 IFile file = (IFile) obj;
117 if (file.getName().startsWith("struts-config")) {
118 result = true;
119 }
120 }
121 return result;
122 }
123
124 /***
125 * Copies from source to destination.
126 *
127 * @param source The source to copy bytes from.
128 * Must not be <code>null</code>.
129 * @param destination The destination to copy bytes to.
130 * Must not be <code>null</code>.
131 * @throws IOException if anything goes wrong
132 */
133 public static void transferStreams(
134 final InputStream source,
135 final OutputStream destination)
136 throws IOException {
137 try {
138 final int bufferSize = 8192;
139 byte[] buffer = new byte[bufferSize];
140 while (true) {
141 int bytesRead = source.read(buffer);
142 if (bytesRead == -1) {
143 break;
144 }
145 destination.write(buffer, 0, bytesRead);
146 }
147 } finally {
148 try {
149 if (source != null) {
150 source.close();
151 }
152 } catch (IOException e) {
153 e.printStackTrace();
154 }
155 try {
156 if (destination != null) {
157 destination.close();
158 }
159 } catch (IOException e) {
160 e.printStackTrace();
161 }
162 }
163 }
164 }