1 32 33 package com.knowgate.dataxslt; 34 35 import java.io.StringBufferInputStream ; 36 import java.io.BufferedInputStream ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.FileInputStream ; 40 41 import java.util.HashMap ; 42 43 import java.util.Date ; 44 45 import com.knowgate.debug.DebugFile; 46 47 56 57 public class FastStreamReplacer { 58 int BufferSize; 59 int iReplacements; 60 StringBuffer oOutStream; 61 62 64 public FastStreamReplacer() { 65 BufferSize = 32767; 66 oOutStream = new StringBuffer (BufferSize); 67 } 68 69 71 public FastStreamReplacer(int iBufferSize) { 72 BufferSize = iBufferSize; 73 oOutStream = new StringBuffer (BufferSize); 74 } 75 76 89 90 public String replace(InputStream oFileInStream, HashMap oMap) throws IOException { 91 92 if (DebugFile.trace) { 93 DebugFile.writeln("Begin FastStreamReplacer.replace([InputStream],[HashMap])"); 94 DebugFile.incIdent(); 95 } 96 97 int iChr; 98 String sKey; 99 Object oValue; 100 101 Date dtToday = new Date (); 102 String sToday = String.valueOf(dtToday.getYear()+1900) + "-" + String.valueOf(dtToday.getMonth()+1) + "-" + String.valueOf(dtToday.getDate()); 103 104 oMap.put("Sistema.Fecha",sToday); 105 oMap.put("System.Date",sToday); 106 107 iReplacements = 0; 108 109 BufferedInputStream oInStream = new BufferedInputStream (oFileInStream, BufferSize); 110 111 oOutStream.setLength(0); 112 113 do { 114 iChr = oInStream.read(); 115 116 if (-1==iChr) 117 break; 118 119 else { 120 121 if (123 == iChr) { 122 iChr = oInStream.read(); 124 if (35 == iChr) { 125 127 iReplacements++; 128 129 sKey = ""; 130 131 do { 132 133 iChr = oInStream.read(); 134 if (-1==iChr || 125==iChr) 135 break; 136 sKey += (char) iChr; 137 138 } while (true); 139 140 oValue = oMap.get(sKey); 141 142 if (null!=oValue) 143 oOutStream.append(((String )oValue)); 144 } 146 else { 147 oOutStream.append((char)123); 148 oOutStream.append((char)iChr); 149 } 150 151 } 153 else 154 oOutStream.append((char)iChr); 155 } 157 } while (true); 158 159 oInStream.close(); 160 161 if (DebugFile.trace) { 162 DebugFile.decIdent(); 163 DebugFile.writeln("End FastStreamReplacer.replace() : " + String.valueOf(oOutStream.length())); 164 } 165 166 return oOutStream.toString(); 167 } 169 183 184 public String replace(StringBuffer oStrBuff, HashMap oMap) 185 throws IOException , IndexOutOfBoundsException { 186 187 if (DebugFile.trace) { 188 DebugFile.writeln("Begin FastStreamReplacer.replace([StringBuffer],[HashMap])"); 189 DebugFile.incIdent(); 190 } 191 192 int iChr; 193 String sKey; 194 Object oValue; 195 196 Date dtToday = new Date (); 197 String sToday = String.valueOf(dtToday.getYear()+1900) + "-" + String.valueOf(dtToday.getMonth()+1) + "-" + String.valueOf(dtToday.getDate()); 198 199 oMap.put("Sistema.Fecha",sToday); 200 oMap.put("System.Date",sToday); 201 202 iReplacements = 0; 203 204 oOutStream.setLength(0); 205 206 int iAt = 0; 207 final int iLen = oStrBuff.length(); 208 209 while (iAt<iLen) { 210 iChr = oStrBuff.charAt(iAt++); 211 212 if (123 == iChr) { 213 iChr = oStrBuff.charAt(iAt++); 215 if (35 == iChr) { 216 iReplacements++; 218 219 sKey = ""; 220 221 while (iAt<iLen) { 222 iChr = oStrBuff.charAt(iAt++); 223 if (125==iChr) break; 224 sKey += (char) iChr; 225 } 227 oValue = oMap.get(sKey); 228 229 if (null!=oValue) 230 oOutStream.append(((String )oValue)); 231 } 233 else { 234 oOutStream.append((char)123); 235 oOutStream.append((char)iChr); 236 } 237 238 } 240 else 241 oOutStream.append((char)iChr); 242 243 } 245 if (DebugFile.trace) { 246 DebugFile.decIdent(); 247 DebugFile.writeln("End FastStreamReplacer.replace() : " + String.valueOf(oOutStream.length())); 248 } 249 250 return oOutStream.toString(); 251 } 253 255 256 268 269 public String replace(String sFilePath, HashMap oMap) throws IOException { 270 271 FileInputStream oStrm = new FileInputStream (sFilePath); 272 String sRetVal = replace(oStrm, oMap); 273 oStrm.close(); 274 275 return sRetVal; 276 } 277 278 280 283 public int lastReplacements() { 284 return iReplacements; 285 } 286 287 289 297 public static HashMap createMap(String [] aKeys, String [] aValues) { 298 299 HashMap oRetVal = new HashMap (5+((aKeys.length*100)/60)); 300 301 for (int k=0; k<aKeys.length; k++) 302 oRetVal.put(aKeys[k], aValues[k]); 303 304 return oRetVal; 305 } 307 309 }
| Popular Tags
|