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.

RtfHyperLink.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. import org.apache.fop.apps.FOPException;
  28. /**
  29. * Creates an hyperlink.
  30. * This class belongs to the fo:basic-link tag processing.
  31. * This work was originally authored by <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
  32. *
  33. */
  34. public class RtfHyperLink
  35. extends RtfContainer
  36. implements IRtfTextContainer,
  37. IRtfTextrunContainer {
  38. //////////////////////////////////////////////////
  39. // @@ Members
  40. //////////////////////////////////////////////////
  41. /** The url of the image */
  42. protected String url;
  43. /** RtfText */
  44. protected RtfText mText;
  45. //////////////////////////////////////////////////
  46. // @@ Construction
  47. //////////////////////////////////////////////////
  48. /**
  49. * A constructor.
  50. *
  51. * @param parent a <code>RtfContainer</code> value
  52. * @param writer a <code>Writer</code> value
  53. * @param str text of the link
  54. * @param attr a <code>RtfAttributes</code> value
  55. * @throws IOException for I/O problems
  56. */
  57. public RtfHyperLink(IRtfTextContainer parent, Writer writer, String str, RtfAttributes attr)
  58. throws IOException {
  59. super((RtfContainer) parent, writer, attr);
  60. new RtfText(this, writer, str, attr);
  61. }
  62. /**
  63. * A constructor.
  64. *
  65. * @param parent a <code>RtfContainer</code> value
  66. * @param writer a <code>Writer</code> value
  67. * @param attr a <code>RtfAttributes</code> value
  68. * @throws IOException for I/O problems
  69. */
  70. public RtfHyperLink(RtfTextrun parent, Writer writer, RtfAttributes attr)
  71. throws IOException {
  72. super((RtfContainer) parent, writer, attr);
  73. }
  74. //////////////////////////////////////////////////
  75. // @@ RtfElement implementation
  76. //////////////////////////////////////////////////
  77. /**
  78. * Writes the RTF content to m_writer.
  79. *
  80. * @exception IOException On error
  81. */
  82. public void writeRtfPrefix() throws IOException {
  83. super.writeGroupMark(true);
  84. super.writeControlWord("field");
  85. super.writeGroupMark(true);
  86. super.writeStarControlWord("fldinst");
  87. writer.write("HYPERLINK \"" + url + "\" ");
  88. super.writeGroupMark(false);
  89. super.writeGroupMark(true);
  90. super.writeControlWord("fldrslt");
  91. // start a group for this paragraph and write our own attributes if needed
  92. if (attrib != null && attrib.isSet("cs")) {
  93. writeGroupMark(true);
  94. writeAttributes(attrib, new String [] {"cs"});
  95. }
  96. }
  97. /**
  98. * Writes the RTF content to m_writer.
  99. *
  100. * @exception IOException On error
  101. */
  102. public void writeRtfSuffix() throws IOException {
  103. if (attrib != null && attrib.isSet("cs")) {
  104. writeGroupMark(false);
  105. }
  106. super.writeGroupMark(false);
  107. super.writeGroupMark(false);
  108. }
  109. //////////////////////////////////////////////////
  110. // @@ IRtfContainer implementation
  111. //////////////////////////////////////////////////
  112. /**
  113. * close current text run if any and start a new one with default attributes
  114. * @param str if not null, added to the RtfText created
  115. * @throws IOException for I/O problems
  116. * @return new RtfText object
  117. */
  118. public RtfText newText(String str) throws IOException {
  119. return newText(str, null);
  120. }
  121. /**
  122. * close current text run if any and start a new one
  123. * @param str if not null, added to the RtfText created
  124. * @param attr attributes of text to add
  125. * @throws IOException for I/O problems
  126. * @return the new RtfText object
  127. */
  128. public RtfText newText(String str, RtfAttributes attr) throws IOException {
  129. closeAll();
  130. mText = new RtfText(this, writer, str, attr);
  131. return mText;
  132. }
  133. /**
  134. * IRtfTextContainer requirement:
  135. * @return a copy of our attributes
  136. * @throws FOPException if attributes cannot be cloned
  137. */
  138. public RtfAttributes getTextContainerAttributes() throws FOPException {
  139. if (attrib == null) {
  140. return null;
  141. }
  142. try {
  143. return (RtfAttributes) this.attrib.clone();
  144. } catch (CloneNotSupportedException e) {
  145. throw new FOPException(e);
  146. }
  147. }
  148. /**
  149. * add a line break
  150. * @throws IOException for I/O problems
  151. */
  152. public void newLineBreak() throws IOException {
  153. new RtfLineBreak(this, writer);
  154. }
  155. //////////////////////////////////////////////////
  156. // @@ Common container methods
  157. //////////////////////////////////////////////////
  158. private void closeCurrentText() throws IOException {
  159. if (mText != null) {
  160. mText.close();
  161. }
  162. }
  163. private void closeAll() throws IOException {
  164. closeCurrentText();
  165. }
  166. //////////////////////////////////////////////////
  167. // @@ Member access
  168. //////////////////////////////////////////////////
  169. /**
  170. * Sets the url of the external link.
  171. *
  172. * @param url Link url like "http://..."
  173. */
  174. public void setExternalURL(String url) {
  175. this.url = url;
  176. }
  177. /**
  178. * Sets the url of the external link.
  179. *
  180. * @param jumpTo Name of the text mark
  181. */
  182. public void setInternalURL(String jumpTo) {
  183. int now = jumpTo.length();
  184. int max = RtfBookmark.MAX_BOOKMARK_LENGTH;
  185. this.url = "#" + jumpTo.substring(0, now > max ? max : now);
  186. this.url = this.url.replace('.', RtfBookmark.REPLACE_CHARACTER);
  187. this.url = this.url.replace(' ', RtfBookmark.REPLACE_CHARACTER);
  188. }
  189. /**
  190. *
  191. * @return false (always)
  192. */
  193. public boolean isEmpty() {
  194. return false;
  195. }
  196. /**
  197. * @return a text run
  198. * @throws IOException if not caught
  199. */
  200. public RtfTextrun getTextrun() throws IOException {
  201. RtfTextrun textrun = RtfTextrun.getTextrun(this, writer, null);
  202. return textrun;
  203. }
  204. }