1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 367482. Patch by Raphael Zulliger with minor modifications.

This commit is contained in:
Sergey Prigogin 2011-12-23 12:08:12 -08:00
parent f8f7295cf1
commit 46e94156ee
2 changed files with 45 additions and 40 deletions

View file

@ -29,4 +29,4 @@ ProcessRunner.argumentsMismatch=Argument type mismatch:
ProcessRunner.error=-->Error: ProcessRunner.error=-->Error:
ProcessRunner.success=-->Success: ProcessRunner.success=-->Success:
ProcessRunner.info=-->Info: ProcessRunner.info=-->Info:
ProcessHelper.fileNotFound=File not found: ProcessHelper.fileNotFound=File not found: ''{0}''

View file

@ -8,6 +8,7 @@
* Contributors: * Contributors:
* Bala Torati (Symbian) - Initial API and implementation * Bala Torati (Symbian) - Initial API and implementation
* Mark Espiritu (VastSystems) - bug 215283 * Mark Espiritu (VastSystems) - bug 215283
* Raphael Zulliger (Indel AG) - [367482] fixed resource leak
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.templateengine.process; package org.eclipse.cdt.core.templateengine.process;
@ -20,7 +21,6 @@ import java.io.InputStreamReader;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.net.URL; import java.net.URL;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -29,9 +29,10 @@ import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import com.ibm.icu.text.MessageFormat;
/** /**
* Acts as Helper class for process the processes i.e., copy, replace and append * Acts as helper class for process the processes i.e., copy, replace and append files.
* files.
*/ */
public class ProcessHelper { public class ProcessHelper {
public static final String CONDITION = "condition"; //$NON-NLS-1$ public static final String CONDITION = "condition"; //$NON-NLS-1$
@ -45,15 +46,15 @@ public class ProcessHelper {
* *
* @param fileContents contents which are appended to the file. * @param fileContents contents which are appended to the file.
* @param toFile a file to append contents. * @param toFile a file to append contents.
* @throws IOException, * @throws IOException exception while writing contents into a file
* exception while writing contents into a file
*
* @since 4.0 * @since 4.0
*/ */
public static void appendFile(String fileContents, File toFile) throws IOException { public static void appendFile(String fileContents, File toFile) throws IOException {
RandomAccessFile raf = null; RandomAccessFile raf = null;
if (!toFile.exists()) { if (!toFile.exists()) {
throw new FileNotFoundException(" The specified destination file does not exists "); //$NON-NLS-1$ throw new FileNotFoundException(MessageFormat.format(
TemplateEngineMessages.getString("ProcessHelper.fileNotFound"), //$NON-NLS-1$
toFile.getPath()));
} else { } else {
try { try {
raf = new RandomAccessFile(toFile, "rw"); //$NON-NLS-1$ raf = new RandomAccessFile(toFile, "rw"); //$NON-NLS-1$
@ -69,7 +70,7 @@ public class ProcessHelper {
/** /**
* This method returns a vector of all replace marker strings. (e.g., * This method returns a vector of all replace marker strings. (e.g.,
* $(item), vector contains 'item' as one item. , ) is the end pattern. * $(item), vector contains 'item' as one item) is the end pattern.
* *
* @param str A given string possibly containing markers. * @param str A given string possibly containing markers.
* @return the set of names occurring within markers * @return the set of names occurring within markers
@ -77,15 +78,17 @@ public class ProcessHelper {
*/ */
public static Set<String> getReplaceKeys(String str) { public static Set<String> getReplaceKeys(String str) {
Set<String> replaceStrings = new HashSet<String>(); Set<String> replaceStrings = new HashSet<String>();
int start= 0, end= 0; int start= 0;
int end= 0;
while ((start = str.indexOf(START_PATTERN, start)) >= 0) { while ((start = str.indexOf(START_PATTERN, start)) >= 0) {
end = str.indexOf(END_PATTERN, start); end = str.indexOf(END_PATTERN, start);
if (end != -1) { if (end != -1) {
replaceStrings.add(str.substring(start + START_PATTERN.length(), end)); replaceStrings.add(str.substring(start + START_PATTERN.length(), end));
start = end + END_PATTERN.length(); start = end + END_PATTERN.length();
} else } else {
start++; start++;
} }
}
return replaceStrings; return replaceStrings;
} }
@ -93,19 +96,18 @@ public class ProcessHelper {
* This method takes a URL as parameter to read the contents, and to add * This method takes a URL as parameter to read the contents, and to add
* into a string buffer. * into a string buffer.
* *
* @param source * @param source URL to read the contents.
* URL to read the contents. * @return string contents of a file specified in the URL source path.
* @return string, contents of a file specified in the URL source path.
* @throws IOException
*
* @since 4.0 * @since 4.0
*/ */
public static String readFromFile(URL source) throws IOException { public static String readFromFile(URL source) throws IOException {
char[] chars = new char[4092]; char[] chars = new char[4092];
InputStreamReader contentsReader = null; InputStreamReader contentsReader = null;
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
if (!new java.io.File(source.getFile()).exists()) { if (!new java.io.File(source.getFile()).exists()) {
throw new FileNotFoundException(TemplateEngineMessages.getString("ProcessHelper.fileNotFound") + source.getFile()); //$NON-NLS-1$ throw new FileNotFoundException(MessageFormat.format(
TemplateEngineMessages.getString("ProcessHelper.fileNotFound"), //$NON-NLS-1$
source.getFile()));
} else { } else {
contentsReader = new InputStreamReader(source.openStream()); contentsReader = new InputStreamReader(source.openStream());
int c; int c;
@ -124,12 +126,8 @@ public class ProcessHelper {
* This method reads contents from source, and writes the contents into * This method reads contents from source, and writes the contents into
* destination file. * destination file.
* *
* @param source * @param source URL to read the contents.
* URL to read the contents. * @param dest destination file to write the contents.
* @param dest
* destination file to write the contents.
* @throws IOException
*
* @since 4.0 * @since 4.0
*/ */
public static void copyBinaryFile(URL source, File dest) throws IOException { public static void copyBinaryFile(URL source, File dest) throws IOException {
@ -137,22 +135,30 @@ public class ProcessHelper {
if (source != null && dest != null) { if (source != null && dest != null) {
File file = new File(source.getFile()); File file = new File(source.getFile());
if (file.isFile()) { if (file.isFile()) {
FileInputStream fis = new FileInputStream(file); FileInputStream in = null;
FileOutputStream fos = new FileOutputStream(dest); FileOutputStream out = null;
int ch; try {
while (true) { in = new FileInputStream(file);
ch = fis.read(bytes); out = new FileOutputStream(dest);
if (ch == -1) { int len;
break; while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
} finally {
try {
if (in != null)
in.close();
} finally {
if (out != null)
out.close();
} }
fos.write(bytes, 0, ch);
} }
} }
} }
} }
/** /**
* This method Creates the Directories in the parent Folder. * This method creates the directories in the parent folder.
* @param projectHandle * @param projectHandle
* @param parentFolder * @param parentFolder
* @throws CoreException * @throws CoreException
@ -169,18 +175,17 @@ public class ProcessHelper {
parentFolder.create(true, true, null); parentFolder.create(true, true, null);
} }
/** /**
* @param string * @param string
* @param macros * @param macros
* @param valueStore * @param valueStore
* @return the Macro Value after expanding the Macros. * @return the macro value after expanding the macros.
* *
* @since 4.0 * @since 4.0
*/ */
public static String getValueAfterExpandingMacros(String string, Set<String> macros, Map<String, String> valueStore) { public static String getValueAfterExpandingMacros(String string, Set<String> macros,
for (Iterator<String> i = macros.iterator(); i.hasNext();) { Map<String, String> valueStore) {
String key = i.next(); for (String key : macros) {
String value = valueStore.get(key); String value = valueStore.get(key);
if (value != null) { if (value != null) {
string = string.replace(START_PATTERN + key + END_PATTERN, value); string = string.replace(START_PATTERN + key + END_PATTERN, value);