Coverage Report - org.apache.maven.doxia.module.twiki.parser.SectionBlock
 
Classes in this File Line Coverage Branch Coverage Complexity
SectionBlock
22%
10/44
62%
5/8
1,727
 
 1  
 package org.apache.maven.doxia.module.twiki.parser;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.lang.reflect.Method;
 23  
 import java.util.Collections;
 24  
 
 25  
 import org.apache.maven.doxia.sink.Sink;
 26  
 
 27  
 /**
 28  
  * Block that represents a section
 29  
  *
 30  
  * @author Juan F. Codagnone
 31  
  * @version $Id: SectionBlock.java 763762 2009-04-09 18:19:56Z ltheussl $
 32  
  */
 33  
 public class SectionBlock
 34  
     extends AbstractFatherBlock
 35  
 {
 36  
     /** {@inheritDoc} */
 37  
     private final String title;
 38  
 
 39  
     /** {@inheritDoc} */
 40  
     private final int level;
 41  
 
 42  
     /**
 43  
      * Creates the SectionBlock.
 44  
      * <p/>
 45  
      * No parameter can be <code>null</code>
 46  
      *
 47  
      * @param title  the section title.
 48  
      * @param level  the section level: 0 < level < 6
 49  
      * @param blocks child blocks
 50  
      */
 51  
     public SectionBlock( final String title, final int level, final Block[] blocks )
 52  
     {
 53  15
         super( blocks );
 54  14
         final int maxLevel = 5;
 55  14
         if ( title == null )
 56  
         {
 57  0
             throw new IllegalArgumentException( "title cant be null" );
 58  
         }
 59  14
         else if ( level < 1 || level > maxLevel )
 60  
         {
 61  2
             throw new IllegalArgumentException( "invalid level: " + level );
 62  
         }
 63  
 
 64  12
         this.title = title;
 65  12
         this.level = level;
 66  12
     }
 67  
 
 68  
     /** {@inheritDoc} */
 69  
     final void before( final Sink sink )
 70  
     {
 71  0
         sectionStart( sink );
 72  0
         sectionTitle( sink );
 73  0
         sink.text( title );
 74  0
         sectionTitle_( sink );
 75  
 
 76  0
     }
 77  
 
 78  
     /** {@inheritDoc} */
 79  
     final void after( final Sink sink )
 80  
     {
 81  0
         sectionEnd( sink );
 82  0
     }
 83  
 
 84  
     /**
 85  
      * call to sink.section<Level>()
 86  
      *
 87  
      * @param sink sink
 88  
      */
 89  
     private void sectionStart( final Sink sink )
 90  
     {
 91  0
         invokeVoidVoid( sink, "section" + level );
 92  0
     }
 93  
 
 94  
     /**
 95  
      * call to sink.section<Level>_()
 96  
      *
 97  
      * @param sink sink
 98  
      */
 99  
     private void sectionEnd( final Sink sink )
 100  
     {
 101  0
         invokeVoidVoid( sink, "section" + level + "_" );
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Let you call sink's methods that returns <code>null</code> and have
 106  
      * no parameters.
 107  
      *
 108  
      * @param sink the Sink
 109  
      * @param name the name of the method to call
 110  
      */
 111  
     private void invokeVoidVoid( final Sink sink, final String name )
 112  
     {
 113  
         try
 114  
         {
 115  0
             final Method m = sink.getClass().getMethod( name, new Class[] {} );
 116  0
             m.invoke( sink, Collections.EMPTY_LIST.toArray() );
 117  
         }
 118  0
         catch ( Exception e )
 119  
         {
 120  
             // FIXME
 121  0
             throw new IllegalArgumentException( "invoking sink's " + name + " method: " + e.getMessage() );
 122  0
         }
 123  0
     }
 124  
 
 125  
     /**
 126  
      * Returns the level.
 127  
      *
 128  
      * @return <code>int</code> with the level.
 129  
      */
 130  
     public final int getLevel()
 131  
     {
 132  1
         return level;
 133  
     }
 134  
 
 135  
     /**
 136  
      * Returns the title.
 137  
      *
 138  
      * @return <code>String</code> with the title.
 139  
      */
 140  
     public final String getTitle()
 141  
     {
 142  2
         return title;
 143  
     }
 144  
 
 145  
     /** {@inheritDoc} */
 146  
     public final String toString()
 147  
     {
 148  0
         final StringBuffer sb = new StringBuffer();
 149  
 
 150  0
         sb.append( "Section  {title: '" );
 151  0
         sb.append( getTitle() );
 152  0
         sb.append( "' level: " );
 153  0
         sb.append( getLevel() );
 154  0
         sb.append( "}: [" );
 155  0
         for ( int i = 0; i < getBlocks().length; i++ )
 156  
         {
 157  0
             final Block block = getBlocks()[i];
 158  
 
 159  0
             sb.append( block.toString() );
 160  0
             sb.append( ", " );
 161  
         }
 162  0
         sb.append( "]" );
 163  0
         return sb.toString();
 164  
     }
 165  
 
 166  
     /** @param sink */
 167  
     private void sectionTitle( final Sink sink )
 168  
     {
 169  0
         invokeVoidVoid( sink, "sectionTitle" + level );
 170  0
     }
 171  
 
 172  
     /** @param sink */
 173  
     private void sectionTitle_( final Sink sink )
 174  
     {
 175  0
         invokeVoidVoid( sink, "sectionTitle" + level + "_" );
 176  0
     }
 177  
 }