Author: tchemit Date: 2009-08-29 16:18:05 +0200 (Sat, 29 Aug 2009) New Revision: 557 Modified: trunk/src/main/java/org/nuiton/util/PluginHelper.java Log: adding usefull method to prefix lines of a text Modified: trunk/src/main/java/org/nuiton/util/PluginHelper.java =================================================================== --- trunk/src/main/java/org/nuiton/util/PluginHelper.java 2009-08-29 11:08:34 UTC (rev 556) +++ trunk/src/main/java/org/nuiton/util/PluginHelper.java 2009-08-29 14:18:05 UTC (rev 557) @@ -113,6 +113,45 @@ } /** + * Prefix the lines of the given content with a given prefix. + * + * @param prefix prefix to add on each line of text + * @param prefixForEmpty prefix to add for empty lines + * @param content the text to treate + * @return the text transformed + * @throws IOException if any reading problem + */ + static public String prefixLines(String prefix, String prefixForEmpty, String content) throws IOException { + BufferedReader reader = null; + try { + reader = new BufferedReader(new java.io.StringReader(content)); + StringBuilder sb = new StringBuilder(); + + String line = reader.readLine(); + while (line != null) { + line = line.trim(); + if (line.isEmpty()) { + sb.append(prefixForEmpty); + } else { + sb.append(prefix); + sb.append(line); + } + line = reader.readLine(); + if (line != null) { + sb.append('\n'); + } + } + + String result = sb.toString(); + return result; + } finally { + if (reader != null) { + reader.close(); + } + } + } + + /** * Obtenir les clefs de toutes les valeurs nulles ou vide a partir * d'un dictionnaire donne. *