1 package net.sf.logdistiller.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.util.Map;
18
19
20
21
22 public class PropertiesReplacer
23 {
24 private final String beginMark;
25
26 private final String endMark;
27
28 private final Map<String, String> properties;
29
30 public PropertiesReplacer( Map<String, String> properties, String beginMark, String endMark )
31 {
32 this.properties = properties;
33 this.beginMark = beginMark;
34 this.endMark = endMark;
35 }
36
37 public PropertiesReplacer( Map<String, String> properties )
38 {
39 this( properties, "${", "}" );
40 }
41
42
43
44
45
46
47
48
49 public String replaceProperties( String source )
50 {
51 if ( source == null )
52 {
53 return null;
54 }
55 int index = 0;
56 int nextBegin, nextEnd;
57 StringBuffer buff = new StringBuffer();
58 while ( ( ( nextBegin = source.indexOf( beginMark, index ) ) >= 0 )
59 && ( ( nextEnd = source.indexOf( endMark, nextBegin ) ) >= 0 ) )
60 {
61 String name = source.substring( nextBegin + 2, nextEnd );
62 String value = properties.get( name );
63 if ( value == null )
64 {
65 throw new IllegalArgumentException( "unknown property '" + name + "'" );
66 }
67 buff.append( source.substring( index, nextBegin ) );
68 buff.append( value );
69 index = nextEnd + 1;
70 }
71 if ( index == 0 )
72 {
73
74 return source;
75 }
76 buff.append( source.substring( index ) );
77 return buff.toString();
78 }
79 }