Coverage Report - org.apache.maven.doxia.module.confluence.ConfluenceParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfluenceParser
95%
38/40
100%
10/10
2,75
 
 1  
 package org.apache.maven.doxia.module.confluence;
 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.Reader;
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.maven.doxia.module.confluence.parser.Block;
 27  
 import org.apache.maven.doxia.module.confluence.parser.BlockParser;
 28  
 import org.apache.maven.doxia.module.confluence.parser.DefinitionListBlockParser;
 29  
 import org.apache.maven.doxia.module.confluence.parser.FigureBlockParser;
 30  
 import org.apache.maven.doxia.module.confluence.parser.HorizontalRuleBlockParser;
 31  
 import org.apache.maven.doxia.module.confluence.parser.ParagraphBlockParser;
 32  
 import org.apache.maven.doxia.module.confluence.parser.SectionBlockParser;
 33  
 import org.apache.maven.doxia.module.confluence.parser.VerbatimBlockParser;
 34  
 import org.apache.maven.doxia.module.confluence.parser.list.ListBlockParser;
 35  
 import org.apache.maven.doxia.module.confluence.parser.table.TableBlockParser;
 36  
 import org.apache.maven.doxia.parser.AbstractTextParser;
 37  
 import org.apache.maven.doxia.parser.ParseException;
 38  
 import org.apache.maven.doxia.sink.Sink;
 39  
 import org.apache.maven.doxia.util.ByLineReaderSource;
 40  
 import org.apache.maven.doxia.util.ByLineSource;
 41  
 
 42  
 /**
 43  
  * Parse the <a href="http://www.atlassian.com/software/confluence/">Confluence</a>.
 44  
  * See <a href="http://confluence.atlassian.com/display/CONF25/Confluence+Notation+Guide+Overview">
 45  
  * Confluence Notation Guide Overview</a>
 46  
  *
 47  
  * @version $Id: ConfluenceParser.java 1090706 2011-04-09 23:15:28Z hboutemy $
 48  
  * @since 1.0
 49  
  * @plexus.component role="org.apache.maven.doxia.parser.Parser" role-hint="confluence"
 50  
  */
 51  
 public class ConfluenceParser
 52  
     extends AbstractTextParser
 53  
 {
 54  
     private BlockParser[] parsers;
 55  
 
 56  
     /**
 57  
      * <p>Constructor for ConfluenceParser.</p>
 58  
      */
 59  
     public ConfluenceParser()
 60  50
     {
 61  50
         init();
 62  50
     }
 63  
 
 64  
     private List<Block> parse( ByLineSource source )
 65  
         throws ParseException
 66  
     {
 67  56
         init();
 68  
 
 69  56
         List<Block> blocks = new ArrayList<Block>();
 70  
 
 71  
         String line;
 72  
 
 73  478
         while ( ( line = source.getNextLine() ) != null )
 74  
         {
 75  
             //boolean accepted = false;
 76  
 
 77  2718
             for ( int i = 0; i < parsers.length; i++ )
 78  
             {
 79  2628
                 BlockParser parser = parsers[i];
 80  
 
 81  2628
                 if ( line.trim().length() == 0 )
 82  
                 {
 83  720
                     continue;
 84  
                 }
 85  
 
 86  1908
                 if ( parser.accept( line, source ) )
 87  
                 {
 88  
                     //accepted = true;
 89  
 
 90  332
                     blocks.add( parser.visit( line, source ) );
 91  
 
 92  332
                     break;
 93  
                 }
 94  
             }
 95  
 
 96  
 /*
 97  
             if ( !accepted )
 98  
             {
 99  
                 throw new ParseException( "don't know how to handle line: " + source.getLineNumber() + ": " + line );
 100  
             }
 101  
 */
 102  
         }
 103  
 
 104  56
         return blocks;
 105  
     }
 106  
 
 107  
     /** {@inheritDoc} */
 108  
     public synchronized void parse( Reader source, Sink sink )
 109  
         throws ParseException
 110  
     {
 111  56
         ByLineSource src = new ByLineReaderSource( source );
 112  
 
 113  
         try
 114  
         {
 115  56
             List<Block> blocks = parse( src );
 116  
 
 117  56
             sink.head();
 118  
 
 119  56
             sink.head_();
 120  
 
 121  56
             sink.body();
 122  
 
 123  56
             for ( Block block : blocks )
 124  
             {
 125  332
                 block.traverse( sink );
 126  
             }
 127  
 
 128  56
             sink.body_();
 129  
         }
 130  0
         catch ( Exception e )
 131  
         {
 132  
             // TODO handle column number
 133  0
             throw new ParseException( e, src.getName(), src.getLineNumber(), -1 );
 134  
         }
 135  
         finally
 136  
         {
 137  56
             setSecondParsing( false );
 138  56
             init();
 139  56
         }
 140  56
     }
 141  
 
 142  
     /** {@inheritDoc} */
 143  
     protected void init()
 144  
     {
 145  162
         super.init();
 146  
 
 147  162
         BlockParser headingParser = new SectionBlockParser();
 148  162
         BlockParser figureParser = new FigureBlockParser();
 149  162
         BlockParser verbatimParser = new VerbatimBlockParser();
 150  162
         BlockParser definitionParser = new DefinitionListBlockParser();
 151  162
         BlockParser horizontalRuleParser = new HorizontalRuleBlockParser();
 152  162
         BlockParser listParser = new ListBlockParser();
 153  162
         BlockParser tableParser = new TableBlockParser();
 154  
 
 155  162
         BlockParser[] subparsers =
 156  
                 new BlockParser[] { headingParser, figureParser, listParser, tableParser, verbatimParser };
 157  162
         BlockParser paragraphParser = new ParagraphBlockParser( subparsers );
 158  
 
 159  162
         this.parsers =
 160  
             new BlockParser[] { headingParser, figureParser, verbatimParser, definitionParser, horizontalRuleParser,
 161  
                 listParser, tableParser, paragraphParser };
 162  162
     }
 163  
 }