Coverage Report - org.apache.maven.doxia.module.confluence.parser.table.TableBlockParser
 
Classes in this File Line Coverage Branch Coverage Complexity
TableBlockParser
97%
39/40
86%
19/22
6
 
 1  
 package org.apache.maven.doxia.module.confluence.parser.table;
 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.io.StringReader;
 23  
 import java.util.ArrayList;
 24  
 import java.util.LinkedList;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.maven.doxia.util.ByLineReaderSource;
 28  
 import org.apache.maven.doxia.module.confluence.ConfluenceMarkup;
 29  
 import org.apache.maven.doxia.module.confluence.parser.FigureBlockParser;
 30  
 import org.apache.maven.doxia.module.confluence.parser.ParagraphBlockParser;
 31  
 import org.apache.maven.doxia.module.confluence.parser.SectionBlockParser;
 32  
 import org.apache.maven.doxia.util.ByLineSource;
 33  
 import org.apache.maven.doxia.module.confluence.parser.BlockParser;
 34  
 import org.apache.maven.doxia.module.confluence.parser.Block;
 35  
 import org.apache.maven.doxia.module.confluence.parser.BoldBlock;
 36  
 import org.apache.maven.doxia.parser.ParseException;
 37  
 import org.codehaus.plexus.util.StringUtils;
 38  
 
 39  
 /**
 40  
  * Parse tables
 41  
  *
 42  
  * @author Juan F. Codagnone
 43  
  * @version $Id: TableBlockParser.java 1090706 2011-04-09 23:15:28Z hboutemy $
 44  
  */
 45  164
 public class TableBlockParser
 46  
     implements BlockParser
 47  
 {
 48  
     private static final String EMPTY_STRING = "";
 49  
     private static final String ANY_CHARACTER = ".*";
 50  
     private static final String ESCAPE_CHARACTER = "\\";
 51  
 
 52  
     /** {@inheritDoc} */
 53  
     public  boolean accept( String line, ByLineSource source )
 54  
     {
 55  382
         return line.startsWith( "|" );
 56  
     }
 57  
 
 58  
     /** {@inheritDoc} */
 59  
     public  Block visit(  String line,  ByLineSource source )
 60  
         throws ParseException
 61  
     {
 62  8
         if ( !accept( line, source ) )
 63  
         {
 64  0
             throw new IllegalAccessError( "call accept before this ;)" );
 65  
         }
 66  
 
 67  8
         List<Block> rows = new ArrayList<Block>();
 68  
 
 69  8
         String l = line;
 70  
 
 71  
         do
 72  
         {
 73  20
             l = l.substring( 0, l.lastIndexOf( "|" ) );
 74  
 
 75  20
             List<Block> cells = new ArrayList<Block>();
 76  
 
 77  20
             BlockParser headingParser = new SectionBlockParser();
 78  20
             BlockParser figureParser = new FigureBlockParser();
 79  20
             BlockParser[] subparsers = new BlockParser[] { headingParser, figureParser };
 80  20
             BlockParser paragraphParser = new ParagraphBlockParser( subparsers );
 81  
 
 82  20
             if ( l.startsWith( "||" ) )
 83  
             {
 84  8
                 String[] text = StringUtils.split( l, "||" );
 85  
 
 86  
 
 87  32
                 for ( int i = 0; i < text.length; i++ )
 88  
                 {
 89  24
                     List<Block> textBlocks = new ArrayList<Block>();
 90  
 
 91  24
                     textBlocks.add( ( ( ParagraphBlockParser) paragraphParser )
 92  
                             .visit(text[i], new ByLineReaderSource( new StringReader( EMPTY_STRING ) ), false ) );
 93  
 
 94  24
                     List<Block> blocks = new ArrayList<Block>();
 95  
 
 96  24
                     blocks.add( new BoldBlock( textBlocks ) );
 97  
 
 98  24
                     cells.add( new TableCellHeaderBlock( blocks ) );
 99  
                 }
 100  8
             }
 101  
             else
 102  
             {
 103  12
                 int it = 0;
 104  12
                 String[] text = StringUtils.split( l, "|" );
 105  12
                 List<String> texts = new LinkedList<String>();
 106  
 
 107  
 
 108  54
                 while ( it < text.length )
 109  
                 {
 110  42
                     if ( text[it].matches( ANY_CHARACTER + ESCAPE_CHARACTER
 111  
                                 + ConfluenceMarkup.LINK_START_MARKUP + ANY_CHARACTER )
 112  
                             && !text[it].matches( ANY_CHARACTER + ESCAPE_CHARACTER
 113  
                                 + ConfluenceMarkup.LINK_END_MARKUP + ANY_CHARACTER ) )
 114  
                     {
 115  4
                         texts.add( text[it] + ConfluenceMarkup.TABLE_CELL_MARKUP + text[it + 1] );
 116  4
                         it += 2;
 117  4
                         continue;
 118  
                      }
 119  38
                     texts.add( text[it] );
 120  38
                     it++;
 121  
                 }
 122  
 
 123  12
                 Object[] pText = texts.toArray();
 124  54
                 for ( int i = 0; i < pText.length; i++ )
 125  
                 {
 126  42
                     List<Block> blocks = new ArrayList<Block>();
 127  
 
 128  42
                     blocks.add( ( (ParagraphBlockParser) paragraphParser ).visit( (String) pText[i],
 129  
                             new ByLineReaderSource( new StringReader( EMPTY_STRING ) ), false ) );
 130  
 
 131  42
                     cells.add( new TableCellBlock( blocks ) );
 132  
                 }
 133  
             }
 134  
 
 135  20
             rows.add( new TableRowBlock( cells ) );
 136  
         }
 137  
 
 138  20
         while ( ( l = source.getNextLine() ) != null && accept( l, source ) );
 139  
 
 140  8
         assert rows.size() >= 1;
 141  
 
 142  8
         return new TableBlock( rows );
 143  
     }
 144  
 }