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 | 78 | super( blocks ); |
54 | 76 | final int maxLevel = 5; |
55 | 76 | if ( title == null ) |
56 | |
{ |
57 | 0 | throw new IllegalArgumentException( "title cant be null" ); |
58 | |
} |
59 | 76 | else if ( level < 1 || level > maxLevel ) |
60 | |
{ |
61 | 4 | throw new IllegalArgumentException( "invalid level: " + level ); |
62 | |
} |
63 | |
|
64 | 72 | this.title = title; |
65 | 72 | this.level = level; |
66 | 72 | } |
67 | |
|
68 | |
|
69 | |
final void before( final Sink sink ) |
70 | |
{ |
71 | 48 | sectionStart( sink ); |
72 | 48 | sectionTitle( sink ); |
73 | 48 | sink.text( title ); |
74 | 48 | sectionTitle_( sink ); |
75 | |
|
76 | 48 | } |
77 | |
|
78 | |
|
79 | |
final void after( final Sink sink ) |
80 | |
{ |
81 | 48 | sectionEnd( sink ); |
82 | 48 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private void sectionStart( final Sink sink ) |
90 | |
{ |
91 | 48 | invokeVoidVoid( sink, "section" + level ); |
92 | 48 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
private void sectionEnd( final Sink sink ) |
100 | |
{ |
101 | 48 | invokeVoidVoid( sink, "section" + level + "_" ); |
102 | 48 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
private void invokeVoidVoid( final Sink sink, final String name ) |
112 | |
{ |
113 | |
try |
114 | |
{ |
115 | 192 | final Method m = sink.getClass().getMethod( name, new Class[] {} ); |
116 | 192 | m.invoke( sink, Collections.EMPTY_LIST.toArray() ); |
117 | |
} |
118 | 0 | catch ( Exception e ) |
119 | |
{ |
120 | |
|
121 | 0 | throw new IllegalArgumentException( "invoking sink's " + name + " method: " + e.getMessage() ); |
122 | 192 | } |
123 | 192 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public final int getLevel() |
131 | |
{ |
132 | 2 | return level; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public final String getTitle() |
141 | |
{ |
142 | 8 | return title; |
143 | |
} |
144 | |
|
145 | |
|
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 | |
|
167 | |
private void sectionTitle( final Sink sink ) |
168 | |
{ |
169 | 48 | invokeVoidVoid( sink, "sectionTitle" + level ); |
170 | 48 | } |
171 | |
|
172 | |
|
173 | |
private void sectionTitle_( final Sink sink ) |
174 | |
{ |
175 | 48 | invokeVoidVoid( sink, "sectionTitle" + level + "_" ); |
176 | 48 | } |
177 | |
} |