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 | 164 | 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 | 382 | return line.startsWith( "|" ); |
56 | |
} |
57 | |
|
58 | |
|
59 | |
public Block visit( String line, ByLineSource source ) |
60 | |
throws ParseException |
61 | |
{ |
62 | 8 | if ( !accept( line, source ) ) |
63 | |
{ |
64 | 0 | throw new IllegalAccessError( "call accept before this ;)" ); |
65 | |
} |
66 | |
|
67 | 8 | List<Block> rows = new ArrayList<Block>(); |
68 | |
|
69 | 8 | String l = line; |
70 | |
|
71 | |
do |
72 | |
{ |
73 | 20 | l = l.substring( 0, l.lastIndexOf( "|" ) ); |
74 | |
|
75 | 20 | List<Block> cells = new ArrayList<Block>(); |
76 | |
|
77 | 20 | BlockParser headingParser = new SectionBlockParser(); |
78 | 20 | BlockParser figureParser = new FigureBlockParser(); |
79 | 20 | BlockParser[] subparsers = new BlockParser[] { headingParser, figureParser }; |
80 | 20 | BlockParser paragraphParser = new ParagraphBlockParser( subparsers ); |
81 | |
|
82 | 20 | if ( l.startsWith( "||" ) ) |
83 | |
{ |
84 | 8 | String[] text = StringUtils.split( l, "||" ); |
85 | |
|
86 | |
|
87 | 32 | for ( int i = 0; i < text.length; i++ ) |
88 | |
{ |
89 | 24 | List<Block> textBlocks = new ArrayList<Block>(); |
90 | |
|
91 | 24 | textBlocks.add( ( ( ParagraphBlockParser) paragraphParser ) |
92 | |
.visit(text[i], new ByLineReaderSource( new StringReader( EMPTY_STRING ) ), false ) ); |
93 | |
|
94 | 24 | List<Block> blocks = new ArrayList<Block>(); |
95 | |
|
96 | 24 | blocks.add( new BoldBlock( textBlocks ) ); |
97 | |
|
98 | 24 | cells.add( new TableCellHeaderBlock( blocks ) ); |
99 | |
} |
100 | 8 | } |
101 | |
else |
102 | |
{ |
103 | 12 | int it = 0; |
104 | 12 | String[] text = StringUtils.split( l, "|" ); |
105 | 12 | List<String> texts = new LinkedList<String>(); |
106 | |
|
107 | |
|
108 | 54 | while ( it < text.length ) |
109 | |
{ |
110 | 42 | 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 | 4 | texts.add( text[it] + ConfluenceMarkup.TABLE_CELL_MARKUP + text[it + 1] ); |
116 | 4 | it += 2; |
117 | 4 | continue; |
118 | |
} |
119 | 38 | texts.add( text[it] ); |
120 | 38 | it++; |
121 | |
} |
122 | |
|
123 | 12 | Object[] pText = texts.toArray(); |
124 | 54 | for ( int i = 0; i < pText.length; i++ ) |
125 | |
{ |
126 | 42 | List<Block> blocks = new ArrayList<Block>(); |
127 | |
|
128 | 42 | blocks.add( ( (ParagraphBlockParser) paragraphParser ).visit( (String) pText[i], |
129 | |
new ByLineReaderSource( new StringReader( EMPTY_STRING ) ), false ) ); |
130 | |
|
131 | 42 | cells.add( new TableCellBlock( blocks ) ); |
132 | |
} |
133 | |
} |
134 | |
|
135 | 20 | rows.add( new TableRowBlock( cells ) ); |
136 | |
} |
137 | |
|
138 | 20 | while ( ( l = source.getNextLine() ) != null && accept( l, source ) ); |
139 | |
|
140 | 8 | assert rows.size() >= 1; |
141 | |
|
142 | 8 | return new TableBlock( rows ); |
143 | |
} |
144 | |
} |