]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/Util.java
bc5bdb891acb4ed1867d52b71fd1aeb284e88f54
[chaz/vimcoder] / src / com / dogcows / Util.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 * The TopCoder Arena editor plug-in providing support for Vim.
11 *
12 * Distributable under the terms and conditions of the 2-clause BSD license;
13 * see the file COPYING for a complete text of the license.
14 */
15 public abstract class Util
16 {
17 /**
18 * Concatenate two arrays into a single array.
19 * @param a First array.
20 * @param b Second array.
21 * @return The combined array.
22 */
23 public static <T> T[] concat(T[] a, T[] b)
24 {
25 T[] result = Arrays.copyOf(a, a.length + b.length);
26 System.arraycopy(b, 0, result, a.length, b.length);
27 return result;
28 }
29
30 /**
31 * Combined string elements from two arrays into a single array, gluing
32 * together elements of the same index with a delimiter string.
33 * @param a First string array.
34 * @param b Second string array.
35 * @param glue The delimiter string.
36 * @return The combined array.
37 */
38 public static String[] combine(String[] a, String[] b, String glue)
39 {
40 String[] result = new String[Math.min(a.length, b.length)];
41 for (int i = 0; i < result.length; ++i)
42 {
43 result[i] = a[i] + glue + b[i];
44 }
45 return result;
46 }
47
48 /**
49 * Join the elements of a string array with a delimiter.
50 * @param a The array.
51 * @param glue The delimiter string.
52 * @return The joined string.
53 */
54 public static String join(String[] a, String glue)
55 {
56 if (a.length == 0) return "";
57 StringBuilder result = new StringBuilder();
58 result.append(a[0]);
59 for (int i = 1; i < a.length; ++i) result.append(glue).append(a[i]);
60 return result.toString();
61 }
62
63 /**
64 * Quote a string by replacing prepending backslashes and double
65 * quotation characters with an extra backslash.
66 * @param The string to be quoted.
67 * @return The quoted string.
68 */
69 public static String quote(String a)
70 {
71 a = a.replaceAll("\\\\", "\\\\\\\\");
72 a = a.replaceAll("\"", "\\\\\\\"");
73 return a;
74 }
75
76 /**
77 * Simply read a file's contents into a string object.
78 * @param file The file to read.
79 * @return The contents of the file.
80 * @throws IOException If the file is not readable.
81 */
82 public static String readFile(File file) throws IOException
83 {
84 StringBuilder text = new StringBuilder();
85
86 BufferedReader reader = new BufferedReader(new FileReader(file.getPath()));
87 try
88 {
89 String line = null;
90
91 while ((line = reader.readLine()) != null)
92 {
93 text.append(line + System.getProperty("line.separator"));
94 }
95 }
96 finally
97 {
98 reader.close();
99 }
100
101 return text.toString();
102 }
103
104 /**
105 * Read a resource file into a string object. The resources should be
106 * placed in the directory `resources' underneath the parent directory of
107 * this class. Reading resources packaged in a jar is allowable.
108 * @param path Relative path to the resource.
109 * @return The contents of the resource.
110 * @throws IOException If the resource is not readable.
111 */
112 public static String readResource(String path) throws IOException
113 {
114 StringBuilder text = new StringBuilder();
115
116 InputStream stream = Util.class.getResourceAsStream("resources/" + path);
117 if (stream != null)
118 {
119 try
120 {
121 byte[] buffer = new byte[4096];
122 int numBytes = 0;
123 while (0 < (numBytes = stream.read(buffer)))
124 {
125 text.append(new String(buffer, 0, numBytes));
126 }
127 }
128 finally
129 {
130 stream.close();
131 }
132 }
133
134 return text.toString();
135 }
136
137 /**
138 * The poor man's template package. Provide a template and a map of terms
139 * to build the result with the terms expanded into the template. Terms
140 * in the template should appear surrounded with dollar signs. For example,
141 * if $MYTERM$ appears in the template, it will be replaced by the value
142 * into the terms map with the key MYTERM (if it exists in the map).
143 * @param template The template string.
144 * @param terms A map of key/value terms.
145 * @return The string expanded from the template and terms.
146 */
147 public static String expandTemplate(String template, Map<String,String> terms)
148 {
149 String text = template;
150 for (String key : terms.keySet())
151 {
152 text = text.replaceAll("\\$" + key + "\\$",
153 Util.quote(terms.get(key)));
154 }
155 return text;
156 }
157 }
158
This page took 0.037959 seconds and 3 git commands to generate.