1 package org.unicode.cldr.util; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.io.PrintWriter; 10 import java.io.Reader; 11 import java.io.Writer; 12 import java.nio.charset.Charset; 13 import java.nio.charset.StandardCharsets; 14 import java.nio.file.Paths; 15 import java.util.Map; 16 17 public class FileCopier { 18 19 /** 20 * Copy the contents of the reader to the Writer, performing all the replacements specified in the map. This method will close 21 * the reader, but will leave the Writer open. The contents of the reader are read one line at a time. 22 * @param rdr 23 * @param replacements 24 * @param out 25 * @throws IOException 26 */ copyAndReplace(Reader rdr, Map<String, String> replacements, Writer out)27 public static void copyAndReplace(Reader rdr, Map<String, String> replacements, Writer out) throws IOException { 28 if (replacements == null || replacements.isEmpty()) { 29 copy(rdr, out); 30 return; 31 } 32 PrintWriter pw = new PrintWriter(out); 33 try (BufferedReader br = new BufferedReader(rdr);) { 34 String line = null; 35 while ((line = br.readLine()) != null) { 36 for (String key : replacements.keySet()) { 37 if (line.contains(key)) { 38 line = line.replaceAll(key, replacements.get(key)); 39 } 40 } 41 pw.println(line); 42 } 43 } finally { 44 pw.flush(); 45 } 46 } 47 48 /** 49 * Copy the resource srcFile to the Writer out, using a Reader with the charset specified; Reader will be closed, Writer will be 50 * flushed and left open. The replacements as specified in the Map will be performed. 51 * @param cls 52 * @param srcFile 53 * @param charSet 54 * @param replacements 55 * @param out 56 * @throws IOException 57 */ copyAndReplace(Class<?> cls, String srcFile, Charset charSet, Map<String, String> replacements, Writer out)58 public static void copyAndReplace(Class<?> cls, String srcFile, Charset charSet, Map<String, String> replacements, 59 Writer out) throws IOException { 60 copyAndReplace(new InputStreamReader(cls.getResourceAsStream(srcFile), charSet), replacements, out); 61 } 62 63 /** 64 * Append all the lines read from the Reader to the writer. Will close the reader, but leave the 65 * writer open, flushing it 66 * @param rdr 67 * @param wr 68 * @throws IOException 69 */ copy(Reader rdr, Writer wr)70 public static void copy(Reader rdr, Writer wr) throws IOException { 71 PrintWriter pw = new PrintWriter(wr); 72 try (BufferedReader br = new BufferedReader(rdr)) { 73 String line = null; 74 while ((line = br.readLine()) != null) { 75 pw.println(line); 76 } 77 } finally { 78 wr.flush(); 79 } 80 } 81 82 /*** 83 * Copy all the contents of the reader to the writer, performing line-by-line replacements, as specified by the map. Closes the 84 * reader, and flushes the writer, but leaves it open. 85 * @param rdr 86 * @param wr 87 * @param replacements 88 * @throws IOException 89 */ copyAndReplace(Reader rdr, Writer wr, Map<String, String> replacements)90 public static void copyAndReplace(Reader rdr, Writer wr, Map<String, String> replacements) throws IOException { 91 if (replacements == null || replacements.isEmpty()) { 92 copy(rdr, wr); 93 return; 94 } 95 PrintWriter pw = new PrintWriter(wr); 96 try (BufferedReader br = new BufferedReader(rdr);) { 97 String line = null; 98 while ((line = br.readLine()) != null) { 99 for (String key : replacements.keySet()) { 100 if (line.contains(key)) { 101 line = line.replaceAll(key, replacements.get(key)); 102 } 103 } 104 pw.println(line); 105 } 106 } finally { 107 pw.flush(); 108 } 109 } 110 111 /** 112 * Copy the resource denoted by sourcefile to the target directory, giving it the new name newName. 113 * @param cls 114 * @param sourceFile 115 * @param targetDirectory 116 * @param newName 117 * @throws IOException 118 */ copy(Class<?> cls, String sourceFile, String targetDirectory, String newName)119 public static void copy(Class<?> cls, String sourceFile, String targetDirectory, String newName) throws IOException { 120 try (InputStream is = cls.getResourceAsStream(sourceFile); 121 Writer wr = new FileWriter(Paths.get(targetDirectory, newName).toFile());) { 122 copy(new InputStreamReader(is), wr); 123 } 124 } 125 126 /** 127 * Writes the resource named sourceFile to the Writer, leaving the writer open, but flushing it. UTF-8 will be used as a charSet 128 * @param cls 129 * @param sourceFile 130 * @param out 131 * @throws IOException 132 */ copy(Class<?> cls, String sourceFile, Writer out)133 public static void copy(Class<?> cls, String sourceFile, Writer out) throws IOException { 134 copy(new InputStreamReader(cls.getResourceAsStream(sourceFile), StandardCharsets.UTF_8), out); 135 } 136 137 /** 138 * Writes the resource given as sourceFile to the Writer, using the specified CharSet. The Writer will be left open, but flushed. 139 * @param cls 140 * @param sourceFile 141 * @param charset 142 * @param out 143 * @throws IOException 144 */ copy(Class<?> cls, String sourceFile, Charset charset, Writer out)145 public static void copy(Class<?> cls, String sourceFile, Charset charset, Writer out) throws IOException { 146 copy(new InputStreamReader(cls.getResourceAsStream(sourceFile), charset), out); 147 } 148 149 /** 150 * Copies the resource accessible as sourceFile to the TargetDirectory, also naming it sourceFile 151 * @param cls 152 * @param sourceFile 153 * @param targetDirectory 154 * @throws IOException 155 */ copy(Class<?> cls, String sourceFile, String targetDirectory)156 public static void copy(Class<?> cls, String sourceFile, String targetDirectory) throws IOException { 157 copy(cls, sourceFile, targetDirectory, sourceFile); 158 } 159 160 /** 161 * Ensure that directory exists 162 */ ensureDirectoryExists(String targetDirectory)163 public static void ensureDirectoryExists(String targetDirectory) { 164 final File targetDir = new File(targetDirectory); 165 if (!targetDir.exists()) { 166 targetDir.mkdirs(); 167 } 168 } 169 copyAndReplace(Class<?> cls, String srcFile, String destDir, String destFile, Map<String, String> replacements)170 public static void copyAndReplace(Class<?> cls, String srcFile, String destDir, String destFile, Map<String, String> replacements) throws IOException { 171 copyAndReplace(new InputStreamReader(cls.getResourceAsStream(srcFile)), 172 replacements, new FileWriter(Paths.get(destDir, destFile).toFile())); 173 174 } 175 }