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  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          super( blocks );
54          final int maxLevel = 5;
55          if ( title == null )
56          {
57              throw new IllegalArgumentException( "title cant be null" );
58          }
59          else if ( level < 1 || level > maxLevel )
60          {
61              throw new IllegalArgumentException( "invalid level: " + level );
62          }
63  
64          this.title = title;
65          this.level = level;
66      }
67  
68      /** {@inheritDoc} */
69      final void before( final Sink sink )
70      {
71          sectionStart( sink );
72          sectionTitle( sink );
73          sink.text( title );
74          sectionTitle_( sink );
75  
76      }
77  
78      /** {@inheritDoc} */
79      final void after( final Sink sink )
80      {
81          sectionEnd( sink );
82      }
83  
84      /**
85       * call to sink.section<Level>()
86       *
87       * @param sink sink
88       */
89      private void sectionStart( final Sink sink )
90      {
91          invokeVoidVoid( sink, "section" + level );
92      }
93  
94      /**
95       * call to sink.section<Level>_()
96       *
97       * @param sink sink
98       */
99      private void sectionEnd( final Sink sink )
100     {
101         invokeVoidVoid( sink, "section" + level + "_" );
102     }
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             final Method m = sink.getClass().getMethod( name, new Class[] {} );
116             m.invoke( sink, Collections.EMPTY_LIST.toArray() );
117         }
118         catch ( Exception e )
119         {
120             // FIXME
121             throw new IllegalArgumentException( "invoking sink's " + name + " method: " + e.getMessage() );
122         }
123     }
124 
125     /**
126      * Returns the level.
127      *
128      * @return <code>int</code> with the level.
129      */
130     public final int getLevel()
131     {
132         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         return title;
143     }
144 
145     /** {@inheritDoc} */
146     public final String toString()
147     {
148         final StringBuffer sb = new StringBuffer();
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 }