Source Code Cross Referenced for StringUtil.java in  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » common » 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 » ESB » cbesb 1.2 » com.bostechcorp.cbesb.common.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * ChainBuilder ESB
003:         *          Visual Enterprise Integration
004:         * 
005:         * Copyright (C) 2006 Bostech Corporation
006:         * 
007:         * This program is free software; you can redistribute it and/or modify it 
008:         * under the terms of the GNU General Public License as published by the 
009:         * Free Software Foundation; either version 2 of the License, or (at your option) 
010:         * any later version.
011:         *
012:         * This program is distributed in the hope that it will be useful, 
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
014:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
015:         * for more details.
016:         * 
017:         * You should have received a copy of the GNU General Public License along with 
018:         * this program; if not, write to the Free Software Foundation, Inc., 
019:         * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020:         *
021:         *
022:         * $Id: StringUtil.java 6618 2007-04-11 01:05:28Z zjin $
023:         */
024:        package com.bostechcorp.cbesb.common.util;
025:
026:        import java.io.ByteArrayOutputStream;
027:        import java.io.PrintStream;
028:        import java.text.NumberFormat;
029:
030:        import org.apache.commons.logging.Log;
031:        import org.apache.commons.logging.LogFactory;
032:
033:        /**
034:         * 
035:         * 
036:         */
037:        public class StringUtil {
038:
039:            // Default Minimum Fractional digits
040:            private static final int MIN_DIGITS = 2;
041:
042:            // Default Maximum Fractional digits
043:            private static final int MAX_DIGITS = 2;
044:
045:            protected static transient Log logger = LogFactory
046:                    .getLog(StringUtil.class);
047:
048:            /**
049:             * This function replaces find string with replace string in host string
050:             * 
051:             */
052:            public static String replace(String host, String find, String replce) {
053:                StringBuffer strOutput = new StringBuffer(32768);
054:                String strInput = host;
055:                int nPos = 0;
056:                while (true) {
057:                    int nIndex = strInput.indexOf(find, nPos);
058:                    if (nIndex < 0) {
059:                        strOutput.append(strInput.substring(nPos));
060:                        break;
061:                    } else {
062:                        strOutput.append(strInput.substring(nPos, nIndex));
063:                        strOutput.append(replce);
064:                        nPos = nIndex + find.length();
065:                    }
066:                }
067:                return strOutput.toString();
068:            }
069:
070:            /**
071:             * Method to format a double value to a string
072:             * 
073:             * @param <b>value
074:             *            </b>The date object to be formatted
075:             * @return <b>String</b> The string representation of the double value
076:             */
077:            public static String format(double value) {
078:                // Number format class to format the values
079:                NumberFormat numFormat = NumberFormat.getInstance();
080:                numFormat.setMinimumFractionDigits(MIN_DIGITS);
081:                numFormat.setMaximumFractionDigits(MAX_DIGITS);
082:                return numFormat.format(value);
083:            }
084:
085:            /**
086:             * Method to format a double value to a string
087:             * 
088:             * @param <b>value
089:             *            </b>The date object to be formatted
090:             * @return <b>String</b> The string representation of the double value
091:             */
092:            public static String formatDouble(String value) {
093:
094:                String valueString = new String();
095:                valueString = value;
096:                // if the value is null then return empty string
097:                if ((valueString == null) || valueString.equals(""))
098:                    return "";
099:
100:                double valueDouble = Double.parseDouble(valueString);
101:                // Number format class to format the values
102:                NumberFormat numFormat = NumberFormat.getInstance();
103:                numFormat.setMinimumFractionDigits(MIN_DIGITS);
104:                numFormat.setMaximumFractionDigits(MAX_DIGITS);
105:                return numFormat.format(valueDouble);
106:            }
107:
108:            /**
109:             * Method to remove decimals from string
110:             * 
111:             * @param <b>value
112:             *            </b>The date object to be formatted
113:             * @return <b>String</b> The string representation of the fromatted string
114:             */
115:            public static String removeDecimals(String value) {
116:
117:                String valueString = new String();
118:                String resultString = new String();
119:                valueString = value;
120:                // if the value is null then return empty string
121:                if ((valueString == null) || valueString.equals("")
122:                        || valueString.equals("0"))
123:                    return "";
124:
125:                int decimalIndex = valueString.indexOf('.');
126:                if (decimalIndex == -1)
127:                    return valueString;
128:
129:                if (valueString.substring(0, 1).equals("0"))
130:                    resultString = valueString.substring(1, decimalIndex)
131:                            + valueString.substring(decimalIndex + 1,
132:                                    valueString.length());
133:                else
134:                    resultString = valueString.substring(0, decimalIndex)
135:                            + valueString.substring(decimalIndex + 1,
136:                                    valueString.length());
137:
138:                return resultString;
139:            }
140:
141:            /**
142:             * Method to remove decimals from string
143:             * 
144:             * @param <b>value
145:             *            </b>The date object to be formatted
146:             * @return <b>String</b> The string representation of the fromatted string
147:             */
148:            public static String formatDoubleNoDecimals(String value) {
149:
150:                String valueString = new String();
151:                valueString = value;
152:                // if the value is null then return empty string
153:                if ((valueString == null) || valueString.equals(""))
154:                    return "";
155:
156:                double valueDouble = Double.parseDouble(valueString);
157:                // Number format class to format the values
158:                NumberFormat numFormat = NumberFormat.getInstance();
159:                numFormat.setMinimumFractionDigits(0);
160:                numFormat.setMaximumFractionDigits(0);
161:                return numFormat.format(valueDouble);
162:            }
163:
164:            /**
165:             * Method to remove decimals from string
166:             * 
167:             * @param <b>value
168:             *            </b>The date object to be formatted
169:             * @return <b>String</b> The string representation of the fromatted string
170:             */
171:            public static double parseDouble(String value) {
172:
173:                String valueString = new String();
174:                valueString = value;
175:                // if the value is null then return empty string
176:                if ((valueString == null) || valueString.equals(""))
177:                    return 0.00;
178:
179:                double doubleValue = Double.parseDouble(value);
180:
181:                return doubleValue;
182:            }
183:
184:            /**
185:             * Method to remove decimals from string
186:             * 
187:             * @param <b>value
188:             *            </b>The date object to be formatted
189:             * @return <b>String</b> The string representation of the fromatted string
190:             */
191:            public static float parseFloat(String value) {
192:
193:                String valueString = new String();
194:                valueString = value;
195:                // if the value is null then return empty string
196:                if ((valueString == null) || valueString.equals(""))
197:                    return 0;
198:
199:                float floatValue = Float.valueOf(valueString).floatValue();
200:
201:                return floatValue;
202:            }
203:
204:            /**
205:             * Method to return integer value from string
206:             * 
207:             * @param <b>value
208:             *            </b>The date object to be formatted
209:             * @return <b>String</b> The string representation of the fromatted string
210:             */
211:            public static int parseInteger(String value) {
212:
213:                String valueString = new String();
214:                valueString = value;
215:                // if the value is null then return empty string
216:                if ((valueString == null) || valueString.equals(""))
217:                    return 0;
218:
219:                int intValue = Integer.valueOf(value).intValue();
220:
221:                return intValue;
222:            }
223:
224:            /**
225:             * Method returns true if value 1 is greater than equal to value2
226:             */
227:            public static boolean greaterThanOrEqualTo(String value1,
228:                    String value2) {
229:
230:                boolean result = false;
231:
232:                Double doubleValue1 = Double.valueOf(value1);
233:                Double doubleValue2 = Double.valueOf(value2);
234:
235:                if ((doubleValue1.compareTo(doubleValue2) == 0)
236:                        || (doubleValue1.compareTo(doubleValue2) > 0))
237:                    result = true;
238:
239:                return result;
240:            }
241:
242:            /**
243:             * Method returns true if value 1 is less than equal to value2
244:             */
245:            public static boolean lessThanOrEqualTo(String value1, String value2) {
246:
247:                boolean result = false;
248:
249:                Double doubleValue1 = Double.valueOf(value1);
250:                Double doubleValue2 = Double.valueOf(value2);
251:
252:                if ((doubleValue1.compareTo(doubleValue2) == 0)
253:                        || (doubleValue1.compareTo(doubleValue2) < 0))
254:                    result = true;
255:
256:                return result;
257:            }
258:
259:            /**
260:             * Method returns true if value 1 is less than value2
261:             */
262:            public static boolean lessThan(String value1, String value2) {
263:
264:                boolean result = false;
265:
266:                Double doubleValue1 = Double.valueOf(value1);
267:                Double doubleValue2 = Double.valueOf(value2);
268:
269:                if (doubleValue1.compareTo(doubleValue2) < 0)
270:                    result = true;
271:
272:                return result;
273:            }
274:
275:            /**
276:             * Method returns true if value 1 is greater than value2
277:             */
278:            public static boolean greaterThan(String value1, String value2) {
279:
280:                boolean result = false;
281:
282:                Double doubleValue1 = Double.valueOf(value1);
283:                Double doubleValue2 = Double.valueOf(value2);
284:
285:                if (doubleValue1.compareTo(doubleValue2) > 0)
286:                    result = true;
287:
288:                return result;
289:            }
290:
291:            public static String renderNonNull(String s) {
292:                return (s == null) ? "" : s;
293:            }
294:
295:            /**
296:             * Method returns true if value 1 is equal to value2
297:             */
298:            public static boolean equalTo(String value1, String value2) {
299:
300:                boolean result = false;
301:
302:                Double doubleValue1 = Double.valueOf(value1);
303:                Double doubleValue2 = Double.valueOf(value2);
304:
305:                if (doubleValue1.compareTo(doubleValue2) == 0)
306:                    result = true;
307:
308:                return result;
309:            }
310:
311:            /**
312:             * Method returns true if string is either null or empty after trimming
313:             */
314:            public static boolean isTrivial(String s) {
315:                if (s == null)
316:                    return true;
317:                if (s.trim().length() == 0)
318:                    return true;
319:                else
320:                    return false;
321:            }
322:
323:            /** Find a particular XML tag value in the XML string */
324:            public static String getTagValue(int offset, String xml,
325:                    String tagName) {
326:                try {
327:                    if (offset < 0)
328:                        offset = 0;
329:
330:                    int startTagBegin = xml.indexOf("<" + tagName, offset);
331:                    if (startTagBegin == -1)
332:                        return "";
333:
334:                    int dataSectionStart = xml.indexOf(">", startTagBegin) + 1;
335:                    if (dataSectionStart == -1)
336:                        return "";
337:
338:                    int dataSectionEnd = xml.indexOf("<", dataSectionStart);
339:
340:                    return xml.substring(dataSectionStart, dataSectionEnd);
341:                } catch (Exception e) {
342:                    logger.error("Exception in getTagValue(): "
343:                            + e.getMessage());
344:                    if (logger.isDebugEnabled()) {
345:                        logger.debug("Exception in getTagValue():", e);
346:                    }
347:                    return "";
348:                }
349:
350:            }
351:
352:            public static String getTagValue(String xml, String tagName) {
353:                return getTagValue(0, xml, tagName);
354:            }
355:
356:            /** Find a particular XML tag value in the XML string */
357:            public static String getTagContent(int offset, String xml,
358:                    String tagName) {
359:                try {
360:                    if (offset < 0)
361:                        offset = 0;
362:
363:                    int startTagBegin = xml.indexOf("<" + tagName, offset);
364:                    if (startTagBegin == -1)
365:                        return "";
366:
367:                    int dataSectionStart = xml.indexOf(">", startTagBegin) + 1;
368:                    if (dataSectionStart == -1)
369:                        return "";
370:
371:                    int dataSectionEnd = xml.indexOf("</" + tagName,
372:                            dataSectionStart);
373:                    if (dataSectionEnd == -1)
374:                        return "";
375:
376:                    return xml.substring(dataSectionStart, dataSectionEnd);
377:                } catch (Exception e) {
378:
379:                    logger.error("Exception in getTagContent(): "
380:                            + e.getMessage());
381:                    if (logger.isDebugEnabled()) {
382:                        logger.debug("Exception in getTagContent():", e);
383:                    }
384:                    return "";
385:                }
386:
387:            }
388:
389:            public static String getTagContnt(String xml, String tagName) {
390:                return getTagContent(0, xml, tagName);
391:            }
392:
393:            public static String appendTagContnt(String xml, String preTagName,
394:                    String parentTagName, String childTagName, String subTag) {
395:                return appendTagContent(0, xml, preTagName, parentTagName,
396:                        childTagName, subTag);
397:            }
398:
399:            /** Find a particular XML tag value in the XML string */
400:            public static String appendTagContent(int offset, String xml,
401:                    String preTagName, String parentTagName,
402:                    String childTagName, String subTag) {
403:                try {
404:                    int lastlacation = -1;
405:
406:                    if (offset < 0)
407:                        offset = 0;
408:
409:                    int parentInd = xml.indexOf("<" + childTagName, offset);
410:                    int childInd = xml.indexOf("<" + childTagName, offset);
411:
412:                    int startTagBegin = xml.indexOf("<" + childTagName, offset);
413:                    if (parentInd == childInd) {
414:                        startTagBegin = -1;
415:                    }
416:                    if (startTagBegin != -1) {
417:                        // using child
418:                        int startTagEnd = xml.indexOf("</" + childTagName,
419:                                startTagBegin);
420:                        if (startTagEnd == -1)
421:                            return xml;
422:                        int endTagEnd = xml.indexOf(">", startTagEnd);
423:                        if (endTagEnd == -1)
424:                            return xml;
425:                        lastlacation = endTagEnd + 1;
426:                        String t = xml.substring(0, lastlacation);
427:                        String b = xml.substring(lastlacation, xml.length());
428:                        return t + subTag + b;
429:                    } else if (preTagName != null) {
430:                        // using preTag
431:                        lastlacation = xml.indexOf("</" + preTagName, offset);
432:                        assert lastlacation != -1;// make sure complier level above
433:                        // 1.4
434:                        lastlacation = xml.indexOf(">", lastlacation) + 1;
435:                        String t = xml.substring(0, lastlacation);
436:                        String b = xml.substring(lastlacation, xml.length());
437:                        return t + subTag + b;
438:                    } else if (parentTagName != null) {
439:                        // using parent
440:                        int pLoc = xml.indexOf("<" + parentTagName, offset);
441:                        assert (pLoc != -1);
442:                        /*
443:                         * should add?
444:                         */
445:                        lastlacation = pLoc;
446:                        lastlacation = xml.indexOf(">", lastlacation) + 1;
447:                        String t = xml.substring(0, lastlacation);
448:                        String b = xml.substring(lastlacation, xml.length());
449:                        return t + subTag + b;
450:                    }
451:                    //			
452:                    return xml;
453:
454:                } catch (Exception e) {
455:
456:                    logger.error("Exception in appendTagContent(): "
457:                            + e.getMessage());
458:                    if (logger.isDebugEnabled()) {
459:                        logger.debug("Exception in appendTagContent():", e);
460:                    }
461:                    return xml;
462:                }
463:
464:            }
465:
466:            public static String removeTagContent(String xml,
467:                    String xmlStartTagString, String xmlEndTagString) {
468:                int offset = 0;
469:                int startTagBegin = xml.indexOf(xmlStartTagString, offset);
470:                int finishTagEnd = xml.indexOf(xmlEndTagString, startTagBegin)
471:                        + xmlEndTagString.length();
472:                if ((startTagBegin == -1) || (finishTagEnd == -1))
473:                    return xml;
474:                String t = xml.substring(0, startTagBegin);
475:                String b = xml.substring(finishTagEnd, xml.length());
476:                return t + b;
477:            }
478:
479:            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
480:
481:            /**
482:             * Replaces all instances of a substring with a replacement string.
483:             * 
484:             * @param original
485:             *            The original string.
486:             * @param from
487:             *            The substring you wish to replace.
488:             * @param to
489:             *            The string you want to substitute.
490:             * @return The original string with all occurences of the substring
491:             *         replaced.
492:             */
493:            public static String replaceString(String original, String from,
494:                    String to) {
495:                StringBuffer result = new StringBuffer();
496:                int start = 0;
497:                int index = original.indexOf(from);
498:
499:                while (index != -1) {
500:                    result.append(original.substring(start, index));
501:                    result.append(to);
502:
503:                    start = index + from.length();
504:                    index = original.indexOf(from, start);
505:                }
506:
507:                result.append(original.substring(start));
508:                return result.toString();
509:            }
510:
511:            public static String trimString(String s, int trimToSize) {
512:                if (s != null && s.length() > trimToSize)
513:                    return s.substring(0, trimToSize);
514:                else
515:                    return s;
516:            }
517:
518:            public static String getStackTraceAsString(Throwable t) {
519:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
520:                PrintStream ps = new PrintStream(new ByteArrayOutputStream());
521:                t.printStackTrace(ps);
522:                return baos.toString();
523:            }
524:
525:            /**
526:             * 
527:             * @param args -
528:             *            a string array
529:             * @return a unique hashcode that depends on the elements in the array and
530:             *         not on their order.
531:             *         <p>
532:             *         Normal array implementations do not implement the hashCode
533:             *         properly. This implementation returns the same value irrespective
534:             *         of the order in which the strings are arranged within the array.
535:             */
536:            public static int arrayHashCode(String[] args) {
537:                if (args == null)
538:                    return 0;
539:                int a = 0;
540:                for (int i = 0; i < args.length; i++) {
541:                    a += args[i].hashCode();
542:                }
543:                return a;
544:            }
545:
546:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.