KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > driver > SimpleDiffFilter


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: SimpleDiffFilter.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.driver;
25
26
27 /**
28  * Filter for simple string matches in input
29  */

30 public class SimpleDiffFilter implements TestDiff.LineFilter {
31     /** Pattern and replacement */
32     private String JavaDoc fPattern;
33     private String JavaDoc fReplace;
34     
35     
36     /**
37      * Constructor for standard test case name
38      */

39     public SimpleDiffFilter(String JavaDoc pattern,
40                             String JavaDoc replace) {
41         fPattern = pattern;
42         fReplace = replace;
43     }
44
45     /** Filter a line */
46     public String JavaDoc filter(String JavaDoc line) {
47         int nextIdx = line.indexOf(fPattern);
48         if (nextIdx < 0) {
49             return line; // fast-path, no match
50
}
51
52         // Match, substitute all instances
53
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(line.length());
54         int startIdx = 0;
55         int len = fPattern.length();
56         while (nextIdx >= 0) {
57             buf.append(line.substring(startIdx, nextIdx));
58             buf.append(fReplace);
59             startIdx = nextIdx + len;
60             nextIdx = line.indexOf(fPattern, startIdx);
61         }
62         if (startIdx < line.length()) {
63             buf.append(line.substring(startIdx));
64         }
65         return buf.toString();
66     }
67 }
68
Popular Tags