chris@1: /*
chris@1: * SONEWS News Server
chris@1: * see AUTHORS for the list of contributors
chris@1: *
chris@1: * This program is free software: you can redistribute it and/or modify
chris@1: * it under the terms of the GNU General Public License as published by
chris@1: * the Free Software Foundation, either version 3 of the License, or
chris@1: * (at your option) any later version.
chris@1: *
chris@1: * This program is distributed in the hope that it will be useful,
chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@1: * GNU General Public License for more details.
chris@1: *
chris@1: * You should have received a copy of the GNU General Public License
chris@1: * along with this program. If not, see .
chris@1: */
chris@1:
chris@1: package org.sonews.util;
chris@1:
chris@1: import java.util.HashMap;
chris@1: import java.util.Map;
chris@1:
chris@1: /**
chris@1: * Class that allows simple String template handling.
chris@1: * @author Christian Lins
chris@1: * @since sonews/0.5.0
chris@1: */
chris@1: public class StringTemplate
chris@1: {
chris@1:
chris@1: private String str = null;
chris@1: private String templateDelimiter = "%";
chris@1: private Map templateValues = new HashMap();
chris@1:
chris@1: public StringTemplate(String str, final String templateDelimiter)
chris@1: {
chris@1: if(str == null || templateDelimiter == null)
chris@1: {
chris@1: throw new IllegalArgumentException("null arguments not allowed");
chris@1: }
chris@1:
chris@1: this.str = str;
chris@1: this.templateDelimiter = templateDelimiter;
chris@1: }
chris@1:
chris@1: public StringTemplate(String str)
chris@1: {
chris@1: this(str, "%");
chris@1: }
chris@1:
chris@1: public StringTemplate set(String template, String value)
chris@1: {
chris@1: if(template == null || value == null)
chris@1: {
chris@1: throw new IllegalArgumentException("null arguments not allowed");
chris@1: }
chris@1:
chris@1: this.templateValues.put(template, value);
chris@1: return this;
chris@1: }
chris@1:
chris@1: public StringTemplate set(String template, long value)
chris@1: {
chris@1: return set(template, Long.toString(value));
chris@1: }
chris@1:
chris@1: public StringTemplate set(String template, double value)
chris@1: {
chris@1: return set(template, Double.toString(value));
chris@1: }
chris@1:
chris@1: public StringTemplate set(String template, Object obj)
chris@1: {
chris@1: if(template == null || obj == null)
chris@1: {
chris@1: throw new IllegalArgumentException("null arguments not allowed");
chris@1: }
chris@1:
chris@1: return set(template, obj.toString());
chris@1: }
chris@1:
chris@1: @Override
chris@1: public String toString()
chris@1: {
chris@1: String ret = str;
chris@1:
chris@1: for(String key : this.templateValues.keySet())
chris@1: {
chris@1: String value = this.templateValues.get(key);
chris@1: ret = ret.replace(templateDelimiter + key, value);
chris@1: }
chris@1:
chris@1: return ret;
chris@1: }
chris@1:
chris@1: }