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 * @version $Id: HRuleBlockParser.html 905940 2014-04-12 16:27:29Z hboutemy $
033 */
034public class HRuleBlockParser
035    implements BlockParser
036{
037    /**
038     * pattern used to detect horizontal rulers
039     */
040    private static final Pattern HRULE_PATTERN = Pattern.compile( "^(---)(-*)(.*)$" );
041
042    /** {@inheritDoc} */
043    public final boolean accept( final String line )
044    {
045        final Matcher m = HRULE_PATTERN.matcher( line );
046        boolean ret = false;
047
048        if ( m.lookingAt() )
049        {
050            final int textGroup = 3;
051            String s = m.group( textGroup );
052            if ( s != null && !s.startsWith( "+" ) )
053            {
054                ret = true;
055            }
056        }
057
058        return ret;
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    public final Block visit( final String line, final ByLineSource source )
065        throws ParseException
066    {
067        Block ret = new HorizontalRuleBlock();
068        final Matcher matcher = HRULE_PATTERN.matcher( line );
069        if ( matcher.lookingAt() )
070        {
071            final int textGroup = 3;
072            source.unget( matcher.group( textGroup ) );
073        }
074        else
075        {
076            throw new ParseException( "i was expecting a hruler!" );
077        }
078
079        return ret;
080    }
081}