1 package org.apache.maven.doxia.module.twiki.parser;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
29
30
31
32
33 public class SectionBlock
34 extends AbstractFatherBlock
35 {
36
37 private final String title;
38
39
40 private final int level;
41
42
43
44
45
46
47
48
49
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
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
79 final void after( final Sink sink )
80 {
81 sectionEnd( sink );
82 }
83
84
85
86
87
88
89 private void sectionStart( final Sink sink )
90 {
91 invokeVoidVoid( sink, "section" + level );
92 }
93
94
95
96
97
98
99 private void sectionEnd( final Sink sink )
100 {
101 invokeVoidVoid( sink, "section" + level + "_" );
102 }
103
104
105
106
107
108
109
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
121 throw new IllegalArgumentException( "invoking sink's " + name + " method: " + e.getMessage() );
122 }
123 }
124
125
126
127
128
129
130 public final int getLevel()
131 {
132 return level;
133 }
134
135
136
137
138
139
140 public final String getTitle()
141 {
142 return title;
143 }
144
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
167 private void sectionTitle( final Sink sink )
168 {
169 invokeVoidVoid( sink, "sectionTitle" + level );
170 }
171
172
173 private void sectionTitle_( final Sink sink )
174 {
175 invokeVoidVoid( sink, "sectionTitle" + level + "_" );
176 }
177 }