001package org.apache.maven.doxia.module.twiki.parser;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025import org.apache.maven.doxia.util.ByLineSource;
026import org.apache.maven.doxia.parser.ParseException;
027
028/**
029 * Block that represents an horizontal rule
030 *
031 * @author Juan F. Codagnone
032 */
033public class HRuleBlockParser
034    implements BlockParser
035{
036    /**
037     * pattern used to detect horizontal rulers
038     */
039    private static final Pattern HRULE_PATTERN = Pattern.compile( "^(---)(-*)(.*)$" );
040
041    /** {@inheritDoc} */
042    public final boolean accept( final String line )
043    {
044        final Matcher m = HRULE_PATTERN.matcher( line );
045        boolean ret = false;
046
047        if ( m.lookingAt() )
048        {
049            final int textGroup = 3;
050            String s = m.group( textGroup );
051            if ( s != null && !s.startsWith( "+" ) )
052            {
053                ret = true;
054            }
055        }
056
057        return ret;
058    }
059
060    /** {@inheritDoc} */
061    public final Block visit( final String line, final ByLineSource source )
062        throws ParseException
063    {
064        Block ret = new HorizontalRuleBlock();
065        final Matcher matcher = HRULE_PATTERN.matcher( line );
066        if ( matcher.lookingAt() )
067        {
068            final int textGroup = 3;
069            source.unget( matcher.group( textGroup ) );
070        }
071        else
072        {
073            throw new ParseException( "i was expecting a hruler!" );
074        }
075
076        return ret;
077    }
078}