]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/Utility.java
basic java template
[chaz/vimcoder] / src / com / dogcows / Utility.java
1
2 package com.dogcows;
3
4 import java.io.*;
5 import java.util.Arrays;
6 import java.util.Map;
7
8 /**
9 * @author Charles McGarvey
10 *
11 */
12 public abstract class Utility
13 {
14
15 public static <T> T[] concat(T[] a, T[] b)
16 {
17 T[] result = Arrays.copyOf(a, a.length + b.length);
18 System.arraycopy(b, 0, result, a.length, b.length);
19 return result;
20 }
21
22 public static String join(String[] a, String glue)
23 {
24 if (a.length == 0) return "";
25 StringBuilder result = new StringBuilder();
26 result.append(a[0]);
27 for (int i = 1; i < a.length; ++i) result.append(glue).append(a[i]);
28 return result.toString();
29 }
30
31 public static String quote(String a)
32 {
33 a = a.replaceAll("\\\\", "\\\\\\\\");
34 a = a.replaceAll("\"", "\\\\\\\"");
35 return a;
36 }
37
38 public static String readFile(File file) throws IOException
39 {
40 StringBuilder text = new StringBuilder();
41
42 BufferedReader reader = new BufferedReader(new FileReader(file.getPath()));
43 try
44 {
45 String line = null;
46
47 while ((line = reader.readLine()) != null)
48 {
49 text.append(line + System.getProperty("line.separator"));
50 }
51 }
52 finally
53 {
54 reader.close();
55 }
56
57 return text.toString();
58 }
59
60 public static String readResource(String path) throws IOException
61 {
62 StringBuilder text = new StringBuilder();
63
64 InputStream stream = Utility.class.getResourceAsStream("resources/" + path);
65 if (stream != null)
66 {
67 try
68 {
69 byte[] buffer = new byte[4096];
70 int numBytes = 0;
71 while (0 < (numBytes = stream.read(buffer)))
72 {
73 text.append(new String(buffer, 0, numBytes));
74 }
75 }
76 finally
77 {
78 stream.close();
79 }
80 }
81
82 return text.toString();
83 }
84
85 public static String expandTemplate(String template, Map<String,String> terms)
86 {
87 String text = template;
88 for (String key : terms.keySet())
89 {
90 text = text.replaceAll("\\$" + key + "\\$",
91 Utility.quote(terms.get(key)));
92 }
93 return text;
94 }
95 }
96
This page took 0.034613 seconds and 4 git commands to generate.