Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TreeUtils |
|
| 3.0;3 |
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 | 0 | { |
35 | 0 | } |
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 | 13 | if (stream != null) |
45 | { | |
46 | 13 | printTree(stream, "", result); |
47 | } | |
48 | 13 | } |
49 | ||
50 | private static void printTree(PrintStream stream, String indent, ConfigurationNode result) | |
51 | { | |
52 | 501 | StringBuffer buffer = new StringBuffer(indent).append("<").append(result.getName()); |
53 | 501 | Iterator<ConfigurationNode> iter = result.getAttributes().iterator(); |
54 | 620 | while (iter.hasNext()) |
55 | { | |
56 | 119 | ConfigurationNode node = iter.next(); |
57 | 119 | buffer.append(" ").append(node.getName()).append("='").append(node.getValue()).append("'"); |
58 | 119 | } |
59 | 501 | buffer.append(">"); |
60 | 501 | stream.print(buffer.toString()); |
61 | 501 | if (result.getValue() != null) |
62 | { | |
63 | 279 | stream.print(result.getValue()); |
64 | } | |
65 | 501 | boolean newline = false; |
66 | 501 | if (result.getChildrenCount() > 0) |
67 | { | |
68 | 173 | stream.print("\n"); |
69 | 173 | iter = result.getChildren().iterator(); |
70 | 661 | while (iter.hasNext()) |
71 | { | |
72 | 488 | printTree(stream, indent + " ", iter.next()); |
73 | } | |
74 | 173 | newline = true; |
75 | } | |
76 | 501 | if (newline) |
77 | { | |
78 | 173 | stream.print(indent); |
79 | } | |
80 | 501 | stream.println("</" + result.getName() + ">"); |
81 | 501 | } |
82 | } |