forked from srizzo/code2code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.java
More file actions
125 lines (108 loc) · 2.85 KB
/
Copy pathFileUtils.java
File metadata and controls
125 lines (108 loc) · 2.85 KB
1
package code2code.utils;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.LinkedHashMap;import java.util.Map;import java.util.Map.Entry;import org.eclipse.core.resources.IContainer;import org.eclipse.core.resources.IFile;import org.eclipse.core.resources.IFolder;import org.eclipse.core.runtime.CoreException;/** * Utils to help with file processing */public final class FileUtils{ /** * TODO: Eli: Why is this reading lines? Should we just consume the stream as is? * * Create a string from the provided InputStream * @param p_stream * @return * the created string * @throws Exception */ public static String toString(InputStream p_stream) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(p_stream)); try { StringBuilder result = new StringBuilder(); String line = reader.readLine(); while (line != null) { result.append(line); line = reader.readLine(); if (line != null) { result.append(System.getProperty("line.separator")); } } return result.toString(); } finally { reader.close(); } } /** * Load a properties file as a map * @param p_input * @return * the properties map * @throws Exception */ public static Map<String, String> loadProperties(InputStream p_input) throws Exception { try { Properties paramsProperties = new Properties(); paramsProperties.load(p_input); Map<String, String> params = new LinkedHashMap<String, String>(); for (Entry<Object, Object> entry : paramsProperties.entrySet()) { params.put((String) entry.getKey(), (String) entry.getValue()); } return params; } finally { p_input.close(); } } /** * Create a folder and its parents * @param folder * @throws CoreException */ public static void createFolderWithParents(IFolder folder) throws CoreException { if (!folder.getParent().exists()) { IFolder parent = (IFolder) folder.getParent(); createFolderWithParents(parent); } if (!folder.exists()) { folder.create(false, true, null); } } /** * Create the parent folders for a file * @param file * @throws Exception */ public static void createParentFolders(IFile file) throws Exception { IContainer parent = file.getParent(); if (parent instanceof IFolder) { IFolder parentFolder = (IFolder) file.getParent(); if (!parentFolder.exists()) { FileUtils.createFolderWithParents(parentFolder); } } }}