org/sonews/util/StringTemplate.java
author cli
Wed Aug 26 10:55:59 2009 +0200 (2009-08-26)
changeset 23 e4345a26f81f
permissions -rw-r--r--
Proper reply on XDAEMON GROUPADD if group already existing (#551).
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.util;
chris@1
    20
chris@1
    21
import java.util.HashMap;
chris@1
    22
import java.util.Map;
chris@1
    23
chris@1
    24
/**
chris@1
    25
 * Class that allows simple String template handling.
chris@1
    26
 * @author Christian Lins
chris@1
    27
 * @since sonews/0.5.0
chris@1
    28
 */
chris@1
    29
public class StringTemplate 
chris@1
    30
{
chris@1
    31
chris@1
    32
  private String              str               = null;
chris@1
    33
  private String              templateDelimiter = "%";
chris@1
    34
  private Map<String, String> templateValues    = new HashMap<String, String>();
chris@1
    35
  
chris@1
    36
  public StringTemplate(String str, final String templateDelimiter)
chris@1
    37
  {
chris@1
    38
    if(str == null || templateDelimiter == null)
chris@1
    39
    {
chris@1
    40
      throw new IllegalArgumentException("null arguments not allowed");
chris@1
    41
    }
chris@1
    42
chris@1
    43
    this.str               = str;
chris@1
    44
    this.templateDelimiter = templateDelimiter;
chris@1
    45
  }
chris@1
    46
  
chris@1
    47
  public StringTemplate(String str)
chris@1
    48
  {
chris@1
    49
    this(str, "%");
chris@1
    50
  }
chris@1
    51
  
chris@1
    52
  public StringTemplate set(String template, String value)
chris@1
    53
  {
chris@1
    54
    if(template == null || value == null)
chris@1
    55
    {
chris@1
    56
      throw new IllegalArgumentException("null arguments not allowed");
chris@1
    57
    }
chris@1
    58
    
chris@1
    59
    this.templateValues.put(template, value);
chris@1
    60
    return this;
chris@1
    61
  }
chris@1
    62
  
chris@1
    63
  public StringTemplate set(String template, long value)
chris@1
    64
  {
chris@1
    65
    return set(template, Long.toString(value));
chris@1
    66
  }
chris@1
    67
  
chris@1
    68
  public StringTemplate set(String template, double value)
chris@1
    69
  {
chris@1
    70
    return set(template, Double.toString(value));
chris@1
    71
  }
chris@1
    72
  
chris@1
    73
  public StringTemplate set(String template, Object obj)
chris@1
    74
  {
chris@1
    75
    if(template == null || obj == null)
chris@1
    76
    {
chris@1
    77
      throw new IllegalArgumentException("null arguments not allowed");
chris@1
    78
    }
chris@1
    79
chris@1
    80
    return set(template, obj.toString());
chris@1
    81
  }
chris@1
    82
  
chris@1
    83
  @Override
chris@1
    84
  public String toString()
chris@1
    85
  {
chris@1
    86
    String ret = str;
chris@1
    87
chris@1
    88
    for(String key : this.templateValues.keySet())
chris@1
    89
    {
chris@1
    90
      String value = this.templateValues.get(key);
chris@1
    91
      ret = ret.replace(templateDelimiter + key, value);
chris@1
    92
    }
chris@1
    93
    
chris@1
    94
    return ret;
chris@1
    95
  }
chris@1
    96
chris@1
    97
}