You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtfPageNumberCitation.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  19. /*
  20. * This file is part of the RTF library of the FOP project, which was originally
  21. * created by Bertrand Delacretaz bdelacretaz@codeconsult.ch and by other
  22. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  23. * the FOP project.
  24. */
  25. import java.io.IOException;
  26. import java.io.Writer;
  27. /**
  28. * <p>Page number citation container.</p>
  29. * <p>This work was authored by Christopher Scott (scottc@westinghouse.com) and
  30. * Boris Pouderous (boris.pouderous@free.fr).</p>
  31. */
  32. public class RtfPageNumberCitation extends RtfContainer {
  33. /* Page field :
  34. "{\field {\*\fldinst {PAGEREF xx}} {\fldrslt}}" where xx represents the
  35. 'id' of the referenced page
  36. */
  37. /** constant for field */
  38. public static final String RTF_FIELD = "field";
  39. /** constant for field pageref model */
  40. public static final String RTF_FIELD_PAGEREF_MODEL = "fldinst { PAGEREF }";
  41. /** constant for field result */
  42. public static final String RTF_FIELD_RESULT = "fldrslt";
  43. // The 'id' of the referenced page
  44. private String id;
  45. /** Create an RTF page number citation as a child of given container with default attributes */
  46. RtfPageNumberCitation(RtfContainer parent, Writer w, String id)
  47. throws IOException {
  48. super(parent, w);
  49. this.id = id;
  50. }
  51. /** Create an RTF page number citation as a child of given
  52. * paragraph, copying its attributes */
  53. RtfPageNumberCitation(RtfParagraph parent, Writer w, String id)
  54. throws IOException {
  55. // add the attributes ant text attributes of the parent paragraph
  56. super((RtfContainer)parent, w, parent.attrib);
  57. if (parent.getTextAttributes() != null) {
  58. attrib.set(parent.getTextAttributes());
  59. }
  60. this.id = id;
  61. }
  62. /**
  63. * Write the content
  64. * @throws IOException for I/O problems
  65. */
  66. protected void writeRtfContent() throws IOException {
  67. // If we have a valid ID
  68. if (isValid()) {
  69. // Build page reference field
  70. String pageRef = RTF_FIELD_PAGEREF_MODEL;
  71. final int insertionIndex = pageRef.indexOf("}");
  72. pageRef = pageRef.substring(0, insertionIndex)
  73. + "\"" + id
  74. + "\"" + " "
  75. + pageRef.substring(insertionIndex, pageRef.length());
  76. id = null;
  77. // Write RTF content
  78. writeGroupMark(true);
  79. writeControlWord(RTF_FIELD);
  80. writeGroupMark(true);
  81. writeAttributes(attrib, RtfText.ATTR_NAMES); // Added by Boris Poudérous
  82. writeStarControlWord(pageRef);
  83. writeGroupMark(false);
  84. writeGroupMark(true);
  85. writeControlWord(RTF_FIELD_RESULT + '#'); //To see where the page-number would be
  86. writeGroupMark(false);
  87. writeGroupMark(false);
  88. }
  89. }
  90. /** checks that the 'ref-id' attribute exists */
  91. private boolean isValid() {
  92. return (id != null);
  93. }
  94. /**
  95. * @return true if this element would generate no "useful" RTF content
  96. */
  97. public boolean isEmpty() {
  98. return false;
  99. }
  100. }