View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration.tree;
18  
19  import java.io.PrintStream;
20  import java.util.Iterator;
21  
22  /**
23   * Utility methods.
24   * @author <a
25   * href="http://commons.apache.org/configuration/team-list.html">Commons
26   * Configuration team</a>
27   * @version $Id: TreeUtils.java 1301991 2012-03-17 20:18:02Z sebb $
28   * @since 1.7
29   */
30  public final class TreeUtils
31  {
32      /** Prevent creating this class. */
33      private TreeUtils()
34      {
35      }
36  
37      /**
38       * Print out the data in the configuration.
39       * @param stream The OutputStream.
40       * @param result The root node of the tree.
41       */
42      public static void printTree(PrintStream stream, ConfigurationNode result)
43      {
44          if (stream != null)
45          {
46              printTree(stream, "", result);
47          }
48      }
49  
50      private static void printTree(PrintStream stream, String indent, ConfigurationNode result)
51      {
52          StringBuffer buffer = new StringBuffer(indent).append("<").append(result.getName());
53          Iterator<ConfigurationNode> iter = result.getAttributes().iterator();
54          while (iter.hasNext())
55          {
56              ConfigurationNode node = iter.next();
57              buffer.append(" ").append(node.getName()).append("='").append(node.getValue()).append("'");
58          }
59          buffer.append(">");
60          stream.print(buffer.toString());
61          if (result.getValue() != null)
62          {
63              stream.print(result.getValue());
64          }
65          boolean newline = false;
66          if (result.getChildrenCount() > 0)
67          {
68              stream.print("\n");
69              iter = result.getChildren().iterator();
70              while (iter.hasNext())
71              {
72                  printTree(stream, indent + "  ", iter.next());
73              }
74              newline = true;
75          }
76          if (newline)
77          {
78              stream.print(indent);
79          }
80          stream.println("</" + result.getName() + ">");
81      }
82  }