View Javadoc

1   /*
2    * Copyright (c) 2003-2004, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of OSJava nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
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  //            str = str.replaceAll( "//$//{"+key+"//}", props.getProperty(key) );
99              str = replace(str, "${"+key+"}", props.getProperty(key), -1 );
100         }
101         return str;
102     }
103     
104     // From Jakarta Commons Lang's StringUtils
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 }