1 17 18 19 20 package org.apache.lenya.util; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.PrintStream ; 27 import java.nio.CharBuffer ; 28 import java.nio.MappedByteBuffer ; 29 import java.nio.channels.FileChannel ; 30 import java.nio.charset.Charset ; 31 import java.nio.charset.CharsetDecoder ; 32 import java.util.regex.Matcher ; 33 import java.util.regex.Pattern ; 34 35 import org.apache.log4j.Category; 36 37 40 public class SED { 41 static Category log = Category.getInstance(SED.class); 42 43 48 public static void main(String [] args) { 49 if (args.length == 0) { 50 System.out.println("Usage: org.apache.lenya.util.SED"); 51 return; 52 } 53 } 54 55 64 public static void replaceAll(File file, String substitute, String substituteReplacement) throws IOException { 65 log.debug("Replace " + substitute + " by " + substituteReplacement); 66 67 Pattern pattern = Pattern.compile(substitute); 68 69 FileInputStream fis = new FileInputStream (file); 71 FileChannel fc = fis.getChannel(); 72 73 int sz = (int)fc.size(); 75 MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); 76 77 Charset charset = Charset.forName("ISO-8859-15"); 80 CharsetDecoder decoder = charset.newDecoder(); 81 CharBuffer cb = decoder.decode(bb); 82 83 Matcher matcher = pattern.matcher(cb); 84 String outString = matcher.replaceAll(substituteReplacement); 85 log.debug(outString); 86 87 88 FileOutputStream fos = new FileOutputStream (file.getAbsolutePath()); 89 PrintStream ps =new PrintStream (fos); 90 ps.print(outString); 91 ps.close(); 92 fos.close(); 93 } 94 } 95 | Popular Tags |