Skip to content

Instantly share code, notes, and snippets.

@subchen
Last active January 3, 2016 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subchen/8470940 to your computer and use it in GitHub Desktop.
Save subchen/8470940 to your computer and use it in GitHub Desktop.
ExtendPropertiesLoader.java
import.packages = \
jetbrick.demo.model1, \
jetbrick.demo.model2, \
jetbrick.demo.model3,
[import.autoscan]
@ = true
packages = '''
jetbrick.demo.model1,
jetbrick.demo.model2,
jetbrick.demo.model3,
'''
[template]
path = /path/to/templates/
reloadable = false
[compile]
strategy = precompile
path = /path/to/temp/
#debug = false
[trim.directive.comments]
@ = false
prefix = <!--
suffix = -->
import java.io.*;
import java.nio.charset.Charset;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
public class ExtendPropertiesLoader {
private static final Properties EMPTY_PROPERTIES = new Properties();
private static final String DEFAULT_ENCODING = "utf-8";
public static Properties load(InputStream is) {
return load(is, DEFAULT_ENCODING);
}
public static Properties load(InputStream is, String encoding) {
if (is == null) return EMPTY_PROPERTIES;
if (encoding == null) {
encoding = DEFAULT_ENCODING;
}
return load(new BufferedReader(new InputStreamReader(is, Charset.forName(encoding))));
}
public static Properties load(BufferedReader reader) {
Properties props = new Properties();
String line = null;
String key = null;
try {
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("#")) {
continue;
} else if (line.startsWith("[") && line.endsWith("]")) {
key = line.substring(1, line.length() - 1).trim();
} else {
int pos = line.indexOf('=');
if (pos <= 0) {
continue;
}
String name = line.substring(0, pos).trim();
if (key != null && key.length() > 0) {
if ("@".equals(name)) {
name = key;
} else {
name = key + '.' + name;
}
}
String value = line.substring(pos + 1).trim();
if (value.startsWith("'''")) {
if (value.length() >= 6 && value.endsWith("'''")) {
props.put(name, value.substring(3, value.length() - 3));
} else {
StringBuilder sb = new StringBuilder();
sb.append(value, 3, value.length()).append('\n');
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.endsWith("'''")) {
sb.append(line, 0, line.length() - 3);
break;
} else {
sb.append(line).append('\n');
}
}
props.put(name, sb.toString());
}
} else if (value.endsWith("\\") && !value.endsWith("\\\\")) {
StringBuilder sb = new StringBuilder();
sb.append(value, 0, value.length() - 1);
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("#")) {
continue;
} else if (line.endsWith("\\") && !line.endsWith("\\\\")) {
sb.append(line, 0, line.length() - 1);
} else {
sb.append(line);
break;
}
}
props.put(name, StringEscapeUtils.unescapeJava(sb.toString()));
} else {
props.put(name, StringEscapeUtils.unescapeJava(value));
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(reader);
}
return props;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment