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 org.apache.maven.doxia.util.ByLineSource;
23  import org.apache.maven.doxia.module.confluence.parser.Block;
24  import org.apache.maven.doxia.module.confluence.parser.BlockParser;
25  import org.apache.maven.doxia.parser.ParseException;
26  
27  /**
28   * <p>ListBlockParser class.</p>
29   *
30   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
31   */
32  public class ListBlockParser
33      implements BlockParser
34  {
35      /** Constant <code>BULLETED_LIST=0</code> */
36      public static final int BULLETED_LIST = 0;
37  
38      /** Constant <code>NUMBERED_LIST=1</code> */
39      public static final int NUMBERED_LIST = 1;
40  
41      /** {@inheritDoc} */
42      public boolean accept( String line, ByLineSource source )
43      {
44          if ( isList( line ) )
45          {
46              return true;
47          }
48  
49          return false;
50      }
51  
52      /** {@inheritDoc} */
53      public Block visit( String line, ByLineSource source )
54          throws ParseException
55      {
56          TreeListBuilder treeListBuilder = new TreeListBuilder();
57  
58          StringBuilder text = new StringBuilder();
59  
60          do
61          {
62              if ( line.trim().length() == 0 )
63              {
64                  break;
65              }
66  
67              if ( text.length() > 0 && isList( line ) )
68              {
69                  // We reached a new line with list prefix
70                  addItem( treeListBuilder, text );
71              }
72  
73              if ( text.length() == 0 )
74              {
75                  text.append( line.trim() );
76              }
77              else
78              {
79                  text.append( " " ).append( line.trim() );
80              }
81  
82          }
83          while ( ( line = source.getNextLine() ) != null );
84  
85          if ( text.length() > 0 )
86          {
87              addItem( treeListBuilder, text );
88          }
89  
90          return treeListBuilder.getBlock();
91      }
92  
93      private void addItem( TreeListBuilder treeListBuilder, StringBuilder text )
94      {
95          String item = text.toString();
96          int level = getLevel( item );
97  
98          if ( isBulletedList( item, level - 1 ) )
99          {
100             treeListBuilder.feedEntry( BULLETED_LIST, level, item.substring( level ) );
101         }
102         else
103         {
104             treeListBuilder.feedEntry( NUMBERED_LIST, level, item.substring( level ) );
105         }
106         text.setLength( 0 );
107     }
108 
109     private int getLevel( String line )
110     {
111         int level = 0;
112 
113         while ( line.charAt( level ) == '*' || line.charAt( level ) == '-' || line.charAt( level ) == '#' )
114         {
115             level++;
116         }
117 
118         return level;
119     }
120 
121     private boolean isBulletedList( String line, int deph )
122     {
123         return ( line.charAt( deph ) == '*' || line.charAt( deph ) == '-' );
124     }
125 
126     private boolean isList( String line )
127     {
128         line = line.trim();
129 
130         if ( line.startsWith( "*" ) || line.startsWith( "-" ) || line.startsWith( "#" ) )
131         {
132             String temp = line.substring( 1 );
133             while ( temp.length() > 0
134                 && ( temp.charAt( 0 ) == '*' || temp.charAt( 0 ) == '-' || temp.charAt( 0 ) == '#' ) )
135             {
136                 temp = temp.substring( 1 );
137             }
138 
139             if ( temp.length() > 0 && temp.charAt( 0 ) == ' ' )
140             {
141                 return true;
142             }
143         }
144 
145         return false;
146     }
147 }