1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.osjava.payload;
33
34 import java.io.BufferedReader;
35 import java.io.IOException;
36 import java.io.StringReader;
37
38 import java.util.ArrayList;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Properties;
42
43 /***
44 * used to handle interpolation concepts, ie:
45 * org.osjava.payload=true
46 * org.osjava.payload.interpolate.endsWith=txt
47 * org.osjava.payload.interpolate.endsWith=xml
48 * org.osjava.payload.interpolate.matches=regexp
49 */
50 class Interpolation {
51
52 private PayloadConfiguration config;
53
54 public Interpolation(PayloadConfiguration configuration) {
55 this.config = configuration;
56 }
57
58 public boolean interpolatableArchive(String name) {
59 Iterator itr = this.config.getArchiveEndsWith().iterator();
60 while(itr.hasNext()) {
61 String substr = (String) itr.next();
62 if(name.endsWith(substr)) {
63 return true;
64 }
65 }
66 itr = this.config.getArchiveMatches().iterator();
67 while(itr.hasNext()) {
68 String pattern = (String) itr.next();
69 if(name.matches(pattern)) {
70 return true;
71 }
72 }
73 return false;
74 }
75
76 public boolean interpolatable(String name) {
77 Iterator itr = this.config.getFileEndsWith().iterator();
78 while(itr.hasNext()) {
79 String substr = (String) itr.next();
80 if(name.endsWith(substr)) {
81 return true;
82 }
83 }
84 itr = this.config.getFileMatches().iterator();
85 while(itr.hasNext()) {
86 String pattern = (String) itr.next();
87 if(name.matches(pattern)) {
88 return true;
89 }
90 }
91 return false;
92 }
93
94 public String interpolate(String str, Properties props) {
95 Iterator itr = props.keySet().iterator();
96 while(itr.hasNext()) {
97 String key = (String) itr.next();
98
99 str = replace(str, "${"+key+"}", props.getProperty(key), -1 );
100 }
101 return str;
102 }
103
104
105 private static String replace(String text, String repl, String with, int max) {
106 if (text == null || repl == null || repl.equals("") || with == null || max == 0) {
107 return text;
108 }
109
110 StringBuffer buf = new StringBuffer(text.length());
111 int start = 0, end = 0;
112 while ((end = text.indexOf(repl, start)) != -1) {
113 buf.append(text.substring(start, end)).append(with);
114 start = end + repl.length();
115
116 if (--max == 0) {
117 break;
118 }
119 }
120 buf.append(text.substring(start));
121 return buf.toString();
122 }
123
124 }