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.ArrayList; 023import java.util.List; 024import java.util.regex.Matcher; 025import java.util.regex.Pattern; 026 027import org.apache.maven.doxia.util.ByLineSource; 028import org.apache.maven.doxia.parser.ParseException; 029 030/** 031 * Parse tables 032 * 033 * @author Juan F. Codagnone 034 * @version $Id: TableBlockParser.html 905940 2014-04-12 16:27:29Z hboutemy $ 035 */ 036public class TableBlockParser 037 implements BlockParser 038{ 039 /** 040 * pattern to detect tables 041 */ 042 private static final Pattern TABLE_PATTERN = Pattern.compile( "^\\s*([|].*[|])+\\s*$" ); 043 044 /** 045 * text parser 046 */ 047 private FormatedTextParser textParser; 048 049 /** {@inheritDoc} */ 050 public final boolean accept( final String line ) 051 { 052 return TABLE_PATTERN.matcher( line ).lookingAt(); 053 } 054 055 /** 056 * {@inheritDoc} 057 */ 058 public final Block visit( final String line, final ByLineSource source ) 059 throws ParseException 060 { 061 if ( !accept( line ) ) 062 { 063 throw new IllegalAccessError( "call accept before this ;)" ); 064 } 065 066 final List<Block> rows = new ArrayList<Block>(); 067 String l = line; 068 069 do 070 { 071 final Matcher m = TABLE_PATTERN.matcher( l ); 072 if ( m.lookingAt() ) 073 { 074 final List<Block> cells = new ArrayList<Block>(); 075 076 /* for each cell... */ 077 for ( int lh = l.indexOf( '|' ) + 1, rh; ( rh = l.indexOf( '|', lh ) ) != -1; lh = rh + 1 ) 078 { 079 final Block[] bs = textParser.parse( l.substring( lh, rh ).trim() ); 080 if ( bs.length == 1 && bs[0] instanceof BoldBlock ) 081 { 082 final Block[] tmp = ( (BoldBlock) bs[0] ).getBlocks(); 083 cells.add( new TableCellHeaderBlock( tmp ) ); 084 } 085 else 086 { 087 cells.add( new TableCellBlock( bs ) ); 088 } 089 } 090 rows.add( new TableRowBlock( (Block[]) cells.toArray( new Block[] {} ) ) ); 091 } 092 093 } 094 while ( ( l = source.getNextLine() ) != null && accept( l ) ); 095 096 assert rows.size() >= 1; 097 098 return new TableBlock( rows.toArray( new Block[] {} ) ); 099 } 100 101 /** 102 * <p>Setter for the field <code>textParser</code>.</p> 103 * 104 * @param textParser text parser to be set 105 */ 106 public final void setTextParser( final FormatedTextParser textParser ) 107 { 108 if ( textParser == null ) 109 { 110 throw new IllegalArgumentException( "argument can't be null" ); 111 } 112 113 this.textParser = textParser; 114 } 115}