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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * @author Christopher Scott, scottc@westinghouse.com
  29. * @author Boris Pouderous, boris.pouderous@free.fr
  30. */
  31. public class RtfPageNumberCitation extends RtfContainer {
  32. /* Page field :
  33. "{\field {\*\fldinst {PAGEREF xx}} {\fldrslt}}" where xx represents the
  34. 'id' of the referenced page
  35. */
  36. /** constant for field */
  37. public static final String RTF_FIELD = "field";
  38. /** constant for field pageref model */
  39. public static final String RTF_FIELD_PAGEREF_MODEL = "fldinst { PAGEREF }";
  40. /** constant for field result */
  41. public static final String RTF_FIELD_RESULT = "fldrslt";
  42. // The 'id' of the referenced page
  43. private String id = null;
  44. /** Create an RTF page number citation as a child of given container with default attributes */
  45. RtfPageNumberCitation (RtfContainer parent, Writer w, String id)
  46. throws IOException {
  47. super(parent, w);
  48. this.id = id;
  49. }
  50. /** Create an RTF page number citation as a child of given
  51. * paragraph, copying its attributes */
  52. RtfPageNumberCitation (RtfParagraph parent, Writer w, String id)
  53. throws IOException {
  54. // add the attributes ant text attributes of the parent paragraph
  55. super((RtfContainer)parent, w, parent.attrib);
  56. if (parent.getTextAttributes() != null) {
  57. attrib.set(parent.getTextAttributes());
  58. }
  59. this.id = id;
  60. }
  61. /**
  62. * Write the content
  63. * @throws IOException for I/O problems
  64. */
  65. protected void writeRtfContent() throws IOException {
  66. // If we have a valid ID
  67. if (isValid()) {
  68. // Build page reference field
  69. String pageRef = RTF_FIELD_PAGEREF_MODEL;
  70. final int insertionIndex = pageRef.indexOf("}");
  71. pageRef = pageRef.substring(0, insertionIndex)
  72. + "\"" + id
  73. + "\"" + " "
  74. + pageRef.substring(insertionIndex, pageRef.length());
  75. id = null;
  76. // Write RTF content
  77. writeGroupMark(true);
  78. writeControlWord(RTF_FIELD);
  79. writeGroupMark(true);
  80. writeAttributes(attrib, RtfText.ATTR_NAMES); // Added by Boris Poudérous
  81. writeStarControlWord(pageRef);
  82. writeGroupMark(false);
  83. writeGroupMark(true);
  84. writeControlWord(RTF_FIELD_RESULT + '#'); //To see where the page-number would be
  85. writeGroupMark(false);
  86. writeGroupMark(false);
  87. }
  88. }
  89. /** checks that the 'ref-id' attribute exists */
  90. private boolean isValid() {
  91. return (id != null);
  92. }
  93. /**
  94. * @return true if this element would generate no "useful" RTF content
  95. */
  96. public boolean isEmpty() {
  97. return false;
  98. }
  99. }