82 File src = new File(srcdir, fname); |
82 File src = new File(srcdir, fname); |
83 if (src.isDirectory()) { |
83 if (src.isDirectory()) { |
84 IOUtils.copyRecursive(src.toPath(), dest); |
84 IOUtils.copyRecursive(src.toPath(), dest); |
85 } else { |
85 } else { |
86 Files.copy(src.toPath(), dest); |
86 Files.copy(src.toPath(), dest); |
87 } |
|
88 } |
|
89 |
|
90 protected InputStream locateResource(String publicName, String category, |
|
91 String defaultName, File customFile, |
|
92 boolean verbose, File publicRoot) throws IOException { |
|
93 InputStream is = null; |
|
94 boolean customFromClasspath = false; |
|
95 boolean customFromFile = false; |
|
96 if (publicName != null) { |
|
97 if (publicRoot != null) { |
|
98 File publicResource = new File(publicRoot, publicName); |
|
99 if (publicResource.exists() && publicResource.isFile()) { |
|
100 is = new FileInputStream(publicResource); |
|
101 } |
|
102 } else { |
|
103 is = getResourceAsStream(publicName); |
|
104 } |
|
105 customFromClasspath = (is != null); |
|
106 } |
|
107 if (is == null && customFile != null) { |
|
108 is = new FileInputStream(customFile); |
|
109 customFromFile = (is != null); |
|
110 } |
|
111 if (is == null && defaultName != null) { |
|
112 is = getResourceAsStream(defaultName); |
|
113 } |
|
114 if (verbose) { |
|
115 String msg = null; |
|
116 if (customFromClasspath) { |
|
117 msg = MessageFormat.format(I18N.getString( |
|
118 "message.using-custom-resource"), |
|
119 category == null ? "" : "[" + category + "] ", publicName); |
|
120 } else if (customFromFile) { |
|
121 msg = MessageFormat.format(I18N.getString( |
|
122 "message.using-custom-resource-from-file"), |
|
123 category == null ? "" : "[" + category + "] ", |
|
124 customFile.getAbsoluteFile()); |
|
125 } else if (is != null) { |
|
126 msg = MessageFormat.format(I18N.getString( |
|
127 "message.using-default-resource"), |
|
128 defaultName, |
|
129 category == null ? "" : "[" + category + "] ", |
|
130 publicName); |
|
131 } else { |
|
132 msg = MessageFormat.format(I18N.getString( |
|
133 "message.no-default-resource"), |
|
134 defaultName == null ? "" : defaultName, |
|
135 category == null ? "" : "[" + category + "] ", |
|
136 publicName); |
|
137 } |
|
138 if (msg != null) { |
|
139 Log.verbose(msg); |
|
140 } |
|
141 } |
|
142 return is; |
|
143 } |
|
144 |
|
145 |
|
146 protected String preprocessTextResource(String publicName, String category, |
|
147 String defaultName, Map<String, String> pairs, |
|
148 boolean verbose, File publicRoot) throws IOException { |
|
149 InputStream inp = locateResource(publicName, category, |
|
150 defaultName, null, verbose, publicRoot); |
|
151 if (inp == null) { |
|
152 throw new RuntimeException( |
|
153 "Module corrupt? No "+defaultName+" resource!"); |
|
154 } |
|
155 |
|
156 try (InputStream is = inp) { |
|
157 //read fully into memory |
|
158 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
159 byte[] buffer = new byte[1024]; |
|
160 int length; |
|
161 while ((length = is.read(buffer)) != -1) { |
|
162 baos.write(buffer, 0, length); |
|
163 } |
|
164 |
|
165 //substitute |
|
166 String result = new String(baos.toByteArray()); |
|
167 for (Map.Entry<String, String> e : pairs.entrySet()) { |
|
168 if (e.getValue() != null) { |
|
169 result = result.replace(e.getKey(), e.getValue()); |
|
170 } |
|
171 } |
|
172 return result; |
|
173 } |
87 } |
174 } |
88 } |
175 |
89 |
176 public void writeCfgFile(Map<String, ? super Object> params, |
90 public void writeCfgFile(Map<String, ? super Object> params, |
177 File cfgFileName) throws IOException { |
91 File cfgFileName) throws IOException { |