1 package org.apache.maven.reporting;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.doxia.sink.Sink;
23 import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
24 import org.apache.maven.doxia.util.HtmlTools;
25
26 import org.apache.maven.shared.utils.StringUtils;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Properties;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public abstract class AbstractMavenReportRenderer
51 implements MavenReportRenderer
52 {
53
54 protected Sink sink;
55
56
57 private int section;
58
59
60
61
62
63
64 public AbstractMavenReportRenderer( Sink sink )
65 {
66 this.sink = sink;
67 }
68
69
70 @Override
71 public void render()
72 {
73 sink.head();
74
75 sink.title();
76 text( getTitle() );
77 sink.title_();
78
79 sink.head_();
80
81 sink.body();
82 renderBody();
83 sink.body_();
84
85 sink.flush();
86
87 sink.close();
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 protected void startSection( String name )
117 {
118 startSection( name, name );
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 protected void startSection( String name, String anchor )
144 {
145 section = section + 1;
146
147 switch ( section )
148 {
149 case 1:
150 sink.section1();
151 sink.sectionTitle1();
152 break;
153 case 2:
154 sink.section2();
155 sink.sectionTitle2();
156 break;
157 case 3:
158 sink.section3();
159 sink.sectionTitle3();
160 break;
161 case 4:
162 sink.section4();
163 sink.sectionTitle4();
164 break;
165 case 5:
166 sink.section5();
167 sink.sectionTitle5();
168 break;
169
170 default:
171
172 break;
173 }
174
175 text( name );
176
177 switch ( section )
178 {
179 case 1:
180 sink.sectionTitle1_();
181 break;
182 case 2:
183 sink.sectionTitle2_();
184 break;
185 case 3:
186 sink.sectionTitle3_();
187 break;
188 case 4:
189 sink.sectionTitle4_();
190 break;
191 case 5:
192 sink.sectionTitle5_();
193 break;
194
195 default:
196
197 break;
198 }
199
200 sink.anchor( HtmlTools.encodeId( anchor ) );
201 sink.anchor_();
202 }
203
204
205
206
207
208
209
210
211
212
213
214 protected void endSection()
215 {
216 switch ( section )
217 {
218 case 1:
219 sink.section1_();
220 break;
221 case 2:
222 sink.section2_();
223 break;
224 case 3:
225 sink.section3_();
226 break;
227 case 4:
228 sink.section4_();
229 break;
230 case 5:
231 sink.section5_();
232 break;
233
234 default:
235
236 break;
237 }
238
239 section = section - 1;
240
241 if ( section < 0 )
242 {
243 throw new IllegalStateException( "Too many closing sections" );
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256 protected void startTable()
257 {
258 startTable( new int[] {Sink.JUSTIFY_LEFT}, false );
259 }
260
261
262
263
264
265
266
267
268
269
270
271 protected void startTable( int[] justification, boolean grid )
272 {
273 sink.table();
274 sink.tableRows( justification, grid );
275 }
276
277
278
279
280
281
282 protected void endTable()
283 {
284 sink.tableRows_();
285 sink.table_();
286 }
287
288
289
290
291
292
293
294
295
296 protected void tableHeaderCell( String text )
297 {
298 sink.tableHeaderCell();
299
300 text( text );
301
302 sink.tableHeaderCell_();
303 }
304
305
306
307
308
309
310
311
312
313 protected void tableCell( String text )
314 {
315 tableCell( text, false );
316 }
317
318
319
320
321
322
323
324
325
326
327
328
329
330 protected void tableCell( String text, boolean asHtml )
331 {
332 sink.tableCell();
333
334 if ( asHtml )
335 {
336 sink.rawText( text );
337 }
338 else
339 {
340 linkPatternedText( text );
341 }
342
343 sink.tableCell_();
344 }
345
346
347
348
349
350
351
352
353
354
355 protected void tableRow( String[] content )
356 {
357 sink.tableRow();
358
359 if ( content != null )
360 {
361 for ( int i = 0; i < content.length; i++ )
362 {
363 tableCell( content[i] );
364 }
365 }
366
367 sink.tableRow_();
368 }
369
370
371
372
373
374
375
376
377
378 protected void tableHeader( String[] content )
379 {
380 sink.tableRow();
381
382 if ( content != null )
383 {
384 for ( int i = 0; i < content.length; i++ )
385 {
386 tableHeaderCell( content[i] );
387 }
388 }
389
390 sink.tableRow_();
391 }
392
393
394
395
396
397
398
399
400
401 protected void tableCaption( String caption )
402 {
403 sink.tableCaption();
404
405 text( caption );
406
407 sink.tableCaption_();
408 }
409
410
411
412
413
414
415
416
417
418
419
420
421
422 protected void paragraph( String paragraph )
423 {
424 sink.paragraph();
425
426 text( paragraph );
427
428 sink.paragraph_();
429 }
430
431
432
433
434
435
436
437
438
439
440 protected void link( String href, String name )
441 {
442 sink.link( href );
443
444 text( name );
445
446 sink.link_();
447 }
448
449
450
451
452
453
454
455
456 protected void text( String text )
457 {
458 if ( StringUtils.isEmpty( text ) )
459 {
460 sink.text( "-" );
461 }
462 else
463 {
464 sink.text( text );
465 }
466 }
467
468
469
470
471
472
473
474
475
476 protected void verbatimText( String text )
477 {
478 sink.verbatim( SinkEventAttributeSet.BOXED );
479
480 text( text );
481
482 sink.verbatim_();
483 }
484
485
486
487
488
489
490
491
492
493
494
495 protected void verbatimLink( String text, String href )
496 {
497 if ( StringUtils.isEmpty( href ) )
498 {
499 verbatimText( text );
500 }
501 else
502 {
503 sink.verbatim( SinkEventAttributeSet.BOXED );
504
505 link( href, text );
506
507 sink.verbatim_();
508 }
509 }
510
511
512
513
514
515
516
517 protected void javaScript( String jsCode )
518 {
519 sink.rawText( "<script type=\"text/javascript\">\n" + jsCode + "</script>" );
520 }
521
522
523
524
525
526
527
528
529
530
531
532 public void linkPatternedText( String text )
533 {
534 if ( StringUtils.isEmpty( text ) )
535 {
536 text( text );
537 }
538 else
539 {
540 List<String> segments = applyPattern( text );
541
542 if ( segments == null )
543 {
544 text( text );
545 }
546 else
547 {
548 for ( Iterator<String> it = segments.iterator(); it.hasNext(); )
549 {
550 String name = it.next();
551 String href = it.next();
552
553 if ( href == null )
554 {
555 text( name );
556 }
557 else
558 {
559 link( href, name );
560 }
561 }
562 }
563 }
564 }
565
566
567
568
569
570
571
572
573
574
575
576 protected static String createLinkPatternedText( String text, String href )
577 {
578 if ( text == null )
579 {
580 return text;
581 }
582
583 if ( href == null )
584 {
585 return text;
586 }
587
588 return '{' + text + ", " + href + '}';
589 }
590
591
592
593
594
595
596
597 protected static String propertiesToString( Properties props )
598 {
599 if ( props == null || props.isEmpty() )
600 {
601 return "";
602 }
603
604 StringBuilder sb = new StringBuilder();
605
606 for ( Map.Entry<?, ?> entry : props.entrySet() )
607 {
608 if ( sb.length() > 0 )
609 {
610 sb.append( ", " );
611 }
612
613 sb.append( entry.getKey() ).append( "=" ).append( entry.getValue() );
614 }
615
616 return sb.toString();
617 }
618
619
620
621
622
623
624
625
626
627
628
629
630 private static List<String> applyPattern( String text )
631 {
632 if ( StringUtils.isEmpty( text ) )
633 {
634 return null;
635 }
636
637
638
639 List<String> segments = new ArrayList<>();
640
641
642 if ( text.indexOf( "${" ) != -1 )
643 {
644 int lastComma = text.lastIndexOf( "," );
645 int lastSemi = text.lastIndexOf( "}" );
646 if ( lastComma != -1 && lastSemi != -1 && lastComma < lastSemi )
647 {
648 segments.add( text.substring( lastComma + 1, lastSemi ).trim() );
649 segments.add( null );
650 }
651 else
652 {
653 segments.add( text );
654 segments.add( null );
655 }
656
657 return segments;
658 }
659
660 boolean inQuote = false;
661 int braceStack = 0;
662 int lastOffset = 0;
663
664 for ( int i = 0; i < text.length(); i++ )
665 {
666 char ch = text.charAt( i );
667
668 if ( ch == '\'' && !inQuote && braceStack == 0 )
669 {
670
671 if ( i + 1 < text.length() && text.charAt( i + 1 ) == '\'' )
672 {
673 i++;
674 segments.add( text.substring( lastOffset, i ) );
675 segments.add( null );
676 lastOffset = i + 1;
677 }
678 else
679 {
680 inQuote = true;
681 }
682 }
683 else
684 {
685 switch ( ch )
686 {
687 case '{':
688 if ( !inQuote )
689 {
690 if ( braceStack == 0 )
691 {
692 if ( i != lastOffset )
693 {
694 segments.add( text.substring( lastOffset, i ) );
695 segments.add( null );
696 }
697 lastOffset = i + 1;
698 }
699 braceStack++;
700 }
701 break;
702 case '}':
703 if ( !inQuote )
704 {
705 braceStack--;
706 if ( braceStack == 0 )
707 {
708 String subString = text.substring( lastOffset, i );
709 lastOffset = i + 1;
710
711 int lastComma = subString.lastIndexOf( "," );
712 if ( lastComma != -1 )
713 {
714 segments.add( subString.substring( 0, lastComma ).trim() );
715 segments.add( subString.substring( lastComma + 1 ).trim() );
716 }
717 else
718 {
719 segments.add( subString );
720 segments.add( null );
721 }
722 }
723 }
724 break;
725 case '\'':
726 inQuote = false;
727 break;
728 default:
729 break;
730 }
731 }
732 }
733
734 if ( !StringUtils.isEmpty( text.substring( lastOffset ) ) )
735 {
736 segments.add( text.substring( lastOffset ) );
737 segments.add( null );
738 }
739
740 if ( braceStack != 0 )
741 {
742 throw new IllegalArgumentException( "Unmatched braces in the pattern." );
743 }
744
745 if ( inQuote )
746 {
747
748
749 }
750
751 return Collections.unmodifiableList( segments );
752 }
753
754
755
756
757
758 @Override
759 public abstract String getTitle();
760
761
762
763
764 protected abstract void renderBody();
765 }