Coverage Report - org.apache.maven.doxia.module.confluence.parser.ParagraphBlockParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ParagraphBlockParser
96%
26/27
92%
13/14
2,6
 
 1  
 package org.apache.maven.doxia.module.confluence.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 org.apache.maven.doxia.parser.ParseException;
 23  
 import org.apache.maven.doxia.util.ByLineSource;
 24  
 
 25  
 /**
 26  
  * <p>ParagraphBlockParser class.</p>
 27  
  *
 28  
  * @version $Id: ParagraphBlockParser.java 775115 2009-05-15 12:54:18Z ltheussl $
 29  
  */
 30  
 public class ParagraphBlockParser
 31  
     implements BlockParser
 32  
 {
 33  
     private BlockParser[] parsers;
 34  
 
 35  
     /**
 36  
      * <p>Constructor for ParagraphBlockParser.</p>
 37  
      *
 38  
      * @param parsers the parsers.
 39  
      */
 40  
     public ParagraphBlockParser( BlockParser[] parsers )
 41  
     {
 42  240
         super();
 43  240
         this.parsers = parsers;
 44  240
     }
 45  
 
 46  
     /** {@inheritDoc} */
 47  
     public boolean accept( String line, ByLineSource source )
 48  
     {
 49  162
         return true;
 50  
     }
 51  
 
 52  
     /**
 53  
      * Visit the Block.
 54  
      *
 55  
      * @param line the line to visit.
 56  
      * @param source the source.
 57  
      * @param generateParagraphTags whether to generate a paragraph.
 58  
      * @return the visited Block.
 59  
      *
 60  
      * @throws org.apache.maven.doxia.parser.ParseException if any
 61  
      */
 62  
     public Block visit( String line, ByLineSource source, boolean generateParagraphTags )
 63  
             throws ParseException
 64  
     {
 65  124
         if ( generateParagraphTags )
 66  
         {
 67  0
             return this.visit( line, source );
 68  
         }
 69  
         else
 70  
         {
 71  124
             ChildBlocksBuilder builder = new ChildBlocksBuilder( line );
 72  124
             return new ParagraphBlock( builder.getBlocks(), generateParagraphTags );
 73  
         }
 74  
     }
 75  
 
 76  
     /** {@inheritDoc} */
 77  
     public Block visit( String line, ByLineSource source )
 78  
         throws ParseException
 79  
     {
 80  
 
 81  162
         ChildBlocksBuilder builder = new ChildBlocksBuilder( appendUntilEmptyLine( line, source ) );
 82  162
         return new ParagraphBlock( builder.getBlocks() );
 83  
     }
 84  
 
 85  
     /**
 86  
      * Slurp lines from the source starting with the given line appending them together into a StringBuffer until an
 87  
      * empty line is reached, and while the source contains more lines. The result can be passed to the
 88  
      * {@link #getBlocks(String)} method.
 89  
      *
 90  
      * @param line the first line
 91  
      * @param source the source to read new lines from
 92  
      * @return a StringBuffer appended with lines
 93  
      * @throws ParseException
 94  
      */
 95  
     private String appendUntilEmptyLine( String line, ByLineSource source )
 96  
         throws ParseException
 97  
     {
 98  162
         StringBuffer text = new StringBuffer();
 99  
 
 100  
         do
 101  
         {
 102  
 
 103  320
             if ( line.trim().length() == 0 )
 104  
             {
 105  128
                 break;
 106  
             }
 107  
 
 108  192
             boolean accepted = false;
 109  1126
             for ( int i = 0; i < parsers.length; i++ )
 110  
             {
 111  942
                 BlockParser parser = parsers[i];
 112  942
                 if ( parser.accept( line, source ) )
 113  
                 {
 114  8
                     accepted = true;
 115  8
                     break;
 116  
                 }
 117  
             }
 118  192
             if ( accepted )
 119  
             {
 120  
                 // Slightly fragile - if any of the parsers need to do this in order to decide whether to accept a line,
 121  
                 // then it will barf because of the contract of ByLineSource
 122  8
                 source.ungetLine();
 123  8
                 break;
 124  
             }
 125  
 
 126  184
             if ( text.length() == 0 )
 127  
             {
 128  162
                 text.append( line.trim() );
 129  
             }
 130  
             else
 131  
             {
 132  22
                 text.append( " " + line.trim() );
 133  
             }
 134  
 
 135  
         }
 136  184
         while ( ( line = source.getNextLine() ) != null );
 137  
 
 138  162
         return text.toString();
 139  
     }
 140  
 
 141  
 }