1 package org.apache.maven.doxia.module.confluence.parser.table;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
43
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
53 public boolean accept( String line, ByLineSource source )
54 {
55 return line.startsWith( "|" );
56 }
57
58
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 }