001/* $Id: Box.java 1102402 2011-05-12 18:03:26Z simonetripodi $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.commons.digester3;
020
021import java.util.LinkedList;
022import java.util.List;
023
024/**
025 * Simple class for use in unit tests. A box has an ID, and can have multiple boxes within it.
026 */
027public class Box
028{
029    private String id;
030
031    private List<Box> children = new LinkedList<Box>();
032
033    public Box()
034    {
035    }
036
037    public String getId()
038    {
039        return id;
040    }
041
042    public void setId( String id )
043    {
044        this.id = id;
045    }
046
047    public void addChild( Box child )
048    {
049        this.children.add( child );
050    }
051
052    public List<Box> getChildren()
053    {
054        return children;
055    }
056
057    @Override
058    public String toString()
059    {
060        StringBuilder buf = new StringBuilder();
061        buf.append( "[Box] id=" );
062        buf.append( id );
063        buf.append( " nchildren=" );
064        buf.append( children.size() );
065
066        for ( Box child : children )
067        {
068            buf.append( "  " );
069            buf.append( child.toString() );
070        }
071        return buf.toString();
072    }
073
074    /**
075     * Return a string containing this object's name value, followed by the names of all child objects (and their
076     * children etc) in pre-order sequence. Each name is separated by a space from the preceding one.
077     */
078    public String getIds()
079    {
080        StringBuilder buf = new StringBuilder();
081        buf.append( this.id );
082        for ( Box child : children )
083        {
084            buf.append( " " );
085            buf.append( child.getIds() );
086        }
087        return buf.toString();
088    }
089}