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