Source Code Cross Referenced for StringToolkit.java in  » Testing » jacareto » jacareto » toolkit » 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 » Testing » jacareto » jacareto.toolkit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Jacareto Copyright (c) 2002-2005
003:         * Applied Computer Science Research Group, Darmstadt University of
004:         * Technology, Institute of Mathematics & Computer Science,
005:         * Ludwigsburg University of Education, and Computer Based
006:         * Learning Research Group, Aachen University. All rights reserved.
007:         *
008:         * Jacareto is free software; you can redistribute it and/or
009:         * modify it under the terms of the GNU General Public
010:         * License as published by the Free Software Foundation; either
011:         * version 2 of the License, or (at your option) any later version.
012:         *
013:         * Jacareto is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016:         * General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU General Public
019:         * License along with Jacareto; if not, write to the Free
020:         * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021:         *
022:         */
023:
024:        package jacareto.toolkit;
025:
026:        import org.apache.regexp.RE;
027:        import org.apache.regexp.RESyntaxException;
028:
029:        import java.util.Dictionary;
030:        import java.util.Enumeration;
031:
032:        /**
033:         * Some useful utilities for string manipulation.
034:         *
035:         * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
036:         * @version 1.0
037:         */
038:        public class StringToolkit {
039:            private static RE identifierRE;
040:
041:            static {
042:                try {
043:                    identifierRE = new RE("[$][{][^}]*[}]");
044:                } catch (RESyntaxException res) {
045:                    System.err
046:                            .println("syntax error in regular expression for identifier detection");
047:                    System.exit(1);
048:                }
049:            }
050:
051:            /**
052:             * Creates a new StringToolkit.
053:             */
054:            public StringToolkit() {
055:            }
056:
057:            /**
058:             * Replaces unprintable characters.
059:             *
060:             * @param source the source string where the characters should be replaced
061:             *
062:             * @return the new string where the characters have been replaced
063:             */
064:            public static String replaceUnprintable(String source) {
065:                String result = source;
066:
067:                try {
068:                    RE re = new RE("[\r]");
069:                    result = re.subst(result, "<R>", RE.REPLACE_ALL);
070:                    re = new RE("[\f]");
071:                    result = re.subst(result, "<F>", RE.REPLACE_ALL);
072:                    re = new RE("\n");
073:                    result = re.subst(result, "<ENTER>", RE.REPLACE_ALL);
074:                    re = new RE("\t");
075:                    result = re.subst(result, "<TAB>", RE.REPLACE_ALL);
076:                } catch (RESyntaxException res) {
077:                    System.err
078:                            .println("syntax error in regular expression when parsing "
079:                                    + source);
080:                }
081:
082:                return result;
083:            }
084:
085:            /**
086:             * Reverses {@link #replaceUnprintable(String)}
087:             *
088:             * @param source DOCUMENT ME!
089:             *
090:             * @return DOCUMENT ME!
091:             */
092:            public static String reverseReplacement(String source) {
093:                String result = source;
094:
095:                try {
096:                    RE re = new RE("<F>");
097:                    result = re.subst(result, "\f", RE.REPLACE_ALL);
098:                    re = new RE("<R>");
099:                    result = re.subst(result, "\r", RE.REPLACE_ALL);
100:                    re = new RE("<TAB>");
101:                    result = re.subst(result, "\t", RE.REPLACE_ALL);
102:                    re = new RE("<ENTER>");
103:                    result = re.subst(result, "\n", RE.REPLACE_ALL);
104:                } catch (RESyntaxException res) {
105:                    System.err
106:                            .println("syntax error in regular expression when parsing "
107:                                    + source);
108:                }
109:
110:                return result;
111:            }
112:
113:            /**
114:             * Returns whether or not the given two strings are equal. Also included <code>null</code>
115:             * tests.
116:             *
117:             * @param string1 the first string
118:             * @param string2 the second string
119:             *
120:             * @return DOCUMENT ME!
121:             */
122:            public static boolean areEqual(String string1, String string2) {
123:                if ((string1 == null) && (string2 == null)) {
124:                    return true;
125:                } else if ((string1 == null) || (string2 == null)) {
126:                    return false;
127:                } else {
128:                    return string1.equals(string2);
129:                }
130:            }
131:
132:            /**
133:             * Returns whether or not a string is defined.
134:             *
135:             * @param string the string
136:             *
137:             * @return <code>true</code> if the string is not <code>null</code> and not equal &quot;&quot;,
138:             *         otherwise <code>false</code>
139:             */
140:            public static boolean isDefined(String string) {
141:                return (string != null) && !string.equals("");
142:            }
143:
144:            /**
145:             * Replaces all occurences of identifier substrings with the value of the identifier. Names and
146:             * values of identifiers are given in a dictionary. Identifier substrings are of the following
147:             * structure: <code>${ID_NAME}</code>. If the value of an identifier contains identifiers
148:             * itself, those will also be replaced by their values. If an identifier is not contained in
149:             * the dictionary, the related substring will not be replaced.
150:             *
151:             * @param idString the string which contains identifiers
152:             * @param idTable the dictionary containing the identifiers (names mapped to values).
153:             *
154:             * @return a new string with the replaced substrings
155:             */
156:            public static String replaceIdentifiers(String idString,
157:                    Dictionary idTable) {
158:                String result = idString;
159:
160:                // Simple test if there is an identifier
161:                if (!identifierRE.match(idString)) {
162:                    return idString;
163:                }
164:
165:                // create a regular expression for every identifier
166:                int numberOfIdentifiers = idTable.size();
167:                RE[] regularExpressions = new RE[numberOfIdentifiers];
168:                String[] values = new String[numberOfIdentifiers];
169:                Enumeration en = idTable.keys();
170:                int index = 0;
171:
172:                while (en.hasMoreElements()) {
173:                    String idName = (String) en.nextElement();
174:
175:                    try {
176:                        regularExpressions[index] = new RE("[$][{]" + idName
177:                                + "[}]");
178:                        values[index] = (String) idTable.get(idName);
179:                        index++;
180:                    } catch (RESyntaxException res) {
181:                        System.err
182:                                .println("syntax error in regular expression for identifier: "
183:                                        + idName);
184:
185:                        return (idString);
186:                    }
187:                }
188:
189:                String oldString = null;
190:
191:                do {
192:                    oldString = result;
193:
194:                    for (int i = 0; i < numberOfIdentifiers; i++) {
195:                        result = regularExpressions[i].subst(result, values[i]);
196:                    }
197:                } while (!result.equals(oldString)
198:                        && identifierRE.match(result));
199:
200:                return (result);
201:            }
202:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.