1 package org.apache.maven.doxia.module.twiki.parser;
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.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.apache.maven.doxia.util.ByLineSource;
29 import org.apache.maven.doxia.parser.ParseException;
30
31 /**
32 * Parse paragraphs.
33 *
34 * @author Juan F. Codagnone
35 * @version $Id: ParagraphBlockParser.java 1090706 2011-04-09 23:15:28Z hboutemy $
36 */
37 public class ParagraphBlockParser
38 implements BlockParser
39 {
40 /**
41 * pattern used to dectect end of paragraph
42 */
43 private final Pattern paragraphSeparator = Pattern.compile( "^(\\s*)$" );
44
45 /**
46 * {@link SectionBlockParser} to use. injected
47 */
48 private SectionBlockParser sectionParser;
49
50 /**
51 * {@link ListBlockParser} to use. injected
52 */
53 private GenericListBlockParser listParser;
54
55 /**
56 * {@link FormatedTextParser} to use. injected
57 */
58 private FormatedTextParser textParser;
59
60 /**
61 * {@link HRuleBlockParser} to use. injected
62 */
63 private HRuleBlockParser hrulerParser;
64
65 /**
66 * {@link TableBlockParser} to use. injected
67 */
68 private TableBlockParser tableBlockParser;
69
70 /**
71 * {@link TableBlockParser} to use. injected
72 */
73 private VerbatimBlockParser verbatimParser;
74
75 /**
76 * no operation block
77 */
78 private static final NopBlock NOP = new NopBlock();
79
80 /** {@inheritDoc} */
81 public final boolean accept( final String line )
82 {
83 return !sectionParser.accept( line ) && !hrulerParser.accept( line ) && !verbatimParser.accept( line );
84 }
85
86 /**
87 * {@inheritDoc}
88 */
89 public final Block visit( final String line, final ByLineSource source )
90 throws ParseException
91 {
92 StringBuffer sb = new StringBuffer();
93 List<Block> childs = new ArrayList<Block>();
94
95 boolean sawText = false;
96
97 /*
98 * 1. Skip begininig new lines
99 * 2. Get the text, while \n\n is not found
100 */
101 boolean pre = false;
102 String l = line;
103 do
104 {
105 Matcher m = paragraphSeparator.matcher( l );
106
107 if ( m.lookingAt() )
108 {
109 if ( sawText )
110 {
111 break;
112 }
113 }
114 else
115 {
116 sawText = true;
117
118 /* be able to parse lists / enumerations */
119 if ( listParser.accept( l ) )
120 {
121 if ( sb.length() != 0 )
122 {
123 childs.addAll( Arrays.asList( textParser.parse( sb.toString().trim() ) ) );
124 sb = new StringBuffer();
125 }
126 childs.add( listParser.visit( l, source ) );
127 }
128 else if ( tableBlockParser.accept( l ) )
129 {
130 childs.add( tableBlockParser.visit( l, source ) );
131 }
132 else
133 {
134 sb.append( l );
135 // specific
136 if ( l.indexOf( "<pre>" ) != -1 )
137 {
138 pre = true;
139 }
140 if ( l.indexOf( "</pre>" ) != -1 )
141 {
142 pre = false;
143 }
144
145 if ( !pre )
146 {
147 sb.append( " " );
148 }
149 else
150 {
151 // TODO use EOL
152 sb.append( "\n" );
153 }
154 }
155 }
156 l = source.getNextLine();
157 }
158 while ( l != null && accept( l ) );
159
160 if ( line != null )
161 {
162 source.ungetLine();
163 }
164
165 if ( sb.length() != 0 )
166 {
167 childs.addAll( Arrays.asList( textParser.parse( sb.toString().trim() ) ) );
168 sb = new StringBuffer();
169 }
170
171 if ( childs.size() == 0 )
172 {
173 return NOP;
174 }
175
176 return new ParagraphBlock( childs.toArray( new Block[] {} ) );
177 }
178
179 /**
180 * Sets the sectionParser.
181 *
182 * @param aSectionParser <code>SectionBlockParser</code> with the sectionParser.
183 */
184 public final void setSectionParser( final SectionBlockParser aSectionParser )
185 {
186 if ( aSectionParser == null )
187 {
188 throw new IllegalArgumentException( "arg can't be null" );
189 }
190 this.sectionParser = aSectionParser;
191 }
192
193 /**
194 * Sets the listParser.
195 *
196 * @param aListParser <code>ListBlockParser</code> with the listParser.
197 */
198 public final void setListParser( final GenericListBlockParser aListParser )
199 {
200 if ( aListParser == null )
201 {
202 throw new IllegalArgumentException( "arg can't be null" );
203 }
204
205 this.listParser = aListParser;
206 }
207
208 /**
209 * Sets the formatTextParser.
210 *
211 * @param aTextParser <code>FormatedTextParser</code>
212 * with the formatTextParser.
213 */
214 public final void setTextParser( final FormatedTextParser aTextParser )
215 {
216 if ( aTextParser == null )
217 {
218 throw new IllegalArgumentException( "arg can't be null" );
219 }
220 this.textParser = aTextParser;
221 }
222
223 /**
224 * Sets the hrulerParser.
225 *
226 * @param aHrulerParser <code>HRuleBlockParser</code> with the hrulerParser.
227 */
228 public final void setHrulerParser( final HRuleBlockParser aHrulerParser )
229 {
230 if ( aHrulerParser == null )
231 {
232 throw new IllegalArgumentException( "arg can't be null" );
233 }
234
235 this.hrulerParser = aHrulerParser;
236 }
237
238 /**
239 * <p>Setter for the field <code>tableBlockParser</code>.</p>
240 *
241 * @param aTableBlockParser Table parser to use
242 */
243 public final void setTableBlockParser( final TableBlockParser aTableBlockParser )
244 {
245 if ( aTableBlockParser == null )
246 {
247 throw new IllegalArgumentException( "arg can't be null" );
248 }
249
250 this.tableBlockParser = aTableBlockParser;
251 }
252
253 /**
254 * Sets the verbatimParser.
255 *
256 * @param aVerbatimParser <code>VerbatimBlockParser</code> with the verbatimParser.
257 * @since 1.1
258 */
259 public final void setVerbatimParser( final VerbatimBlockParser aVerbatimParser )
260 {
261 if ( aVerbatimParser == null )
262 {
263 throw new IllegalArgumentException( "arg can't be null" );
264 }
265 this.verbatimParser = aVerbatimParser;
266 }
267 }