    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 *     https://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 */
    017package org.apache.commons.configuration2.tree;
    018
    019import java.io.PrintStream;
    020
    021/**
    022 * Utility methods.
    023 *
    024 * @since 1.7
    025 */
    026public final class TreeUtils {
    027    /**
    028     * Print out the data in the configuration.
    029     *
    030     * @param stream The OutputStream.
    031     * @param result The root node of the tree.
    032     */
    033    public static void printTree(final PrintStream stream, final ImmutableNode result) {
    034        if (stream != null) {
    035            printTree(stream, "", result);
    036        }
    037    }
    038
    039    private static void printTree(final PrintStream stream, final String indent, final ImmutableNode result) {
    040        final StringBuilder buffer = new StringBuilder(indent).append("<").append(result.getNodeName());
    041        result.getAttributes().forEach((k, v) -> buffer.append(' ').append(k).append("='").append(v).append("'"));
    042        buffer.append(">");
    043        stream.print(buffer.toString());
    044        if (result.getValue() != null) {
    045            stream.print(result.getValue());
    046        }
    047        boolean newline = false;
    048        if (!result.getChildren().isEmpty()) {
    049            stream.print("\n");
    050            result.forEach(child -> printTree(stream, indent + "  ", child));
    051            newline = true;
    052        }
    053        if (newline) {
    054            stream.print(indent);
    055        }
    056        stream.println("</" + result.getNodeName() + ">");
    057    }
    058
    059    /** Prevent creating this class. */
    060    private TreeUtils() {
    061    }
    062}