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  public class TreeListBuilder
32  {
33      private TreeComponent root;
34  
35      private TreeComponent current;
36  
37      TreeListBuilder()
38      {
39          root = new TreeComponent( null, "root", 0 );
40  
41          current = root;
42      }
43  
44      void feedEntry( int type, int level, String text )
45      {
46          int currentDepth = current.getDepth();
47  
48          int incomingLevel = level - 1;
49  
50          if ( incomingLevel == currentDepth )
51          {
52              // nothing to move
53          }
54          else if ( incomingLevel > currentDepth )
55          {
56              // el actual ahora es el �ltimo que insert�
57              List<TreeComponent> components = current.getChildren();
58  
59              if ( components.size() == 0 )
60              {
61                  /* for example:
62                   *        * item1
63                   *     * item2
64                   */
65                  for ( int i = 0, n = incomingLevel - currentDepth; i < n; i++ )
66                  {
67                      current = current.addChildren( "", type );
68                  }
69              }
70              else
71              {
72                  current = components.get( components.size() - 1 );
73              }
74          }
75          else
76          {
77              for ( int i = 0, n = currentDepth - incomingLevel; i < n; i++ )
78              {
79                  current = current.getFather();
80  
81                  if ( current == null )
82                  {
83                      throw new IllegalStateException();
84                  }
85              }
86          }
87          current.addChildren( text.trim(), type );
88      }
89  
90      ListBlock getBlock()
91      {
92          return getList( root );
93      }
94  
95      private ListBlock getList( TreeComponent treeComponent )
96      {
97          List<Block> list = getListItems( treeComponent );
98  
99          int type = treeComponent.getChildren().get( 0 ).getType();
100 
101         if ( type == ListBlockParser.BULLETED_LIST )
102         {
103             return new BulletedListBlock( list );
104         }
105 
106         return new NumberedListBlock( list );
107     }
108 
109     private List<Block> getListItems( TreeComponent tc )
110     {
111         List<Block> blocks = new ArrayList<>();
112 
113         for ( TreeComponent child : tc.getChildren() )
114         {
115             List<Block> childBlocks = new ArrayList<>();
116 
117             if ( child.getFather() != null )
118             {
119                 childBlocks.addAll( new ChildBlocksBuilder( child.getText() ).getBlocks() );
120             }
121 
122             if ( child.getChildren().size() != 0 )
123             {
124                 blocks.add( new ListItemBlock( childBlocks, getList( child ) ) );
125             }
126             else
127             {
128                 blocks.add( new ListItemBlock( childBlocks ) );
129             }
130         }
131 
132         return blocks;
133     }
134 }