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.

AbstractRetrieveMarker.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.XMLObj;
  29. import org.apache.fop.fo.flow.table.Table;
  30. /**
  31. * Abstract base class for the <a href="http://www.w3.org/TR/xsl/#fo_retrieve-marker">
  32. * <code>fo:retrieve-marker</code></a> and
  33. * <a href="http://www.w3.org/TR/xsl/#fo_retrieve-table-marker">
  34. * <code>fo:retrieve-table-marker</code></a> formatting objects.
  35. */
  36. public abstract class AbstractRetrieveMarker extends FObjMixed {
  37. private PropertyList propertyList;
  38. private String retrieveClassName;
  39. private int position;
  40. private String positionLabel;
  41. private int boundary;
  42. private String boundaryLabel;
  43. /**
  44. * Create a new AbstractRetrieveMarker instance that
  45. * is a child of the given {@link FONode}
  46. *
  47. * @param parent the parent {@link FONode}
  48. */
  49. public AbstractRetrieveMarker(FONode parent) {
  50. super(parent);
  51. }
  52. /**
  53. * {@inheritDoc}
  54. * <p>XSL Content Model: empty
  55. */
  56. protected void validateChildNode(Locator loc, String nsURI, String localName)
  57. throws ValidationException {
  58. if (FO_URI.equals(nsURI)) {
  59. invalidChildError(loc, nsURI, localName);
  60. }
  61. }
  62. /**
  63. * {@inheritDoc}
  64. * Store a reference to the parent {@link PropertyList}
  65. * to be used when the retrieve-marker is resolved.
  66. */
  67. public void bind(PropertyList pList) throws FOPException {
  68. super.bind(pList);
  69. this.retrieveClassName = pList.get(PR_RETRIEVE_CLASS_NAME).getString();
  70. if (retrieveClassName == null || retrieveClassName.equals("")) {
  71. missingPropertyError("retrieve-class-name");
  72. }
  73. this.propertyList = pList.getParentPropertyList();
  74. }
  75. private PropertyList createPropertyListFor(FObj fo, PropertyList parent) {
  76. return getBuilderContext().getPropertyListMaker().make(fo, parent);
  77. }
  78. private void cloneSingleNode(FONode child, FONode newParent,
  79. Marker marker, PropertyList parentPropertyList)
  80. throws FOPException {
  81. if (child != null) {
  82. FONode newChild = child.clone(newParent, true);
  83. if (child instanceof FObj) {
  84. Marker.MarkerPropertyList pList;
  85. PropertyList newPropertyList = createPropertyListFor(
  86. (FObj) newChild, parentPropertyList);
  87. pList = marker.getPropertyListFor(child);
  88. newChild.processNode(
  89. child.getLocalName(),
  90. getLocator(),
  91. pList,
  92. newPropertyList);
  93. addChildTo(newChild, newParent);
  94. switch ( newChild.getNameId() ) {
  95. case FO_TABLE:
  96. Table t = (Table) child;
  97. cloneSubtree(t.getColumns().iterator(),
  98. newChild, marker, newPropertyList);
  99. cloneSingleNode(t.getTableHeader(),
  100. newChild, marker, newPropertyList);
  101. cloneSingleNode(t.getTableFooter(),
  102. newChild, marker, newPropertyList);
  103. cloneSubtree(child.getChildNodes(),
  104. newChild, marker, newPropertyList);
  105. break;
  106. case FO_LIST_ITEM:
  107. ListItem li = (ListItem) child;
  108. cloneSingleNode(li.getLabel(),
  109. newChild, marker, newPropertyList);
  110. cloneSingleNode(li.getBody(),
  111. newChild, marker, newPropertyList);
  112. break;
  113. default:
  114. cloneSubtree(child.getChildNodes(),
  115. newChild, marker, newPropertyList);
  116. break;
  117. }
  118. } else if (child instanceof FOText) {
  119. FOText ft = (FOText) newChild;
  120. ft.bind(parentPropertyList);
  121. addChildTo(newChild, newParent);
  122. } else if (child instanceof XMLObj) {
  123. addChildTo(newChild, newParent);
  124. }
  125. // trigger end-of-node white-space handling
  126. // and finalization for table-FOs
  127. newChild.finalizeNode();
  128. }
  129. }
  130. /**
  131. * Clone the FO nodes in the parent iterator,
  132. * attach the new nodes to the new parent,
  133. * and map the new nodes to the existing property lists.
  134. * FOText nodes are also in the new map, with a null value.
  135. * Clone the subtree by a recursive call to this method.
  136. * @param parentIter the iterator over the children of the old parent
  137. * @param newParent the new parent for the cloned nodes
  138. * @param marker the marker that contains the old property list mapping
  139. * @param parentPropertyList the parent PropertyList
  140. * @throws FOPException in case there was an error
  141. */
  142. private void cloneSubtree(Iterator parentIter, FONode newParent,
  143. Marker marker, PropertyList parentPropertyList)
  144. throws FOPException {
  145. if (parentIter != null) {
  146. FONode child;
  147. while (parentIter.hasNext()) {
  148. child = (FONode) parentIter.next();
  149. cloneSingleNode(child, newParent,
  150. marker, parentPropertyList);
  151. }
  152. }
  153. }
  154. private void cloneFromMarker(Marker marker)
  155. throws FOPException {
  156. cloneSubtree(marker.getChildNodes(), this,
  157. marker, propertyList);
  158. handleWhiteSpaceFor(this, null);
  159. }
  160. /**
  161. * Clone the subtree of the given marker
  162. *
  163. * @param marker the marker that is to be cloned
  164. */
  165. public void bindMarker(Marker marker) {
  166. // clean up remnants from a possible earlier layout
  167. if (firstChild != null) {
  168. currentTextNode = null;
  169. firstChild = null;
  170. }
  171. if (marker.getChildNodes() != null) {
  172. try {
  173. cloneFromMarker(marker);
  174. } catch (FOPException exc) {
  175. getFOValidationEventProducer().markerCloningFailed(this,
  176. marker.getMarkerClassName(), exc, getLocator());
  177. }
  178. } else if (log.isDebugEnabled()) {
  179. log.debug("Empty marker retrieved...");
  180. }
  181. }
  182. /**
  183. * Return the value for the <code>retrieve-class-name</code>
  184. * property
  185. *
  186. * @return the value for retrieve-class-name
  187. */
  188. public String getRetrieveClassName() {
  189. return this.retrieveClassName;
  190. }
  191. protected void setBoundaryLabel(String label) {
  192. this.boundaryLabel = label;
  193. }
  194. protected void setPositionLabel(String label) {
  195. this.positionLabel = label;
  196. }
  197. public String getBoundaryLabel() {
  198. return this.boundaryLabel;
  199. }
  200. public String getPositionLabel() {
  201. return this.positionLabel;
  202. }
  203. protected void setPosition(int position) {
  204. this.position = position;
  205. }
  206. protected void setBoundary(int boundary) {
  207. this.boundary = boundary;
  208. }
  209. public int getPosition() {
  210. return this.position;
  211. }
  212. public int getBoundary() {
  213. return this.boundary;
  214. }
  215. public abstract String getLocalName();
  216. public abstract int getNameId();
  217. public void changePositionTo(int position) {
  218. this.position = position;
  219. }
  220. }