001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Nicolas Modrzyk
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.common.util;
022:
023: import java.util.ArrayList;
024: import java.util.Enumeration;
025: import java.util.Hashtable;
026:
027: /**
028: * This class defines reading and writing convenient methods
029: *
030: * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
031: * @version 1.0
032: */
033: public class ReadWrite {
034:
035: /**
036: * Write the content of the <code>Hashtable</code> in a readable format
037: *
038: * @param table the hashtable to get keys and values from
039: * @param prefix prefix some values with tabs
040: * @return <code>String</code> conversion for the table content
041: */
042: public static String write(Hashtable table, boolean prefix) {
043: if (table == null)
044: return "";
045: StringBuffer buffer = new StringBuffer();
046: Enumeration e = table.keys();
047: Object o;
048: while (e.hasMoreElements()) {
049: o = e.nextElement();
050: if (o.toString().indexOf(".path") != -1) {
051: // This is a class path, make it look nice
052: buffer.append(o + " = "
053: + System.getProperty("line.separator"));
054: String substring = (String) table.get(o);
055: int index;
056: while (true) {
057: index = substring.indexOf(':');
058: if (index == -1)
059: break;
060: if (prefix)
061: buffer.append("\t\t");
062: buffer.append(substring.substring(0, index)
063: + System.getProperty("line.separator"));
064: substring = substring.substring(index + 1);
065: }
066: if (prefix)
067: buffer.append("\t\t");
068: buffer.append(substring
069: + System.getProperty("line.separator"));
070: } else {
071: buffer.append(o + " = " + table.get(o)
072: + System.getProperty("line.separator"));
073: }
074: }
075: return buffer.toString();
076: }
077:
078: /**
079: * Write the content of the <code>ArrayList<code> in a readable format
080: *
081: * @param list the list to get the values from
082: * @param listName give the prefix names for values
083: * @param writeCountKey should we write the count keys
084: * @return <code>String</code> conversion for the list content
085: */
086: public static String write(ArrayList list, String listName,
087: boolean writeCountKey) {
088: if (list == null)
089: return "";
090: StringBuffer buffer = new StringBuffer();
091: int size = list.size();
092: if (writeCountKey)
093: buffer.append(listName + ".items.count=" + size
094: + System.getProperty("line.separator"));
095: for (int i = 0; i < size; i++)
096: buffer.append(listName + "." + i + "=" + list.get(i)
097: + System.getProperty("line.separator"));
098: return buffer.toString();
099: }
100: }
|