Coverage Report - org.apache.maven.doxia.module.twiki.parser.TableBlockParser
 
Classes in this File Line Coverage Branch Coverage Complexity
TableBlockParser
92%
23/25
70%
14/20
4,333
 
 1  
 package org.apache.maven.doxia.module.twiki.parser;
 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  
 import java.util.regex.Matcher;
 25  
 import java.util.regex.Pattern;
 26  
 
 27  
 import org.apache.maven.doxia.util.ByLineSource;
 28  
 import org.apache.maven.doxia.parser.ParseException;
 29  
 
 30  
 /**
 31  
  * Parse tables
 32  
  *
 33  
  * @author Juan F. Codagnone
 34  
  * @version $Id: TableBlockParser.java 1090706 2011-04-09 23:15:28Z hboutemy $
 35  
  */
 36  198
 public class TableBlockParser
 37  
     implements BlockParser
 38  
 {
 39  
     /**
 40  
      * pattern to detect tables
 41  
      */
 42  2
     private static final Pattern TABLE_PATTERN = Pattern.compile( "^\\s*([|].*[|])+\\s*$" );
 43  
 
 44  
     /**
 45  
      * text parser
 46  
      */
 47  
     private FormatedTextParser textParser;
 48  
 
 49  
     /** {@inheritDoc} */
 50  
     public final boolean accept( final String line )
 51  
     {
 52  148
         return TABLE_PATTERN.matcher( line ).lookingAt();
 53  
     }
 54  
 
 55  
     /**
 56  
      * {@inheritDoc}
 57  
      */
 58  
     public final Block visit( final String line, final ByLineSource source )
 59  
         throws ParseException
 60  
     {
 61  8
         if ( !accept( line ) )
 62  
         {
 63  0
             throw new IllegalAccessError( "call accept before this ;)" );
 64  
         }
 65  
 
 66  8
         final List<Block> rows = new ArrayList<Block>();
 67  8
         String l = line;
 68  
 
 69  
         do
 70  
         {
 71  22
             final Matcher m = TABLE_PATTERN.matcher( l );
 72  22
             if ( m.lookingAt() )
 73  
             {
 74  22
                 final List<Block> cells = new ArrayList<Block>();
 75  
 
 76  
                 /* for each cell... */
 77  82
                 for ( int lh = l.indexOf( '|' ) + 1, rh; ( rh = l.indexOf( '|', lh ) ) != -1; lh = rh + 1 )
 78  
                 {
 79  60
                     final Block[] bs = textParser.parse( l.substring( lh, rh ).trim() );
 80  60
                     if ( bs.length == 1 && bs[0] instanceof BoldBlock )
 81  
                     {
 82  16
                         final Block[] tmp = ( (BoldBlock) bs[0] ).getBlocks();
 83  16
                         cells.add( new TableCellHeaderBlock( tmp ) );
 84  16
                     }
 85  
                     else
 86  
                     {
 87  44
                         cells.add( new TableCellBlock( bs ) );
 88  
                     }
 89  
                 }
 90  22
                 rows.add( new TableRowBlock( (Block[]) cells.toArray( new Block[] {} ) ) );
 91  
             }
 92  
 
 93  
         }
 94  22
         while ( ( l = source.getNextLine() ) != null && accept( l ) );
 95  
 
 96  8
         assert rows.size() >= 1;
 97  
 
 98  8
         return new TableBlock( rows.toArray( new Block[] {} ) );
 99  
     }
 100  
 
 101  
     /**
 102  
      * <p>Setter for the field <code>textParser</code>.</p>
 103  
      *
 104  
      * @param textParser text parser to be set
 105  
      */
 106  
     public final void setTextParser( final FormatedTextParser textParser )
 107  
     {
 108  208
         if ( textParser == null )
 109  
         {
 110  0
             throw new IllegalArgumentException( "argument can't be null" );
 111  
         }
 112  
 
 113  208
         this.textParser = textParser;
 114  208
     }
 115  
 }