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