001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.fulcrum.yaafi.framework.util;
020    
021    import java.util.Map;
022    
023    import org.apache.avalon.framework.configuration.Configuration;
024    import org.apache.avalon.framework.configuration.ConfigurationException;
025    import org.apache.avalon.framework.configuration.DefaultConfiguration;
026    import org.apache.avalon.framework.logger.Logger;
027    
028    /**
029     * Helper class to expand the value and all attributes.
030     *
031     * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
032     */
033    public class ConfigurationUtil
034    {
035        /**
036         * Expand place holders found in values or attrbute values with the
037         * content of the given variables. The implementation assumes that
038         * the given configuration can be cast to a DefaultConfiguration
039         * otherwise we can't use any setters.
040         *
041         * @param logger the logger to write diagnostic messages
042         * @param defaultConfiguration the configuration
043         * @param vars the map holding the variables
044         * @throws ConfigurationException parsing the configuration failed
045         */
046        public static void expand(Logger logger, DefaultConfiguration defaultConfiguration, Map vars) throws ConfigurationException
047        {
048            if((vars == null) || (vars.size() == 0))
049            {
050                return;
051            }
052    
053            // update the value of the configuration element
054    
055            if(defaultConfiguration.getValue(null) != null)
056            {
057                String oldValue = defaultConfiguration.getValue();
058                String newValue = ConfigurationUtil.expand(oldValue, vars);
059                defaultConfiguration.setValue(newValue);
060    
061                if(oldValue.equals(newValue) == false)
062                {
063                    logger.debug("Changed element <"
064                        + defaultConfiguration.getName()
065                        + "> from '"
066                        + oldValue
067                        + "' ==> '"
068                        + newValue
069                        + "'"
070                        );
071                }
072            }
073    
074            // update all attributes
075    
076            String attributeName = null;
077            String[] attributeNames = defaultConfiguration.getAttributeNames();
078    
079            for(int i=0; i<attributeNames.length; i++)
080            {
081                attributeName = attributeNames[i];
082                String oldAttributeValue = defaultConfiguration.getAttribute(attributeName);
083                String newAttributeValue = ConfigurationUtil.expand(oldAttributeValue, vars);
084                defaultConfiguration.setAttribute(attributeName, newAttributeValue);
085    
086                if(oldAttributeValue.equals(newAttributeValue) == false)
087                {
088                    logger.debug("Changed attribute '"
089                        + defaultConfiguration.getName() + "@" + attributeName
090                        + "' from '"
091                        + oldAttributeValue
092                        + "' ==> '"
093                        + newAttributeValue
094                        + "'"
095                        );
096                }
097            }
098    
099            // and now recurse through all children (children are in general a lot of work)
100    
101            Configuration[] children = defaultConfiguration.getChildren();
102    
103            for(int i=0; i<children.length; i++)
104            {
105                ConfigurationUtil.expand(logger, ((DefaultConfiguration) children[i]), vars);
106            }
107        }
108    
109        /**
110         * @return the expand a string
111         */
112        private static String expand(String value, Map vars)
113        {
114            return StringUtils.stringSubstitution(value, vars, true).toString();
115        }
116    }