KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > Support


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.deliver.util;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 /**
33  * A support class for nice static methods whoch could be used every where.
34  * @author Per Jonsson - per.jonsson@it-huset.se
35  */

36 public class Support
37 {
38
39     /**
40      * Converts a property like string to a map. Where the rowdelmiter is "\n"
41      * and and property value delimiter is "="
42      * @param text the text to parse/convert
43      * @return a map with values or an empty map if none.
44      * @author Per Jonsson - per.jonsson@it-huset.se
45      */

46
47     public static Map JavaDoc convertTextToMap( String JavaDoc text )
48     {
49         return convertTextToMap( text, "=", "\n" );
50     }
51
52     /**
53      * Converts a property like string to a map. Where the rowdelmiter and
54      * property value delimiter can be defined.
55      * @param text the text to parse/convert
56      * @param propertyValueDelim the delimiter between the property and value,
57      * ie "="
58      * @param rowDelim the row delimiter, normaly \n.
59      * @return a map with values or an empty map if none.
60      * @author Per Jonsson - per.jonsson@it-huset.se
61      */

62     public static Map JavaDoc convertTextToMap( String JavaDoc text, String JavaDoc propertyValueDelim, String JavaDoc rowDelim )
63     {
64         Map JavaDoc map = new HashMap JavaDoc();
65         if ( text != null )
66         {
67             StringTokenizer JavaDoc rowTok = new StringTokenizer JavaDoc( text, rowDelim, false );
68             while ( rowTok.hasMoreTokens() )
69             {
70                 String JavaDoc propVal = rowTok.nextToken();
71                 int index = propVal.indexOf( propertyValueDelim );
72                 if ( index > 0 )
73                 {
74                     map.put( propVal.substring( 0, index ).trim(), propVal.substring( index + 1 ).trim() );
75                 }
76             }
77         }
78         return map;
79     }
80
81     /**
82      * Converts (rather loads) an text String to a Properties object and returns
83      * it.
84      * @param text the text to convert into a Properties / Map object
85      * @return an Map with key values or empty map if none found.
86      */

87     public static Map JavaDoc convertTextToProperties( String JavaDoc text )
88     {
89         Properties JavaDoc properties = new Properties JavaDoc();
90         try
91         {
92             ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc( text.getBytes("ISO-8859-1") );
93             properties.load( is );
94             is.close();
95         }
96         catch ( Exception JavaDoc ignore )
97         {
98             // ignore
99
}
100         
101         return properties;
102     }
103
104     /**
105      * @param args
106      */

107     public static void main( String JavaDoc[] args )
108     {
109         // TODO Auto-generated method stub
110

111     }
112
113 }
114
Popular Tags