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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 117 118 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
|
package org.apache.maven.doxia.module.fo;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.IOException;
import java.io.Writer;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Stack;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML.Tag;
import org.apache.maven.doxia.document.DocumentCover;
import org.apache.maven.doxia.document.DocumentMeta;
import org.apache.maven.doxia.document.DocumentModel;
import org.apache.maven.doxia.document.DocumentTOC;
import org.apache.maven.doxia.document.DocumentTOCItem;
import org.apache.maven.doxia.sink.SinkEventAttributeSet;
import org.apache.maven.doxia.sink.SinkEventAttributes;
import org.apache.maven.doxia.util.DoxiaUtils;
import org.apache.maven.doxia.util.HtmlTools;
import org.codehaus.plexus.util.StringUtils;
/**
* A Doxia Sink that produces an aggregated FO model. The usage is similar to the following:
*
* <pre>
* FoAggregateSink sink = new FoAggregateSink( writer );
* sink.setDocumentModel( documentModel );
* sink.beginDocument();
* sink.coverPage();
* sink.toc();
* ...
* sink.endDocument();
* </pre>
*
* <b>Note</b>: the documentModel object contains several
* <a href="http://maven.apache.org/doxia/doxia/doxia-core/document.html">document metadata</a>, but only a few
* of them are used in this sink (i.e. author, confidential, date and title), the others are ignored.
*
* @author ltheussl
* @version $Id: FoAggregateSink.java 811802 2009-09-06 10:49:37Z vsiveton $
* @since 1.1
*/
public class FoAggregateSink
extends FoSink
{
/**
* No Table Of Content.
* @see #setDocumentModel(DocumentModel, int)
*/
public static int TOC_NONE = 0;
/**
* Table Of Content at the start of the document.
* @see #setDocumentModel(DocumentModel, int)
*/
public static int TOC_START = 1;
/**
* Table Of Content at the end of the document.
* @see #setDocumentModel(DocumentModel, int)
*/
public static int TOC_END = 2;
// TODO: make configurable
private static final String COVER_HEADER_HEIGHT = "1.5in";
/** The document model to be used by this sink. */
private DocumentModel docModel;
/** Counts the current chapter level. */
private int chapter = 0;
/** Name of the source file of the current document, relative to the source root. */
private String docName;
/** Title of the chapter, used in the page header. */
private String docTitle = "";
/** Content in head is ignored in aggregated documents. */
private boolean ignoreText;
/** Current position of the TOC, see {@link #TOC_POSITION} */
private int tocPosition;
/** Used to get the current position in the TOC. */
private final Stack tocStack = new Stack();
/**
* Constructor.
*
* @param writer The writer for writing the result.
*/
public FoAggregateSink( Writer writer )
{
super( writer );
}
/** {@inheritDoc} */
public void head()
{
head( null );
}
/** {@inheritDoc} */
public void head( SinkEventAttributes attributes )
{
init();
ignoreText = true;
}
/** {@inheritDoc} */
public void head_()
{
ignoreText = false;
writeEOL();
}
/** {@inheritDoc} */
public void title()
{
title( null );
}
/** {@inheritDoc} */
public void title( SinkEventAttributes attributes )
{
// ignored
}
/** {@inheritDoc} */
public void title_()
{
// ignored
}
/** {@inheritDoc} */
public void author()
{
author( null );
}
/** {@inheritDoc} */
public void author( SinkEventAttributes attributes )
{
// ignored
}
/** {@inheritDoc} */
public void author_()
{
// ignored
}
/** {@inheritDoc} */
public void date()
{
date( null );
}
/** {@inheritDoc} */
public void date( SinkEventAttributes attributes )
{
// ignored
}
/** {@inheritDoc} */
public void date_()
{
// ignored
}
/** {@inheritDoc} */
public void body()
{
body( null );
}
/** {@inheritDoc} */
public void body( SinkEventAttributes attributes )
{
chapter++;
resetSectionCounter();
startPageSequence( getHeaderText(), getFooterText() );
if ( docName == null )
{
getLog().warn( "No document root specified, local links will not be resolved correctly!" );
}
else
{
writeStartTag( BLOCK_TAG, "id", docName );
}
}
/** {@inheritDoc} */
public void body_()
{
writeEOL();
writeEndTag( BLOCK_TAG );
writeEndTag( FLOW_TAG );
writeEndTag( PAGE_SEQUENCE_TAG );
// reset document name
docName = null;
}
/**
* Sets the title of the current document. This is used as a chapter title in the page header.
*
* @param title the title of the current document.
*/
public void setDocumentTitle( String title )
{
this.docTitle = title;
if ( title == null )
{
this.docTitle = "";
}
}
/**
* Sets the name of the current source document, relative to the source root.
* Used to resolve links to other source documents.
*
* @param name the name for the current document.
*/
public void setDocumentName( String name )
{
this.docName = getIdName( name );
}
/**
* Sets the DocumentModel to be used by this sink. The DocumentModel provides all the meta-information
* required to render a document, eg settings for the cover page, table of contents, etc.
* <br/>
* By default, a TOC will be added at the beginning of the document.
*
* @param model the DocumentModel.
* @see #setDocumentModel(DocumentModel, String)
* @see #TOC_START
*/
public void setDocumentModel( DocumentModel model )
{
setDocumentModel( model, TOC_START );
}
/**
* Sets the DocumentModel to be used by this sink. The DocumentModel provides all the meta-information
* required to render a document, eg settings for the cover page, table of contents, etc.
*
* @param model the DocumentModel, could be null.
* @param tocPos should be one of these values: {@link #TOC_NONE}, {@link #TOC_START} and {@link #TOC_END}.
* @since 1.1.2
*/
public void setDocumentModel( DocumentModel model, int tocPos )
{
this.docModel = model;
if ( !( tocPos == TOC_NONE || tocPos == TOC_START || tocPos == TOC_END ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Unrecognized value for tocPosition: " + tocPos + ", using no toc." );
}
tocPos = TOC_NONE;
}
this.tocPosition = tocPos;
if ( this.docModel != null && this.docModel.getToc() != null && this.tocPosition != TOC_NONE )
{
DocumentTOCItem tocItem = new DocumentTOCItem();
tocItem.setName( this.docModel.getToc().getName() );
tocItem.setRef( "./toc" );
List items = new LinkedList();
if ( this.tocPosition == TOC_START )
{
items.add( tocItem );
}
items.addAll( this.docModel.getToc().getItems() );
if ( this.tocPosition == TOC_END )
{
items.add( tocItem );
}
this.docModel.getToc().setItems( items );
}
}
/**
* Translates the given name to a usable id.
* Prepends "./" and strips any extension.
*
* @param name the name for the current document.
* @return String
*/
private String getIdName( String name )
{
if ( StringUtils.isEmpty( name ) )
{
getLog().warn( "Empty document reference, links will not be resolved correctly!" );
return "";
}
String idName = name.replace( '\\', '/' );
// prepend "./" and strip extension
if ( !idName.startsWith( "./" ) )
{
idName = "./" + idName;
}
if ( idName.substring( 2 ).lastIndexOf( "." ) != -1 )
{
idName = idName.substring( 0, idName.lastIndexOf( "." ) );
}
while ( idName.indexOf( "//" ) != -1 )
{
idName = StringUtils.replace( idName, "//", "/" );
}
return idName;
}
// -----------------------------------------------------------------------
//
// -----------------------------------------------------------------------
/** {@inheritDoc} */
public void figureGraphics( String name )
{
figureGraphics( name, null );
}
/** {@inheritDoc} */
public void figureGraphics( String src, SinkEventAttributes attributes )
{
String anchor = src;
while ( anchor.startsWith( "./" ) )
{
anchor = anchor.substring( 2 );
}
if ( anchor.startsWith( "../" ) && docName != null )
{
anchor = resolveLinkRelativeToBase( anchor );
}
super.figureGraphics( anchor, attributes );
}
/** {@inheritDoc} */
public void anchor( String name )
{
anchor( name, null );
}
/** {@inheritDoc} */
public void anchor( String name, SinkEventAttributes attributes )
{
if ( name == null )
{
throw new NullPointerException( "Anchor name cannot be null!" );
}
String anchor = name;
if ( !DoxiaUtils.isValidId( anchor ) )
{
anchor = DoxiaUtils.encodeId( name, true );
String msg = "Modified invalid anchor name: '" + name + "' to '" + anchor + "'";
logMessage( "modifiedLink", msg );
}
anchor = "#" + anchor;
if ( docName != null )
{
anchor = docName + anchor;
}
writeStartTag( INLINE_TAG, "id", anchor );
}
/** {@inheritDoc} */
public void link( String name )
{
link( name, null );
}
/** {@inheritDoc} */
public void link( String name, SinkEventAttributes attributes )
{
if ( name == null )
{
throw new NullPointerException( "Link name cannot be null!" );
}
if ( DoxiaUtils.isExternalLink( name ) )
{
// external links
writeStartTag( BASIC_LINK_TAG, "external-destination", HtmlTools.escapeHTML( name ) );
writeStartTag( INLINE_TAG, "href.external" );
return;
}
while ( name.indexOf( "//" ) != -1 )
{
name = StringUtils.replace( name, "//", "/" );
}
if ( DoxiaUtils.isInternalLink( name ) )
{
// internal link (ie anchor is in the same source document)
String anchor = name.substring( 1 );
if ( !DoxiaUtils.isValidId( anchor ) )
{
String tmp = anchor;
anchor = DoxiaUtils.encodeId( anchor, true );
String msg = "Modified invalid anchor name: '" + tmp + "' to '" + anchor + "'";
logMessage( "modifiedLink", msg );
}
if ( docName != null )
{
anchor = docName + "#" + anchor;
}
writeStartTag( BASIC_LINK_TAG, "internal-destination", HtmlTools.escapeHTML( anchor ) );
writeStartTag( INLINE_TAG, "href.internal" );
}
else if ( name.startsWith( "../" ) )
{
// local link (ie anchor is not in the same source document)
if ( docName == null )
{
// can't resolve link without base, fop will issue a warning
writeStartTag( BASIC_LINK_TAG, "internal-destination", HtmlTools.escapeHTML( name ) );
writeStartTag( INLINE_TAG, "href.internal" );
return;
}
String anchor = resolveLinkRelativeToBase( chopExtension( name ) );
writeStartTag( BASIC_LINK_TAG, "internal-destination", HtmlTools.escapeHTML( anchor ) );
writeStartTag( INLINE_TAG, "href.internal" );
}
else
{
// local link (ie anchor is not in the same source document)
String anchor = name;
if ( anchor.startsWith( "./" ) )
{
this.link( anchor.substring( 2 ) );
return;
}
anchor = chopExtension( anchor );
String base = docName.substring( 0, docName.lastIndexOf( "/" ) );
anchor = base + "/" + anchor;
writeStartTag( BASIC_LINK_TAG, "internal-destination", HtmlTools.escapeHTML( anchor ) );
writeStartTag( INLINE_TAG, "href.internal" );
}
}
// only call this if docName != null !!!
private String resolveLinkRelativeToBase( String name )
{
String anchor = name;
String base = docName.substring( 0, docName.lastIndexOf( "/" ) );
if ( base.indexOf( "/" ) != -1 )
{
while ( anchor.startsWith( "../" ) )
{
base = base.substring( 0, base.lastIndexOf( "/" ) );
anchor = anchor.substring( 3 );
if ( base.lastIndexOf( "/" ) == -1 )
{
while ( anchor.startsWith( "../" ) )
{
anchor = anchor.substring( 3 );
}
break;
}
}
}
return base + "/" + anchor;
}
private String chopExtension( String name )
{
String anchor = name;
int dot = anchor.lastIndexOf( "." );
if ( dot != -1 && dot != anchor.length() && anchor.charAt( dot + 1 ) != '/' )
{
int hash = anchor.indexOf( "#", dot );
if ( hash != -1 )
{
int dot2 = anchor.indexOf( ".", hash );
if ( dot2 != -1 )
{
anchor =
anchor.substring( 0, dot ) + "#" + HtmlTools.encodeId( anchor.substring( hash + 1, dot2 ) );
}
else
{
anchor =
anchor.substring( 0, dot ) + "#"
+ HtmlTools.encodeId( anchor.substring( hash + 1, anchor.length() ) );
}
}
else
{
anchor = anchor.substring( 0, dot );
}
}
return anchor;
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*
* Writes a start tag, prepending EOL.
*/
protected void writeStartTag( Tag tag, String attributeId )
{
if ( !ignoreText )
{
super.writeStartTag( tag, attributeId );
}
}
/**
* {@inheritDoc}
*
* Writes a start tag, prepending EOL.
*/
protected void writeStartTag( Tag tag, String id, String name )
{
if ( !ignoreText )
{
super.writeStartTag( tag, id, name );
}
}
/**
* {@inheritDoc}
*
* Writes an end tag, appending EOL.
*/
protected void writeEndTag( Tag t )
{
if ( !ignoreText )
{
super.writeEndTag( t );
}
}
/**
* {@inheritDoc}
*
* Writes a simple tag, appending EOL.
*/
protected void writeEmptyTag( Tag tag, String attributeId )
{
if ( !ignoreText )
{
super.writeEmptyTag( tag, attributeId );
}
}
/**
* {@inheritDoc}
*
* Writes a text, swallowing any exceptions.
*/
protected void write( String text )
{
if ( !ignoreText )
{
super.write( text );
}
}
/**
* {@inheritDoc}
*
* Writes a text, appending EOL.
*/
protected void writeln( String text )
{
if ( !ignoreText )
{
super.writeln( text );
}
}
/**
* {@inheritDoc}
*
* Writes content, escaping special characters.
*/
protected void content( String text )
{
if ( !ignoreText )
{
super.content( text );
}
}
/**
* Writes EOL.
*/
protected void newline()
{
if ( !ignoreText )
{
writeEOL();
}
}
/**
* Starts a page sequence, depending on the current chapter.
*
* @param headerText The text to write in the header, if null, nothing is written.
* @param footerText The text to write in the footer, if null, nothing is written.
*/
protected void startPageSequence( String headerText, String footerText )
{
if ( chapter == 1 )
{
startPageSequence( "0", headerText, footerText );
}
else
{
startPageSequence( "auto", headerText, footerText );
}
}
/**
* Returns the text to write in the header of each page.
*
* @return String
*/
protected String getHeaderText()
{
return Integer.toString( chapter ) + " " + docTitle;
}
/**
* Returns the text to write in the footer of each page.
*
* @return String
*/
protected String getFooterText()
{
int actualYear;
String add = " • " + getBundle( Locale.US ).getString( "footer.rights" );
String companyName = "";
if ( docModel != null && docModel.getMeta() != null && docModel.getMeta().isConfidential() )
{
add = add + " • " + getBundle( Locale.US ).getString( "footer.confidential" );
}
if ( docModel != null && docModel.getCover() != null && docModel.getCover().getCompanyName() != null )
{
companyName = docModel.getCover().getCompanyName();
}
if ( docModel != null && docModel.getMeta() != null && docModel.getMeta().getDate() != null )
{
Calendar date = Calendar.getInstance();
date.setTime( docModel.getMeta().getDate() );
actualYear = date.get( Calendar.YEAR );
}
else
{
actualYear = Calendar.getInstance().get( Calendar.YEAR );
}
return "©" + actualYear + ", " + companyName + add;
}
/**
* {@inheritDoc}
*
* Returns the current chapter number as a string.
*/
protected String getChapterString()
{
return Integer.toString( chapter ) + ".";
}
/**
* {@inheritDoc}
*
* Writes a 'xsl-region-before' block.
*/
protected void regionBefore( String headerText )
{
writeStartTag( STATIC_CONTENT_TAG, "flow-name", "xsl-region-before" );
writeln( "<fo:table table-layout=\"fixed\" width=\"100%\" >" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "5.625in" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "0.625in" );
writeStartTag( TABLE_BODY_TAG, "" );
writeStartTag( TABLE_ROW_TAG, "" );
writeStartTag( TABLE_CELL_TAG, "" );
writeStartTag( BLOCK_TAG, "header.style" );
if ( headerText != null )
{
write( headerText );
}
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeStartTag( TABLE_CELL_TAG, "" );
writeStartTag( BLOCK_TAG, "page.number" );
writeEmptyTag( PAGE_NUMBER_TAG, "" );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
writeEndTag( TABLE_BODY_TAG );
writeEndTag( TABLE_TAG );
writeEndTag( STATIC_CONTENT_TAG );
}
/**
* {@inheritDoc}
*
* Writes a 'xsl-region-after' block.
*/
protected void regionAfter( String footerText )
{
writeStartTag( STATIC_CONTENT_TAG, "flow-name", "xsl-region-after" );
writeStartTag( BLOCK_TAG, "footer.style" );
if ( footerText != null )
{
write( footerText );
}
writeEndTag( BLOCK_TAG );
writeEndTag( STATIC_CONTENT_TAG );
}
/**
* {@inheritDoc}
*
* Writes a chapter heading.
*/
protected void chapterHeading( String headerText, boolean chapterNumber )
{
writeStartTag( BLOCK_TAG, "" );
writeStartTag( LIST_BLOCK_TAG, "" );
writeStartTag( LIST_ITEM_TAG, "" );
writeln( "<fo:list-item-label end-indent=\"6.375in\" start-indent=\"-1in\">" );
writeStartTag( BLOCK_TAG, "outdented.number.style" );
if ( chapterNumber )
{
writeStartTag( BLOCK_TAG, "chapter.title" );
write( Integer.toString( chapter ) );
writeEndTag( BLOCK_TAG );
}
writeEndTag( BLOCK_TAG );
writeEndTag( LIST_ITEM_LABEL_TAG );
writeln( "<fo:list-item-body end-indent=\"1in\" start-indent=\"0in\">" );
writeStartTag( BLOCK_TAG, "chapter.title" );
if ( headerText == null )
{
write( docTitle );
}
else
{
write( headerText );
}
writeEndTag( BLOCK_TAG );
writeEndTag( LIST_ITEM_BODY_TAG );
writeEndTag( LIST_ITEM_TAG );
writeEndTag( LIST_BLOCK_TAG );
writeEndTag( BLOCK_TAG );
writeStartTag( BLOCK_TAG, "space-after.optimum", "0em" );
writeEmptyTag( LEADER_TAG, "chapter.rule" );
writeEndTag( BLOCK_TAG );
}
/**
* Writes a table of contents. The DocumentModel has to contain a DocumentTOC for this to work.
*/
public void toc()
{
if ( docModel == null || docModel.getToc() == null || docModel.getToc().getItems() == null
|| this.tocPosition == TOC_NONE )
{
return;
}
DocumentTOC toc = docModel.getToc();
writeln( "<fo:page-sequence master-reference=\"toc\" initial-page-number=\"1\" format=\"i\">" );
regionBefore( toc.getName() );
regionAfter( getFooterText() );
writeStartTag( FLOW_TAG, "flow-name", "xsl-region-body" );
writeStartTag( BLOCK_TAG, "id", "./toc" );
chapterHeading( toc.getName(), false );
writeln( "<fo:table table-layout=\"fixed\" width=\"100%\" >" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "0.45in" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "0.4in" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "0.4in" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "5in" ); // TODO {$maxBodyWidth - 1.25}in
writeStartTag( TABLE_BODY_TAG );
writeTocItems( toc.getItems(), 1 );
writeEndTag( TABLE_BODY_TAG );
writeEndTag( TABLE_TAG );
writeEndTag( BLOCK_TAG );
writeEndTag( FLOW_TAG );
writeEndTag( PAGE_SEQUENCE_TAG );
}
private void writeTocItems( List tocItems, int level )
{
final int maxTocLevel = 4;
if ( level < 1 || level > maxTocLevel )
{
return;
}
tocStack.push( new NumberedListItem( NUMBERING_DECIMAL ) );
for ( Iterator k = tocItems.iterator(); k.hasNext(); )
{
DocumentTOCItem tocItem = (DocumentTOCItem) k.next();
String ref = getIdName( tocItem.getRef() );
writeStartTag( TABLE_ROW_TAG, "keep-with-next", "auto" );
if ( level > 2 )
{
for ( int i = 0; i < level - 2; i++ )
{
writeStartTag( TABLE_CELL_TAG );
writeSimpleTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
}
}
writeStartTag( TABLE_CELL_TAG, "toc.cell" );
writeStartTag( BLOCK_TAG, "toc.number.style" );
NumberedListItem current = (NumberedListItem) tocStack.peek();
current.next();
write( currentTocNumber() );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
String span = "3";
if ( level > 2 )
{
span = Integer.toString( 5 - level );
}
writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", span, "toc.cell" );
MutableAttributeSet atts = getFoConfiguration().getAttributeSet( "toc.h" + level + ".style" );
atts.addAttribute( "text-align-last", "justify" );
writeStartTag( BLOCK_TAG, atts );
writeStartTag( BASIC_LINK_TAG, "internal-destination", ref );
write( tocItem.getName() );
writeEndTag( BASIC_LINK_TAG );
writeEmptyTag( LEADER_TAG, "toc.leader.style" );
writeStartTag( INLINE_TAG, "page.number" );
writeEmptyTag( PAGE_NUMBER_CITATION_TAG, "ref-id", ref );
writeEndTag( INLINE_TAG );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
if ( tocItem.getItems() != null )
{
writeTocItems( tocItem.getItems(), level + 1 );
}
}
tocStack.pop();
}
private String currentTocNumber()
{
String ch = ( (NumberedListItem) tocStack.get( 0 ) ).getListItemSymbol();
for ( int i = 1; i < tocStack.size(); i++ )
{
ch = ch + "." + ( (NumberedListItem) tocStack.get( i ) ).getListItemSymbol();
}
return ch;
}
/**
* {@inheritDoc}
*
* Writes a fo:bookmark-tree. The DocumentModel has to contain a DocumentTOC for this to work.
*/
protected void pdfBookmarks()
{
if ( docModel == null || docModel.getToc() == null )
{
return;
}
writeStartTag( BOOKMARK_TREE_TAG );
renderBookmarkItems( docModel.getToc().getItems() );
writeEndTag( BOOKMARK_TREE_TAG );
}
private void renderBookmarkItems( List items )
{
for ( Iterator k = items.iterator(); k.hasNext(); )
{
DocumentTOCItem tocItem = (DocumentTOCItem) k.next();
String ref = getIdName( tocItem.getRef() );
writeStartTag( BOOKMARK_TAG, "internal-destination", ref );
writeStartTag( BOOKMARK_TITLE_TAG );
write( tocItem.getName() );
writeEndTag( BOOKMARK_TITLE_TAG );
if ( tocItem.getItems() != null )
{
renderBookmarkItems( tocItem.getItems() );
}
writeEndTag( BOOKMARK_TAG );
}
}
/**
* Writes a cover page. The DocumentModel has to contain a DocumentMeta for this to work.
*/
public void coverPage()
{
if ( this.docModel == null )
{
return;
}
DocumentCover cover = docModel.getCover();
DocumentMeta meta = docModel.getMeta();
if ( cover == null && meta == null )
{
return; // no information for cover page: ignore
}
// TODO: remove hard-coded settings
writeStartTag( PAGE_SEQUENCE_TAG, "master-reference", "cover-page" );
writeStartTag( FLOW_TAG, "flow-name", "xsl-region-body" );
writeStartTag( BLOCK_TAG, "text-align", "center" );
writeln( "<fo:table table-layout=\"fixed\" width=\"100%\" >" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "3.125in" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "3.125in" );
writeStartTag( TABLE_BODY_TAG );
writeCoverHead( cover );
writeCoverBody( cover, meta );
writeCoverFooter( cover, meta );
writeEndTag( TABLE_BODY_TAG );
writeEndTag( TABLE_TAG );
writeEndTag( BLOCK_TAG );
writeEndTag( FLOW_TAG );
writeEndTag( PAGE_SEQUENCE_TAG );
}
private void writeCoverHead( DocumentCover cover )
{
if ( cover == null )
{
return;
}
String compLogo = cover.getCompanyLogo();
String projLogo = cover.getProjectLogo();
writeStartTag( TABLE_ROW_TAG, "height", COVER_HEADER_HEIGHT );
writeStartTag( TABLE_CELL_TAG );
if ( StringUtils.isNotEmpty( compLogo ) )
{
SinkEventAttributeSet atts = new SinkEventAttributeSet();
atts.addAttribute( "text-align", "left" );
atts.addAttribute( "vertical-align", "top" );
writeStartTag( BLOCK_TAG, atts );
figureGraphics( compLogo, getGraphicsAttributes( compLogo ) );
writeEndTag( BLOCK_TAG );
}
writeSimpleTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeStartTag( TABLE_CELL_TAG );
if ( StringUtils.isNotEmpty( projLogo ) )
{
SinkEventAttributeSet atts = new SinkEventAttributeSet();
atts.addAttribute( "text-align", "right" );
atts.addAttribute( "vertical-align", "top" );
writeStartTag( BLOCK_TAG, atts );
figureGraphics( projLogo, getGraphicsAttributes( projLogo ) );
writeEndTag( BLOCK_TAG );
}
writeSimpleTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
}
private void writeCoverBody( DocumentCover cover, DocumentMeta meta )
{
if ( cover == null && meta == null )
{
return;
}
String subtitle = null;
String title = null;
String type = null;
String version = null;
if ( cover == null )
{
// aleady checked that meta != null
getLog().debug( "The DocumentCover is not defined, using the DocumentMeta title as cover title." );
title = meta.getTitle();
}
else
{
subtitle = cover.getCoverSubTitle();
title = cover.getCoverTitle();
type = cover.getCoverType();
version = cover.getCoverVersion();
}
writeln( "<fo:table-row keep-with-previous=\"always\" height=\"0.014in\">" );
writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", "2" );
writeStartTag( BLOCK_TAG, "line-height", "0.014in" );
writeEmptyTag( LEADER_TAG, "chapter.rule" );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
writeStartTag( TABLE_ROW_TAG, "height", "7.447in" );
writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", "2" );
writeln( "<fo:table table-layout=\"fixed\" width=\"100%\" >" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "2.083in" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "2.083in" );
writeEmptyTag( TABLE_COLUMN_TAG, "column-width", "2.083in" );
writeStartTag( TABLE_BODY_TAG );
writeStartTag( TABLE_ROW_TAG );
writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", "3" );
writeSimpleTag( BLOCK_TAG );
writeEmptyTag( BLOCK_TAG, "space-before", "3.2235in" );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
writeStartTag( TABLE_ROW_TAG );
writeStartTag( TABLE_CELL_TAG );
writeEmptyTag( BLOCK_TAG, "space-after", "0.5in" );
writeEndTag( TABLE_CELL_TAG );
writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", "2", "cover.border.left" );
writeStartTag( BLOCK_TAG, "cover.title" );
write( title == null ? "" : title );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
writeStartTag( TABLE_ROW_TAG );
writeStartTag( TABLE_CELL_TAG );
writeSimpleTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", "2", "cover.border.left.bottom" );
writeStartTag( BLOCK_TAG, "cover.subtitle" );
write( subtitle == null ? ( version == null ? "" : " v. " + version ) : subtitle );
writeEndTag( BLOCK_TAG );
writeStartTag( BLOCK_TAG, "cover.subtitle" );
write( type == null ? "" : type );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
writeEndTag( TABLE_BODY_TAG );
writeEndTag( TABLE_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
writeStartTag( TABLE_ROW_TAG, "height", "0.014in" );
writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", "2" );
writeln( "<fo:block space-after=\"0.2in\" line-height=\"0.014in\">" );
writeEmptyTag( LEADER_TAG, "chapter.rule" );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
writeStartTag( TABLE_ROW_TAG );
writeStartTag( TABLE_CELL_TAG, "number-columns-spanned", "2" );
writeSimpleTag( BLOCK_TAG );
writeEmptyTag( BLOCK_TAG, "space-before", "0.2in" );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
}
private void writeCoverFooter( DocumentCover cover, DocumentMeta meta )
{
if ( cover == null && meta == null )
{
return;
}
String date = null;
String compName = null;
if ( cover == null )
{
// aleady checked that meta != null
getLog().debug( "The DocumentCover is not defined, using the DocumentMeta author as company name." );
compName = meta.getAuthor();
}
else
{
compName = cover.getCompanyName();
if ( cover.getCoverdate() == null )
{
cover.setCoverDate( new Date() );
date = cover.getCoverdate();
cover.setCoverDate( null );
}
else
{
date = cover.getCoverdate();
}
}
writeStartTag( TABLE_ROW_TAG, "height", "0.3in" );
writeStartTag( TABLE_CELL_TAG );
MutableAttributeSet att = getFoConfiguration().getAttributeSet( "cover.subtitle" );
att.addAttribute( "height", "0.3in" );
att.addAttribute( "text-align", "left" );
writeStartTag( BLOCK_TAG, att );
write( compName == null ? ( cover.getAllAuthorNames() == null ? "" : cover.getAllAuthorNames() ) : compName );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeStartTag( TABLE_CELL_TAG );
att = getFoConfiguration().getAttributeSet( "cover.subtitle" );
att.addAttribute( "height", "0.3in" );
att.addAttribute( "text-align", "right" );
writeStartTag( BLOCK_TAG, att );
write( date == null ? "" : date );
writeEndTag( BLOCK_TAG );
writeEndTag( TABLE_CELL_TAG );
writeEndTag( TABLE_ROW_TAG );
}
private ResourceBundle getBundle( Locale locale )
{
return ResourceBundle.getBundle( "doxia-fo", locale, this.getClass().getClassLoader() );
}
private SinkEventAttributeSet getGraphicsAttributes( String logo )
{
MutableAttributeSet atts = null;
try
{
atts = DoxiaUtils.getImageAttributes( logo );
}
catch ( IOException e )
{
getLog().debug( e );
}
if ( atts == null )
{
return new SinkEventAttributeSet( new String[] { SinkEventAttributes.HEIGHT, COVER_HEADER_HEIGHT } );
}
// FOP dpi: 72
// Max width : 3.125 inch, table cell size, see #coverPage()
final int maxWidth = 225; // 3.125 * 72
if ( Integer.parseInt( atts.getAttribute( SinkEventAttributes.WIDTH ).toString() ) > maxWidth )
{
atts.addAttribute( "content-width", "3.125in" );
}
return new SinkEventAttributeSet( atts );
}
}
|