1 package org.apache.maven.doxia.module.confluence;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
49
50
51 @Component( role = Parser.class, hint = "confluence" )
52 public class ConfluenceParser
53 extends AbstractTextParser
54 {
55 private BlockParser[] parsers;
56
57
58
59
60 public ConfluenceParser()
61 {
62 init();
63 }
64
65 private List<Block> parse( ByLineSource source )
66 throws ParseException
67 {
68 init();
69
70 List<Block> blocks = new ArrayList<>();
71
72 String line;
73
74 while ( ( line = source.getNextLine() ) != null )
75 {
76
77
78 for ( BlockParser parser : parsers )
79 {
80 if ( line.trim().length() == 0 )
81 {
82 continue;
83 }
84
85 if ( parser.accept( line, source ) )
86 {
87
88
89 blocks.add( parser.visit( line, source ) );
90
91 break;
92 }
93 }
94
95
96
97
98
99
100
101 }
102
103 return blocks;
104 }
105
106
107 @Override
108 public void parse( Reader source, Sink sink )
109 throws ParseException
110 {
111 parse( source, sink, "" );
112 }
113
114
115 @Override
116 public synchronized void parse( Reader source, Sink sink, String reference )
117 throws ParseException
118 {
119 ByLineSource src = new ByLineReaderSource( source, reference );
120
121 try
122 {
123 List<Block> blocks = parse( src );
124
125 sink.head();
126
127 sink.head_();
128
129 sink.body();
130
131 for ( Block block : blocks )
132 {
133 block.traverse( sink );
134 }
135
136 sink.body_();
137 }
138 catch ( Exception e )
139 {
140
141 throw new ParseException( e, src.getName(), src.getLineNumber(), -1 );
142 }
143 finally
144 {
145 setSecondParsing( false );
146 init();
147 }
148 }
149
150
151
152
153 protected void init()
154 {
155 super.init();
156
157 BlockParser headingParser = new SectionBlockParser();
158 BlockParser figureParser = new FigureBlockParser();
159 BlockParser verbatimParser = new VerbatimBlockParser();
160 BlockParser definitionParser = new DefinitionListBlockParser();
161 BlockParser horizontalRuleParser = new HorizontalRuleBlockParser();
162 BlockParser listParser = new ListBlockParser();
163 BlockParser tableParser = new TableBlockParser();
164
165 BlockParser[] subparsers =
166 new BlockParser[] { headingParser, figureParser, listParser, tableParser, verbatimParser };
167 BlockParser paragraphParser = new ParagraphBlockParser( subparsers );
168
169 this.parsers =
170 new BlockParser[] { headingParser, figureParser, verbatimParser, definitionParser, horizontalRuleParser,
171 listParser, tableParser, paragraphParser };
172 }
173 }