1 | /* |
2 | * Copyright (c) 2009 The JOMC Project |
3 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * o Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * |
13 | * o Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in |
15 | * the documentation and/or other materials provided with the |
16 | * distribution. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * |
30 | * $Id: JavaSources.java 1435 2010-01-30 01:52:40Z schulte2005 $ |
31 | * |
32 | */ |
33 | package org.jomc.tools; |
34 | |
35 | import java.io.File; |
36 | import java.io.IOException; |
37 | import java.io.StringWriter; |
38 | import java.text.MessageFormat; |
39 | import java.util.ResourceBundle; |
40 | import java.util.logging.Level; |
41 | import org.apache.commons.io.FileUtils; |
42 | import org.apache.velocity.Template; |
43 | import org.apache.velocity.VelocityContext; |
44 | import org.jomc.model.Dependencies; |
45 | import org.jomc.model.Implementation; |
46 | import org.jomc.model.Messages; |
47 | import org.jomc.model.Module; |
48 | import org.jomc.model.Properties; |
49 | import org.jomc.model.Specification; |
50 | import org.jomc.model.Specifications; |
51 | import org.jomc.util.LineEditor; |
52 | import org.jomc.util.Section; |
53 | import org.jomc.util.SectionEditor; |
54 | import org.jomc.util.TrailingWhitespaceEditor; |
55 | |
56 | /** |
57 | * Manages Java source code. |
58 | * |
59 | * <p><b>Use cases</b><br/><ul> |
60 | * <li>{@link #manageSources(java.io.File) }</li> |
61 | * <li>{@link #manageSources(org.jomc.model.Module, java.io.File) }</li> |
62 | * <li>{@link #manageSources(org.jomc.model.Specification, java.io.File) }</li> |
63 | * <li>{@link #manageSources(org.jomc.model.Implementation, java.io.File) }</li> |
64 | * </ul></p> |
65 | * |
66 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
67 | * @version $Id: JavaSources.java 1435 2010-01-30 01:52:40Z schulte2005 $ |
68 | * |
69 | * @see #getModules() |
70 | */ |
71 | public class JavaSources extends JomcTool |
72 | { |
73 | |
74 | /** Constant for the name of the constructors source code section. */ |
75 | private static final String CONSTRUCTORS_SECTION_NAME = "Constructors"; |
76 | |
77 | /** Constant for the name of the default constructor source code section. */ |
78 | private static final String DEFAULT_CONSTRUCTOR_SECTION_NAME = "Default Constructor"; |
79 | |
80 | /** Constant for the name of the dependencies source code section. */ |
81 | private static final String DEPENDENCIES_SECTION_NAME = "Dependencies"; |
82 | |
83 | /** Constant for the name of the properties source code section. */ |
84 | private static final String PROPERTIES_SECTION_NAME = "Properties"; |
85 | |
86 | /** Constant for the name of the messages source code section. */ |
87 | private static final String MESSAGES_SECTION_NAME = "Messages"; |
88 | |
89 | /** Constant for the name of the license source code section. */ |
90 | private static final String LICENSE_SECTION_NAME = "License Header"; |
91 | |
92 | /** Constant for the name of the documentation source code section. */ |
93 | private static final String DOCUMENTATION_SECTION_NAME = "Documentation"; |
94 | |
95 | /** Constant for the name of the implementation annotations source code section. */ |
96 | private static final String ANNOTATIONS_SECTION_NAME = "Annotations"; |
97 | |
98 | /** Name of the generator. */ |
99 | private static final String GENERATOR_NAME = JavaSources.class.getName(); |
100 | |
101 | /** Constant for the version of the generator. */ |
102 | private static final String GENERATOR_VERSION = "1.0"; |
103 | |
104 | /** Name of the {@code implementation-constructors-head.vm} template. */ |
105 | private static final String CONSTRUCTORS_HEAD_TEMPLATE = "implementation-constructors-head.vm"; |
106 | |
107 | /** Name of the {@code implementation-constructors-tail.vm} template. */ |
108 | private static final String CONSTRUCTORS_TAIL_TEMPLATE = "implementation-constructors-tail.vm"; |
109 | |
110 | /** Name of the {@code implementation-dependencies.vm} template. */ |
111 | private static final String DEPENDENCIES_TEMPLATE = "implementation-dependencies.vm"; |
112 | |
113 | /** Name of the {@code implementation-properties.vm} template. */ |
114 | private static final String PROPERTIES_TEMPLATE = "implementation-properties.vm"; |
115 | |
116 | /** Name of the {@code implementation-messages.vm} template. */ |
117 | private static final String MESSAGES_TEMPLATE = "implementation-messages.vm"; |
118 | |
119 | /** Name of the {@code specification-license.vm} template. */ |
120 | private static final String SPECIFICATION_LICENSE_TEMPLATE = "specification-license.vm"; |
121 | |
122 | /** Name of the {@code implementation-license.vm} template. */ |
123 | private static final String IMPLEMENTATION_LICENSE_TEMPLATE = "implementation-license.vm"; |
124 | |
125 | /** Name of the {@code specification-documentation.vm} template. */ |
126 | private static final String SPECIFICATION_DOCUMENTATION_TEMPLATE = "specification-documentation.vm"; |
127 | |
128 | /** Name of the {@code implementation-documentation.vm} template. */ |
129 | private static final String IMPLEMENTATION_DOCUMENTATION_TEMPLATE = "implementation-documentation.vm"; |
130 | |
131 | /** Name of the {@code Implementation.java.vm} template. */ |
132 | private static final String IMPLEMENTATION_TEMPLATE = "Implementation.java.vm"; |
133 | |
134 | /** Name of the {@code Specification.java.vm} template. */ |
135 | private static final String SPECIFICATION_TEMPLATE = "Specification.java.vm"; |
136 | |
137 | /** Name of the {@code specification-annotations.vm} template. */ |
138 | private static final String SPECIFICATION_ANNOTATIONS_TEMPLATE = "specification-annotations.vm"; |
139 | |
140 | /** Name of the {@code implementation-annotations.vm} template. */ |
141 | private static final String IMPLEMENTATION_ANNOTATIONS_TEMPLATE = "implementation-annotations.vm"; |
142 | |
143 | /** Creates a new {@code JavaSources} instance. */ |
144 | public JavaSources() |
145 | { |
146 | super(); |
147 | } |
148 | |
149 | /** |
150 | * Creates a new {@code JavaSources} instance taking a {@code JavaSources} instance to initialize the instance with. |
151 | * |
152 | * @param tool The instance to initialize the new instance with, |
153 | */ |
154 | public JavaSources( final JavaSources tool ) |
155 | { |
156 | super( tool ); |
157 | } |
158 | |
159 | /** |
160 | * Manages the source code of the modules of the instance. |
161 | * |
162 | * @param sourcesDirectory The directory holding the sources to manage. |
163 | * |
164 | * @throws NullPointerException if {@code sourcesDirectory} is {@code null}. |
165 | * @throws ToolException if managing sources fails. |
166 | * |
167 | * @see #manageSources(org.jomc.model.Module, java.io.File) |
168 | */ |
169 | public void manageSources( final File sourcesDirectory ) throws ToolException |
170 | { |
171 | if ( sourcesDirectory == null ) |
172 | { |
173 | throw new NullPointerException( "sourcesDirectory" ); |
174 | } |
175 | |
176 | for ( Module m : this.getModules().getModule() ) |
177 | { |
178 | this.manageSources( m, sourcesDirectory ); |
179 | } |
180 | } |
181 | |
182 | /** |
183 | * Manages the source code of a given module of the modules of the instance. |
184 | * |
185 | * @param module The module to process. |
186 | * @param sourcesDirectory The directory holding the sources to manage. |
187 | * |
188 | * @throws NullPointerException if {@code module} or {@code sourcesDirectory} is {@code null}. |
189 | * @throws ToolException if managing sources fails. |
190 | * |
191 | * @see #manageSources(org.jomc.model.Specification, java.io.File) |
192 | * @see #manageSources(org.jomc.model.Implementation, java.io.File) |
193 | */ |
194 | public void manageSources( final Module module, final File sourcesDirectory ) throws ToolException |
195 | { |
196 | if ( module == null ) |
197 | { |
198 | throw new NullPointerException( "module" ); |
199 | } |
200 | if ( sourcesDirectory == null ) |
201 | { |
202 | throw new NullPointerException( "sourcesDirectory" ); |
203 | } |
204 | |
205 | if ( module.getSpecifications() != null ) |
206 | { |
207 | for ( Specification s : module.getSpecifications().getSpecification() ) |
208 | { |
209 | this.manageSources( s, sourcesDirectory ); |
210 | } |
211 | } |
212 | if ( module.getImplementations() != null ) |
213 | { |
214 | for ( Implementation i : module.getImplementations().getImplementation() ) |
215 | { |
216 | this.manageSources( i, sourcesDirectory ); |
217 | } |
218 | } |
219 | } |
220 | |
221 | /** |
222 | * Manages the source code of a given specification of the modules of the instance. |
223 | * |
224 | * @param specification The specification to process. |
225 | * @param sourcesDirectory The directory holding the sources to manage. |
226 | * |
227 | * @throws NullPointerException if {@code specification} or {@code sourcesDirectory} is {@code null}. |
228 | * @throws ToolException if managing sources fails. |
229 | * |
230 | * @see #getSpecificationEditor(org.jomc.model.Specification) |
231 | */ |
232 | public void manageSources( final Specification specification, final File sourcesDirectory ) throws ToolException |
233 | { |
234 | if ( specification == null ) |
235 | { |
236 | throw new NullPointerException( "specification" ); |
237 | } |
238 | if ( sourcesDirectory == null ) |
239 | { |
240 | throw new NullPointerException( "sourcesDirectory" ); |
241 | } |
242 | |
243 | try |
244 | { |
245 | final Implementation i = this.getModules().getImplementation( specification.getIdentifier() ); |
246 | |
247 | if ( i != null && i.isClassDeclaration() ) |
248 | { |
249 | this.manageSources( i, sourcesDirectory ); |
250 | } |
251 | else if ( specification.isClassDeclaration() ) |
252 | { |
253 | final File f = |
254 | new File( sourcesDirectory, specification.getIdentifier().replace( '.', '/' ) + ".java" ); |
255 | |
256 | final String content = f.exists() |
257 | ? FileUtils.readFileToString( f, this.getInputEncoding() ) |
258 | : this.getSpecificationTemplate( specification ); |
259 | |
260 | final JavaSpecificationEditor editor = this.getSpecificationEditor( specification ); |
261 | final String edited; |
262 | try |
263 | { |
264 | edited = editor.edit( content ); |
265 | } |
266 | catch ( final IOException e ) |
267 | { |
268 | throw new ToolException( getMessage( "failedEditing", f.getCanonicalPath(), e.getMessage() ), e ); |
269 | } |
270 | |
271 | if ( !editor.isLicenseSectionPresent() && this.isLoggable( Level.INFO ) ) |
272 | { |
273 | this.log( Level.INFO, getMessage( "missingOptionalSection", LICENSE_SECTION_NAME, |
274 | f.getCanonicalPath() ), null ); |
275 | |
276 | } |
277 | |
278 | if ( !editor.isAnnotationsSectionPresent() ) |
279 | { |
280 | throw new IOException( getMessage( "missingSection", ANNOTATIONS_SECTION_NAME, |
281 | f.getCanonicalPath() ) ); |
282 | |
283 | } |
284 | |
285 | if ( !editor.isDocumentationSectionPresent() && this.isLoggable( Level.INFO ) ) |
286 | { |
287 | this.log( Level.INFO, getMessage( "missingOptionalSection", DOCUMENTATION_SECTION_NAME, |
288 | f.getCanonicalPath() ), null ); |
289 | |
290 | } |
291 | |
292 | if ( !edited.equals( content ) ) |
293 | { |
294 | if ( !f.getParentFile().exists() && !f.getParentFile().mkdirs() ) |
295 | { |
296 | throw new ToolException( getMessage( "failedCreatingDirectory", |
297 | f.getParentFile().getAbsolutePath() ) ); |
298 | |
299 | } |
300 | |
301 | if ( this.isLoggable( Level.INFO ) ) |
302 | { |
303 | this.log( Level.INFO, getMessage( "editing", f.getCanonicalPath() ), null ); |
304 | } |
305 | |
306 | FileUtils.writeStringToFile( f, edited, this.getOutputEncoding() ); |
307 | } |
308 | } |
309 | } |
310 | catch ( final IOException e ) |
311 | { |
312 | throw new ToolException( e ); |
313 | } |
314 | } |
315 | |
316 | /** |
317 | * Manages the source code of a given implementation of the modules of the instance. |
318 | * |
319 | * @param implementation The implementation to process. |
320 | * @param sourcesDirectory The directory holding the sources to manage. |
321 | * |
322 | * @throws NullPointerException if {@code implementation} or {@code sourcesDirectory} is {@code null}. |
323 | * @throws ToolException if managing sources fails. |
324 | * |
325 | * @see #getImplementationEditor(org.jomc.model.Implementation) |
326 | */ |
327 | public void manageSources( final Implementation implementation, final File sourcesDirectory ) throws ToolException |
328 | { |
329 | if ( implementation == null ) |
330 | { |
331 | throw new NullPointerException( "implementation" ); |
332 | } |
333 | if ( sourcesDirectory == null ) |
334 | { |
335 | throw new NullPointerException( "sourcesDirectory" ); |
336 | } |
337 | |
338 | try |
339 | { |
340 | if ( implementation.isClassDeclaration() ) |
341 | { |
342 | final File f = new File( sourcesDirectory, implementation.getClazz().replace( '.', '/' ) + ".java" ); |
343 | final String content = f.exists() |
344 | ? FileUtils.readFileToString( f, this.getInputEncoding() ) |
345 | : this.getImplementationTemplate( implementation ); |
346 | |
347 | final JavaImplementationEditor editor = this.getImplementationEditor( implementation ); |
348 | final String edited; |
349 | try |
350 | { |
351 | edited = editor.edit( content ); |
352 | } |
353 | catch ( final IOException e ) |
354 | { |
355 | throw new ToolException( getMessage( "failedEditing", f.getCanonicalPath(), e.getMessage() ), e ); |
356 | } |
357 | |
358 | if ( !editor.isLicenseSectionPresent() && this.isLoggable( Level.INFO ) ) |
359 | { |
360 | this.log( Level.INFO, getMessage( "missingOptionalSection", LICENSE_SECTION_NAME, |
361 | f.getCanonicalPath() ), null ); |
362 | |
363 | } |
364 | |
365 | if ( !editor.isAnnotationsSectionPresent() ) |
366 | { |
367 | throw new ToolException( getMessage( "missingSection", ANNOTATIONS_SECTION_NAME, |
368 | f.getCanonicalPath() ) ); |
369 | |
370 | } |
371 | |
372 | if ( !editor.isDocumentationSectionPresent() && this.isLoggable( Level.INFO ) ) |
373 | { |
374 | this.log( Level.INFO, getMessage( "missingOptionalSection", DOCUMENTATION_SECTION_NAME, |
375 | f.getCanonicalPath() ), null ); |
376 | |
377 | } |
378 | |
379 | if ( !editor.isConstructorsSectionPresent() ) |
380 | { |
381 | final Specifications specifications = |
382 | this.getModules().getSpecifications( implementation.getIdentifier() ); |
383 | |
384 | if ( specifications != null && |
385 | !( specifications.getSpecification().isEmpty() && specifications.getReference().isEmpty() ) ) |
386 | { |
387 | throw new ToolException( getMessage( "missingSection", CONSTRUCTORS_SECTION_NAME, |
388 | f.getCanonicalPath() ) ); |
389 | |
390 | } |
391 | else if ( this.isLoggable( Level.INFO ) ) |
392 | { |
393 | this.log( Level.INFO, getMessage( "missingOptionalSection", CONSTRUCTORS_SECTION_NAME, |
394 | f.getCanonicalPath() ), null ); |
395 | |
396 | } |
397 | } |
398 | else if ( !editor.isDefaultConstructorSectionPresent() ) |
399 | { |
400 | throw new ToolException( getMessage( "missingSection", DEFAULT_CONSTRUCTOR_SECTION_NAME, |
401 | f.getCanonicalPath() ) ); |
402 | |
403 | } |
404 | |
405 | if ( !editor.isPropertiesSectionPresent() ) |
406 | { |
407 | final Properties properties = this.getModules().getProperties( implementation.getIdentifier() ); |
408 | |
409 | if ( properties != null && !properties.getProperty().isEmpty() ) |
410 | { |
411 | throw new ToolException( getMessage( "missingSection", PROPERTIES_SECTION_NAME, |
412 | f.getCanonicalPath() ) ); |
413 | |
414 | } |
415 | else if ( this.isLoggable( Level.INFO ) ) |
416 | { |
417 | this.log( Level.INFO, getMessage( "missingOptionalSection", PROPERTIES_SECTION_NAME, |
418 | f.getCanonicalPath() ), null ); |
419 | |
420 | } |
421 | } |
422 | |
423 | if ( !editor.isDependenciesSectionPresent() ) |
424 | { |
425 | final Dependencies dependencies = |
426 | this.getModules().getDependencies( implementation.getIdentifier() ); |
427 | |
428 | if ( dependencies != null && !dependencies.getDependency().isEmpty() ) |
429 | { |
430 | throw new ToolException( getMessage( "missingSection", DEPENDENCIES_SECTION_NAME, |
431 | f.getCanonicalPath() ) ); |
432 | |
433 | } |
434 | else if ( this.isLoggable( Level.INFO ) ) |
435 | { |
436 | this.log( Level.INFO, getMessage( "missingOptionalSection", DEPENDENCIES_SECTION_NAME, |
437 | f.getCanonicalPath() ), null ); |
438 | |
439 | } |
440 | } |
441 | |
442 | if ( !editor.isMessagesSectionPresent() ) |
443 | { |
444 | final Messages messages = this.getModules().getMessages( implementation.getIdentifier() ); |
445 | |
446 | if ( messages != null && !messages.getMessage().isEmpty() ) |
447 | { |
448 | throw new ToolException( getMessage( "missingSection", MESSAGES_SECTION_NAME, |
449 | f.getCanonicalPath() ) ); |
450 | |
451 | } |
452 | else if ( this.isLoggable( Level.INFO ) ) |
453 | { |
454 | this.log( Level.INFO, getMessage( "missingOptionalSection", MESSAGES_SECTION_NAME, |
455 | f.getCanonicalPath() ), null ); |
456 | |
457 | } |
458 | } |
459 | |
460 | if ( !edited.equals( content ) ) |
461 | { |
462 | if ( !f.getParentFile().exists() && !f.getParentFile().mkdirs() ) |
463 | { |
464 | throw new ToolException( getMessage( "failedCreatingDirectory", |
465 | f.getParentFile().getAbsolutePath() ) ); |
466 | |
467 | } |
468 | |
469 | if ( this.isLoggable( Level.INFO ) ) |
470 | { |
471 | this.log( Level.INFO, getMessage( "editing", f.getCanonicalPath() ), null ); |
472 | } |
473 | |
474 | FileUtils.writeStringToFile( f, edited, this.getOutputEncoding() ); |
475 | } |
476 | } |
477 | } |
478 | catch ( final IOException e ) |
479 | { |
480 | throw new ToolException( e ); |
481 | } |
482 | } |
483 | |
484 | /** |
485 | * Gets a new editor for editing Java specification source code. |
486 | * |
487 | * @param specification The specification to create a new editor for. |
488 | * |
489 | * @return A new editor for editing the source code of {@code specification}. |
490 | * |
491 | * @throws NullPointerException if {@code specification} is {@code null}. |
492 | */ |
493 | public JavaSpecificationEditor getSpecificationEditor( final Specification specification ) |
494 | { |
495 | if ( specification == null ) |
496 | { |
497 | throw new NullPointerException( "specification" ); |
498 | } |
499 | |
500 | return new JavaSpecificationEditor( new TrailingWhitespaceEditor(), specification ); |
501 | } |
502 | |
503 | /** |
504 | * Gets a new editor for editing Java implementation source code. |
505 | * |
506 | * @param implementation The implementation to create a new editor for. |
507 | * |
508 | * @return A new editor for editing the source code of {@code implementation}. |
509 | * |
510 | * @throws NullPointerException if {@code implementation} is {@code null}. |
511 | */ |
512 | public JavaImplementationEditor getImplementationEditor( final Implementation implementation ) |
513 | { |
514 | if ( implementation == null ) |
515 | { |
516 | throw new NullPointerException( "implementation" ); |
517 | } |
518 | |
519 | return new JavaImplementationEditor( new TrailingWhitespaceEditor(), implementation ); |
520 | } |
521 | |
522 | /** |
523 | * Gets the velocity context used for merging templates. |
524 | * |
525 | * @return The velocity context used for merging templates. |
526 | */ |
527 | @Override |
528 | public VelocityContext getVelocityContext() |
529 | { |
530 | final VelocityContext ctx = super.getVelocityContext(); |
531 | ctx.put( "generatorName", GENERATOR_NAME ); |
532 | ctx.put( "generatorVersion", GENERATOR_VERSION ); |
533 | return ctx; |
534 | } |
535 | |
536 | /** |
537 | * Gets the Java source code template of specification. |
538 | * |
539 | * @param specification The specification to get the source code template of. |
540 | * |
541 | * @throws ToolException if getting the source code section fails. |
542 | */ |
543 | private String getSpecificationTemplate( final Specification specification ) throws ToolException |
544 | { |
545 | try |
546 | { |
547 | final StringWriter writer = new StringWriter(); |
548 | final VelocityContext ctx = this.getVelocityContext(); |
549 | final Template template = this.getVelocityTemplate( SPECIFICATION_TEMPLATE ); |
550 | ctx.put( "specification", specification ); |
551 | ctx.put( "template", template ); |
552 | template.merge( ctx, writer ); |
553 | writer.close(); |
554 | return writer.toString(); |
555 | } |
556 | catch ( final IOException e ) |
557 | { |
558 | throw new ToolException( e ); |
559 | } |
560 | } |
561 | |
562 | /** |
563 | * Gets the Java source code template of an implementation. |
564 | * |
565 | * @param implementation The implementation to get the source code template of. |
566 | * |
567 | * @throws ToolException if getting the source code section fails. |
568 | */ |
569 | private String getImplementationTemplate( final Implementation implementation ) throws ToolException |
570 | { |
571 | try |
572 | { |
573 | final StringWriter writer = new StringWriter(); |
574 | final VelocityContext ctx = this.getVelocityContext(); |
575 | final Template template = this.getVelocityTemplate( IMPLEMENTATION_TEMPLATE ); |
576 | ctx.put( "implementation", implementation ); |
577 | ctx.put( "template", template ); |
578 | template.merge( ctx, writer ); |
579 | writer.close(); |
580 | return writer.toString(); |
581 | } |
582 | catch ( final IOException e ) |
583 | { |
584 | throw new ToolException( e ); |
585 | } |
586 | } |
587 | |
588 | /** |
589 | * Gets the Java source code of the license section of a specification. |
590 | * |
591 | * @param specification The specification to get the source code of the license section of. |
592 | * |
593 | * @throws ToolException if getting the source code section fails. |
594 | */ |
595 | private String getLicenseSection( final Specification specification ) throws ToolException |
596 | { |
597 | try |
598 | { |
599 | final StringWriter writer = new StringWriter(); |
600 | final VelocityContext ctx = this.getVelocityContext(); |
601 | final Template template = this.getVelocityTemplate( SPECIFICATION_LICENSE_TEMPLATE ); |
602 | ctx.put( "specification", specification ); |
603 | ctx.put( "template", template ); |
604 | template.merge( ctx, writer ); |
605 | writer.close(); |
606 | return writer.toString(); |
607 | } |
608 | catch ( final IOException e ) |
609 | { |
610 | throw new ToolException( e ); |
611 | } |
612 | } |
613 | |
614 | /** |
615 | * Gets the Java source code of the license section of an implementation.. |
616 | * |
617 | * @param implementation The implementation to get the source code of the license section of. |
618 | * |
619 | * @throws ToolException if getting the source code section fails. |
620 | */ |
621 | private String getLicenseSection( final Implementation implementation ) throws ToolException |
622 | { |
623 | try |
624 | { |
625 | final StringWriter writer = new StringWriter(); |
626 | final VelocityContext ctx = this.getVelocityContext(); |
627 | final Template template = this.getVelocityTemplate( IMPLEMENTATION_LICENSE_TEMPLATE ); |
628 | ctx.put( "implementation", implementation ); |
629 | ctx.put( "template", template ); |
630 | template.merge( ctx, writer ); |
631 | writer.close(); |
632 | return writer.toString(); |
633 | } |
634 | catch ( final IOException e ) |
635 | { |
636 | throw new ToolException( e ); |
637 | } |
638 | } |
639 | |
640 | /** |
641 | * Gets the Java source code of the specification annotations section. |
642 | * |
643 | * @param specification The specification to get the source code of the annotations section of. |
644 | * |
645 | * @throws ToolException if getting the source code section fails. |
646 | */ |
647 | private String getAnnotationsSection( final Specification specification ) throws ToolException |
648 | { |
649 | try |
650 | { |
651 | final StringWriter writer = new StringWriter(); |
652 | final VelocityContext ctx = this.getVelocityContext(); |
653 | final Template template = this.getVelocityTemplate( SPECIFICATION_ANNOTATIONS_TEMPLATE ); |
654 | ctx.put( "specification", specification ); |
655 | ctx.put( "template", template ); |
656 | template.merge( ctx, writer ); |
657 | writer.close(); |
658 | return writer.toString(); |
659 | } |
660 | catch ( final IOException e ) |
661 | { |
662 | throw new ToolException( e ); |
663 | } |
664 | } |
665 | |
666 | /** |
667 | * Gets the Java source code of the implementation annotations section. |
668 | * |
669 | * @param implementation The implementation to get the source code of the annotations section of. |
670 | * |
671 | * @throws ToolException if getting the source code section fails. |
672 | */ |
673 | private String getAnnotationsSection( final Implementation implementation ) throws ToolException |
674 | { |
675 | try |
676 | { |
677 | final StringWriter writer = new StringWriter(); |
678 | final VelocityContext ctx = this.getVelocityContext(); |
679 | final Template template = this.getVelocityTemplate( IMPLEMENTATION_ANNOTATIONS_TEMPLATE ); |
680 | ctx.put( "implementation", implementation ); |
681 | ctx.put( "template", template ); |
682 | template.merge( ctx, writer ); |
683 | writer.close(); |
684 | return writer.toString(); |
685 | } |
686 | catch ( final IOException e ) |
687 | { |
688 | throw new ToolException( e ); |
689 | } |
690 | } |
691 | |
692 | /** |
693 | * Gets the Java source code of the documentation section of a specification. |
694 | * |
695 | * @param specification The specification to get the source code section of. |
696 | * |
697 | * @throws ToolException if getting the source code section fails. |
698 | */ |
699 | private String getDocumentationSection( final Specification specification ) throws ToolException |
700 | { |
701 | try |
702 | { |
703 | final StringWriter writer = new StringWriter(); |
704 | final VelocityContext ctx = this.getVelocityContext(); |
705 | final Template template = this.getVelocityTemplate( SPECIFICATION_DOCUMENTATION_TEMPLATE ); |
706 | ctx.put( "specification", specification ); |
707 | ctx.put( "template", template ); |
708 | template.merge( ctx, writer ); |
709 | writer.close(); |
710 | return writer.toString(); |
711 | } |
712 | catch ( final IOException e ) |
713 | { |
714 | throw new ToolException( e ); |
715 | } |
716 | } |
717 | |
718 | /** |
719 | * Gets the Java source code of the documentation section of an implementation. |
720 | * |
721 | * @param implementation The implementation to get the source code section of. |
722 | * |
723 | * @throws ToolException if getting the source code section fails. |
724 | */ |
725 | private String getDocumentationSection( final Implementation implementation ) throws ToolException |
726 | { |
727 | try |
728 | { |
729 | final StringWriter writer = new StringWriter(); |
730 | final VelocityContext ctx = this.getVelocityContext(); |
731 | final Template template = this.getVelocityTemplate( IMPLEMENTATION_DOCUMENTATION_TEMPLATE ); |
732 | ctx.put( "implementation", implementation ); |
733 | ctx.put( "template", template ); |
734 | template.merge( ctx, writer ); |
735 | writer.close(); |
736 | return writer.toString(); |
737 | } |
738 | catch ( final IOException e ) |
739 | { |
740 | throw new ToolException( e ); |
741 | } |
742 | } |
743 | |
744 | /** |
745 | * Gets the Java source code of the constructors section head content of an implementation. |
746 | * |
747 | * @param implementation The implementation to get the constructors section head content of. |
748 | * |
749 | * @throws ToolException if getting the source code section fails. |
750 | */ |
751 | private String getConstructorsSectionHeadContent( final Implementation implementation ) throws ToolException |
752 | { |
753 | try |
754 | { |
755 | final StringWriter writer = new StringWriter(); |
756 | final VelocityContext ctx = this.getVelocityContext(); |
757 | final Template template = this.getVelocityTemplate( CONSTRUCTORS_HEAD_TEMPLATE ); |
758 | ctx.put( "implementation", implementation ); |
759 | ctx.put( "template", template ); |
760 | template.merge( ctx, writer ); |
761 | writer.close(); |
762 | return writer.toString(); |
763 | } |
764 | catch ( final IOException e ) |
765 | { |
766 | throw new ToolException( e ); |
767 | } |
768 | } |
769 | |
770 | /** |
771 | * Gets the Java source code of the constructors section tail content of an implementation. |
772 | * |
773 | * @param implementation The implementation to get the constructors section tail content of. |
774 | * |
775 | * @throws ToolException if getting the source code section fails. |
776 | */ |
777 | private String getConstructorsSectionTailContent( final Implementation implementation ) throws ToolException |
778 | { |
779 | try |
780 | { |
781 | final StringWriter writer = new StringWriter(); |
782 | final VelocityContext ctx = this.getVelocityContext(); |
783 | final Template template = this.getVelocityTemplate( CONSTRUCTORS_TAIL_TEMPLATE ); |
784 | ctx.put( "implementation", implementation ); |
785 | ctx.put( "template", template ); |
786 | template.merge( ctx, writer ); |
787 | writer.close(); |
788 | return writer.toString(); |
789 | } |
790 | catch ( final IOException e ) |
791 | { |
792 | throw new ToolException( e ); |
793 | } |
794 | } |
795 | |
796 | /** |
797 | * Gets the Java source code of the dependencies section of an implementation. |
798 | * |
799 | * @param implementation The implementation to get the source code of the dependencies section of. |
800 | * |
801 | * @throws ToolException if getting the source code section fails. |
802 | */ |
803 | private String getDependenciesSection( final Implementation implementation ) throws ToolException |
804 | { |
805 | try |
806 | { |
807 | final StringWriter writer = new StringWriter(); |
808 | final VelocityContext ctx = this.getVelocityContext(); |
809 | final Template template = this.getVelocityTemplate( DEPENDENCIES_TEMPLATE ); |
810 | ctx.put( "implementation", implementation ); |
811 | ctx.put( "template", template ); |
812 | template.merge( ctx, writer ); |
813 | writer.close(); |
814 | return writer.toString(); |
815 | } |
816 | catch ( final IOException e ) |
817 | { |
818 | throw new ToolException( e ); |
819 | } |
820 | } |
821 | |
822 | /** |
823 | * Gets the Java source code of the properties section of an implementation. |
824 | * |
825 | * @param implementation The implementation to get the source code of the properties section of. |
826 | * |
827 | * @throws ToolException if getting the source code section fails. |
828 | */ |
829 | private String getPropertiesSection( final Implementation implementation ) throws ToolException |
830 | { |
831 | try |
832 | { |
833 | final StringWriter writer = new StringWriter(); |
834 | final VelocityContext ctx = this.getVelocityContext(); |
835 | final Template template = this.getVelocityTemplate( PROPERTIES_TEMPLATE ); |
836 | ctx.put( "implementation", implementation ); |
837 | ctx.put( "template", template ); |
838 | template.merge( ctx, writer ); |
839 | writer.close(); |
840 | return writer.toString(); |
841 | } |
842 | catch ( final IOException e ) |
843 | { |
844 | throw new ToolException( e ); |
845 | } |
846 | } |
847 | |
848 | /** |
849 | * Gets the Java source code of the messages section of an implementation. |
850 | * |
851 | * @param implementation The implementation to get the source code of the messages section of. |
852 | * |
853 | * @throws ToolException if getting the source code section fails. |
854 | */ |
855 | private String getMessagesSection( final Implementation implementation ) throws ToolException |
856 | { |
857 | try |
858 | { |
859 | final StringWriter writer = new StringWriter(); |
860 | final VelocityContext ctx = this.getVelocityContext(); |
861 | final Template template = this.getVelocityTemplate( MESSAGES_TEMPLATE ); |
862 | ctx.put( "implementation", implementation ); |
863 | ctx.put( "template", template ); |
864 | template.merge( ctx, writer ); |
865 | writer.close(); |
866 | return writer.toString(); |
867 | } |
868 | catch ( final IOException e ) |
869 | { |
870 | throw new ToolException( e ); |
871 | } |
872 | } |
873 | |
874 | private static String getMessage( final String key, final Object... arguments ) |
875 | { |
876 | if ( key == null ) |
877 | { |
878 | throw new NullPointerException( "key" ); |
879 | } |
880 | |
881 | return MessageFormat.format( ResourceBundle.getBundle( JavaSources.class.getName().replace( '.', '/' ) ). |
882 | getString( key ), arguments ); |
883 | |
884 | } |
885 | |
886 | /** |
887 | * Extension to {@code SectionEditor} for editing Java source code. |
888 | * |
889 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
890 | * @version $Id: JavaSources.java 1435 2010-01-30 01:52:40Z schulte2005 $ |
891 | */ |
892 | public abstract class JavaEditor extends SectionEditor |
893 | { |
894 | |
895 | /** Flag indicating that the source code of the editor contains a license section. */ |
896 | private boolean licenseSectionPresent; |
897 | |
898 | /** Flag indicating that the source code of the editor contains an annotations section. */ |
899 | private boolean annotationsSectionPresent; |
900 | |
901 | /** Flag indicating that the source code of the editor contains a documentation section. */ |
902 | private boolean documentationSectionPresent; |
903 | |
904 | /** Creates a new {@code JavaEditor} instance. */ |
905 | public JavaEditor() |
906 | { |
907 | super(); |
908 | } |
909 | |
910 | /** |
911 | * Creates a new {@code JavaEditor} instance taking a {@code LineEditor} to chain. |
912 | * |
913 | * @param lineEditor The editor to chain. |
914 | */ |
915 | public JavaEditor( final LineEditor lineEditor ) |
916 | { |
917 | super( lineEditor ); |
918 | } |
919 | |
920 | @Override |
921 | public String getOutput( final Section section ) throws IOException |
922 | { |
923 | if ( section == null ) |
924 | { |
925 | throw new NullPointerException( "section" ); |
926 | } |
927 | |
928 | this.licenseSectionPresent = false; |
929 | this.annotationsSectionPresent = false; |
930 | this.documentationSectionPresent = false; |
931 | return super.getOutput( section ); |
932 | } |
933 | |
934 | @Override |
935 | public void editSection( final Section section ) throws IOException |
936 | { |
937 | if ( section == null ) |
938 | { |
939 | throw new NullPointerException( "section" ); |
940 | } |
941 | |
942 | if ( section.getName() != null ) |
943 | { |
944 | if ( LICENSE_SECTION_NAME.equals( section.getName() ) ) |
945 | { |
946 | this.editLicenseSection( section ); |
947 | this.licenseSectionPresent = true; |
948 | } |
949 | if ( ANNOTATIONS_SECTION_NAME.equals( section.getName() ) ) |
950 | { |
951 | this.editAnnotationsSection( section ); |
952 | this.annotationsSectionPresent = true; |
953 | } |
954 | if ( DOCUMENTATION_SECTION_NAME.equals( section.getName() ) ) |
955 | { |
956 | this.editDocumentationSection( section ); |
957 | this.documentationSectionPresent = true; |
958 | } |
959 | } |
960 | } |
961 | |
962 | /** |
963 | * Edits the license section of the source code of the editor. |
964 | * |
965 | * @param s The section to edit. |
966 | * |
967 | * @throws NullPointerException if {@code s} is {@code null}. |
968 | * @throws IOException if editing {@code s} fails. |
969 | */ |
970 | public abstract void editLicenseSection( final Section s ) throws IOException; |
971 | |
972 | /** |
973 | * Edits the annotations section of the source code of the editor. |
974 | * |
975 | * @param s The section to edit. |
976 | * |
977 | * @throws NullPointerException if {@code s} is {@code null}. |
978 | * @throws IOException if editing {@code s} fails. |
979 | */ |
980 | public abstract void editAnnotationsSection( final Section s ) throws IOException; |
981 | |
982 | /** |
983 | * Edits the documentation section of the source code of the editor. |
984 | * |
985 | * @param s The section to edit. |
986 | * |
987 | * @throws NullPointerException if {@code s} is {@code null}. |
988 | * @throws IOException if editing {@code s} fails. |
989 | */ |
990 | public abstract void editDocumentationSection( final Section s ) throws IOException; |
991 | |
992 | /** |
993 | * Gets a flag indicating that the source code of the editor contains a license section. |
994 | * |
995 | * @return {@code true} if the source code of the editor contains a license section; {@code false} if the |
996 | * source code of the editor does not contain a license section. |
997 | */ |
998 | public boolean isLicenseSectionPresent() |
999 | { |
1000 | return this.licenseSectionPresent; |
1001 | } |
1002 | |
1003 | /** |
1004 | * Gets a flag indicating that the source code of the editor contains an annotations section. |
1005 | * |
1006 | * @return {@code true} if the source code of the editor contains an annotations section; {@code false} if the |
1007 | * source code of the editor does not contain an annotations section. |
1008 | */ |
1009 | public boolean isAnnotationsSectionPresent() |
1010 | { |
1011 | return this.annotationsSectionPresent; |
1012 | } |
1013 | |
1014 | /** |
1015 | * Gets a flag indicating that the source code of the editor contains a documentation section. |
1016 | * |
1017 | * @return {@code true} if the source code of the editor contains a documentation section; {@code false} if the |
1018 | * source code of the editor does not contain a documentation section. |
1019 | */ |
1020 | public boolean isDocumentationSectionPresent() |
1021 | { |
1022 | return this.documentationSectionPresent; |
1023 | } |
1024 | |
1025 | } |
1026 | |
1027 | /** |
1028 | * Extension to {@code JavaEditor} for editing specification source code. |
1029 | * |
1030 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
1031 | * @version $Id: JavaSources.java 1435 2010-01-30 01:52:40Z schulte2005 $ |
1032 | */ |
1033 | public class JavaSpecificationEditor extends JavaEditor |
1034 | { |
1035 | |
1036 | /** The specification to edit. */ |
1037 | private Specification specification; |
1038 | |
1039 | /** |
1040 | * Creates a new {@code JavaSpecificationEditor} instance for editing the source code of a given specification. |
1041 | * |
1042 | * @param specification The specification to edit. |
1043 | */ |
1044 | public JavaSpecificationEditor( final Specification specification ) |
1045 | { |
1046 | super(); |
1047 | this.specification = specification; |
1048 | } |
1049 | |
1050 | /** |
1051 | * Creates a new {@code JavaSpecificationEditor} instance for editing the source code of a given specification |
1052 | * taking a {@code LineEditor} to chain. |
1053 | * |
1054 | * @param lineEditor The editor to chain. |
1055 | * @param specification The specification to edit. |
1056 | */ |
1057 | public JavaSpecificationEditor( final LineEditor lineEditor, final Specification specification ) |
1058 | { |
1059 | super( lineEditor ); |
1060 | this.specification = specification; |
1061 | } |
1062 | |
1063 | /** |
1064 | * Edits the license section of the source code of the editor. |
1065 | * |
1066 | * @param s The section to edit. |
1067 | * |
1068 | * @throws NullPointerException if {@code s} is {@code null}. |
1069 | * @throws IOException if editing {@code s} fails. |
1070 | */ |
1071 | public void editLicenseSection( final Section s ) throws IOException |
1072 | { |
1073 | if ( s == null ) |
1074 | { |
1075 | throw new NullPointerException( "s" ); |
1076 | } |
1077 | |
1078 | try |
1079 | { |
1080 | s.getHeadContent().setLength( 0 ); |
1081 | if ( this.specification != null ) |
1082 | { |
1083 | s.getHeadContent().append( getLicenseSection( this.specification ) ); |
1084 | } |
1085 | } |
1086 | catch ( final ToolException e ) |
1087 | { |
1088 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1089 | } |
1090 | } |
1091 | |
1092 | /** |
1093 | * Edits the annotations section of the source code of the editor. |
1094 | * |
1095 | * @param s The section to edit. |
1096 | * |
1097 | * @throws NullPointerException if {@code s} is {@code null}. |
1098 | * @throws IOException if editing {@code s} fails. |
1099 | */ |
1100 | public void editAnnotationsSection( final Section s ) throws IOException |
1101 | { |
1102 | if ( s == null ) |
1103 | { |
1104 | throw new NullPointerException( "s" ); |
1105 | } |
1106 | |
1107 | try |
1108 | { |
1109 | s.getHeadContent().setLength( 0 ); |
1110 | if ( this.specification != null ) |
1111 | { |
1112 | s.getHeadContent().append( getAnnotationsSection( this.specification ) ); |
1113 | } |
1114 | } |
1115 | catch ( final ToolException e ) |
1116 | { |
1117 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1118 | } |
1119 | } |
1120 | |
1121 | /** |
1122 | * Edits the documentation section of the source code of the editor. |
1123 | * |
1124 | * @param s The section to edit. |
1125 | * |
1126 | * @throws NullPointerException if {@code s} is {@code null}. |
1127 | * @throws IOException if editing {@code s} fails. |
1128 | */ |
1129 | public void editDocumentationSection( final Section s ) throws IOException |
1130 | { |
1131 | if ( s == null ) |
1132 | { |
1133 | throw new NullPointerException( "s" ); |
1134 | } |
1135 | |
1136 | try |
1137 | { |
1138 | s.getHeadContent().setLength( 0 ); |
1139 | if ( this.specification != null ) |
1140 | { |
1141 | s.getHeadContent().append( getDocumentationSection( this.specification ) ); |
1142 | } |
1143 | } |
1144 | catch ( final ToolException e ) |
1145 | { |
1146 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1147 | } |
1148 | } |
1149 | |
1150 | } |
1151 | |
1152 | /** |
1153 | * Extension to {@code JavaEditor} for editing implementation source code. |
1154 | * |
1155 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
1156 | * @version $Id: JavaSources.java 1435 2010-01-30 01:52:40Z schulte2005 $ |
1157 | */ |
1158 | public class JavaImplementationEditor extends JavaEditor |
1159 | { |
1160 | |
1161 | /** The implementation to edit. */ |
1162 | private Implementation implementation; |
1163 | |
1164 | /** Flag indicating that the source code of the editor contains a constructors section. */ |
1165 | private boolean constructorsSectionPresent; |
1166 | |
1167 | /** Flag indicating that the source code of the editor contains a default constructor section. */ |
1168 | private boolean defaultConstructorSectionPresent; |
1169 | |
1170 | /** Flag indicating that the source code of the editor contains a messages section. */ |
1171 | private boolean messagesSectionPresent; |
1172 | |
1173 | /** Flag indicating that the source code of the editor contains a dependencies section. */ |
1174 | private boolean dependenciesSectionPresent; |
1175 | |
1176 | /** Flag indicating that the source code of the editor contains a properties section. */ |
1177 | private boolean propertiesSectionPresent; |
1178 | |
1179 | /** |
1180 | * Creates a new {@code JavaImplementationEditor} instance for editing the source code of a given implementation. |
1181 | * |
1182 | * @param implementation The implementation to edit. |
1183 | */ |
1184 | public JavaImplementationEditor( final Implementation implementation ) |
1185 | { |
1186 | super(); |
1187 | this.implementation = implementation; |
1188 | } |
1189 | |
1190 | /** |
1191 | * Creates a new {@code JavaImplementationEditor} instance for editing the source code of a given implementation |
1192 | * taking a {@code LineEditor} to chain. |
1193 | * |
1194 | * @param lineEditor The editor to chain. |
1195 | * @param implementation The implementation to edit. |
1196 | */ |
1197 | public JavaImplementationEditor( final LineEditor lineEditor, final Implementation implementation ) |
1198 | { |
1199 | super( lineEditor ); |
1200 | this.implementation = implementation; |
1201 | } |
1202 | |
1203 | @Override |
1204 | public String getOutput( final Section section ) throws IOException |
1205 | { |
1206 | if ( section == null ) |
1207 | { |
1208 | throw new NullPointerException( "section" ); |
1209 | } |
1210 | |
1211 | this.constructorsSectionPresent = false; |
1212 | this.defaultConstructorSectionPresent = false; |
1213 | this.messagesSectionPresent = false; |
1214 | this.dependenciesSectionPresent = false; |
1215 | this.propertiesSectionPresent = false; |
1216 | return super.getOutput( section ); |
1217 | } |
1218 | |
1219 | @Override |
1220 | public void editSection( final Section section ) throws IOException |
1221 | { |
1222 | if ( section == null ) |
1223 | { |
1224 | throw new NullPointerException( "section" ); |
1225 | } |
1226 | |
1227 | super.editSection( section ); |
1228 | |
1229 | if ( section.getName() != null ) |
1230 | { |
1231 | if ( CONSTRUCTORS_SECTION_NAME.equals( section.getName() ) ) |
1232 | { |
1233 | this.editConstructorsSection( section ); |
1234 | this.constructorsSectionPresent = true; |
1235 | } |
1236 | else if ( DEFAULT_CONSTRUCTOR_SECTION_NAME.equals( section.getName() ) ) |
1237 | { |
1238 | this.editDefaultConstructorSection( section ); |
1239 | this.defaultConstructorSectionPresent = true; |
1240 | } |
1241 | else if ( DEPENDENCIES_SECTION_NAME.equals( section.getName() ) ) |
1242 | { |
1243 | this.editDependenciesSection( section ); |
1244 | this.dependenciesSectionPresent = true; |
1245 | } |
1246 | else if ( MESSAGES_SECTION_NAME.equals( section.getName() ) ) |
1247 | { |
1248 | this.editMessagesSection( section ); |
1249 | this.messagesSectionPresent = true; |
1250 | } |
1251 | else if ( PROPERTIES_SECTION_NAME.equals( section.getName() ) ) |
1252 | { |
1253 | this.editPropertiesSection( section ); |
1254 | this.propertiesSectionPresent = true; |
1255 | } |
1256 | } |
1257 | } |
1258 | |
1259 | /** |
1260 | * Edits the license section of the source code of the editor. |
1261 | * |
1262 | * @param s The section to edit. |
1263 | * |
1264 | * @throws IOException if editing {@code s} fails. |
1265 | */ |
1266 | public void editLicenseSection( final Section s ) throws IOException |
1267 | { |
1268 | if ( s == null ) |
1269 | { |
1270 | throw new NullPointerException( "s" ); |
1271 | } |
1272 | |
1273 | try |
1274 | { |
1275 | s.getHeadContent().setLength( 0 ); |
1276 | if ( this.implementation != null ) |
1277 | { |
1278 | s.getHeadContent().append( getLicenseSection( this.implementation ) ); |
1279 | } |
1280 | } |
1281 | catch ( final ToolException e ) |
1282 | { |
1283 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1284 | } |
1285 | } |
1286 | |
1287 | /** |
1288 | * Edits the annotations section of the source code of the editor. |
1289 | * |
1290 | * @param s The section to edit. |
1291 | * |
1292 | * @throws NullPointerException if {@code s} is {@code null}. |
1293 | * @throws IOException if editing {@code s} fails. |
1294 | */ |
1295 | public void editAnnotationsSection( final Section s ) throws IOException |
1296 | { |
1297 | if ( s == null ) |
1298 | { |
1299 | throw new NullPointerException( "s" ); |
1300 | } |
1301 | |
1302 | try |
1303 | { |
1304 | s.getHeadContent().setLength( 0 ); |
1305 | if ( this.implementation != null ) |
1306 | { |
1307 | s.getHeadContent().append( getAnnotationsSection( this.implementation ) ); |
1308 | } |
1309 | } |
1310 | catch ( final ToolException e ) |
1311 | { |
1312 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1313 | } |
1314 | } |
1315 | |
1316 | /** |
1317 | * Edits the documentation section of the source code of the editor. |
1318 | * |
1319 | * @param s The section to edit. |
1320 | * |
1321 | * @throws NullPointerException if {@code s} is {@code null}. |
1322 | * @throws IOException if editing {@code s} fails. |
1323 | */ |
1324 | public void editDocumentationSection( final Section s ) throws IOException |
1325 | { |
1326 | if ( s == null ) |
1327 | { |
1328 | throw new NullPointerException( "s" ); |
1329 | } |
1330 | |
1331 | try |
1332 | { |
1333 | s.getHeadContent().setLength( 0 ); |
1334 | if ( this.implementation != null ) |
1335 | { |
1336 | s.getHeadContent().append( getDocumentationSection( this.implementation ) ); |
1337 | } |
1338 | } |
1339 | catch ( final ToolException e ) |
1340 | { |
1341 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1342 | } |
1343 | } |
1344 | |
1345 | /** |
1346 | * Edits the constructors section of the source code of the editor. |
1347 | * |
1348 | * @param s The section to edit. |
1349 | * |
1350 | * @throws NullPointerException if {@code s} is {@code null}. |
1351 | * @throws IOException if editing {@code s} fails. |
1352 | */ |
1353 | public void editConstructorsSection( final Section s ) throws IOException |
1354 | { |
1355 | if ( s == null ) |
1356 | { |
1357 | throw new NullPointerException( "s" ); |
1358 | } |
1359 | |
1360 | try |
1361 | { |
1362 | s.getHeadContent().setLength( 0 ); |
1363 | s.getTailContent().setLength( 0 ); |
1364 | |
1365 | if ( this.implementation != null ) |
1366 | { |
1367 | s.getHeadContent().append( getConstructorsSectionHeadContent( this.implementation ) ); |
1368 | s.getTailContent().append( getConstructorsSectionTailContent( this.implementation ) ); |
1369 | } |
1370 | |
1371 | for ( Section child : s.getSections() ) |
1372 | { |
1373 | if ( child.getName() != null && DEFAULT_CONSTRUCTOR_SECTION_NAME.equals( child.getName() ) ) |
1374 | { |
1375 | this.defaultConstructorSectionPresent = true; |
1376 | break; |
1377 | } |
1378 | } |
1379 | |
1380 | if ( !this.defaultConstructorSectionPresent ) |
1381 | { |
1382 | final Section defaultCtor = new Section(); |
1383 | defaultCtor.setName( DEFAULT_CONSTRUCTOR_SECTION_NAME ); |
1384 | defaultCtor.setStartingLine( " // SECTION-START[" + DEFAULT_CONSTRUCTOR_SECTION_NAME + "]" ); |
1385 | defaultCtor.setEndingLine( " // SECTION-END" ); |
1386 | defaultCtor.getHeadContent().append( " super();" ).append( this.getLineSeparator() ); |
1387 | s.getSections().add( defaultCtor ); |
1388 | this.defaultConstructorSectionPresent = true; |
1389 | } |
1390 | } |
1391 | catch ( final ToolException e ) |
1392 | { |
1393 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1394 | } |
1395 | } |
1396 | |
1397 | /** |
1398 | * Edits the default constructor section of the source code of the editor. |
1399 | * |
1400 | * @param s The section to edit. |
1401 | * |
1402 | * @throws NullPointerException if {@code s} is {@code null}. |
1403 | * @throws IOException if editing {@code s} fails. |
1404 | */ |
1405 | public void editDefaultConstructorSection( final Section s ) throws IOException |
1406 | { |
1407 | if ( s == null ) |
1408 | { |
1409 | throw new NullPointerException( "s" ); |
1410 | } |
1411 | |
1412 | if ( s.getHeadContent().toString().trim().length() == 0 ) |
1413 | { |
1414 | s.getHeadContent().setLength( 0 ); |
1415 | |
1416 | if ( this.implementation != null ) |
1417 | { |
1418 | s.getHeadContent().append( " super();" ).append( this.getLineSeparator() ); |
1419 | } |
1420 | } |
1421 | } |
1422 | |
1423 | /** |
1424 | * Edits the dependencies section of the source code of the editor. |
1425 | * |
1426 | * @param s The section to edit. |
1427 | * |
1428 | * @throws NullPointerException if {@code s} is {@code null}. |
1429 | * @throws IOException if editing {@code s} fails. |
1430 | */ |
1431 | public void editDependenciesSection( final Section s ) throws IOException |
1432 | { |
1433 | if ( s == null ) |
1434 | { |
1435 | throw new NullPointerException( "s" ); |
1436 | } |
1437 | |
1438 | try |
1439 | { |
1440 | s.getHeadContent().setLength( 0 ); |
1441 | if ( this.implementation != null ) |
1442 | { |
1443 | s.getHeadContent().append( getDependenciesSection( this.implementation ) ); |
1444 | } |
1445 | } |
1446 | catch ( final ToolException e ) |
1447 | { |
1448 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1449 | } |
1450 | } |
1451 | |
1452 | /** |
1453 | * Edits the messages section of the source code of the editor. |
1454 | * |
1455 | * @param s The section to edit. |
1456 | * |
1457 | * @throws NullPointerException if {@code s} is {@code null}. |
1458 | * @throws IOException if editing {@code s} fails. |
1459 | */ |
1460 | public void editMessagesSection( final Section s ) throws IOException |
1461 | { |
1462 | if ( s == null ) |
1463 | { |
1464 | throw new NullPointerException( "s" ); |
1465 | } |
1466 | |
1467 | try |
1468 | { |
1469 | s.getHeadContent().setLength( 0 ); |
1470 | if ( this.implementation != null ) |
1471 | { |
1472 | s.getHeadContent().append( getMessagesSection( this.implementation ) ); |
1473 | } |
1474 | } |
1475 | catch ( final ToolException e ) |
1476 | { |
1477 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1478 | } |
1479 | } |
1480 | |
1481 | /** |
1482 | * Edits the properties section of the source code of the editor. |
1483 | * |
1484 | * @param s The section to edit. |
1485 | * |
1486 | * @throws NullPointerException if {@code s} is {@code null}. |
1487 | * @throws IOException if editing {@code s} fails. |
1488 | */ |
1489 | public void editPropertiesSection( final Section s ) throws IOException |
1490 | { |
1491 | if ( s == null ) |
1492 | { |
1493 | throw new NullPointerException( "s" ); |
1494 | } |
1495 | |
1496 | try |
1497 | { |
1498 | s.getHeadContent().setLength( 0 ); |
1499 | if ( this.implementation != null ) |
1500 | { |
1501 | s.getHeadContent().append( getPropertiesSection( this.implementation ) ); |
1502 | } |
1503 | } |
1504 | catch ( final ToolException e ) |
1505 | { |
1506 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
1507 | } |
1508 | } |
1509 | |
1510 | /** |
1511 | * Gets a flag indicating that the source code of the editor contains a constructors section. |
1512 | * |
1513 | * @return {@code true} if the source code of the editor contains a constructors section; {@code false} if the |
1514 | * source code of the editor does not contain a constructors section. |
1515 | */ |
1516 | public boolean isConstructorsSectionPresent() |
1517 | { |
1518 | return this.constructorsSectionPresent; |
1519 | } |
1520 | |
1521 | /** |
1522 | * Gets a flag indicating that the source code of the editor contains a default constructor section. |
1523 | * |
1524 | * @return {@code true} if the source code of the editor contains a default constructor section; {@code false} |
1525 | * if the source code of the editor does not contain a default constructor section. |
1526 | */ |
1527 | public boolean isDefaultConstructorSectionPresent() |
1528 | { |
1529 | return this.defaultConstructorSectionPresent; |
1530 | } |
1531 | |
1532 | /** |
1533 | * Gets a flag indicating that the source code of the editor contains a messages section. |
1534 | * |
1535 | * @return {@code true} if the source code of the editor contains a messages section; {@code false} |
1536 | * if the source code of the editor does not contain a messages section. |
1537 | */ |
1538 | public boolean isMessagesSectionPresent() |
1539 | { |
1540 | return this.messagesSectionPresent; |
1541 | } |
1542 | |
1543 | /** |
1544 | * Gets a flag indicating that the source code of the editor contains a dependencies section. |
1545 | * |
1546 | * @return {@code true} if the source code of the editor contains a dependencies section; {@code false} |
1547 | * if the source code of the editor does not contain a dependencies section. |
1548 | */ |
1549 | public boolean isDependenciesSectionPresent() |
1550 | { |
1551 | return this.dependenciesSectionPresent; |
1552 | } |
1553 | |
1554 | /** |
1555 | * Gets a flag indicating that the source code of the editor contains a properties section. |
1556 | * |
1557 | * @return {@code true} if the source code of the editor contains a properties section; {@code false} |
1558 | * if the source code of the editor does not contain a properties section. |
1559 | */ |
1560 | public boolean isPropertiesSectionPresent() |
1561 | { |
1562 | return this.propertiesSectionPresent; |
1563 | } |
1564 | |
1565 | } |
1566 | |
1567 | } |