Coverage Report - org.apache.maven.doxia.module.twiki.parser.SectionBlockParser
 
Classes in this File Line Coverage Branch Coverage Complexity
SectionBlockParser
87%
28/32
81%
18/22
3,667
 
 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.regex.Matcher;
 24  
 import java.util.regex.Pattern;
 25  
 
 26  
 import org.apache.maven.doxia.util.ByLineSource;
 27  
 import org.apache.maven.doxia.parser.ParseException;
 28  
 
 29  
 /**
 30  
  * Parse looking for sections
 31  
  *
 32  
  * @author Juan F. Codagnone
 33  
  * @version $Id: SectionBlockParser.java 1090706 2011-04-09 23:15:28Z hboutemy $
 34  
  */
 35  95
 public class SectionBlockParser
 36  
     implements BlockParser
 37  
 {
 38  
     /**
 39  
      * '---++ Header', '---## Header'
 40  
      */
 41  1
     private static final Pattern HEADER_DA = Pattern.compile( "^---([+]+)\\s*(.+)\\s*$" );
 42  
 
 43  
     /**
 44  
      * {@link ParagraphBlockParser} to use. injected
 45  
      */
 46  
     private ParagraphBlockParser paraParser;
 47  
 
 48  
     /**
 49  
      * {@link ParagraphBlockParser} to use. injected
 50  
      */
 51  
     private HRuleBlockParser hrulerParser;
 52  
 
 53  
     /** {@link VerbatimBlockParser} */
 54  
     private VerbatimBlockParser verbatimBlockParser;
 55  
 
 56  
     /**
 57  
      * {@inheritDoc}
 58  
      */
 59  
     public final boolean accept( final String line )
 60  
     {
 61  53
         return HEADER_DA.matcher( line ).lookingAt();
 62  
     }
 63  
 
 64  
     /**
 65  
      * {@inheritDoc}
 66  
      */
 67  
     public final Block visit( final String line, final ByLineSource source )
 68  
         throws ParseException
 69  
     {
 70  6
         final Matcher m = HEADER_DA.matcher( line );
 71  
 
 72  6
         if ( !m.lookingAt() )
 73  
         {
 74  0
             throw new IllegalArgumentException( "don't know how to handle: " + line );
 75  
         }
 76  
 
 77  
         String newLine;
 78  6
         final ArrayList<Block> blocks = new ArrayList<Block>();
 79  
 
 80  14
         while ( ( newLine = source.getNextLine() ) != null && !accept( newLine ) )
 81  
         {
 82  8
             if ( hrulerParser.accept( newLine ) )
 83  
             {
 84  1
                 blocks.add( hrulerParser.visit( newLine, source ) );
 85  
             }
 86  
             else
 87  
             {
 88  7
                 if ( verbatimBlockParser.accept( newLine ) )
 89  
                 {
 90  1
                     blocks.add( verbatimBlockParser.visit( newLine, source ) );
 91  
                 }
 92  
                 else
 93  
                 {
 94  6
                     blocks.add( paraParser.visit( newLine, source ) );
 95  
                 }
 96  
             }
 97  
         }
 98  
 
 99  6
         if ( newLine != null )
 100  
         {
 101  1
             source.ungetLine();
 102  
         }
 103  
 
 104  6
         return new SectionBlock( m.group( 2 ), getLevel( m.group( 1 ) ), blocks.toArray( new Block[] {} ) );
 105  
     }
 106  
 
 107  
     /**
 108  
      * @param s "++"
 109  
      * @return tha level of the section
 110  
      * @throws IllegalArgumentException on error
 111  
      */
 112  
     static int getLevel( final String s )
 113  
         throws IllegalArgumentException
 114  
     {
 115  25
         for ( int i = 0, n = s.length(); i < n; i++ )
 116  
         {
 117  18
             if ( s.charAt( i ) != '+' )
 118  
             {
 119  1
                 throw new IllegalArgumentException( "the argument must have only" + " '+' characters" );
 120  
             }
 121  
         }
 122  7
         return s.length();
 123  
     }
 124  
 
 125  
     /**
 126  
      * Sets the paraParser.
 127  
      *
 128  
      * @param paraParser <code>ParagraphBlockParser</code> with the paraParser.
 129  
      */
 130  
     public final void setParaParser( final ParagraphBlockParser paraParser )
 131  
     {
 132  95
         if ( paraParser == null )
 133  
         {
 134  0
             throw new IllegalArgumentException( "argument can't be null" );
 135  
         }
 136  95
         this.paraParser = paraParser;
 137  95
     }
 138  
 
 139  
     /**
 140  
      * Sets the hrulerParser.
 141  
      *
 142  
      * @param hrulerParser <code>HRuleBlockParser</code> with the hrulerParser.
 143  
      */
 144  
     public final void setHrulerParser( final HRuleBlockParser hrulerParser )
 145  
     {
 146  95
         if ( hrulerParser == null )
 147  
         {
 148  0
             throw new IllegalArgumentException( "argument can't be null" );
 149  
         }
 150  95
         this.hrulerParser = hrulerParser;
 151  95
     }
 152  
 
 153  
     /**
 154  
      * Sets the verbatimBlockParser.
 155  
      *
 156  
      * @param verbatimBlockParser <code>VerbatimBlockParser</code> with the verbatimBlockParser.
 157  
      * @since 1.1
 158  
      */
 159  
     public final void setVerbatimBlockParser( VerbatimBlockParser verbatimBlockParser )
 160  
     {
 161  96
         if ( verbatimBlockParser == null )
 162  
         {
 163  0
             throw new IllegalArgumentException( "argument can't be null" );
 164  
         }
 165  96
         this.verbatimBlockParser = verbatimBlockParser;
 166  96
     }
 167  
 }