Coverage Report - org.apache.maven.doxia.module.confluence.parser.list.ListBlockParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ListBlockParser
100%
37/37
93%
41/44
4,833
 
 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  162
 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  418
         if ( isList( line ) )
 46  
         {
 47  62
             return true;
 48  
         }
 49  
 
 50  356
         return false;
 51  
     }
 52  
 
 53  
     /** {@inheritDoc} */
 54  
     public Block visit( String line, ByLineSource source )
 55  
         throws ParseException
 56  
     {
 57  60
         TreeListBuilder treeListBuilder = new TreeListBuilder();
 58  
 
 59  60
         StringBuffer text = new StringBuffer();
 60  
 
 61  
         do
 62  
         {
 63  180
             if ( line.trim().length() == 0 )
 64  
             {
 65  46
                 break;
 66  
             }
 67  
 
 68  134
             if ( text.length() > 0 && isList( line ) )
 69  
             {
 70  
                 // We reached a new line with list prefix
 71  58
                 addItem( treeListBuilder, text );
 72  
             }
 73  
 
 74  134
             if ( text.length() == 0 )
 75  
             {
 76  118
                 text.append( line.trim() );
 77  
             }
 78  
             else
 79  
             {
 80  16
                 text.append( " " + line.trim() );
 81  
             }
 82  
 
 83  
         }
 84  134
         while ( ( line = source.getNextLine() ) != null );
 85  
 
 86  60
         if ( text.length() > 0 )
 87  
         {
 88  60
             addItem( treeListBuilder, text );
 89  
         }
 90  
 
 91  60
         return treeListBuilder.getBlock();
 92  
     }
 93  
 
 94  
     private void addItem( TreeListBuilder treeListBuilder, StringBuffer text )
 95  
     {
 96  118
         String item = text.toString();
 97  118
         int level = getLevel( item );
 98  
 
 99  118
         if ( isBulletedList( item, level - 1 ) )
 100  
         {
 101  96
             treeListBuilder.feedEntry( BULLETED_LIST, level, item.substring( level ) );
 102  
         }
 103  
         else
 104  
         {
 105  22
             treeListBuilder.feedEntry( NUMBERED_LIST, level, item.substring( level ) );
 106  
         }
 107  118
         text.setLength( 0 );
 108  118
     }
 109  
 
 110  
     private int getLevel( String line )
 111  
     {
 112  118
         int level = 0;
 113  
 
 114  256
         while ( line.charAt( level ) == '*' || line.charAt( level ) == '-' || line.charAt( level ) == '#' )
 115  
         {
 116  138
             level++;
 117  
         }
 118  
 
 119  118
         return level;
 120  
     }
 121  
 
 122  
     private boolean isBulletedList( String line, int deph )
 123  
     {
 124  118
         return ( line.charAt( deph ) == '*' || line.charAt( deph ) == '-' );
 125  
     }
 126  
 
 127  
     private boolean isList( String line )
 128  
     {
 129  492
         line = line.trim();
 130  
 
 131  492
         if ( line.startsWith( "*" ) || line.startsWith( "-" ) || line.startsWith( "#" ) )
 132  
         {
 133  126
             String temp = line.substring( 1 );
 134  150
             while ( temp.length() > 0 && ( temp.charAt( 0 ) == '*' || temp.charAt( 0 ) == '-' || temp.charAt( 0 ) == '#' ) )
 135  
             {
 136  24
                 temp = temp.substring( 1 );
 137  
             }
 138  
 
 139  126
             if ( temp.length() > 0 && temp.charAt( 0 ) == ' ' )
 140  
             {
 141  120
                 return true;
 142  
             }
 143  
         }
 144  
 
 145  372
         return false;
 146  
     }
 147  
 }