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.

BuilderContext.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. package org.apache.fop.render.rtf;
  52. import java.util.Stack;
  53. import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfOptions;
  54. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfContainer;
  55. /** A BuilderContext holds context information when building an RTF document
  56. *
  57. * @author Bertrand Delacretaz <bdelacretaz@codeconsult.ch>
  58. * @author putzi
  59. * @author Peter Herweg <pherweg@web.de>
  60. *
  61. * This class was originally developed by Bertrand Delacretaz bdelacretaz@codeconsult.ch
  62. * for the JFOR project and is now integrated into FOP.
  63. */
  64. class BuilderContext {
  65. /** stack of RtfContainers */
  66. private final Stack m_containers = new Stack();
  67. /** stack of TableContexts */
  68. private final Stack m_tableContexts = new Stack();
  69. /** stack of IBuilders */
  70. private final Stack m_builders = new Stack();
  71. /** Rtf options */
  72. IRtfOptions m_options;
  73. BuilderContext(IRtfOptions rtfOptions) {
  74. m_options = rtfOptions;
  75. }
  76. /** find first object of given class from top of stack s
  77. * @return null if not found
  78. */
  79. private Object getObjectFromStack(Stack s, Class desiredClass) {
  80. Object result = null;
  81. final Stack copy = (Stack)s.clone();
  82. while (!copy.isEmpty()) {
  83. final Object o = copy.pop();
  84. if (desiredClass.isAssignableFrom(o.getClass())) {
  85. result = o;
  86. break;
  87. }
  88. }
  89. return result;
  90. }
  91. /* find the "nearest" IBuilder of given class /
  92. Object getBuilder(Class builderClass,boolean required)
  93. throws Exception
  94. {
  95. final IBuilder result = (IBuilder)getObjectFromStack(m_builders,builderClass);
  96. if(result == null && required) {
  97. throw new Exception(
  98. "IBuilder of class '" + builderClass.getName() + "' not found on builders stack"
  99. );
  100. }
  101. return result;
  102. }*/
  103. /** find the "nearest" container that implements the given interface on our stack
  104. * @param required if true, ConverterException is thrown if no container found
  105. * @param forWhichBuilder used in error message if container not found
  106. */
  107. RtfContainer getContainer(Class containerClass, boolean required,
  108. Object /*IBuilder*/ forWhichBuilder) throws Exception {
  109. // TODO what to do if the desired container is not at the top of the stack?
  110. // close top-of-stack container?
  111. final RtfContainer result = (RtfContainer)getObjectFromStack(m_containers,
  112. containerClass);
  113. if (result == null && required) {
  114. throw new Exception(
  115. "No RtfContainer of class '" + containerClass.getName()
  116. + "' available for '" + forWhichBuilder.getClass().getName() + "' builder"
  117. );
  118. }
  119. return result;
  120. }
  121. /** push an RtfContainer on our stack */
  122. void pushContainer(RtfContainer c) {
  123. m_containers.push(c);
  124. }
  125. /**
  126. * In some cases an RtfContainer must be replaced by another one on the
  127. * stack. This happens when handling nested fo:blocks for example: after
  128. * handling a nested block the enclosing block must switch to a new
  129. * paragraph container to handle what follows the nested block.
  130. * TODO: what happens to elements that are "more on top" than oldC on the
  131. * stack? shouldn't they be closed or something?
  132. */
  133. void replaceContainer(RtfContainer oldC, RtfContainer newC)
  134. throws Exception {
  135. // treating the Stack as a Vector allows such manipulations (yes, I hear you screaming ;-)
  136. final int index = m_containers.indexOf(oldC);
  137. if (index < 0) {
  138. throw new Exception("container to replace not found:" + oldC);
  139. }
  140. m_containers.setElementAt(newC, index);
  141. }
  142. /** pop the topmost RtfContainer from our stack */
  143. void popContainer() {
  144. m_containers.pop();
  145. }
  146. /* push an IBuilder to our stack /
  147. void pushBuilder(IBuilder b)
  148. {
  149. m_builders.push(b);
  150. }*/
  151. /** pop the topmost IBuilder from our stack and return previous builder on stack
  152. * @return null if builders stack is empty
  153. IBuilder popBuilderAndGetPreviousOne()
  154. {
  155. IBuilder result = null;
  156. m_builders.pop();
  157. if(!m_builders.isEmpty()) {
  158. result = (IBuilder)m_builders.peek();
  159. }
  160. return result;
  161. }
  162. */
  163. /** return the current TableContext */
  164. TableContext getTableContext() {
  165. return (TableContext)m_tableContexts.peek();
  166. }
  167. /** push a TableContext to our stack */
  168. void pushTableContext(TableContext tc) {
  169. m_tableContexts.push(tc);
  170. }
  171. /** pop a TableContext from our stack */
  172. void popTableContext() {
  173. m_tableContexts.pop();
  174. }
  175. }