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    package org.apache.commons.configuration.tree;
018    
019    import java.io.PrintStream;
020    import java.util.Iterator;
021    
022    /**
023     * Utility methods.
024     * @author <a
025     * href="http://commons.apache.org/configuration/team-list.html">Commons
026     * Configuration team</a>
027     * @version $Id: TreeUtils.java 1206489 2011-11-26 16:43:03Z oheger $
028     * @since 1.7
029     */
030    public final class TreeUtils
031    {
032        /** Prevent creating this class. */
033        private TreeUtils()
034        {
035        }
036    
037        /**
038         * Print out the data in the configuration.
039         * @param stream The OutputStream.
040         * @param result The root node of the tree.
041         */
042        public static void printTree(PrintStream stream, ConfigurationNode result)
043        {
044            if (stream != null)
045            {
046                printTree(stream, "", result);
047            }
048        }
049    
050        private static void printTree(PrintStream stream, String indent, ConfigurationNode result)
051        {
052            StringBuffer buffer = new StringBuffer(indent).append("<").append(result.getName());
053            Iterator<ConfigurationNode> iter = result.getAttributes().iterator();
054            while (iter.hasNext())
055            {
056                ConfigurationNode node = (ConfigurationNode) iter.next();
057                buffer.append(" ").append(node.getName()).append("='").append(node.getValue()).append("'");
058            }
059            buffer.append(">");
060            stream.print(buffer.toString());
061            if (result.getValue() != null)
062            {
063                stream.print(result.getValue());
064            }
065            boolean newline = false;
066            if (result.getChildrenCount() > 0)
067            {
068                stream.print("\n");
069                iter = result.getChildren().iterator();
070                while (iter.hasNext())
071                {
072                    printTree(stream, indent + "  ", (ConfigurationNode) iter.next());
073                }
074                newline = true;
075            }
076            if (newline)
077            {
078                stream.print(indent);
079            }
080            stream.println("</" + result.getName() + ">");
081        }
082    }