1 package org.apache.maven.doxia.index;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23 import java.util.ArrayList;
24 import java.util.Collections;
25
26 import org.codehaus.plexus.util.StringUtils;
27
28
29
30
31
32
33
34 public class IndexEntry
35 {
36
37
38
39 private final IndexEntry parent;
40
41
42
43
44 private String id;
45
46
47
48
49 private String title;
50
51
52
53
54 private List<IndexEntry> childEntries = new ArrayList<IndexEntry>();
55
56
57
58
59 private static final String EOL = System.getProperty( "line.separator" );
60
61
62
63
64
65
66 public IndexEntry( String newId )
67 {
68 this( null, newId );
69 }
70
71
72
73
74
75
76
77 public IndexEntry( IndexEntry newParent, String newId )
78 {
79 this.parent = newParent;
80 this.id = newId;
81
82 if ( parent != null )
83 {
84 parent.childEntries.add( this );
85 }
86 }
87
88
89
90
91
92
93 public IndexEntry getParent()
94 {
95 return parent;
96 }
97
98
99
100
101
102
103 public String getId()
104 {
105 return id;
106 }
107
108
109
110
111
112
113 protected void setId( String id )
114 {
115 this.id = id;
116 }
117
118
119
120
121
122
123 public String getTitle()
124 {
125 return title;
126 }
127
128
129
130
131
132
133 public void setTitle( String newTitle )
134 {
135 this.title = newTitle;
136 }
137
138
139
140
141
142
143 public List<IndexEntry> getChildEntries()
144 {
145 return Collections.unmodifiableList( childEntries );
146 }
147
148
149
150
151
152
153 public void setChildEntries( List<IndexEntry> entries )
154 {
155 if ( entries == null )
156 {
157 childEntries = new ArrayList<IndexEntry>();
158 }
159
160 this.childEntries = entries;
161 }
162
163
164
165
166
167
168
169
170
171
172 public IndexEntry getNextEntry()
173 {
174 if ( parent == null )
175 {
176 return null;
177 }
178
179 List<IndexEntry> entries = parent.getChildEntries();
180
181 int index = entries.indexOf( this );
182
183 if ( index + 1 >= entries.size() )
184 {
185 return null;
186 }
187
188 return entries.get( index + 1 );
189 }
190
191
192
193
194
195
196 public IndexEntry getPrevEntry()
197 {
198 if ( parent == null )
199 {
200 return null;
201 }
202
203 List<IndexEntry> entries = parent.getChildEntries();
204
205 int index = entries.indexOf( this );
206
207 if ( index == 0 )
208 {
209 return null;
210 }
211
212 return entries.get( index - 1 );
213 }
214
215
216
217
218
219
220 public IndexEntry getFirstEntry()
221 {
222 List<IndexEntry> entries = getChildEntries();
223
224 if ( entries.size() == 0 )
225 {
226 return null;
227 }
228
229 return entries.get( 0 );
230 }
231
232
233
234
235
236
237 public IndexEntry getLastEntry()
238 {
239 List<IndexEntry> entries = getChildEntries();
240
241 if ( entries.size() == 0 )
242 {
243 return null;
244 }
245
246 return entries.get( entries.size() - 1 );
247 }
248
249
250
251
252
253
254 public IndexEntry getRootEntry()
255 {
256 List<IndexEntry> entries = getChildEntries();
257
258 if ( entries.size() == 0 )
259 {
260 return null;
261 }
262 else if ( entries.size() > 1 )
263 {
264 throw new RuntimeException( "This index has more than one root entry" );
265 }
266 else
267 {
268 return entries.get( 0 );
269 }
270 }
271
272
273
274
275
276
277
278
279
280
281 public String toString()
282 {
283 return toString( 0 );
284 }
285
286
287
288
289
290
291
292 public String toString( int depth )
293 {
294 StringBuilder message = new StringBuilder();
295
296 message.append( "Id: " ).append( id );
297
298 if ( StringUtils.isNotEmpty( title ) )
299 {
300 message.append( ", title: " ).append( title );
301 }
302
303 message.append( EOL );
304
305 StringBuilder indent = new StringBuilder( "" );
306
307 for ( int i = 0; i < depth; i++ )
308 {
309 indent.append( " " );
310 }
311
312 for ( IndexEntry entry : getChildEntries() )
313 {
314 message.append( indent ).append( entry.toString( depth + 1 ) );
315 }
316
317 return message.toString();
318 }
319 }