001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.configuration;
019:
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.Vector;
025:
026: import org.apache.commons.collections.ExtendedProperties;
027: import org.apache.commons.lang.StringUtils;
028:
029: /**
030: * Configuration converter. Helper class to convert between Configuration,
031: * ExtendedProperties and standard Properties.
032: *
033: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
034: * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
035: */
036: public final class ConfigurationConverter {
037: /**
038: * Private constructor prevents instances from being created.
039: */
040: private ConfigurationConverter() {
041: // to prevent instanciation...
042: }
043:
044: /**
045: * Convert a ExtendedProperties class into a Configuration class.
046: *
047: * @param eprops ExtendedProperties object to convert
048: * @return Configuration created from the ExtendedProperties
049: */
050: public static Configuration getConfiguration(
051: ExtendedProperties eprops) {
052: return new MapConfiguration(eprops);
053: }
054:
055: /**
056: * Convert a standard Properties class into a configuration class.
057: *
058: * @param props properties object to convert
059: * @return Configuration configuration created from the Properties
060: */
061: public static Configuration getConfiguration(Properties props) {
062: return new MapConfiguration(props);
063: }
064:
065: /**
066: * Convert a Configuration class into a ExtendedProperties class.
067: *
068: * @param config Configuration object to convert
069: * @return ExtendedProperties created from the Configuration
070: */
071: public static ExtendedProperties getExtendedProperties(
072: Configuration config) {
073: ExtendedProperties props = new ExtendedProperties();
074:
075: Iterator keys = config.getKeys();
076:
077: while (keys.hasNext()) {
078: String key = (String) keys.next();
079: Object property = config.getProperty(key);
080:
081: // turn lists into vectors
082: if (property instanceof List) {
083: property = new Vector((List) property);
084: }
085:
086: props.setProperty(key, property);
087: }
088:
089: return props;
090: }
091:
092: /**
093: * Convert a Configuration class into a Properties class. List properties
094: * are joined into a string using the delimiter of the configuration if it
095: * extends AbstractConfiguration, and a comma otherwise.
096: *
097: * @param config Configuration object to convert
098: * @return Properties created from the Configuration
099: */
100: public static Properties getProperties(Configuration config) {
101: Properties props = new Properties();
102:
103: char delimiter = (config instanceof AbstractConfiguration) ? ((AbstractConfiguration) config)
104: .getListDelimiter()
105: : ',';
106:
107: Iterator keys = config.getKeys();
108: while (keys.hasNext()) {
109: String key = (String) keys.next();
110: List list = config.getList(key);
111:
112: // turn the list into a string
113: props.setProperty(key, StringUtils.join(list.iterator(),
114: delimiter));
115: }
116:
117: return props;
118: }
119:
120: /**
121: * Convert a Configuration class into a Map class.
122: *
123: * @param config Configuration object to convert
124: * @return Map created from the Configuration
125: */
126: public static Map getMap(Configuration config) {
127: return new ConfigurationMap(config);
128: }
129:
130: }
|