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.

RetrieveMarker.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.flow;
  19. import java.util.Iterator;
  20. import org.xml.sax.Locator;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.fo.FONode;
  23. import org.apache.fop.fo.FOText;
  24. import org.apache.fop.fo.FObj;
  25. import org.apache.fop.fo.FObjMixed;
  26. import org.apache.fop.fo.PropertyList;
  27. import org.apache.fop.fo.ValidationException;
  28. import org.apache.fop.fo.flow.table.Table;
  29. import org.apache.fop.fo.flow.table.TableFObj;
  30. /**
  31. * Class modelling the fo:retrieve-marker object.
  32. * This will create a layout manager that will retrieve
  33. * a marker based on the information.
  34. */
  35. public class RetrieveMarker extends FObjMixed {
  36. // The value of properties relevant for fo:retrieve-marker.
  37. private String retrieveClassName;
  38. private int retrievePosition;
  39. private int retrieveBoundary;
  40. // End of property values
  41. private PropertyList propertyList;
  42. /**
  43. * Create a retrieve marker object.
  44. * @param parent FONode that is the parent of this object
  45. * @see org.apache.fop.fo.FONode#FONode(FONode)
  46. */
  47. public RetrieveMarker(FONode parent) {
  48. super(parent);
  49. }
  50. /** {@inheritDoc} */
  51. public void bind(PropertyList pList) throws FOPException {
  52. if (findAncestor(FO_STATIC_CONTENT) < 0) {
  53. invalidChildError(locator, getParent().getName(), FO_URI, getName(),
  54. "rule.retrieveMarkerDescendatOfStaticContent");
  55. }
  56. retrieveClassName = pList.get(PR_RETRIEVE_CLASS_NAME).getString();
  57. retrievePosition = pList.get(PR_RETRIEVE_POSITION).getEnum();
  58. retrieveBoundary = pList.get(PR_RETRIEVE_BOUNDARY).getEnum();
  59. if (retrieveClassName == null || retrieveClassName.equals("")) {
  60. missingPropertyError("retrieve-class-name");
  61. }
  62. propertyList = pList.getParentPropertyList();
  63. }
  64. /**
  65. * {@inheritDoc}
  66. * XSL Content Model: empty
  67. */
  68. protected void validateChildNode(Locator loc, String nsURI, String localName)
  69. throws ValidationException {
  70. if (FO_URI.equals(nsURI)) {
  71. invalidChildError(loc, nsURI, localName);
  72. }
  73. }
  74. /**
  75. * @return the "retrieve-class-name" property.
  76. */
  77. public String getRetrieveClassName() {
  78. return retrieveClassName;
  79. }
  80. /**
  81. * @return the "retrieve-position" property (enum value).
  82. */
  83. public int getRetrievePosition() {
  84. return retrievePosition;
  85. }
  86. /**
  87. * @return the "retrieve-boundary" property (enum value).
  88. */
  89. public int getRetrieveBoundary() {
  90. return retrieveBoundary;
  91. }
  92. private PropertyList createPropertyListFor(FObj fo, PropertyList parent) {
  93. return getFOEventHandler().getPropertyListMaker().make(fo, parent);
  94. }
  95. private void cloneSingleNode(FONode child, FONode newParent,
  96. Marker marker, PropertyList parentPropertyList)
  97. throws FOPException {
  98. if (child != null) {
  99. FONode newChild = child.clone(newParent, true);
  100. if (child instanceof FObj) {
  101. Marker.MarkerPropertyList pList;
  102. PropertyList newPropertyList = createPropertyListFor(
  103. (FObj) newChild, parentPropertyList);
  104. pList = marker.getPropertyListFor(child);
  105. newChild.processNode(
  106. child.getLocalName(),
  107. getLocator(),
  108. pList,
  109. newPropertyList);
  110. if (newChild instanceof TableFObj) {
  111. // TODO calling startOfNode (and endOfNode, below) on other fobjs may
  112. // have undesirable side-effects. This is really ugly and will need to
  113. // be addressed sooner or later
  114. ((TableFObj) newChild).startOfNode();
  115. }
  116. addChildTo(newChild, (FObj) newParent);
  117. if (newChild.getNameId() == FO_TABLE) {
  118. Table t = (Table) child;
  119. cloneSubtree(t.getColumns().listIterator(),
  120. newChild, marker, newPropertyList);
  121. cloneSingleNode(t.getTableHeader(),
  122. newChild, marker, newPropertyList);
  123. cloneSingleNode(t.getTableFooter(),
  124. newChild, marker, newPropertyList);
  125. }
  126. cloneSubtree(child.getChildNodes(), newChild,
  127. marker, newPropertyList);
  128. if (newChild instanceof TableFObj) {
  129. // TODO this is ugly
  130. ((TableFObj) newChild).endOfNode();
  131. }
  132. } else if (child instanceof FOText) {
  133. FOText ft = (FOText) newChild;
  134. ft.bind(parentPropertyList);
  135. addChildTo(newChild, (FObj) newParent);
  136. }
  137. if (newChild instanceof FObjMixed) {
  138. handleWhiteSpaceFor((FObjMixed) newChild);
  139. }
  140. }
  141. }
  142. /**
  143. * Clone the FO nodes in the parent iterator,
  144. * attach the new nodes to the new parent,
  145. * and map the new nodes to the existing property lists.
  146. * FOText nodes are also in the new map, with a null value.
  147. * Clone the subtree by a recursive call to this method.
  148. * @param parentIter the iterator over the children of the old parent
  149. * @param newParent the new parent for the cloned nodes
  150. * @param marker the marker that contains the old property list mapping
  151. * @param descPLists the map of the new nodes to property lists
  152. */
  153. private void cloneSubtree(Iterator parentIter, FONode newParent,
  154. Marker marker, PropertyList parentPropertyList)
  155. throws FOPException {
  156. if (parentIter != null) {
  157. FONode child;
  158. while (parentIter.hasNext()) {
  159. child = (FONode) parentIter.next();
  160. cloneSingleNode(child, newParent,
  161. marker, parentPropertyList);
  162. }
  163. }
  164. }
  165. private void cloneFromMarker(Marker marker)
  166. throws FOPException {
  167. // clean up remnants from a possible earlier layout
  168. if (firstChild != null) {
  169. currentTextNode = null;
  170. firstChild = null;
  171. }
  172. cloneSubtree(marker.getChildNodes(), this,
  173. marker, propertyList);
  174. handleWhiteSpaceFor(this);
  175. }
  176. /**
  177. * Clone the subtree of the given marker
  178. *
  179. * @param marker the marker that is to be cloned
  180. */
  181. public void bindMarker(Marker marker) {
  182. if (marker.getChildNodes() != null) {
  183. try {
  184. cloneFromMarker(marker);
  185. } catch (FOPException exc) {
  186. getFOValidationEventProducer().markerCloningFailed(this,
  187. marker.getMarkerClassName(), exc, getLocator());
  188. return;
  189. }
  190. } else if (log.isDebugEnabled()) {
  191. log.debug("Empty marker retrieved...");
  192. }
  193. return;
  194. }
  195. /** {@inheritDoc} */
  196. public String getLocalName() {
  197. return "retrieve-marker";
  198. }
  199. /** {@inheritDoc} */
  200. public int getNameId() {
  201. return FO_RETRIEVE_MARKER;
  202. }
  203. }