001package org.apache.maven.doxia.module.confluence.parser.list;
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 org.apache.maven.doxia.util.ByLineSource;
023import org.apache.maven.doxia.module.confluence.parser.Block;
024import org.apache.maven.doxia.module.confluence.parser.BlockParser;
025import org.apache.maven.doxia.parser.ParseException;
026
027/**
028 * <p>ListBlockParser class.</p>
029 *
030 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
031 * @version $Id$
032 */
033public class ListBlockParser
034    implements BlockParser
035{
036    /** Constant <code>BULLETED_LIST=0</code> */
037    public static final int BULLETED_LIST = 0;
038
039    /** Constant <code>NUMBERED_LIST=1</code> */
040    public static final int NUMBERED_LIST = 1;
041
042    /** {@inheritDoc} */
043    public boolean accept( String line, ByLineSource source )
044    {
045        if ( isList( line ) )
046        {
047            return true;
048        }
049
050        return false;
051    }
052
053    /** {@inheritDoc} */
054    public Block visit( String line, ByLineSource source )
055        throws ParseException
056    {
057        TreeListBuilder treeListBuilder = new TreeListBuilder();
058
059        StringBuilder text = new StringBuilder();
060
061        do
062        {
063            if ( line.trim().length() == 0 )
064            {
065                break;
066            }
067
068            if ( text.length() > 0 && isList( line ) )
069            {
070                // We reached a new line with list prefix
071                addItem( treeListBuilder, text );
072            }
073
074            if ( text.length() == 0 )
075            {
076                text.append( line.trim() );
077            }
078            else
079            {
080                text.append( " " + line.trim() );
081            }
082
083        }
084        while ( ( line = source.getNextLine() ) != null );
085
086        if ( text.length() > 0 )
087        {
088            addItem( treeListBuilder, text );
089        }
090
091        return treeListBuilder.getBlock();
092    }
093
094    private void addItem( TreeListBuilder treeListBuilder, StringBuilder text )
095    {
096        String item = text.toString();
097        int level = getLevel( item );
098
099        if ( isBulletedList( item, level - 1 ) )
100        {
101            treeListBuilder.feedEntry( BULLETED_LIST, level, item.substring( level ) );
102        }
103        else
104        {
105            treeListBuilder.feedEntry( NUMBERED_LIST, level, item.substring( level ) );
106        }
107        text.setLength( 0 );
108    }
109
110    private int getLevel( String line )
111    {
112        int level = 0;
113
114        while ( line.charAt( level ) == '*' || line.charAt( level ) == '-' || line.charAt( level ) == '#' )
115        {
116            level++;
117        }
118
119        return level;
120    }
121
122    private boolean isBulletedList( String line, int deph )
123    {
124        return ( line.charAt( deph ) == '*' || line.charAt( deph ) == '-' );
125    }
126
127    private boolean isList( String line )
128    {
129        line = line.trim();
130
131        if ( line.startsWith( "*" ) || line.startsWith( "-" ) || line.startsWith( "#" ) )
132        {
133            String temp = line.substring( 1 );
134            while ( temp.length() > 0
135                && ( temp.charAt( 0 ) == '*' || temp.charAt( 0 ) == '-' || temp.charAt( 0 ) == '#' ) )
136            {
137                temp = temp.substring( 1 );
138            }
139
140            if ( temp.length() > 0 && temp.charAt( 0 ) == ' ' )
141            {
142                return true;
143            }
144        }
145
146        return false;
147    }
148}