View Javadoc

1   package org.apache.maven.doxia.module.confluence.parser.list;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.doxia.module.confluence.parser.Block;
26  import org.apache.maven.doxia.module.confluence.parser.ChildBlocksBuilder;
27  
28  /**
29   * <p>TreeListBuilder class.</p>
30   *
31   * @version $Id: TreeListBuilder.java 1090706 2011-04-09 23:15:28Z hboutemy $
32   */
33  public class TreeListBuilder
34  {
35      private TreeComponent root;
36  
37      private TreeComponent current;
38  
39      TreeListBuilder()
40      {
41          root = new TreeComponent( null, "root", 0 );
42  
43          current = root;
44      }
45  
46      void feedEntry( int type, int level, String text )
47      {
48          int currentDepth = current.getDepth();
49  
50          int incomingLevel = level - 1;
51  
52          if ( incomingLevel == currentDepth )
53          {
54              // nothing to move
55          }
56          else if ( incomingLevel > currentDepth )
57          {
58              // el actual ahora es el �ltimo que insert�
59              List<TreeComponent> components = current.getChildren();
60  
61              if ( components.size() == 0 )
62              {
63                  /* for example:
64                   *        * item1
65                   *     * item2
66                   */
67                  for ( int i = 0, n = incomingLevel - currentDepth; i < n; i++ )
68                  {
69                      current = current.addChildren( "", type );
70                  }
71              }
72              else
73              {
74                  current = components.get( components.size() - 1 );
75              }
76          }
77          else
78          {
79              for ( int i = 0, n = currentDepth - incomingLevel; i < n; i++ )
80              {
81                  current = current.getFather();
82  
83                  if ( current == null )
84                  {
85                      throw new IllegalStateException();
86                  }
87              }
88          }
89          current.addChildren( text.trim(), type );
90      }
91  
92      ListBlock getBlock()
93      {
94          return getList( root );
95      }
96  
97      private ListBlock getList( TreeComponent treeComponent )
98      {
99          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 }