001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.jellytools.util;
043:
044: import java.io.BufferedReader;
045: import java.io.IOException;
046: import java.io.PrintStream;
047: import java.io.StringReader;
048: import java.util.ArrayList;
049:
050: /** Filters string, you can simple use replaceString() method or create string
051: * filter for more sophisticated filtering.
052: *
053: * <p>
054: * Usage:<br>
055: * <pre>
056: * StringFilter sf = new StringFilter();
057: * // remove 1st comment
058: * sf.addReplaceFilter("/*", "*/", "");
059: * // replace all multiple spaces
060: * sf.addReplaceAllFilter(" ", " ");
061: * // change author name
062: * sf.addReplaceFilter("author: ", "", "author: spl@sun.com");
063: * String string = "/* comment */ 4s 2s 3s author: xxx@sun.com";
064: * String result = sf.filter(string);
065: * </pre>
066: *
067: * @author Martin.Schovanek@sun.com
068: * @see #replaceString(String original, String begin, String end, String replace)
069: */
070: public class StringFilter {
071: ArrayList<Pattern> filter = new ArrayList<Pattern>();
072:
073: /** Adds replace pattern into Filter.
074: * @see #replaceString(String original, String begin, String end, String replace )
075: * @param begin the begin of substring to be find
076: * @param end the end of substring to be find
077: * @param replace text to replace
078: */
079: public void addReplaceFilter(String begin, String end,
080: String replace) {
081: filter.add(new Pattern(begin, end, replace));
082: }
083:
084: /** Adds replace pattern into Filter.
085: * @see #replaceString(String original, String begin, String end, String replace )
086: * @param find text to find
087: * @param replace text to replace
088: */
089: public void addReplaceAllFilter(String find, String replace) {
090: filter.add(new Pattern(find, find, replace));
091: }
092:
093: /** Filters string.
094: * @param str text to filter
095: * @return filtred string
096: */
097: public String filter(String str) {
098: for (int i = 0; i < filter.size(); i++) {
099: Pattern p = (Pattern) filter.get(i);
100: if (p != null)
101: str = replaceString(str, p.begin, p.end, p.replace);
102: }
103: return str;
104: }
105:
106: /** Finds substring which starts with first occurrence of 'begin' and ends
107: * with nearest next occurrence of 'end' and replces it with 'replace '.<p>
108: *
109: * Usage:
110: * <br><pre>
111: * replaceString("a-bcd-ef", "b", "d", "") => a--ef
112: * replaceString("abc-def", "", "c", "") => -def
113: * replaceString("ab-cdef", "c", "", "") => ab-
114: * replaceString("ab-cdef-ab", "ab", "ab", "AB") => AB-cdef-AB
115: * replaceString("abcdef", "", "", "AB") => abcdef
116: * </pre>
117: *
118: * @return filtred string
119: * @param replace text to replace
120: * @param original the original string
121: * @param begin the begin of substring to be find
122: * @param end the end of substring to be find
123: */
124: public static String replaceString(String original, String begin,
125: String end, String replace) {
126: boolean replaceAll = false;
127: int from;
128: int to;
129: int offset = 0;
130:
131: if (isEmpty(original) || (isEmpty(begin) && isEmpty(end)))
132: return original;
133: if (begin.equals(end))
134: replaceAll = true;
135: do {
136: from = isEmpty(begin) ? 0 : original.indexOf(begin, offset);
137: if (from < 0)
138: break;
139: if (isEmpty(end)) {
140: to = original.length();
141: } else {
142: to = original.indexOf(end, from);
143: if (to < 0)
144: break;
145: to += end.length();
146: }
147: original = original.substring(0, from) + replace
148: + original.substring(to, original.length());
149: offset = from + replace.length();
150: } while (replaceAll);
151:
152: return original;
153: }
154:
155: /** Finds and replace all substrings.
156: * @see #replaceString(String original, String begin, String end, String replace)
157: * @param original the original string
158: * @param find text to find
159: * @param replace text to replace
160: * @return filtred string
161: */
162: public static String replaceStringAll(String original, String find,
163: String replace) {
164: return replaceString(original, find, find, replace);
165: }
166:
167: private static boolean isEmpty(String s) {
168: return s == "" || s == null;
169: }
170:
171: /** Filters input string to a PrintStream.
172: * @param input string to be filtered
173: * @param output stream to write results in
174: */
175: public void filterLinesToStream(String input, PrintStream output) {
176: BufferedReader br = new BufferedReader(new StringReader(input));
177: try {
178: for (;;) {
179: String str = br.readLine();
180: if (str == null)
181: break;
182: str = filter(str);
183: output.println(str);
184: }
185: } catch (IOException e) {
186: } finally {
187: try {
188: br.close();
189: } catch (IOException e) {
190: }
191: }
192: }
193:
194: private class Pattern {
195: private String begin;
196: private String end;
197: private String replace;
198:
199: public Pattern(String begin, String end, String replace) {
200: this.begin = begin;
201: this.end = end;
202: this.replace = replace;
203: }
204: }
205: }
|