Source Code Cross Referenced for Utils.java in  » Database-JDBC-Connection-Pool » octopus » org » webdocwf » util » xml » 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 » Database JDBC Connection Pool » octopus » org.webdocwf.util.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:            Copyright (C) 2003  Together
003:            This library is free software; you can redistribute it and/or
004:            modify it under the terms of the GNU Lesser General Public
005:            License as published by the Free Software Foundation; either
006:            version 2.1 of the License, or (at your option) any later version.
007:            This library is distributed in the hope that it will be useful,
008:            but WITHOUT ANY WARRANTY; without even the implied warranty of
009:            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
010:            Lesser General Public License for more details.
011:            You should have received a copy of the GNU Lesser General Public
012:            License along with this library; if not, write to the Free Software
013:            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
014:         */
015:
016:        package org.webdocwf.util.xml;
017:
018:        import java.io.*;
019:        import java.util.*;
020:
021:        /**
022:         * Utility methods for xml jdbc.
023:         *
024:         * @author     Zoran Milakovic
025:         */
026:
027:        public class Utils {
028:
029:            //keywords escape
030:            public static final String[] keywords = { "AND", "WHERE", "FROM",
031:                    "SET", "IS", "CREATE TABLE", "PRIMARYKEY", "NOT NULL",
032:                    "INT0", "INSERT", "VALUES" };
033:            public static final String keywordEscape = "~#~KEYWORD~#~";
034:
035:            public static String handleBinaryString(String binaryString,
036:                    List binaryStreamObjectList) {
037:                String retVal = binaryString;
038:                if (retVal.startsWith(XmlSqlParser.BINARY_STREAM_OBJECT)) {
039:                    int index = Integer.valueOf(
040:                            retVal.substring(XmlSqlParser.BINARY_STREAM_OBJECT
041:                                    .length())).intValue();
042:                    //check for null values
043:                    if (binaryStreamObjectList.get(index - 1) != null)
044:                        retVal = binaryStreamObjectList.get(index - 1)
045:                                .toString();
046:                    else
047:                        retVal = null;
048:                }
049:                return retVal;
050:            }
051:
052:            public static String handleQuotedString(String quotedString) {
053:                String retVal = quotedString;
054:                if ((retVal.startsWith("'") && retVal.endsWith("'"))) {
055:                    if (!retVal.equals("''")) {
056:                        retVal = retVal.substring(retVal.indexOf("'") + 1,
057:                                retVal.lastIndexOf("'"));
058:                    } else {
059:                        retVal = "";
060:                    }
061:                }
062:                return retVal;
063:            }
064:
065:            /**
066:             * Replace all occurence of forReplace with replaceWith in input string.
067:             */
068:            public static String replaceAll(String input, String forReplace,
069:                    String replaceWith) {
070:                StringBuffer result = new StringBuffer();
071:                boolean hasMore = true;
072:                while (hasMore) {
073:                    int start = input.indexOf(forReplace);
074:                    int end = start + forReplace.length();
075:                    if (start != -1) {
076:                        result.append(input.substring(0, start) + replaceWith);
077:                        input = input.substring(end);
078:                    } else {
079:                        hasMore = false;
080:                        result.append(input);
081:                    }
082:                }
083:                if (result.toString().equals(""))
084:                    return input; //nothing is changed
085:                else
086:                    return result.toString();
087:            }
088:
089:            //Helper methods to work with BinaryStream object.
090:
091:            /**
092:             * This method transform binary object to string object
093:             * @param b is array of bytes which represents binary object
094:             * @return string representation of binary object
095:             */
096:            public static String bytesToHexString(byte[] b) {
097:                String hexString = null;
098:                try {
099:                    if (b != null) {
100:                        ByteArrayInputStream is = new ByteArrayInputStream(b);
101:                        hexString = streamToHexString(is);
102:                        return hexString;
103:                    } else {
104:                        return null;
105:                    }
106:                } catch (Exception e) {
107:                }
108:                return hexString;
109:            }
110:
111:            /**
112:             * This method transform string object to binary object (array of bytes)
113:             * @param val is string representation of binary object
114:             * @return binary object
115:             */
116:            public static byte[] hexStringToBytes(String val) {
117:                byte[] buf = new byte[val.length() / 2];
118:                final char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6',
119:                        '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
120:                byte[] hexMap = new byte[256];
121:                for (int i = 0; i < hexBytes.length; i++) {
122:                    hexMap[hexBytes[i]] = (byte) i;
123:                }
124:                int pos = 0;
125:                for (int i = 0; i < buf.length; i++) {
126:                    buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
127:                    buf[i] += hexMap[val.charAt(pos++)];
128:                }
129:                return buf;
130:            }
131:
132:            /**
133:             *
134:             * @param is
135:             * @return String that represent InputStream is.
136:             * @throws IOException
137:             */
138:            public static String streamToHexString(InputStream is)
139:                    throws IOException {
140:                try {
141:                    char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6', '7',
142:                            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
143:                    int c;
144:                    StringBuffer hexString = new StringBuffer();
145:                    while ((c = is.read()) >= 0) {
146:                        hexString.append(hexBytes[(c >> 4) & 0xf]);
147:                        hexString.append(hexBytes[c & 0xf]);
148:                    }
149:                    return hexString.toString();
150:                } catch (Exception e) {
151:                    throw new IOException(e.getMessage());
152:                }
153:            }
154:
155:            /**
156:             * Method replace all keywords in string passed as parameter.
157:             * @param s String within replace should be done.
158:             * @return String with special character array instead of keywords.
159:             */
160:            public static String replaceKeywords(String s, HashMap oldValues) {
161:                String retVal = s;
162:                int index = 1;
163:                for (int i = 0; i < keywords.length; i++) {
164:                    StringBuffer result = new StringBuffer();
165:                    boolean hasMore = true;
166:                    while (hasMore) {
167:                        int start = retVal.toUpperCase().indexOf(
168:                                keywords[i].toUpperCase());
169:                        int end = start + keywords[i].length();
170:                        if (start != -1) {
171:                            String newValue = keywordEscape
172:                                    + String.valueOf(index);
173:                            while (oldValues.containsKey(newValue)) {
174:                                index++;
175:                                newValue = keywordEscape
176:                                        + String.valueOf(index);
177:                            }
178:                            result
179:                                    .append(retVal.substring(0, start)
180:                                            + newValue);
181:                            oldValues.put(newValue, retVal
182:                                    .substring(start, end));
183:                            retVal = retVal.substring(end);
184:                        } else {
185:                            hasMore = false;
186:                            result.append(retVal);
187:                        }
188:                    }
189:                    if (!result.toString().equals(""))
190:                        retVal = result.toString();
191:                }
192:                return retVal;
193:            }
194:
195:            /**
196:             * Method replace all keywords in string passed as parameter.
197:             * @param s String within replace should be done.
198:             * @return String with special character array instead of keywords.
199:             */
200:            public static String replaceKeywordsBack(String s, HashMap oldValues) {
201:                String retVal = s;
202:                Set keys = oldValues.keySet();
203:                Iterator it = keys.iterator();
204:                Object key = "";
205:                String value = "";
206:                while (it.hasNext()) {
207:                    key = it.next();
208:                    value = (String) oldValues.get(key.toString());
209:                    if (value != null)
210:                        retVal = replaceAll(retVal, key.toString(), value
211:                                .toString());
212:                }
213:                return retVal;
214:            }
215:
216:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.