1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.util;
32
33 import java.io.IOException;
34 import java.text.MessageFormat;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.ResourceBundle;
38 import java.util.Stack;
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class SectionEditor extends LineEditor
53 {
54
55
56 private static final String DEFAULT_SECTION_START = "SECTION-START[";
57
58
59 private static final String DEFAULT_SECTION_END = "SECTION-END";
60
61
62 private Stack<Section> stack;
63
64
65 private final Map<String, Boolean> presenceFlags = new HashMap<String, Boolean>();
66
67
68 public SectionEditor()
69 {
70 this( null, null );
71 }
72
73
74
75
76
77
78 public SectionEditor( final String lineSeparator )
79 {
80 this( null, lineSeparator );
81 }
82
83
84
85
86
87
88 public SectionEditor( final LineEditor editor )
89 {
90 this( editor, null );
91 }
92
93
94
95
96
97
98
99 public SectionEditor( final LineEditor editor, final String lineSeparator )
100 {
101 super( editor, lineSeparator );
102 }
103
104 @Override
105 protected final String editLine( final String line ) throws IOException
106 {
107 if ( this.stack == null )
108 {
109 final Section root = new Section();
110 root.setMode( Section.MODE_HEAD );
111 this.stack = new Stack<Section>();
112 this.stack.push( root );
113 }
114
115 Section current = this.stack.peek();
116 String replacement = null;
117
118 if ( line != null )
119 {
120 final Section child = this.getSection( line );
121
122 if ( child != null )
123 {
124 child.setStartingLine( line );
125 child.setMode( Section.MODE_HEAD );
126
127 if ( current.getMode() == Section.MODE_TAIL && current.getTailContent().length() > 0 )
128 {
129 final Section s = new Section();
130 s.getHeadContent().append( current.getTailContent() );
131 current.getTailContent().setLength( 0 );
132 current.getSections().add( s );
133 current = s;
134 this.stack.push( current );
135 }
136
137 current.getSections().add( child );
138 current.setMode( Section.MODE_TAIL );
139 this.stack.push( child );
140 }
141 else if ( this.isSectionFinished( line ) )
142 {
143 final Section s = this.stack.pop();
144 s.setEndingLine( line );
145
146 if ( this.stack.isEmpty() )
147 {
148 this.stack = null;
149 throw new IOException( getMessage( "unexpectedEndOfSection", this.getLineNumber() ) );
150 }
151
152 if ( this.stack.peek().getName() == null && this.stack.size() > 1 )
153 {
154 this.stack.pop();
155 }
156 }
157 else
158 {
159 switch ( current.getMode() )
160 {
161 case Section.MODE_HEAD:
162 current.getHeadContent().append( line ).append( this.getLineSeparator() );
163 break;
164
165 case Section.MODE_TAIL:
166 current.getTailContent().append( line ).append( this.getLineSeparator() );
167 break;
168
169 default:
170 throw new AssertionError( current.getMode() );
171
172 }
173 }
174 }
175 else
176 {
177 final Section root = this.stack.pop();
178
179 if ( !this.stack.isEmpty() )
180 {
181 this.stack = null;
182 throw new IOException( getMessage( "unexpectedEndOfFile", this.getLineNumber(), root.getName() ) );
183 }
184
185 replacement = this.getOutput( root );
186 this.stack = null;
187 }
188
189 return replacement;
190 }
191
192
193
194
195
196
197
198
199
200
201
202 protected Section getSection( final String line ) throws IOException
203 {
204 Section s = null;
205
206 if ( line != null )
207 {
208 final int markerIndex = line.indexOf( DEFAULT_SECTION_START );
209
210 if ( markerIndex != -1 )
211 {
212 final int startIndex = markerIndex + DEFAULT_SECTION_START.length();
213 final int endIndex = line.indexOf( ']', startIndex );
214
215 if ( endIndex == -1 )
216 {
217 throw new IOException( getMessage( "sectionMarkerParseFailure", line, this.getLineNumber() ) );
218 }
219
220 s = new Section();
221 s.setName( line.substring( startIndex, endIndex ) );
222 }
223 }
224
225 return s;
226 }
227
228
229
230
231
232
233
234
235
236
237
238 protected boolean isSectionFinished( final String line ) throws IOException
239 {
240 return line != null && line.indexOf( DEFAULT_SECTION_END ) != -1;
241 }
242
243
244
245
246
247
248
249
250
251
252
253 protected void editSection( final Section section ) throws IOException
254 {
255 if ( section == null )
256 {
257 throw new NullPointerException( "section" );
258 }
259
260 if ( section.getName() != null )
261 {
262 this.presenceFlags.put( section.getName(), Boolean.TRUE );
263 }
264 }
265
266
267
268
269
270
271
272
273
274 private void editSections( final Section section ) throws IOException
275 {
276 if ( section == null )
277 {
278 throw new NullPointerException( "section" );
279 }
280
281 this.editSection( section );
282 for ( int i = 0, s0 = section.getSections().size(); i < s0; i++ )
283 {
284 this.editSections( section.getSections().get( i ) );
285 }
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300 protected String getOutput( final Section section ) throws IOException
301 {
302 if ( section == null )
303 {
304 throw new NullPointerException( "section" );
305 }
306
307 this.presenceFlags.clear();
308 this.editSections( section );
309 return this.renderSections( section, new StringBuilder( 512 ) ).toString();
310 }
311
312
313
314
315
316
317
318
319
320 public boolean isSectionPresent( final String sectionName )
321 {
322 return sectionName != null && this.presenceFlags.get( sectionName ) != null
323 && this.presenceFlags.get( sectionName ).booleanValue();
324
325 }
326
327
328
329
330
331
332
333
334
335 private StringBuilder renderSections( final Section section, final StringBuilder buffer )
336 {
337 if ( section.getStartingLine() != null )
338 {
339 buffer.append( section.getStartingLine() ).append( this.getLineSeparator() );
340 }
341
342 buffer.append( section.getHeadContent() );
343
344 for ( int i = 0, s0 = section.getSections().size(); i < s0; i++ )
345 {
346 this.renderSections( section.getSections().get( i ), buffer );
347 }
348
349 buffer.append( section.getTailContent() );
350
351 if ( section.getEndingLine() != null )
352 {
353 buffer.append( section.getEndingLine() ).append( this.getLineSeparator() );
354 }
355
356 return buffer;
357 }
358
359 private static String getMessage( final String key, final Object... arguments )
360 {
361 return MessageFormat.format( ResourceBundle.getBundle( SectionEditor.class.getName().
362 replace( '.', '/' ) ).getString( key ), arguments );
363
364 }
365
366 }