KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > util > ReadWrite


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk
20  * Contributor(s): ______________________.
21  */

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

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

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

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