1 17 package org.apache.forrest.conf; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.util.Enumeration ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.StringTokenizer ; 28 29 import org.apache.commons.lang.StringUtils; 30 31 36 public class AntProperties extends Properties 37 { 38 39 public AntProperties() { 40 super(); 41 } 42 43 public AntProperties(Properties arg0) { 44 super(arg0); 45 putAllProperties(arg0); 46 } 47 48 public synchronized void load(InputStream arg0) throws IOException { 49 super.load(arg0); 50 51 BufferedReader in = null; 52 try { 53 54 in = new BufferedReader (new InputStreamReader (arg0)); 55 56 String currentLine, name, value; 57 int splitIndex; 58 59 while ((currentLine = in.readLine()) != null) { 60 if (!currentLine.startsWith("#") 62 && !(currentLine.trim().length() == 0)) { 63 splitIndex = currentLine.indexOf('='); 64 name = currentLine.substring(0, splitIndex).trim(); 65 value = currentLine.substring(splitIndex + 1).trim(); 66 this.put(name, value); 67 } 68 } 69 } finally { 70 if (in != null) { 71 try { 72 in.close(); 73 } catch (IOException e) {} 74 } 75 } 76 } 77 78 public synchronized Object put(Object name, Object value) { 79 if (!super.containsKey(name)) { 82 Enumeration names = super.propertyNames(); 83 while (names.hasMoreElements()) { 84 String currentName = (String ) names.nextElement(); 85 String valueToSearchFor = "${" + currentName + "}"; 86 String valueToReplaceWith = (String ) super.get(currentName); 87 value = StringUtils.replace(value.toString(), valueToSearchFor, 88 valueToReplaceWith); 89 } 90 return super.put(name, value); 91 } 92 93 return null; 94 } 95 96 public synchronized void putAllProperties(Map arg0) { 97 Iterator i = arg0.entrySet().iterator(); 98 while (i.hasNext()) { 99 Map.Entry me = (Map.Entry )i.next(); 100 this.put(me.getKey(), me.getValue()); 101 } 102 } 103 104 public synchronized Object setProperty(String name, String value) { 105 return this.put(name, value); 106 } 107 108 public String filter(String stringToFilter) { 109 110 StringBuffer resultStringBuffer = new StringBuffer (); 111 112 StringTokenizer st = new StringTokenizer (stringToFilter, "@", true); 113 114 STATE state; 115 116 if (stringToFilter.startsWith("@")) { 117 state = new STATE(STATE.START_TOKEN); 118 } else { 119 state = new STATE(STATE.STRING_NOT_TO_FILTER); 120 } 121 122 for (String currentToken; st.hasMoreTokens(); state.increment()) { 123 124 currentToken = st.nextToken(); 125 126 if (state.isStringToFilter()) { 127 resultStringBuffer.append(getProperty(currentToken, "@" 128 + currentToken + "@")); 129 } else if (state.isStringNOTToFilter()) { 130 resultStringBuffer.append(currentToken); 131 } 132 } 133 134 return resultStringBuffer.toString(); 135 } 136 137 private static class STATE 138 { 139 140 final static int STRING_NOT_TO_FILTER = 0; 141 final static int START_TOKEN = 1; 142 final static int STRING_TO_FILTER = 2; 143 final static int END_TOKEN = 3; 144 145 private int currentState; 146 147 STATE(int startState) { 148 this.currentState = startState; 149 } 150 151 void increment() { 152 currentState++; 153 if (currentState > 3) { 154 currentState = 0; 155 } 156 157 } 158 159 boolean isStringToFilter() { 160 return (currentState == STRING_TO_FILTER); 161 } 162 163 boolean isStringNOTToFilter() { 164 return (currentState == STRING_NOT_TO_FILTER); 165 } 166 } 167 168 } 169 | Popular Tags |