001package org.apache.maven.doxia.module.confluence.parser.list;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.maven.doxia.module.confluence.parser.Block;
026import org.apache.maven.doxia.module.confluence.parser.ChildBlocksBuilder;
027
028/**
029 * <p>TreeListBuilder class.</p>
030 *
031 * @version $Id: TreeListBuilder.html 905940 2014-04-12 16:27:29Z hboutemy $
032 */
033public class TreeListBuilder
034{
035    private TreeComponent root;
036
037    private TreeComponent current;
038
039    TreeListBuilder()
040    {
041        root = new TreeComponent( null, "root", 0 );
042
043        current = root;
044    }
045
046    void feedEntry( int type, int level, String text )
047    {
048        int currentDepth = current.getDepth();
049
050        int incomingLevel = level - 1;
051
052        if ( incomingLevel == currentDepth )
053        {
054            // nothing to move
055        }
056        else if ( incomingLevel > currentDepth )
057        {
058            // el actual ahora es el �ltimo que insert�
059            List<TreeComponent> components = current.getChildren();
060
061            if ( components.size() == 0 )
062            {
063                /* for example:
064                 *        * item1
065                 *     * item2
066                 */
067                for ( int i = 0, n = incomingLevel - currentDepth; i < n; i++ )
068                {
069                    current = current.addChildren( "", type );
070                }
071            }
072            else
073            {
074                current = components.get( components.size() - 1 );
075            }
076        }
077        else
078        {
079            for ( int i = 0, n = currentDepth - incomingLevel; i < n; i++ )
080            {
081                current = current.getFather();
082
083                if ( current == null )
084                {
085                    throw new IllegalStateException();
086                }
087            }
088        }
089        current.addChildren( text.trim(), type );
090    }
091
092    ListBlock getBlock()
093    {
094        return getList( root );
095    }
096
097    private ListBlock getList( TreeComponent treeComponent )
098    {
099        List<Block> list = getListItems( treeComponent );
100
101        int type = treeComponent.getChildren().get( 0 ).getType();
102
103        if ( type == ListBlockParser.BULLETED_LIST )
104        {
105            return new BulletedListBlock( list );
106        }
107
108        return new NumberedListBlock( list );
109    }
110
111    private List<Block> getListItems( TreeComponent tc )
112    {
113        List<Block> blocks = new ArrayList<Block>();
114
115        for ( TreeComponent child : tc.getChildren() )
116        {
117            List<Block> childBlocks = new ArrayList<Block>();
118
119            if ( child.getFather() != null )
120            {
121                childBlocks.addAll( new ChildBlocksBuilder( child.getText() ).getBlocks() );
122            }
123
124            if ( child.getChildren().size() != 0 )
125            {
126                blocks.add( new ListItemBlock( childBlocks, getList( child ) ) );
127            }
128            else
129            {
130                blocks.add( new ListItemBlock( childBlocks ) );
131            }
132        }
133
134        return blocks;
135    }
136}