選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RtfContainer.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. /*
  52. * This file is part of the RTF library of the FOP project, which was originally
  53. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  54. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  55. * the FOP project.
  56. */
  57. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  58. import java.io.Writer;
  59. import java.util.LinkedList;
  60. import java.util.List;
  61. import java.util.Iterator;
  62. import java.io.IOException;
  63. import org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException;
  64. /** An RtfElement that can contain other elements.
  65. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  66. */
  67. public class RtfContainer extends RtfElement {
  68. private LinkedList children; // 'final' removed by Boris Poudérous on 07/22/2002
  69. private RtfOptions options = new RtfOptions();
  70. private RtfElement lastChild;
  71. /** Create an RTF container as a child of given container */
  72. RtfContainer(RtfContainer parent, Writer w) throws IOException {
  73. this(parent, w, null);
  74. }
  75. /** Create an RTF container as a child of given container with given attributes */
  76. RtfContainer(RtfContainer parent, Writer w, RtfAttributes attr) throws IOException {
  77. super(parent, w, attr);
  78. children = new LinkedList();
  79. }
  80. /**
  81. * set options
  82. * @param opt options to set
  83. */
  84. public void setOptions(RtfOptions opt) {
  85. options = opt;
  86. }
  87. /**
  88. * add a child element to this
  89. * @param e child element to add
  90. * @throws RtfStructureException for trying to add an invalid child (??)
  91. */
  92. protected void addChild(RtfElement e)
  93. throws RtfStructureException {
  94. if (isClosed()) {
  95. // No childs should be added to a container that has been closed
  96. final StringBuffer sb = new StringBuffer();
  97. sb.append("addChild: container already closed (parent=");
  98. sb.append(this.getClass().getName());
  99. sb.append(" child=");
  100. sb.append(e.getClass().getName());
  101. sb.append(")");
  102. final String msg = sb.toString();
  103. // warn of this problem
  104. final RtfFile rf = getRtfFile();
  105. // if(rf.getLog() != null) {
  106. // rf.getLog().logWarning(msg);
  107. // }
  108. // TODO this should be activated to help detect XSL-FO constructs
  109. // that we do not handle properly.
  110. /*
  111. throw new RtfStructureException(msg);
  112. */
  113. }
  114. children.add(e);
  115. lastChild = e;
  116. }
  117. /**
  118. * @return a copy of our children's list
  119. */
  120. public List getChildren() {
  121. return (List)children.clone();
  122. }
  123. /**
  124. * @return the number of children
  125. */
  126. public int getChildCount() {
  127. return children.size();
  128. }
  129. /**
  130. * Add by Boris Poudérous on 07/22/2002
  131. * Set the children list
  132. * @param children list of child objects
  133. * @return true if process succeeded
  134. */
  135. public boolean setChildren (List children) {
  136. if (children instanceof LinkedList) {
  137. this.children = (LinkedList)children;
  138. return true;
  139. }
  140. return false;
  141. }
  142. /**
  143. * write RTF code of all our children
  144. * @throws IOException for I/O problems
  145. */
  146. protected void writeRtfContent()
  147. throws IOException {
  148. for (Iterator it = children.iterator(); it.hasNext();) {
  149. final RtfElement e = (RtfElement)it.next();
  150. e.writeRtf();
  151. }
  152. }
  153. /** return our options */
  154. RtfOptions getOptions() {
  155. return options;
  156. }
  157. /** true if this (recursively) contains at least one RtfText object */
  158. boolean containsText() {
  159. boolean result = false;
  160. for (Iterator it = children.iterator(); it.hasNext();) {
  161. final RtfElement e = (RtfElement)it.next();
  162. if (e instanceof RtfText) {
  163. result = !e.isEmpty();
  164. } else if (e instanceof RtfContainer) {
  165. if (((RtfContainer)e).containsText()) {
  166. result = true;
  167. }
  168. }
  169. if (result) {
  170. break;
  171. }
  172. }
  173. return result;
  174. }
  175. /** debugging to given Writer */
  176. void dump(Writer w, int indent)
  177. throws IOException {
  178. super.dump(w, indent);
  179. for (Iterator it = children.iterator(); it.hasNext();) {
  180. final RtfElement e = (RtfElement)it.next();
  181. e.dump(w, indent + 1);
  182. }
  183. }
  184. /**
  185. * minimal debugging display
  186. * @return String representation of object contents
  187. */
  188. public String toString() {
  189. return super.toString() + " (" + getChildCount() + " children)";
  190. }
  191. /**
  192. * @return false if empty or if our options block writing
  193. */
  194. protected boolean okToWriteRtf() {
  195. boolean result = super.okToWriteRtf() && !isEmpty();
  196. if (result && !options.renderContainer(this)) {
  197. result = false;
  198. }
  199. return result;
  200. }
  201. /**
  202. * @return true if this element would generate no "useful" RTF content,
  203. * i.e. (for RtfContainer) true if it has no children where isEmpty() is false
  204. */
  205. public boolean isEmpty() {
  206. boolean result = true;
  207. for (Iterator it = children.iterator(); it.hasNext();) {
  208. final RtfElement e = (RtfElement)it.next();
  209. if (!e.isEmpty()) {
  210. result = false;
  211. break;
  212. }
  213. }
  214. return result;
  215. }
  216. }