Coverage Report - org.apache.maven.doxia.module.twiki.parser.ParagraphBlockParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ParagraphBlockParser
82%
52/63
70%
31/44
4,5
 
 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.Arrays;
 24  
 import java.util.List;
 25  
 import java.util.regex.Matcher;
 26  
 import java.util.regex.Pattern;
 27  
 
 28  
 import org.apache.maven.doxia.util.ByLineSource;
 29  
 import org.apache.maven.doxia.parser.ParseException;
 30  
 
 31  
 /**
 32  
  * Parse paragraphs.
 33  
  *
 34  
  * @author Juan F. Codagnone
 35  
  * @version $Id: ParagraphBlockParser.java 1090706 2011-04-09 23:15:28Z hboutemy $
 36  
  */
 37  95
 public class ParagraphBlockParser
 38  
     implements BlockParser
 39  
 {
 40  
     /**
 41  
      * pattern used to dectect end of paragraph
 42  
      */
 43  95
     private final Pattern paragraphSeparator = Pattern.compile( "^(\\s*)$" );
 44  
 
 45  
     /**
 46  
      * {@link SectionBlockParser} to use. injected
 47  
      */
 48  
     private SectionBlockParser sectionParser;
 49  
 
 50  
     /**
 51  
      * {@link ListBlockParser} to use. injected
 52  
      */
 53  
     private GenericListBlockParser listParser;
 54  
 
 55  
     /**
 56  
      * {@link FormatedTextParser} to use. injected
 57  
      */
 58  
     private FormatedTextParser textParser;
 59  
 
 60  
     /**
 61  
      * {@link HRuleBlockParser} to use. injected
 62  
      */
 63  
     private HRuleBlockParser hrulerParser;
 64  
 
 65  
     /**
 66  
      * {@link TableBlockParser} to use. injected
 67  
      */
 68  
     private TableBlockParser tableBlockParser;
 69  
 
 70  
     /**
 71  
      *  {@link TableBlockParser} to use. injected
 72  
      */
 73  
     private VerbatimBlockParser verbatimParser;
 74  
 
 75  
     /**
 76  
      * no operation block
 77  
      */
 78  1
     private static final NopBlock NOP = new NopBlock();
 79  
 
 80  
     /** {@inheritDoc} */
 81  
     public final boolean accept( final String line )
 82  
     {
 83  28
         return !sectionParser.accept( line ) && !hrulerParser.accept( line ) && !verbatimParser.accept( line );
 84  
     }
 85  
 
 86  
     /**
 87  
      * {@inheritDoc}
 88  
      */
 89  
     public final Block visit( final String line, final ByLineSource source )
 90  
         throws ParseException
 91  
     {
 92  16
         StringBuffer sb = new StringBuffer();
 93  16
         List<Block> childs = new ArrayList<Block>();
 94  
 
 95  16
         boolean sawText = false;
 96  
 
 97  
         /*
 98  
         * 1. Skip begininig new lines
 99  
         * 2. Get the text, while \n\n is not found
 100  
         */
 101  16
         boolean pre = false;
 102  16
         String l = line;
 103  
         do
 104  
         {
 105  35
             Matcher m = paragraphSeparator.matcher( l );
 106  
 
 107  35
             if ( m.lookingAt() )
 108  
             {
 109  11
                 if ( sawText )
 110  
                 {
 111  5
                     break;
 112  
                 }
 113  
             }
 114  
             else
 115  
             {
 116  24
                 sawText = true;
 117  
 
 118  
                 /* be able to parse lists / enumerations */
 119  24
                 if ( listParser.accept( l ) )
 120  
                 {
 121  2
                     if ( sb.length() != 0 )
 122  
                     {
 123  1
                         childs.addAll( Arrays.asList( textParser.parse( sb.toString().trim() ) ) );
 124  1
                         sb = new StringBuffer();
 125  
                     }
 126  2
                     childs.add( listParser.visit( l, source ) );
 127  
                 }
 128  22
                 else if ( tableBlockParser.accept( l ) )
 129  
                 {
 130  0
                     childs.add( tableBlockParser.visit( l, source ) );
 131  
                 }
 132  
                 else
 133  
                 {
 134  22
                     sb.append( l );
 135  
                     // specific
 136  22
                     if ( l.indexOf( "<pre>" ) != -1 )
 137  
                     {
 138  0
                         pre = true;
 139  
                     }
 140  22
                     if ( l.indexOf( "</pre>" ) != -1 )
 141  
                     {
 142  0
                         pre = false;
 143  
                     }
 144  
 
 145  22
                     if ( !pre )
 146  
                     {
 147  22
                         sb.append( " " );
 148  
                     }
 149  
                     else
 150  
                     {
 151  
                         // TODO use EOL
 152  0
                         sb.append( "\n" );
 153  
                     }
 154  
                 }
 155  
             }
 156  30
             l = source.getNextLine();
 157  
         }
 158  30
         while ( l != null && accept( l ) );
 159  
 
 160  16
         if ( line != null )
 161  
         {
 162  16
             source.ungetLine();
 163  
         }
 164  
 
 165  16
         if ( sb.length() != 0 )
 166  
         {
 167  16
             childs.addAll( Arrays.asList( textParser.parse( sb.toString().trim() ) ) );
 168  16
             sb = new StringBuffer();
 169  
         }
 170  
 
 171  16
         if ( childs.size() == 0 )
 172  
         {
 173  0
             return NOP;
 174  
         }
 175  
 
 176  16
         return new ParagraphBlock( childs.toArray( new Block[] {} ) );
 177  
     }
 178  
 
 179  
     /**
 180  
      * Sets the sectionParser.
 181  
      *
 182  
      * @param aSectionParser <code>SectionBlockParser</code> with the sectionParser.
 183  
      */
 184  
     public final void setSectionParser( final SectionBlockParser aSectionParser )
 185  
     {
 186  95
         if ( aSectionParser == null )
 187  
         {
 188  0
             throw new IllegalArgumentException( "arg can't be null" );
 189  
         }
 190  95
         this.sectionParser = aSectionParser;
 191  95
     }
 192  
 
 193  
     /**
 194  
      * Sets the listParser.
 195  
      *
 196  
      * @param aListParser <code>ListBlockParser</code> with the listParser.
 197  
      */
 198  
     public final void setListParser( final GenericListBlockParser aListParser )
 199  
     {
 200  95
         if ( aListParser == null )
 201  
         {
 202  0
             throw new IllegalArgumentException( "arg can't be null" );
 203  
         }
 204  
 
 205  95
         this.listParser = aListParser;
 206  95
     }
 207  
 
 208  
     /**
 209  
      * Sets the formatTextParser.
 210  
      *
 211  
      * @param aTextParser <code>FormatedTextParser</code>
 212  
      *                   with the formatTextParser.
 213  
      */
 214  
     public final void setTextParser( final FormatedTextParser aTextParser )
 215  
     {
 216  95
         if ( aTextParser == null )
 217  
         {
 218  0
             throw new IllegalArgumentException( "arg can't be null" );
 219  
         }
 220  95
         this.textParser = aTextParser;
 221  95
     }
 222  
 
 223  
     /**
 224  
      * Sets the hrulerParser.
 225  
      *
 226  
      * @param aHrulerParser <code>HRuleBlockParser</code> with the hrulerParser.
 227  
      */
 228  
     public final void setHrulerParser( final HRuleBlockParser aHrulerParser )
 229  
     {
 230  95
         if ( aHrulerParser == null )
 231  
         {
 232  0
             throw new IllegalArgumentException( "arg can't be null" );
 233  
         }
 234  
 
 235  95
         this.hrulerParser = aHrulerParser;
 236  95
     }
 237  
 
 238  
     /**
 239  
      * <p>Setter for the field <code>tableBlockParser</code>.</p>
 240  
      *
 241  
      * @param aTableBlockParser Table parser to use
 242  
      */
 243  
     public final void setTableBlockParser( final TableBlockParser aTableBlockParser )
 244  
     {
 245  95
         if ( aTableBlockParser == null )
 246  
         {
 247  0
             throw new IllegalArgumentException( "arg can't be null" );
 248  
         }
 249  
 
 250  95
         this.tableBlockParser = aTableBlockParser;
 251  95
     }
 252  
 
 253  
     /**
 254  
      * Sets the verbatimParser.
 255  
      *
 256  
      * @param aVerbatimParser <code>VerbatimBlockParser</code> with the verbatimParser.
 257  
      * @since 1.1
 258  
      */
 259  
     public final void setVerbatimParser( final VerbatimBlockParser aVerbatimParser )
 260  
     {
 261  95
         if ( aVerbatimParser == null )
 262  
         {
 263  0
             throw new IllegalArgumentException( "arg can't be null" );
 264  
         }
 265  95
         this.verbatimParser = aVerbatimParser;
 266  95
     }
 267  
 }