001: /**
002: Copyright (C) 2002-2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: */package org.webdocwf.util.loader;
019:
020: import java.io.*;
021: import java.util.*;
022:
023: /**
024: * Utility methods for csv jdbc.
025: *
026: * @author Zoran Milakovic
027: */
028:
029: public class Utils {
030:
031: /**
032: * Replace all occurence of forReplace with replaceWith in input string.
033: * @param input represents input string
034: * @param forReplace represents substring for replace
035: * @param replaceWith represents replaced string value
036: * @return new string with replaced values
037: */
038: public static String replaceAll(String input, String forReplace,
039: String replaceWith) {
040: if (input == null)
041: return "null";
042: //return null;
043: StringBuffer result = new StringBuffer();
044: boolean hasMore = true;
045: while (hasMore) {
046: int start = input.indexOf(forReplace);
047: int end = start + forReplace.length();
048: if (start != -1) {
049: result.append(input.substring(0, start) + replaceWith);
050: input = input.substring(end);
051: } else {
052: hasMore = false;
053: result.append(input);
054: }
055: }
056: if (result.toString().equals(""))
057: return input; //nothing is changed
058: else
059: return result.toString();
060: }
061:
062: public static String handleQuotedString(String quotedString) {
063: if (quotedString == null)
064: return "null";
065: //return null;
066: String retVal = quotedString;
067: if ((retVal.startsWith("'") && retVal.endsWith("'"))) {
068: if (!retVal.equals("''")) {
069: retVal = retVal.substring(retVal.indexOf("'") + 1,
070: retVal.lastIndexOf("'"));
071: } else {
072: retVal = "";
073: }
074: }
075: //else {
076: // if( retVal.equals("null") )
077: // retVal = null;
078: //}
079: return retVal;
080: }
081:
082: public static String getAbsolutePathFromDatabaseURL(
083: String urlPrefix, String loaderJobFile,
084: String urlToDatabase, boolean fileSystemDatabase) {
085:
086: if (fileSystemDatabase == true) {
087: String pathToDatabase = urlToDatabase.substring(urlPrefix
088: .length());
089: File file = new File(pathToDatabase);
090: if (!file.isAbsolute()) {
091: pathToDatabase = loaderJobFile + pathToDatabase;
092: File absolutePath = new File(pathToDatabase);
093: try {
094: pathToDatabase = absolutePath.getCanonicalPath();
095: } catch (Exception ex) {
096: ex.printStackTrace();
097: }
098: urlToDatabase = urlToDatabase.substring(0, (urlPrefix
099: .length()))
100: + pathToDatabase;
101: }
102: }
103: return urlToDatabase;
104: }
105:
106: public static void log(String msg) {
107: try {
108: File file = new File("c:/log.txt");
109: if (!file.exists()) {
110: file.createNewFile();
111: }
112: RandomAccessFile fileLogr = new RandomAccessFile(
113: "c:/log.txt", "rw");
114: fileLogr.seek(fileLogr.length());
115: fileLogr.writeBytes(msg + "\r\n");
116: fileLogr.close();
117: } catch (Exception ex) {
118: ex.printStackTrace();
119: }
120: }
121:
122: }
|