View Javadoc
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  
24  import org.apache.maven.doxia.sink.Sink;
25  
26  /**
27   * Block that represents a section
28   *
29   * @author Juan F. Codagnone
30   */
31  public class SectionBlock
32      extends AbstractFatherBlock
33  {
34      /** {@inheritDoc} */
35      private final String title;
36  
37      /** {@inheritDoc} */
38      private final int level;
39  
40      /**
41       * Creates the SectionBlock.
42       *
43       * @param title  the section title, cannot be {@code null}
44       * @param level  the section level: 0 < level < 6
45       * @param blocks child blocks, cannot be {@code null}
46       */
47      public SectionBlock( final String title, final int level, final Block[] blocks )
48      {
49          super( blocks );
50          final int maxLevel = 5;
51          if ( title == null )
52          {
53              throw new IllegalArgumentException( "title cant be null" );
54          }
55          else if ( level < 1 || level > maxLevel )
56          {
57              throw new IllegalArgumentException( "invalid level: " + level );
58          }
59  
60          this.title = title;
61          this.level = level;
62      }
63  
64      /** {@inheritDoc} */
65      final void before( final Sink sink )
66      {
67          sectionStart( sink );
68          sectionTitle( sink );
69          sink.text( title );
70          sectionTitle_( sink );
71  
72      }
73  
74      /** {@inheritDoc} */
75      final void after( final Sink sink )
76      {
77          sectionEnd( sink );
78      }
79  
80      /**
81       * call to sink.section<Level>()
82       *
83       * @param sink sink
84       */
85      private void sectionStart( final Sink sink )
86      {
87          invokeVoidVoid( sink, "section" + level );
88      }
89  
90      /**
91       * call to sink.section<Level>_()
92       *
93       * @param sink sink
94       */
95      private void sectionEnd( final Sink sink )
96      {
97          invokeVoidVoid( sink, "section" + level + "_" );
98      }
99  
100     /**
101      * Let you call sink's methods that returns <code>null</code> and have
102      * no parameters.
103      *
104      * @param sink the Sink
105      * @param name the name of the method to call
106      */
107     private void invokeVoidVoid( final Sink sink, final String name )
108     {
109         try
110         {
111             final Method m = sink.getClass().getMethod( name );
112             m.invoke( sink );
113         }
114         catch ( Exception e )
115         {
116             // FIXME
117             throw new IllegalArgumentException( "invoking sink's " + name + " method: " + e.getMessage() );
118         }
119     }
120 
121     /**
122      * Returns the level.
123      *
124      * @return <code>int</code> with the level.
125      */
126     public final int getLevel()
127     {
128         return level;
129     }
130 
131     /**
132      * Returns the title.
133      *
134      * @return <code>String</code> with the title.
135      */
136     public final String getTitle()
137     {
138         return title;
139     }
140 
141     /**
142      * {@inheritDoc}
143      *
144      * @return a {@link java.lang.String} object.
145      */
146     public final String toString()
147     {
148         final StringBuilder sb = new StringBuilder();
149 
150         sb.append( "Section  {title: '" );
151         sb.append( getTitle() );
152         sb.append( "' level: " );
153         sb.append( getLevel() );
154         sb.append( "}: [" );
155         for ( int i = 0; i < getBlocks().length; i++ )
156         {
157             final Block block = getBlocks()[i];
158 
159             sb.append( block.toString() );
160             sb.append( ", " );
161         }
162         sb.append( "]" );
163         return sb.toString();
164     }
165 
166     /** @param sink */
167     private void sectionTitle( final Sink sink )
168     {
169         invokeVoidVoid( sink, "sectionTitle" + level );
170     }
171 
172     /** @param sink */
173     private void sectionTitle_( final Sink sink )
174     {
175         invokeVoidVoid( sink, "sectionTitle" + level + "_" );
176     }
177 }