KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > PropertiesEditor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.propertyeditors;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 /**
27  * Custom {@link java.beans.PropertyEditor} for {@link Properties} objects.
28  *
29  * <p>Handles conversion from content {@link String} to <code>Properties</code> object.
30  * Also handles {@link Map} to <code>Properties</code> conversion, for populating
31  * a <code>Properties</code> object via XML "map" entries.
32  *
33  * <p>The required format is defined in the standard <code>Properties</code>
34  * documentation. Each property must be on a new line.
35  *
36  * @author Rod Johnson
37  * @author Juergen Hoeller
38  * @see java.util.Properties#load
39  */

40 public class PropertiesEditor extends PropertyEditorSupport JavaDoc {
41     
42     /**
43      * Any of these characters, if they're first after whitespace or first
44      * on a line, mean that the line is a comment and should be ignored.
45      */

46     private final static String JavaDoc COMMENT_MARKERS = "#!";
47
48
49     /**
50      * Convert {@link String} into {@link Properties}, considering it as
51      * properties content.
52      * @param text the text to be so converted
53      */

54     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
55         Properties JavaDoc props = new Properties JavaDoc();
56         if (text != null) {
57             try {
58                 // Must use the ISO-8859-1 encoding because Properties.load(stream) expects it.
59
props.load(new ByteArrayInputStream JavaDoc(text.getBytes("ISO-8859-1")));
60                 dropComments(props);
61             }
62             catch (IOException JavaDoc ex) {
63                 // Should never happen.
64
throw new IllegalArgumentException JavaDoc(
65                         "Failed to parse [" + text + "] into Properties: " + ex.getMessage());
66             }
67         }
68         setValue(props);
69     }
70
71     /**
72      * Take {@link Properties} as-is; convert {@link Map} into <code>Properties</code>.
73      */

74     public void setValue(Object JavaDoc value) {
75         if (!(value instanceof Properties JavaDoc) && value instanceof Map JavaDoc) {
76             Properties JavaDoc props = new Properties JavaDoc();
77             props.putAll((Map JavaDoc) value);
78             super.setValue(props);
79         }
80         else {
81             super.setValue(value);
82         }
83     }
84
85     /**
86      * Remove comment lines, even if they contain whitespace before the
87      * comment marker. This happens automatically on JDK >= 1.4, but we
88      * need to do this manually on JDK 1.3.
89      */

90     private void dropComments(Properties JavaDoc props) {
91         Iterator JavaDoc keys = props.keySet().iterator();
92         while (keys.hasNext()) {
93             String JavaDoc key = (String JavaDoc) keys.next();
94             // A comment line starts with one of our comment markers.
95
if (key.length() > 0 && COMMENT_MARKERS.indexOf(key.charAt(0)) != -1) {
96                 keys.remove();
97             }
98         }
99     }
100
101 }
102
Popular Tags