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.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.apache.maven.doxia.util.ByLineSource;
27  import org.apache.maven.doxia.parser.ParseException;
28  
29  /**
30   * Parse looking for sections
31   *
32   * @author Juan F. Codagnone
33   */
34  public class SectionBlockParser
35      implements BlockParser
36  {
37      /**
38       * '---++ Header', '---## Header'
39       */
40      private static final Pattern HEADER_DA = Pattern.compile( "^---([+]+)\\s*(.+)\\s*$" );
41  
42      /**
43       * {@link ParagraphBlockParser} to use. injected
44       */
45      private ParagraphBlockParser paraParser;
46  
47      /**
48       * {@link ParagraphBlockParser} to use. injected
49       */
50      private HRuleBlockParser hrulerParser;
51  
52      /** {@link VerbatimBlockParser} */
53      private VerbatimBlockParser verbatimBlockParser;
54  
55      /** {@inheritDoc} */
56      public final boolean accept( final String line )
57      {
58          return HEADER_DA.matcher( line ).lookingAt();
59      }
60  
61      /** {@inheritDoc} */
62      public final Block visit( final String line, final ByLineSource source )
63          throws ParseException
64      {
65          final Matcher m = HEADER_DA.matcher( line );
66  
67          if ( !m.lookingAt() )
68          {
69              throw new IllegalArgumentException( "don't know how to handle: " + line );
70          }
71  
72          String newLine;
73          final ArrayList<Block> blocks = new ArrayList<>();
74  
75          while ( ( newLine = source.getNextLine() ) != null && !accept( newLine ) )
76          {
77              if ( hrulerParser.accept( newLine ) )
78              {
79                  blocks.add( hrulerParser.visit( newLine, source ) );
80              }
81              else
82              {
83                  if ( verbatimBlockParser.accept( newLine ) )
84                  {
85                      blocks.add( verbatimBlockParser.visit( newLine, source ) );
86                  }
87                  else
88                  {
89                      blocks.add( paraParser.visit( newLine, source ) );
90                  }
91              }
92          }
93  
94          if ( newLine != null )
95          {
96              source.ungetLine();
97          }
98  
99          return new SectionBlock( m.group( 2 ), getLevel( m.group( 1 ) ), blocks.toArray( new Block[] {} ) );
100     }
101 
102     /**
103      * @param s "++"
104      * @return tha level of the section
105      * @throws IllegalArgumentException on error
106      */
107     static int getLevel( final String s )
108         throws IllegalArgumentException
109     {
110         for ( int i = 0, n = s.length(); i < n; i++ )
111         {
112             if ( s.charAt( i ) != '+' )
113             {
114                 throw new IllegalArgumentException( "the argument must have only" + " '+' characters" );
115             }
116         }
117         return s.length();
118     }
119 
120     /**
121      * Sets the paraParser.
122      *
123      * @param paraParser <code>ParagraphBlockParser</code> with the paraParser.
124      */
125     public final void setParaParser( final ParagraphBlockParser paraParser )
126     {
127         if ( paraParser == null )
128         {
129             throw new IllegalArgumentException( "argument can't be null" );
130         }
131         this.paraParser = paraParser;
132     }
133 
134     /**
135      * Sets the hrulerParser.
136      *
137      * @param hrulerParser <code>HRuleBlockParser</code> with the hrulerParser.
138      */
139     public final void setHrulerParser( final HRuleBlockParser hrulerParser )
140     {
141         if ( hrulerParser == null )
142         {
143             throw new IllegalArgumentException( "argument can't be null" );
144         }
145         this.hrulerParser = hrulerParser;
146     }
147 
148     /**
149      * Sets the verbatimBlockParser.
150      *
151      * @param verbatimBlockParser <code>VerbatimBlockParser</code> with the verbatimBlockParser.
152      * @since 1.1
153      */
154     public final void setVerbatimBlockParser( VerbatimBlockParser verbatimBlockParser )
155     {
156         if ( verbatimBlockParser == null )
157         {
158             throw new IllegalArgumentException( "argument can't be null" );
159         }
160         this.verbatimBlockParser = verbatimBlockParser;
161     }
162 }