001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.propertyeditors;
018:
019: import java.beans.PropertyEditorSupport;
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Properties;
025:
026: /**
027: * Custom {@link java.beans.PropertyEditor} for {@link Properties} objects.
028: *
029: * <p>Handles conversion from content {@link String} to <code>Properties</code> object.
030: * Also handles {@link Map} to <code>Properties</code> conversion, for populating
031: * a <code>Properties</code> object via XML "map" entries.
032: *
033: * <p>The required format is defined in the standard <code>Properties</code>
034: * documentation. Each property must be on a new line.
035: *
036: * @author Rod Johnson
037: * @author Juergen Hoeller
038: * @see java.util.Properties#load
039: */
040: public class PropertiesEditor extends PropertyEditorSupport {
041:
042: /**
043: * Any of these characters, if they're first after whitespace or first
044: * on a line, mean that the line is a comment and should be ignored.
045: */
046: private final static String COMMENT_MARKERS = "#!";
047:
048: /**
049: * Convert {@link String} into {@link Properties}, considering it as
050: * properties content.
051: * @param text the text to be so converted
052: */
053: public void setAsText(String text) throws IllegalArgumentException {
054: Properties props = new Properties();
055: if (text != null) {
056: try {
057: // Must use the ISO-8859-1 encoding because Properties.load(stream) expects it.
058: props.load(new ByteArrayInputStream(text
059: .getBytes("ISO-8859-1")));
060: dropComments(props);
061: } catch (IOException ex) {
062: // Should never happen.
063: throw new IllegalArgumentException("Failed to parse ["
064: + text + "] into Properties: "
065: + ex.getMessage());
066: }
067: }
068: setValue(props);
069: }
070:
071: /**
072: * Take {@link Properties} as-is; convert {@link Map} into <code>Properties</code>.
073: */
074: public void setValue(Object value) {
075: if (!(value instanceof Properties) && value instanceof Map) {
076: Properties props = new Properties();
077: props.putAll((Map) value);
078: super .setValue(props);
079: } else {
080: super .setValue(value);
081: }
082: }
083:
084: /**
085: * Remove comment lines, even if they contain whitespace before the
086: * comment marker. This happens automatically on JDK >= 1.4, but we
087: * need to do this manually on JDK 1.3.
088: */
089: private void dropComments(Properties props) {
090: Iterator keys = props.keySet().iterator();
091: while (keys.hasNext()) {
092: String key = (String) keys.next();
093: // A comment line starts with one of our comment markers.
094: if (key.length() > 0
095: && COMMENT_MARKERS.indexOf(key.charAt(0)) != -1) {
096: keys.remove();
097: }
098: }
099: }
100:
101: }
|