Source Code Cross Referenced for StringFilter.java in  » IDE-Netbeans » jellytools » org » netbeans » jellytools » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » jellytools » org.netbeans.jellytools.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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("/*",  "*&#47;", "");
059:         * // replace all multiple spaces
060:         * sf.addReplaceAllFilter("  ", " ");
061:         * // change author name
062:         * sf.addReplaceFilter("author: ", "", "author: spl@sun.com");
063:         * String string = "/* comment *&#47;    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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.