001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.console;
007:
008: import java.util.Arrays;
009: import java.util.Collections;
010: import java.util.LinkedHashMap;
011: import java.util.List;
012: import java.util.Map;
013: import java.util.regex.Matcher;
014: import java.util.regex.Pattern;
015:
016: /**
017: * @author Arjohn Kampman
018: */
019: class ConfigTemplate {
020:
021: /*-----------*
022: * Constants *
023: *-----------*/
024:
025: private static final Pattern TOKEN_PATTERN = Pattern
026: .compile("\\{%[\\p{Print}&&[^\\}]]+%\\}");
027:
028: /*-----------*
029: * Variables *
030: *-----------*/
031:
032: private String template;
033:
034: private Map<String, List<String>> variableMap = new LinkedHashMap<String, List<String>>();
035:
036: /*--------------*
037: * Constructors *
038: *--------------*/
039:
040: public ConfigTemplate(String template) {
041: setTemplate(template);
042: }
043:
044: /*---------*
045: * Methods *
046: *---------*/
047:
048: public String getTemplate() {
049: return template;
050: }
051:
052: public void setTemplate(String template) {
053: if (template == null) {
054: throw new IllegalArgumentException(
055: "template must not be null");
056: }
057:
058: this .template = template;
059:
060: parseTemplate();
061: }
062:
063: private void parseTemplate() {
064: Matcher matcher = TOKEN_PATTERN.matcher(template);
065: while (matcher.find()) {
066: String group = matcher.group();
067: String[] tokensArray = group.substring(2,
068: group.length() - 2).split("\\|");
069: List<String> tokens = Arrays.asList(tokensArray);
070:
071: String var = tokens.get(0).trim();
072: List<String> values = tokens.subList(1, tokens.size());
073:
074: if (var.length() == 0) {
075: throw new IllegalArgumentException(
076: "Illegal template token: " + matcher.group());
077: }
078:
079: if (!variableMap.containsKey(var)) {
080: variableMap.put(var, values);
081: }
082: }
083: }
084:
085: public Map<String, List<String>> getVariableMap() {
086: return Collections.unmodifiableMap(variableMap);
087: }
088:
089: public String render(Map<String, String> valueMap) {
090: StringBuffer result = new StringBuffer(template.length());
091:
092: Matcher matcher = TOKEN_PATTERN.matcher(template);
093: while (matcher.find()) {
094: String group = matcher.group();
095: String[] tokensArray = group.substring(2,
096: group.length() - 2).split("\\|");
097:
098: String var = tokensArray[0].trim();
099:
100: String value = valueMap.get(var);
101: if (value == null) {
102: List<String> values = variableMap.get(var);
103: if (!values.isEmpty()) {
104: value = values.get(0);
105: } else {
106: value = "";
107: }
108: }
109:
110: // if (value == null) {
111: // throw new IllegalArgumentException("No value specified for variable " + var);
112: // }
113:
114: matcher.appendReplacement(result, value);
115: }
116:
117: matcher.appendTail(result);
118:
119: return result.toString();
120: }
121: }
|