KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > CmsPropertyUtils


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsPropertyUtils.java,v $
3  * Date : $Date: 2005/06/23 11:11:24 $
4  * Version: $Revision: 1.9 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import java.io.File JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.Arrays JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 import org.apache.commons.collections.ExtendedProperties;
42
43 /**
44  * Utilities to access property files.<p>
45  *
46  * @author Alexander Kandzior
47  *
48  * @version $Revision: 1.9 $
49  *
50  * @since 6.0.0
51  */

52 public final class CmsPropertyUtils {
53
54     /**
55      * Hides the public constructor.<p>
56      */

57     private CmsPropertyUtils() {
58
59         // noop
60
}
61
62     /**
63      * Loads an extended property file and performs escaping/unescaping of "," and "=" entries.<p>
64      *
65      * @param file the file tp read the properties from
66      * @return the initialized extended properties
67      * @throws IOException in case of IO errors
68      */

69     public static ExtendedProperties loadProperties(String JavaDoc file) throws IOException JavaDoc {
70
71         FileInputStream JavaDoc input = null;
72         ExtendedProperties properties = null;
73
74         try {
75             input = new FileInputStream JavaDoc(new File JavaDoc(file));
76             properties = new ExtendedProperties();
77             properties.load(input);
78             input.close();
79         } catch (IOException JavaDoc e) {
80             try {
81                 input.close();
82             } catch (Exception JavaDoc ex) {
83                 // nothing we can do
84
}
85             throw e;
86         }
87
88         for (Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext();) {
89             String JavaDoc key = (String JavaDoc)i.next();
90             Object JavaDoc obj = properties.get(key);
91             String JavaDoc[] value = {};
92
93             if (obj instanceof Vector JavaDoc) {
94                 value = (String JavaDoc[])((Vector JavaDoc)obj).toArray(value);
95             } else {
96                 String JavaDoc[] v = {(String JavaDoc)obj};
97                 value = v;
98             }
99
100             for (int j = 0; j < value.length; j++) {
101                 value[j] = CmsStringUtil.substitute(value[j], "\\,", ",");
102                 value[j] = CmsStringUtil.substitute(value[j], "\\=", "=");
103             }
104
105             if (value.length > 1) {
106                 properties.put(key, new Vector JavaDoc(Arrays.asList(value)));
107             } else {
108                 properties.put(key, value[0]);
109             }
110         }
111
112         return properties;
113     }
114 }
115
Popular Tags