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