View Javadoc
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.parser.Parser;
39  import org.apache.maven.doxia.sink.Sink;
40  import org.apache.maven.doxia.util.ByLineReaderSource;
41  import org.apache.maven.doxia.util.ByLineSource;
42  import org.codehaus.plexus.component.annotations.Component;
43  
44  /**
45   * Parse the <a href="http://www.atlassian.com/software/confluence/">Confluence</a>.
46   * See <a href="http://confluence.atlassian.com/display/CONF25/Confluence+Notation+Guide+Overview">
47   * Confluence Notation Guide Overview</a>
48   *
49   * @version $Id: ConfluenceParser.java 1726913 2016-01-26 22:01:54Z rfscholte $
50   * @since 1.0
51   */
52  @Component( role = Parser.class, hint = "confluence" )
53  public class ConfluenceParser
54      extends AbstractTextParser
55  {
56      private BlockParser[] parsers;
57  
58      /**
59       * <p>Constructor for ConfluenceParser.</p>
60       */
61      public ConfluenceParser()
62      {
63          init();
64      }
65  
66      private List<Block> parse( ByLineSource source )
67          throws ParseException
68      {
69          init();
70  
71          List<Block> blocks = new ArrayList<Block>();
72  
73          String line;
74  
75          while ( ( line = source.getNextLine() ) != null )
76          {
77              //boolean accepted = false;
78  
79              for ( BlockParser parser : parsers )
80              {
81                  if ( line.trim().length() == 0 )
82                  {
83                      continue;
84                  }
85  
86                  if ( parser.accept( line, source ) )
87                  {
88                      //accepted = true;
89  
90                      blocks.add( parser.visit( line, source ) );
91  
92                      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         return blocks;
105     }
106 
107     @Override
108     public void parse( Reader source, Sink sink )
109         throws ParseException
110     {
111         parse( source, sink, "" );
112     }
113 
114     @Override
115     public synchronized void parse( Reader source, Sink sink, String reference )
116         throws ParseException
117     {
118         ByLineSource src = new ByLineReaderSource( source, reference );
119 
120         try
121         {
122             List<Block> blocks = parse( src );
123 
124             sink.head();
125 
126             sink.head_();
127 
128             sink.body();
129 
130             for ( Block block : blocks )
131             {
132                 block.traverse( sink );
133             }
134 
135             sink.body_();
136         }
137         catch ( Exception e )
138         {
139             // TODO handle column number
140             throw new ParseException( e, src.getName(), src.getLineNumber(), -1 );
141         }
142         finally
143         {
144             setSecondParsing( false );
145             init();
146         }
147     }
148 
149     /** {@inheritDoc} */
150     protected void init()
151     {
152         super.init();
153 
154         BlockParser headingParser = new SectionBlockParser();
155         BlockParser figureParser = new FigureBlockParser();
156         BlockParser verbatimParser = new VerbatimBlockParser();
157         BlockParser definitionParser = new DefinitionListBlockParser();
158         BlockParser horizontalRuleParser = new HorizontalRuleBlockParser();
159         BlockParser listParser = new ListBlockParser();
160         BlockParser tableParser = new TableBlockParser();
161 
162         BlockParser[] subparsers =
163                 new BlockParser[] { headingParser, figureParser, listParser, tableParser, verbatimParser };
164         BlockParser paragraphParser = new ParagraphBlockParser( subparsers );
165 
166         this.parsers =
167             new BlockParser[] { headingParser, figureParser, verbatimParser, definitionParser, horizontalRuleParser,
168                 listParser, tableParser, paragraphParser };
169     }
170 }