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