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