Author: tchemit Date: 2010-04-04 11:50:41 +0200 (Sun, 04 Apr 2010) New Revision: 678 Log: add setLastModified and getLinesAsURL in PluginHelper Modified: trunk/src/main/java/org/nuiton/plugin/PluginHelper.java Modified: trunk/src/main/java/org/nuiton/plugin/PluginHelper.java =================================================================== --- trunk/src/main/java/org/nuiton/plugin/PluginHelper.java 2010-03-27 20:04:17 UTC (rev 677) +++ trunk/src/main/java/org/nuiton/plugin/PluginHelper.java 2010-04-04 09:50:41 UTC (rev 678) @@ -27,6 +27,7 @@ import org.codehaus.plexus.util.IOUtil; import java.io.*; +import java.net.URL; import java.text.MessageFormat; import java.util.*; import java.util.Map.Entry; @@ -45,7 +46,10 @@ public static String removeSnapshotSuffix(String versionId) { if (versionId.endsWith(SNAPSHOT_SUFFIX)) { // remove snapshot suffix - versionId = versionId.substring(0, versionId.length() - SNAPSHOT_SUFFIX.length()); + versionId = versionId.substring( + 0, + versionId.length() - SNAPSHOT_SUFFIX.length() + ); } return versionId; } @@ -63,25 +67,32 @@ * @param list la liste a convertir * @param type le type des elements de la liste * @return la liste typee - * @throws IllegalArgumentException si un element de la liste en entree n'est - * pas en adequation avec le type voulue. + * @throws IllegalArgumentException si un element de la liste en entree + * n'est pas en adequation avec le type + * voulue. */ @SuppressWarnings({"unchecked"}) - static public <O> List<O> toGenericList(List<?> list, Class<O> type) throws IllegalArgumentException { + static public <O> List<O> toGenericList( + List<?> list, Class<O> type) throws IllegalArgumentException { if (list.isEmpty()) { return (List<O>) list; } for (Object o : list) { - if (!(type.isAssignableFrom(o.getClass()))) { - throw new IllegalArgumentException("can not cast List with object of type " + o.getClass() + " to " + type + " type!"); + if (!type.isAssignableFrom(o.getClass())) { + throw new IllegalArgumentException( + "can not cast List with object of type " + + o.getClass() + " to " + type + " type!"); } } return (List<O>) list; } static final protected double[] timeFactors = {1000000, 1000, 60, 60, 24}; + static final protected String[] timeUnites = {"ns", "ms", "s", "m", "h", "d"}; + static final protected double[] memoryFactors = {1024, 1024, 1024, 1024}; + static final protected String[] memoryUnites = {"o", "Ko", "Mo", "Go", "To"}; static public String convertMemory(long value) { @@ -96,7 +107,9 @@ return convertTime(value2 - value); } - static public String convert(long value, double[] factors, String[] unites) { + static public String convert(long value, + double[] factors, + String[] unites) { long sign = value == 0 ? 1 : value / Math.abs(value); int i = 0; double tmp = Math.abs(value); @@ -107,13 +120,13 @@ tmp *= sign; String result; result = MessageFormat.format("{0,number,0.###}{1}", tmp, - unites[i]); + unites[i]); return result; } /** - * Split the given {@code text} using the {@code separator} and trim - * each part of result. + * Split the given {@code text} using the {@code separator} and trim each + * part of result. * * @param txt text to split * @param separator the split separator @@ -137,10 +150,12 @@ * @return the text transformed * @throws IOException if any reading problem */ - static public String prefixLines(String prefix, String prefixForEmpty, String content) throws IOException { + static public String prefixLines(String prefix, + String prefixForEmpty, + String content) throws IOException { BufferedReader reader = null; + reader = new BufferedReader(new StringReader(content)); try { - reader = new BufferedReader(new java.io.StringReader(content)); StringBuilder sb = new StringBuilder(); String line = reader.readLine(); @@ -161,15 +176,13 @@ String result = sb.toString(); return result; } finally { - if (reader != null) { - reader.close(); - } + reader.close(); } } /** - * Obtenir les clefs de toutes les valeurs nulles ou vide a partir - * d'un dictionnaire donne. + * Obtenir les clefs de toutes les valeurs nulles ou vide a partir d'un + * dictionnaire donne. * * @param map le dictionner a parcourir * @return la liste des clefs dont la valeur est nulle ou vide @@ -193,7 +206,9 @@ * @return {@code true} if the resources was added (not already existing) * @since 1.1.1 */ - public static boolean addResourceDir(File dir, MavenProject project, String... includes) { + public static boolean addResourceDir(File dir, + MavenProject project, + String... includes) { List<?> resources = project.getResources(); boolean added = addResourceDir(dir, project, resources, includes); return added; @@ -208,7 +223,9 @@ * @return {@code true} if the resources was added (not already existing) * @since 1.1.1 */ - public static boolean addTestResourceDir(File dir, MavenProject project, String... includes) { + public static boolean addTestResourceDir(File dir, + MavenProject project, + String... includes) { List<?> resources = project.getTestResources(); boolean added = addResourceDir(dir, project, resources, includes); return added; @@ -224,7 +241,10 @@ * @return {@code true} if the resource was added (not already existing) * @since 1.1.1 */ - public static boolean addResourceDir(File dir, MavenProject project, List<?> resources, String... includes) { + public static boolean addResourceDir(File dir, + MavenProject project, + List<?> resources, + String... includes) { String newresourceDir = dir.getAbsolutePath(); boolean shouldAdd = true; for (Object o : resources) { @@ -258,10 +278,12 @@ * @param newresourceDir the new resource directory to add * @param project the maven project to modifiy * @return {@code true} if resources was added - * @deprecated since 1.1.1, prefer use the {@link #addResourceDir(java.io.File, org.apache.maven.project.MavenProject, String...)} + * @deprecated since 1.1.1, prefer use the {@link #addResourceDir(File, + * MavenProject, String...)} */ @Deprecated - public static boolean addResourceDir(String newresourceDir, MavenProject project) { + public static boolean addResourceDir(String newresourceDir, + MavenProject project) { List<?> resources = project.getResources(); boolean added = addResourceDir(newresourceDir, project, resources); return added; @@ -271,10 +293,12 @@ * @param newresourceDir the new resource directory to add * @param project the maven project to modifiy * @return {@code true} if resources was added - * @deprecated since 1.1.1, prefer use the {@link #addTestResourceDir(java.io.File, org.apache.maven.project.MavenProject, String...)} + * @deprecated since 1.1.1, prefer use the {@link #addTestResourceDir(File, + * MavenProject, String...)} */ @Deprecated - public static boolean addTestResourceDir(String newresourceDir, MavenProject project) { + public static boolean addTestResourceDir(String newresourceDir, + MavenProject project) { List<?> resources = project.getTestResources(); boolean added = addResourceDir(newresourceDir, project, resources); return added; @@ -285,10 +309,13 @@ * @param project the maven project to modifiy * @param resources the known resources for the maven project * @return {@code true} if resources was added - * @deprecated since 1.1.1, prefer use the {@link #addResourceDir(java.io.File, org.apache.maven.project.MavenProject, java.util.List, String...)} + * @deprecated since 1.1.1, prefer use the {@link #addResourceDir(File, + * MavenProject, List, String...)} */ @Deprecated - public static boolean addResourceDir(String newresourceDir, MavenProject project, List<?> resources) { + public static boolean addResourceDir(String newresourceDir, + MavenProject project, + List<?> resources) { boolean shouldAdd = true; for (Object o : resources) { Resource r = (Resource) o; @@ -314,7 +341,8 @@ * Cretae the directory (and his parents) if necessary. * * @param dir the directory to create if not exisiting - * @return {@code true} if directory was created, {@code false} if was no need to create it + * @return {@code true} if directory was created, {@code false} if was no + * need to create it * @throws IOException if could not create directory * @since 1.1.1 */ @@ -370,19 +398,31 @@ * @throws IOException if could not rename the file * @since 1.2.0 */ - public static void renameFile(File file, File destination) throws IOException { + public static void renameFile(File file, + File destination) throws IOException { boolean b = file.renameTo(destination); if (!b) { - throw new IOException("could not rename " + file + " to " + destination); + throw new IOException( + "could not rename " + file + " to " + destination); } } + public static void setLastModified(File file, + long lastModified) throws IOException { + boolean b = file.setLastModified(lastModified); + if (!b) { + throw new IOException( + "could not changed lastModified [" + lastModified + + "] for " + file); + } + } + /** * Permet de copier le fichier source vers le fichier cible. * * @param source le fichier source * @param target le fichier cible - * @throws java.io.IOException Erreur de copie + * @throws IOException Erreur de copie */ public static void copy(File source, File target) throws IOException { createDirectoryIfNecessary(target.getParentFile()); @@ -398,9 +438,11 @@ * @return the content of the file * @throws IOException if IO pb */ - static public String readAsString(File file, String encoding) throws IOException { + static public String readAsString(File file, + String encoding) throws IOException { FileInputStream inf = new FileInputStream(file); - BufferedReader in = new BufferedReader(new InputStreamReader(inf, encoding)); + BufferedReader in = new BufferedReader( + new InputStreamReader(inf, encoding)); try { return IOUtil.toString(in); } finally { @@ -416,7 +458,7 @@ * @return the content of the file * @throws IOException if IO pb */ - static public String readAsString(java.io.Reader reader) throws IOException { + static public String readAsString(Reader reader) throws IOException { return IOUtil.toString(reader); } @@ -428,20 +470,22 @@ * @param encoding l'encoding d'ecriture * @throws IOException if IO pb */ - static public void writeString(File file, String content, String encoding) throws IOException { + static public void writeString(File file, String content, + String encoding) throws IOException { createDirectoryIfNecessary(file.getParentFile()); BufferedWriter out = null; + out = new BufferedWriter(new OutputStreamWriter( + new FileOutputStream(file), encoding)); try { - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)); IOUtil.copy(content, out); } finally { - if (out != null) { - out.close(); - } + out.close(); } } - public static List<File> getIncludedFiles(File dir, String[] includes, String[] excludes) { + public static List<File> getIncludedFiles(File dir, + String[] includes, + String[] excludes) { DirectoryScanner ds = new DirectoryScanner(); List<File> result = new ArrayList<File>(); ds.setBasedir(dir); @@ -461,14 +505,20 @@ return result; } - public static void copyFiles(File src, File dst, String[] includes, String[] excludes, boolean overwrite) throws IOException { + public static void copyFiles(File src, File dst, + String[] includes, + String[] excludes, + boolean overwrite) throws IOException { PluginIOContext c = new PluginIOContext(); c.setInput(src); c.setOutput(dst); copyFiles(c, includes, excludes, overwrite); } - public static void copyFiles(PluginIOContext p, String[] includes, String[] excludes, boolean overwrite) throws IOException { + public static void copyFiles(PluginIOContext p, + String[] includes, + String[] excludes, + boolean overwrite) throws IOException { DirectoryScanner ds = new DirectoryScanner(); @@ -495,7 +545,11 @@ } } - public static void expandFiles(PluginIOContext p, String[] includes, String[] excludes, String[] zipIncludes, boolean overwrite) throws IOException { + public static void expandFiles(PluginIOContext p, + String[] includes, + String[] excludes, + String[] zipIncludes, + boolean overwrite) throws IOException { DirectoryScanner ds = new DirectoryScanner(); @@ -528,11 +582,15 @@ } public static String getRelativePath(File base, File file) { - String result = file.getAbsolutePath().substring(base.getAbsolutePath().length() + 1); + String result = file.getAbsolutePath().substring( + base.getAbsolutePath().length() + 1); return result; } - public static void expandFile(File src, File dst, String[] includes, boolean overwrite) throws IOException { + public static void expandFile(File src, + File dst, + String[] includes, + boolean overwrite) throws IOException { ZipFile zipFile = new ZipFile(src); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { @@ -561,12 +619,18 @@ */ public static String[] getLines(File src) throws IOException { List<String> result = new ArrayList<String>(); - BufferedReader stream = new BufferedReader(new InputStreamReader(new FileInputStream(src))); - while (stream.ready()) { - String line = stream.readLine().trim(); - if (!line.isEmpty()) { - result.add(line); + BufferedReader stream = new BufferedReader( + new InputStreamReader(new FileInputStream(src))); + + try { + while (stream.ready()) { + String line = stream.readLine().trim(); + if (!line.isEmpty()) { + result.add(line); + } } + } finally { + stream.close(); } return result.toArray(new String[result.size()]); } @@ -579,9 +643,25 @@ public static File[] getLinesAsFiles(File src) throws IOException { List<File> result = new ArrayList<File>(); for (String line : getLines(src)) { - if (!line.isEmpty()) + if (!line.isEmpty()) { result.add(new File(line)); + } } return result.toArray(new File[result.size()]); } + + /** + * @param src the source file to read + * @return the url instanciated from lines of the source file. + * @throws IOException if any pb while reading file + */ + public static URL[] getLinesAsURL(File src) throws IOException { + List<URL> result = new ArrayList<URL>(); + for (String line : getLines(src)) { + if (!line.isEmpty()) { + result.add(new URL(line)); + } + } + return result.toArray(new URL[result.size()]); + } }
participants (1)
-
tchemit@users.nuiton.org