KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > util > SED


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: SED.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.util;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.nio.CharBuffer JavaDoc;
28 import java.nio.MappedByteBuffer JavaDoc;
29 import java.nio.channels.FileChannel JavaDoc;
30 import java.nio.charset.Charset JavaDoc;
31 import java.nio.charset.CharsetDecoder JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34
35 import org.apache.log4j.Category;
36
37 /**
38  * Similar to the UNIX sed
39  */

40 public class SED {
41     static Category log = Category.getInstance(SED.class);
42
43     /**
44      * Command Line Interface
45      *
46      * @param args DOCUMENT ME!
47      */

48     public static void main(String JavaDoc[] args) {
49         if (args.length == 0) {
50             System.out.println("Usage: org.apache.lenya.util.SED");
51             return;
52         }
53     }
54
55     /**
56      * Substitute prefix, e.g. ".*world.*" by "universe"
57      *
58      * @param file File which sed shall be applied
59      * @param prefixSubstitute Prefix which shall be replaced
60      * @param substituteReplacement Prefix which is going to replace the original
61      *
62      * @throws IOException DOCUMENT ME!
63      */

64     public static void replaceAll(File JavaDoc file, String JavaDoc substitute, String JavaDoc substituteReplacement) throws IOException JavaDoc {
65         log.debug("Replace " + substitute + " by " + substituteReplacement);
66
67         Pattern JavaDoc pattern = Pattern.compile(substitute);
68
69         // Open the file and then get a channel from the stream
70
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
71         FileChannel JavaDoc fc = fis.getChannel();
72
73         // Get the file's size and then map it into memory
74
int sz = (int)fc.size();
75         MappedByteBuffer JavaDoc bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
76
77     // Decode the file into a char buffer
78
// Charset and decoder for ISO-8859-15
79
Charset JavaDoc charset = Charset.forName("ISO-8859-15");
80         CharsetDecoder JavaDoc decoder = charset.newDecoder();
81     CharBuffer JavaDoc cb = decoder.decode(bb);
82
83         Matcher JavaDoc matcher = pattern.matcher(cb);
84         String JavaDoc outString = matcher.replaceAll(substituteReplacement);
85         log.debug(outString);
86
87
88         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file.getAbsolutePath());
89         PrintStream JavaDoc ps =new PrintStream JavaDoc(fos);
90         ps.print(outString);
91         ps.close();
92         fos.close();
93     }
94 }
95
Popular Tags