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.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.apache.maven.doxia.util.ByLineSource;
26  import org.apache.maven.doxia.parser.ParseException;
27  
28  /**
29   * Block that represents an horizontal rule
30   *
31   * @author Juan F. Codagnone
32   */
33  public class HRuleBlockParser
34      implements BlockParser
35  {
36      /**
37       * pattern used to detect horizontal rulers
38       */
39      private static final Pattern HRULE_PATTERN = Pattern.compile( "^(---)(-*)(.*)$" );
40  
41      /** {@inheritDoc} */
42      public final boolean accept( final String line )
43      {
44          final Matcher m = HRULE_PATTERN.matcher( line );
45          boolean ret = false;
46  
47          if ( m.lookingAt() )
48          {
49              final int textGroup = 3;
50              String s = m.group( textGroup );
51              if ( s != null && !s.startsWith( "+" ) )
52              {
53                  ret = true;
54              }
55          }
56  
57          return ret;
58      }
59  
60      /** {@inheritDoc} */
61      public final Block visit( final String line, final ByLineSource source )
62          throws ParseException
63      {
64          Block ret = new HorizontalRuleBlock();
65          final Matcher matcher = HRULE_PATTERN.matcher( line );
66          if ( matcher.lookingAt() )
67          {
68              final int textGroup = 3;
69              source.unget( matcher.group( textGroup ) );
70          }
71          else
72          {
73              throw new ParseException( "i was expecting a hruler!" );
74          }
75  
76          return ret;
77      }
78  }