001package org.apache.maven.doxia.module.confluence; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.Reader; 023import java.util.ArrayList; 024import java.util.List; 025 026import org.apache.maven.doxia.module.confluence.parser.Block; 027import org.apache.maven.doxia.module.confluence.parser.BlockParser; 028import org.apache.maven.doxia.module.confluence.parser.DefinitionListBlockParser; 029import org.apache.maven.doxia.module.confluence.parser.FigureBlockParser; 030import org.apache.maven.doxia.module.confluence.parser.HorizontalRuleBlockParser; 031import org.apache.maven.doxia.module.confluence.parser.ParagraphBlockParser; 032import org.apache.maven.doxia.module.confluence.parser.SectionBlockParser; 033import org.apache.maven.doxia.module.confluence.parser.VerbatimBlockParser; 034import org.apache.maven.doxia.module.confluence.parser.list.ListBlockParser; 035import org.apache.maven.doxia.module.confluence.parser.table.TableBlockParser; 036import org.apache.maven.doxia.parser.AbstractTextParser; 037import org.apache.maven.doxia.parser.ParseException; 038import org.apache.maven.doxia.parser.Parser; 039import org.apache.maven.doxia.sink.Sink; 040import org.apache.maven.doxia.util.ByLineReaderSource; 041import org.apache.maven.doxia.util.ByLineSource; 042import org.codehaus.plexus.component.annotations.Component; 043 044/** 045 * Parse the <a href="http://www.atlassian.com/software/confluence/">Confluence</a>. 046 * See <a href="http://confluence.atlassian.com/display/CONF25/Confluence+Notation+Guide+Overview"> 047 * Confluence Notation Guide Overview</a> 048 * 049 * @version $Id$ 050 * @since 1.0 051 */ 052@Component( role = Parser.class, hint = "confluence" ) 053public class ConfluenceParser 054 extends AbstractTextParser 055{ 056 private BlockParser[] parsers; 057 058 /** 059 * <p>Constructor for ConfluenceParser.</p> 060 */ 061 public ConfluenceParser() 062 { 063 init(); 064 } 065 066 private List<Block> parse( ByLineSource source ) 067 throws ParseException 068 { 069 init(); 070 071 List<Block> blocks = new ArrayList<Block>(); 072 073 String line; 074 075 while ( ( line = source.getNextLine() ) != null ) 076 { 077 //boolean accepted = false; 078 079 for ( BlockParser parser : parsers ) 080 { 081 if ( line.trim().length() == 0 ) 082 { 083 continue; 084 } 085 086 if ( parser.accept( line, source ) ) 087 { 088 //accepted = true; 089 090 blocks.add( parser.visit( line, source ) ); 091 092 break; 093 } 094 } 095 096/* 097 if ( !accepted ) 098 { 099 throw new ParseException( "don't know how to handle line: " + source.getLineNumber() + ": " + line ); 100 } 101*/ 102 } 103 104 return blocks; 105 } 106 107 /** {@inheritDoc} */ 108 public synchronized void parse( Reader source, Sink sink ) 109 throws ParseException 110 { 111 ByLineSource src = new ByLineReaderSource( source ); 112 113 try 114 { 115 List<Block> blocks = parse( src ); 116 117 sink.head(); 118 119 sink.head_(); 120 121 sink.body(); 122 123 for ( Block block : blocks ) 124 { 125 block.traverse( sink ); 126 } 127 128 sink.body_(); 129 } 130 catch ( Exception e ) 131 { 132 // TODO handle column number 133 throw new ParseException( e, src.getName(), src.getLineNumber(), -1 ); 134 } 135 finally 136 { 137 setSecondParsing( false ); 138 init(); 139 } 140 } 141 142 /** {@inheritDoc} */ 143 protected void init() 144 { 145 super.init(); 146 147 BlockParser headingParser = new SectionBlockParser(); 148 BlockParser figureParser = new FigureBlockParser(); 149 BlockParser verbatimParser = new VerbatimBlockParser(); 150 BlockParser definitionParser = new DefinitionListBlockParser(); 151 BlockParser horizontalRuleParser = new HorizontalRuleBlockParser(); 152 BlockParser listParser = new ListBlockParser(); 153 BlockParser tableParser = new TableBlockParser(); 154 155 BlockParser[] subparsers = 156 new BlockParser[] { headingParser, figureParser, listParser, tableParser, verbatimParser }; 157 BlockParser paragraphParser = new ParagraphBlockParser( subparsers ); 158 159 this.parsers = 160 new BlockParser[] { headingParser, figureParser, verbatimParser, definitionParser, horizontalRuleParser, 161 listParser, tableParser, paragraphParser }; 162 } 163}