📄 utils.java
字号:
/**
Copyright (C) 2002-2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.relique.jdbc.csv;
import java.io.*;
import java.util.*;
/**
* Utility methods for csv jdbc.
*
* @author Zoran Milakovic
*/
public class Utils {
//keywords escape
public static final String[] keywords = { "AND", "WHERE", "FROM", "SET", "IS", "CREATE TABLE", "INT0", "INSERT", "VALUES" };
public static final String keywordEscape = "~#~KEYWORD~#~";
/**
* Replace all occurence of forReplace with replaceWith in input string.
* @param input represents input string
* @param forReplace represents substring for replace
* @param replaceWith represents replaced string value
* @return new string with replaced values
*/
public static String replaceAll(
String input,
String forReplace,
String replaceWith) {
if( input == null )
return null;
StringBuffer result = new StringBuffer();
boolean hasMore = true;
while (hasMore) {
int start = input.indexOf(forReplace);
int end = start + forReplace.length();
if (start != -1) {
result.append(input.substring(0, start) + replaceWith);
input = input.substring(end);
}
else {
hasMore = false;
result.append(input);
}
}
if (result.toString().equals(""))
return input; //nothing is changed
else
return result.toString();
}
/**
* This method transform binary object to string object
* @param b is array of bytes which represents binary object
* @return string representation of binary object
*/
public static String bytesToHexString(byte[] b) {
String hexString = null;
try {
if (b != null) {
ByteArrayInputStream is = new ByteArrayInputStream(b);
hexString = streamToHexString(is);
return hexString;
}
else {
return null;
}
}
catch (Exception e) {
}
return hexString;
}
public static String handleBinaryString(String binaryString, List binaryStreamObjectList) {
if( binaryString == null )
return null;
String retVal = binaryString;
if (retVal.startsWith(CsvSqlParser.BINARY_STREAM_OBJECT)) {
int index = Integer.valueOf(retVal.substring(CsvSqlParser.
BINARY_STREAM_OBJECT.length())).intValue();
//check for null values
if( binaryStreamObjectList.get(index - 1 ) != null )
retVal = binaryStreamObjectList.get(index - 1).toString();
else retVal = null;
}
return retVal;
}
public static String handleQuotedString(String quotedString) {
if( quotedString == null )
return null;
String retVal = quotedString;
if ( (retVal.startsWith("'") && retVal.endsWith("'"))) {
if (!retVal.equals("''")) {
retVal = retVal.substring(retVal.indexOf("'") + 1,
retVal.lastIndexOf("'"));
}
else {
retVal = "";
}
} else {
if( retVal.equals("null") )
retVal = null;
}
return retVal;
}
public static String[] replaceLineBrakesAndCarrReturn(
String[] toReplace,
String lineBreakEscape,
String carriageReturnEscape) {
String[] retVal = new String[toReplace.length];
for (int i = 0; i < toReplace.length; i++) {
if (toReplace[i] != null) {
retVal[i] = replaceAll(toReplace[i], "\n", lineBreakEscape);
retVal[i] = replaceAll(retVal[i], "\r", carriageReturnEscape);
}
}
return retVal;
}
/**
* Compare two values.
* @param valA first value
* @param valB second value
* @return true if values are equal, false otherwise
*/
public static boolean compareValues(String valA, String valB) {
boolean retVal = false;
if( valA == null && valB == null )
retVal = true;
else if( valA == null && valB != null )
retVal = false;
else if( valA != null && valB == null )
retVal = false;
else if( valA.equals(valB) )
retVal = true;
return retVal;
}
/**
* This method transform string object to binary object (array of bytes)
* @param val is string representation of binary object
* @return binary object
*/
public static byte[] hexStringToBytes(String val) {
byte[] buf = new byte[val.length() / 2];
final char[] hexBytes = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F'
};
byte[] hexMap = new byte[256];
for (int i = 0; i < hexBytes.length; i++) {
hexMap[hexBytes[i]] = (byte) i;
}
int pos = 0;
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
buf[i] += hexMap[val.charAt(pos++)];
}
return buf;
}
/**
*
* @param is
* @return String that represent InputStream is.
* @throws IOException
*/
public static String streamToHexString(InputStream is) throws IOException {
try {
char[] hexBytes = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F'};
int c;
StringBuffer hexString = new StringBuffer();
while ( (c = is.read()) >= 0) {
hexString.append(hexBytes[ (c >> 4) & 0xf]);
hexString.append(hexBytes[c & 0xf]);
}
return hexString.toString();
}
catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* Method replace all keywords in string passed as parameter.
* @param s String within replace should be done.
* @param oldValues HashMap with old values.
* @param startIndex start index for indexing replacements.
* @return String with special character array instead of keywords.
*/
public static String replaceKeywords(String s, HashMap oldValues, int startIndex) {
String retVal = s;
// int index = 1;
int index = startIndex;
for (int i = 0; i < keywords.length; i++) {
StringBuffer result = new StringBuffer();
boolean hasMore = true;
while (hasMore) {
int start = retVal.toUpperCase().indexOf(keywords[i].toUpperCase());
int end = start + keywords[i].length();
if (start != -1) {
result.append(retVal.substring(0, start) + keywordEscape + index);
oldValues.put(keywordEscape + index, retVal.substring(start, end));
retVal = retVal.substring(end);
}
else {
hasMore = false;
result.append(retVal);
}
index++;
}
if (!result.toString().equals(""))
retVal = result.toString();
}
return retVal;
}
/**
* Method replace all keywords in string passed as parameter.
* @param s String within replace should be done.
* @param oldValues HashMap with old values.
* @return String with special character array instead of keywords.
*/
public static String replaceKeywordsBack(String s, HashMap oldValues) {
String retVal = s;
Set keys = oldValues.keySet();
Iterator it = keys.iterator();
Object key = "";
String value = "";
while( it.hasNext() ) {
key = it.next();
value = (String)oldValues.get(key.toString());
if( value != null )
retVal = replaceAll(retVal, key.toString(), value.toString());
}
return retVal;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -