KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > xml > Utils


1 /*
2     Copyright (C) 2003 Together
3     This library is free software; you can redistribute it and/or
4     modify it under the terms of the GNU Lesser General Public
5     License as published by the Free Software Foundation; either
6     version 2.1 of the License, or (at your option) any later version.
7     This library is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10     Lesser General Public License for more details.
11     You should have received a copy of the GNU Lesser General Public
12     License along with this library; if not, write to the Free Software
13     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */

15
16 package org.webdocwf.util.xml;
17
18 import java.io.*;
19 import java.util.*;
20
21 /**
22  * Utility methods for xml jdbc.
23  *
24  * @author Zoran Milakovic
25  */

26
27 public class Utils {
28
29   //keywords escape
30
public static final String JavaDoc[] keywords = { "AND", "WHERE", "FROM", "SET", "IS", "CREATE TABLE",
31                                             "PRIMARYKEY", "NOT NULL", "INT0", "INSERT", "VALUES" };
32   public static final String JavaDoc keywordEscape = "~#~KEYWORD~#~";
33
34
35   public static String JavaDoc handleBinaryString(String JavaDoc binaryString, List binaryStreamObjectList) {
36     String JavaDoc retVal = binaryString;
37     if (retVal.startsWith(XmlSqlParser.BINARY_STREAM_OBJECT)) {
38       int index = Integer.valueOf(retVal.substring(XmlSqlParser.
39           BINARY_STREAM_OBJECT.length())).intValue();
40       //check for null values
41
if( binaryStreamObjectList.get(index - 1 ) != null )
42         retVal = binaryStreamObjectList.get(index - 1).toString();
43       else retVal = null;
44     }
45     return retVal;
46   }
47
48
49   public static String JavaDoc handleQuotedString( String JavaDoc quotedString ) {
50     String JavaDoc retVal = quotedString;
51     if ( ( retVal.startsWith( "'" ) && retVal.endsWith( "'" ) ) ) {
52       if ( !retVal.equals( "''" ) ) {
53         retVal = retVal.substring( retVal.indexOf( "'" ) + 1,
54                                    retVal.lastIndexOf( "'" ) );
55       }
56       else {
57         retVal = "";
58       }
59     }
60     return retVal;
61   }
62
63   /**
64    * Replace all occurence of forReplace with replaceWith in input string.
65    */

66   public static String JavaDoc replaceAll(
67       String JavaDoc input,
68       String JavaDoc forReplace,
69       String JavaDoc replaceWith ) {
70     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
71     boolean hasMore = true;
72     while ( hasMore ) {
73       int start = input.indexOf( forReplace );
74       int end = start + forReplace.length();
75       if ( start != -1 ) {
76         result.append( input.substring( 0, start ) + replaceWith );
77         input = input.substring( end );
78       }
79       else {
80         hasMore = false;
81         result.append( input );
82       }
83     }
84     if ( result.toString().equals( "" ) )
85       return input; //nothing is changed
86
else
87       return result.toString();
88   }
89
90   //Helper methods to work with BinaryStream object.
91

92   /**
93    * This method transform binary object to string object
94    * @param b is array of bytes which represents binary object
95    * @return string representation of binary object
96    */

97   public static String JavaDoc bytesToHexString( byte[] b ) {
98     String JavaDoc hexString = null;
99     try {
100       if ( b != null ) {
101         ByteArrayInputStream is = new ByteArrayInputStream( b );
102         hexString = streamToHexString( is );
103         return hexString;
104       }
105       else {
106         return null;
107       }
108     }
109     catch ( Exception JavaDoc e ) {
110     }
111     return hexString;
112   }
113
114   /**
115    * This method transform string object to binary object (array of bytes)
116    * @param val is string representation of binary object
117    * @return binary object
118    */

119   public static byte[] hexStringToBytes( String JavaDoc val ) {
120     byte[] buf = new byte[val.length() / 2];
121     final char[] hexBytes = {
122         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
123         'E', 'F'
124     };
125     byte[] hexMap = new byte[256];
126     for ( int i = 0; i < hexBytes.length; i++ ) {
127       hexMap[hexBytes[i]] = ( byte ) i;
128     }
129     int pos = 0;
130     for ( int i = 0; i < buf.length; i++ ) {
131       buf[i] = ( byte ) ( hexMap[val.charAt( pos++ )] << 4 );
132       buf[i] += hexMap[val.charAt( pos++ )];
133     }
134     return buf;
135   }
136
137   /**
138    *
139    * @param is
140    * @return String that represent InputStream is.
141    * @throws IOException
142    */

143   public static String JavaDoc streamToHexString( InputStream is ) throws IOException {
144     try {
145       char[] hexBytes = {
146           '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
147           'E', 'F'};
148       int c;
149       StringBuffer JavaDoc hexString = new StringBuffer JavaDoc();
150       while ( ( c = is.read() ) >= 0 ) {
151         hexString.append( hexBytes[ ( c >> 4 ) & 0xf] );
152         hexString.append( hexBytes[c & 0xf] );
153       }
154       return hexString.toString();
155     }
156     catch ( Exception JavaDoc e ) {
157       throw new IOException( e.getMessage() );
158     }
159   }
160
161   /**
162    * Method replace all keywords in string passed as parameter.
163    * @param s String within replace should be done.
164    * @return String with special character array instead of keywords.
165    */

166   public static String JavaDoc replaceKeywords(String JavaDoc s, HashMap oldValues) {
167     String JavaDoc retVal = s;
168     int index = 1;
169     for (int i = 0; i < keywords.length; i++) {
170       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
171       boolean hasMore = true;
172       while (hasMore) {
173         int start = retVal.toUpperCase().indexOf(keywords[i].toUpperCase());
174         int end = start + keywords[i].length();
175         if (start != -1) {
176           String JavaDoc newValue = keywordEscape + String.valueOf(index);
177           while( oldValues.containsKey(newValue) ) {
178             index++;
179             newValue = keywordEscape + String.valueOf(index);
180           }
181           result.append(retVal.substring(0, start) + newValue);
182           oldValues.put(newValue, retVal.substring(start, end));
183           retVal = retVal.substring(end);
184         }
185         else {
186           hasMore = false;
187           result.append(retVal);
188         }
189       }
190       if (!result.toString().equals(""))
191         retVal = result.toString();
192     }
193     return retVal;
194   }
195
196
197   /**
198    * Method replace all keywords in string passed as parameter.
199    * @param s String within replace should be done.
200    * @return String with special character array instead of keywords.
201    */

202   public static String JavaDoc replaceKeywordsBack(String JavaDoc s, HashMap oldValues) {
203     String JavaDoc retVal = s;
204     Set keys = oldValues.keySet();
205     Iterator it = keys.iterator();
206     Object JavaDoc key = "";
207     String JavaDoc value = "";
208     while( it.hasNext() ) {
209       key = it.next();
210       value = (String JavaDoc)oldValues.get(key.toString());
211       if( value != null )
212       retVal = replaceAll(retVal, key.toString(), value.toString());
213     }
214     return retVal;
215   }
216
217 }
218
219
Popular Tags