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   * @version $Id: ListBlockParser.java 947266 2010-05-22 07:49:50Z ltheussl $
32   */
33  public class ListBlockParser
34      implements BlockParser
35  {
36      /** Constant <code>BULLETED_LIST=0</code> */
37      public static final int BULLETED_LIST = 0;
38  
39      /** Constant <code>NUMBERED_LIST=1</code> */
40      public static final int NUMBERED_LIST = 1;
41  
42      /** {@inheritDoc} */
43      public boolean accept( String line, ByLineSource source )
44      {
45          if ( isList( line ) )
46          {
47              return true;
48          }
49  
50          return false;
51      }
52  
53      /** {@inheritDoc} */
54      public Block visit( String line, ByLineSource source )
55          throws ParseException
56      {
57          TreeListBuilder treeListBuilder = new TreeListBuilder();
58  
59          StringBuffer text = new StringBuffer();
60  
61          do
62          {
63              if ( line.trim().length() == 0 )
64              {
65                  break;
66              }
67  
68              if ( text.length() > 0 && isList( line ) )
69              {
70                  // We reached a new line with list prefix
71                  addItem( treeListBuilder, text );
72              }
73  
74              if ( text.length() == 0 )
75              {
76                  text.append( line.trim() );
77              }
78              else
79              {
80                  text.append( " " + line.trim() );
81              }
82  
83          }
84          while ( ( line = source.getNextLine() ) != null );
85  
86          if ( text.length() > 0 )
87          {
88              addItem( treeListBuilder, text );
89          }
90  
91          return treeListBuilder.getBlock();
92      }
93  
94      private void addItem( TreeListBuilder treeListBuilder, StringBuffer text )
95      {
96          String item = text.toString();
97          int level = getLevel( item );
98  
99          if ( isBulletedList( item, level - 1 ) )
100         {
101             treeListBuilder.feedEntry( BULLETED_LIST, level, item.substring( level ) );
102         }
103         else
104         {
105             treeListBuilder.feedEntry( NUMBERED_LIST, level, item.substring( level ) );
106         }
107         text.setLength( 0 );
108     }
109 
110     private int getLevel( String line )
111     {
112         int level = 0;
113 
114         while ( line.charAt( level ) == '*' || line.charAt( level ) == '-' || line.charAt( level ) == '#' )
115         {
116             level++;
117         }
118 
119         return level;
120     }
121 
122     private boolean isBulletedList( String line, int deph )
123     {
124         return ( line.charAt( deph ) == '*' || line.charAt( deph ) == '-' );
125     }
126 
127     private boolean isList( String line )
128     {
129         line = line.trim();
130 
131         if ( line.startsWith( "*" ) || line.startsWith( "-" ) || line.startsWith( "#" ) )
132         {
133             String temp = line.substring( 1 );
134             while ( temp.length() > 0 && ( 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 }