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.

FObjMixed.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.fo;
  19. import org.xml.sax.Locator;
  20. import org.apache.fop.apps.FOPException;
  21. /**
  22. * Abstract base class for representation of mixed content formatting objects
  23. * (= those that can contain both child {@link FONode}s and <code>#PCDATA</code>).
  24. */
  25. public abstract class FObjMixed extends FObj {
  26. /** Represents accumulated, pending FO text. See {@link #flushText()}. */
  27. private FOText ft;
  28. /** Used for white-space handling; start CharIterator at node ... */
  29. protected FONode currentTextNode;
  30. /** Used in creating pointers between subsequent {@link FOText} nodes
  31. * in the same {@link org.apache.fop.fo.flow.Block}
  32. * (for handling text-transform) */
  33. protected FOText lastFOTextProcessed;
  34. /**
  35. * Base constructor
  36. *
  37. * @param parent FONode that is the parent of this object
  38. */
  39. protected FObjMixed(FONode parent) {
  40. super(parent);
  41. }
  42. @Override
  43. public FONode clone(FONode parent, boolean removeChildren) throws FOPException {
  44. flushText();
  45. FObjMixed clone = (FObjMixed) super.clone(parent, removeChildren);
  46. if (removeChildren) {
  47. clone.currentTextNode = null;
  48. }
  49. return clone;
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. protected void characters(char[] data, int start, int length,
  54. PropertyList pList,
  55. Locator locator) throws FOPException {
  56. if (ft == null) {
  57. ft = new FOText(this);
  58. ft.setLocator(locator);
  59. if (!inMarker()) {
  60. ft.bind(pList);
  61. }
  62. }
  63. ft.characters(data, start, length, null, null);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void endOfNode() throws FOPException {
  68. super.endOfNode();
  69. if (!inMarker() || getNameId() == FO_MARKER) {
  70. // send character[s]() events to the FOEventHandler
  71. sendCharacters();
  72. }
  73. }
  74. /**
  75. * Handles white-space for the node that is passed in,
  76. * starting at its current text-node
  77. * (used by {@link org.apache.fop.fo.flow.RetrieveMarker}
  78. * to trigger 'end-of-node' white-space handling)
  79. *
  80. * @param fobj the node for which to handle white-space
  81. * @param nextChild the next child to be added
  82. */
  83. protected static void handleWhiteSpaceFor(FObjMixed fobj, FONode nextChild) {
  84. fobj.getBuilderContext().getXMLWhiteSpaceHandler()
  85. .handleWhiteSpace(fobj, fobj.currentTextNode, nextChild);
  86. }
  87. /**
  88. * Creates block-pointers between subsequent FOText nodes
  89. * in the same Block. (used for handling text-transform)
  90. *
  91. * TODO: !! Revisit: does not take into account fo:characters !!
  92. *
  93. * @throws FOPException if there is a problem during processing
  94. */
  95. private void flushText() throws FOPException {
  96. if (ft != null) {
  97. FOText lft = ft;
  98. /* make sure nested calls to itself have no effect */
  99. ft = null;
  100. if (getNameId() == FO_BLOCK) {
  101. lft.createBlockPointers((org.apache.fop.fo.flow.Block) this);
  102. this.lastFOTextProcessed = lft;
  103. } else if (getNameId() != FO_MARKER
  104. && getNameId() != FO_TITLE
  105. && getNameId() != FO_BOOKMARK_TITLE) {
  106. FONode fo = parent;
  107. int foNameId = fo.getNameId();
  108. while (foNameId != FO_BLOCK
  109. && foNameId != FO_MARKER
  110. && foNameId != FO_TITLE
  111. && foNameId != FO_BOOKMARK_TITLE
  112. && foNameId != FO_PAGE_SEQUENCE) {
  113. fo = fo.getParent();
  114. foNameId = fo.getNameId();
  115. }
  116. if (foNameId == FO_BLOCK) {
  117. lft.createBlockPointers((org.apache.fop.fo.flow.Block) fo);
  118. ((FObjMixed) fo).lastFOTextProcessed = lft;
  119. } else if (foNameId == FO_PAGE_SEQUENCE
  120. && lft.willCreateArea()) {
  121. log.error("Could not create block pointers."
  122. + " FOText w/o Block ancestor.");
  123. }
  124. }
  125. this.addChildNode(lft);
  126. }
  127. }
  128. private void sendCharacters() throws FOPException {
  129. if (this.currentTextNode != null) {
  130. FONodeIterator nodeIter
  131. = this.getChildNodes(this.currentTextNode);
  132. FONode node;
  133. while (nodeIter.hasNext()) {
  134. node = nodeIter.nextNode();
  135. assert (node instanceof FOText
  136. || node.getNameId() == FO_CHARACTER);
  137. if (node.getNameId() == FO_CHARACTER) {
  138. node.startOfNode();
  139. }
  140. node.endOfNode();
  141. }
  142. }
  143. this.currentTextNode = null;
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. protected void addChildNode(FONode child) throws FOPException {
  148. flushText();
  149. if (!inMarker()) {
  150. if (child instanceof FOText || child.getNameId() == FO_CHARACTER) {
  151. if (this.currentTextNode == null) {
  152. this.currentTextNode = child;
  153. }
  154. } else {
  155. // handle white-space for all text up to here
  156. handleWhiteSpaceFor(this, child);
  157. // send character[s]() events to the FOEventHandler
  158. sendCharacters();
  159. }
  160. }
  161. super.addChildNode(child);
  162. }
  163. /** {@inheritDoc} */
  164. @Override
  165. public void removeChild(FONode child) {
  166. super.removeChild(child);
  167. if (child == this.currentTextNode) {
  168. // reset to following sibling
  169. this.currentTextNode = child.siblings != null ? child.siblings[1] : null;
  170. }
  171. }
  172. /** {@inheritDoc} */
  173. @Override
  174. public void finalizeNode() throws FOPException {
  175. flushText();
  176. if (!inMarker() || getNameId() == FO_MARKER) {
  177. handleWhiteSpaceFor(this, null);
  178. }
  179. }
  180. /**
  181. * Returns a {@link CharIterator} over this FO's character content
  182. *
  183. * @return iterator for this object
  184. */
  185. @Override
  186. public CharIterator charIterator() {
  187. return new RecursiveCharIterator(this);
  188. }
  189. }