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