KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > util > ReadWrite


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.util;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30
31 /**
32  * This class defines reading and writing convenient methods
33  *
34  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
35  * @version 1.0
36  */

37 public class ReadWrite
38 {
39
40   /**
41    * Write the content of the <code>Hashtable</code> in a readable format
42    *
43    * @param table the hashtable to get keys and values from
44    * @param prefix prefix some values with tabs
45    * @return <code>String</code> conversion for the table content
46    */

47   public static String JavaDoc write(Hashtable JavaDoc table, boolean prefix)
48   {
49     if (table == null)
50       return "";
51     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
52     Enumeration JavaDoc e = table.keys();
53     Object JavaDoc o;
54     while (e.hasMoreElements())
55     {
56       o = e.nextElement();
57       if (o.toString().indexOf(".path") != -1)
58       {
59         // This is a class path, make it look nice
60
buffer.append(o + " = " + System.getProperty("line.separator"));
61         String JavaDoc substring = (String JavaDoc) table.get(o);
62         int index;
63         while (true)
64         {
65           index = substring.indexOf(':');
66           if (index == -1)
67             break;
68           if (prefix)
69             buffer.append("\t\t");
70           buffer.append(substring.substring(0, index)
71               + System.getProperty("line.separator"));
72           substring = substring.substring(index + 1);
73         }
74         if (prefix)
75           buffer.append("\t\t");
76         buffer.append(substring + System.getProperty("line.separator"));
77       }
78       else
79       {
80         buffer.append(o + " = " + table.get(o)
81             + System.getProperty("line.separator"));
82       }
83     }
84     return buffer.toString();
85   }
86
87   /**
88    * Write the content of the <code>ArrayList<code> in a readable format
89    *
90    * @param list the list to get the values from
91    * @param listName give the prefix names for values
92    * @param writeCountKey should we write the count keys
93    * @return <code>String</code> conversion for the list content
94    */

95   public static String JavaDoc write(ArrayList JavaDoc list, String JavaDoc listName,
96       boolean writeCountKey)
97   {
98     if (list == null)
99       return "";
100     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
101     int size = list.size();
102     if (writeCountKey)
103       buffer.append(listName + ".items.count=" + size
104           + System.getProperty("line.separator"));
105     for (int i = 0; i < size; i++)
106       buffer.append(listName + "." + i + "=" + list.get(i)
107           + System.getProperty("line.separator"));
108     return buffer.toString();
109   }
110 }
Popular Tags