KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 package org.webdocwf.util.loader;
20
21 import java.io.*;
22 import java.util.*;
23
24 /**
25  * Utility methods for csv jdbc.
26  *
27  * @author Zoran Milakovic
28  */

29
30 public class Utils {
31
32   /**
33    * Replace all occurence of forReplace with replaceWith in input string.
34    * @param input represents input string
35    * @param forReplace represents substring for replace
36    * @param replaceWith represents replaced string value
37    * @return new string with replaced values
38    */

39   public static String JavaDoc replaceAll(String JavaDoc input, String JavaDoc forReplace,
40                                   String JavaDoc replaceWith) {
41     if( input == null )
42       return "null";
43       //return null;
44
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
45     boolean hasMore = true;
46     while (hasMore) {
47       int start = input.indexOf(forReplace);
48       int end = start + forReplace.length();
49       if (start != -1) {
50         result.append(input.substring(0, start) + replaceWith);
51         input = input.substring(end);
52       }
53       else {
54         hasMore = false;
55         result.append(input);
56       }
57     }
58     if (result.toString().equals(""))
59       return input; //nothing is changed
60
else
61       return result.toString();
62   }
63
64
65   public static String JavaDoc handleQuotedString(String JavaDoc quotedString) {
66     if( quotedString == null )
67       return "null";
68       //return null;
69
String JavaDoc retVal = quotedString;
70     if ( (retVal.startsWith("'") && retVal.endsWith("'"))) {
71       if (!retVal.equals("''")) {
72         retVal = retVal.substring(retVal.indexOf("'") + 1,
73                                   retVal.lastIndexOf("'"));
74       }
75       else {
76         retVal = "";
77       }
78     }
79     //else {
80
// if( retVal.equals("null") )
81
// retVal = null;
82
//}
83
return retVal;
84   }
85
86
87 public static String JavaDoc getAbsolutePathFromDatabaseURL(String JavaDoc urlPrefix,
88       String JavaDoc loaderJobFile, String JavaDoc urlToDatabase, boolean fileSystemDatabase) {
89
90   if(fileSystemDatabase==true){
91     String JavaDoc pathToDatabase=urlToDatabase.substring(urlPrefix.length());
92     File file=new File(pathToDatabase);
93     if (!file.isAbsolute()){
94       pathToDatabase=loaderJobFile + pathToDatabase;
95       File absolutePath=new File(pathToDatabase);
96       try {
97         pathToDatabase=absolutePath.getCanonicalPath();
98       }
99       catch (Exception JavaDoc ex) {
100       ex.printStackTrace();
101       }
102       urlToDatabase = urlToDatabase.substring(0, (urlPrefix.length())) +
103                       pathToDatabase;
104     }
105   }
106   return urlToDatabase;
107 }
108
109 public static void log(String JavaDoc msg) {
110     try {
111             File file = new File("c:/log.txt");
112             if (!file.exists()) {
113               file.createNewFile();
114             }
115             RandomAccessFile fileLogr = new RandomAccessFile("c:/log.txt", "rw");
116             fileLogr.seek(fileLogr.length());
117             fileLogr.writeBytes( msg + "\r\n");
118             fileLogr.close();
119         }
120         catch (Exception JavaDoc ex) {
121           ex.printStackTrace();
122         }
123 }
124
125
126
127
128 }
129
Popular Tags