View Javadoc

1   package org.apache.maven.doxia.module.confluence.parser.table;
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.StringReader;
23  import java.util.ArrayList;
24  import java.util.LinkedList;
25  import java.util.List;
26  
27  import org.apache.maven.doxia.util.ByLineReaderSource;
28  import org.apache.maven.doxia.module.confluence.ConfluenceMarkup;
29  import org.apache.maven.doxia.module.confluence.parser.FigureBlockParser;
30  import org.apache.maven.doxia.module.confluence.parser.ParagraphBlockParser;
31  import org.apache.maven.doxia.module.confluence.parser.SectionBlockParser;
32  import org.apache.maven.doxia.util.ByLineSource;
33  import org.apache.maven.doxia.module.confluence.parser.BlockParser;
34  import org.apache.maven.doxia.module.confluence.parser.Block;
35  import org.apache.maven.doxia.module.confluence.parser.BoldBlock;
36  import org.apache.maven.doxia.parser.ParseException;
37  import org.codehaus.plexus.util.StringUtils;
38  
39  /**
40   * Parse tables
41   *
42   * @author Juan F. Codagnone
43   * @version $Id: TableBlockParser.java 1090706 2011-04-09 23:15:28Z hboutemy $
44   */
45  public class TableBlockParser
46      implements BlockParser
47  {
48      private static final String EMPTY_STRING = "";
49      private static final String ANY_CHARACTER = ".*";
50      private static final String ESCAPE_CHARACTER = "\\";
51  
52      /** {@inheritDoc} */
53      public  boolean accept( String line, ByLineSource source )
54      {
55          return line.startsWith( "|" );
56      }
57  
58      /** {@inheritDoc} */
59      public  Block visit(  String line,  ByLineSource source )
60          throws ParseException
61      {
62          if ( !accept( line, source ) )
63          {
64              throw new IllegalAccessError( "call accept before this ;)" );
65          }
66  
67          List<Block> rows = new ArrayList<Block>();
68  
69          String l = line;
70  
71          do
72          {
73              l = l.substring( 0, l.lastIndexOf( "|" ) );
74  
75              List<Block> cells = new ArrayList<Block>();
76  
77              BlockParser headingParser = new SectionBlockParser();
78              BlockParser figureParser = new FigureBlockParser();
79              BlockParser[] subparsers = new BlockParser[] { headingParser, figureParser };
80              BlockParser paragraphParser = new ParagraphBlockParser( subparsers );
81  
82              if ( l.startsWith( "||" ) )
83              {
84                  String[] text = StringUtils.split( l, "||" );
85  
86  
87                  for ( int i = 0; i < text.length; i++ )
88                  {
89                      List<Block> textBlocks = new ArrayList<Block>();
90  
91                      textBlocks.add( ( ( ParagraphBlockParser) paragraphParser )
92                              .visit(text[i], new ByLineReaderSource( new StringReader( EMPTY_STRING ) ), false ) );
93  
94                      List<Block> blocks = new ArrayList<Block>();
95  
96                      blocks.add( new BoldBlock( textBlocks ) );
97  
98                      cells.add( new TableCellHeaderBlock( blocks ) );
99                  }
100             }
101             else
102             {
103                 int it = 0;
104                 String[] text = StringUtils.split( l, "|" );
105                 List<String> texts = new LinkedList<String>();
106 
107 
108                 while ( it < text.length )
109                 {
110                     if ( text[it].matches( ANY_CHARACTER + ESCAPE_CHARACTER
111                                 + ConfluenceMarkup.LINK_START_MARKUP + ANY_CHARACTER )
112                             && !text[it].matches( ANY_CHARACTER + ESCAPE_CHARACTER
113                                 + ConfluenceMarkup.LINK_END_MARKUP + ANY_CHARACTER ) )
114                     {
115                         texts.add( text[it] + ConfluenceMarkup.TABLE_CELL_MARKUP + text[it + 1] );
116                         it += 2;
117                         continue;
118                      }
119                     texts.add( text[it] );
120                     it++;
121                 }
122 
123                 Object[] pText = texts.toArray();
124                 for ( int i = 0; i < pText.length; i++ )
125                 {
126                     List<Block> blocks = new ArrayList<Block>();
127 
128                     blocks.add( ( (ParagraphBlockParser) paragraphParser ).visit( (String) pText[i],
129                             new ByLineReaderSource( new StringReader( EMPTY_STRING ) ), false ) );
130 
131                     cells.add( new TableCellBlock( blocks ) );
132                 }
133             }
134 
135             rows.add( new TableRowBlock( cells ) );
136         }
137 
138         while ( ( l = source.getNextLine() ) != null && accept( l, source ) );
139 
140         assert rows.size() >= 1;
141 
142         return new TableBlock( rows );
143     }
144 }