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.

FONode.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. // Java
  20. import java.util.ListIterator;
  21. import java.util.Map;
  22. import org.xml.sax.Attributes;
  23. import org.xml.sax.Locator;
  24. import org.xml.sax.helpers.LocatorImpl;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.xmlgraphics.util.QName;
  28. import org.apache.fop.apps.FOPException;
  29. import org.apache.fop.apps.FOUserAgent;
  30. import org.apache.fop.fo.extensions.ExtensionAttachment;
  31. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  32. import org.apache.fop.fo.extensions.InternalElementMapping;
  33. import org.apache.fop.fo.extensions.svg.SVGElementMapping;
  34. import org.apache.fop.fo.pagination.Root;
  35. import org.apache.fop.util.CharUtilities;
  36. import org.apache.fop.util.ContentHandlerFactory;
  37. import org.apache.fop.util.text.AdvancedMessageFormat.Function;
  38. /**
  39. * Base class for nodes in the XML tree
  40. */
  41. public abstract class FONode implements Cloneable {
  42. /** the XSL-FO namespace URI */
  43. protected static final String FO_URI = FOElementMapping.URI;
  44. /** FOP's proprietary extension namespace URI */
  45. protected static final String FOX_URI = ExtensionElementMapping.URI;
  46. /** Parent FO node */
  47. protected FONode parent;
  48. /** pointer to the sibling nodes */
  49. protected FONode[] siblings;
  50. /**
  51. * Marks the location of this object from the input FO
  52. * <br>Call <code>locator.getSystemId()</code>,
  53. * <code>getLineNumber()</code>,
  54. * <code>getColumnNumber()</code> for file, line, column
  55. * information
  56. */
  57. protected Locator locator;
  58. /** Logger for fo-tree related messages **/
  59. protected static final Log log = LogFactory.getLog(FONode.class);
  60. /**
  61. * Base constructor
  62. *
  63. * @param parent parent of this node
  64. */
  65. protected FONode(FONode parent) {
  66. this.parent = parent;
  67. }
  68. /**
  69. * Performs a shallow cloning operation, sets the clone's parent,
  70. * and optionally cleans the list of child nodes
  71. *
  72. * @param cloneparent the intended parent of the clone
  73. * @param removeChildren if true, clean the list of child nodes
  74. * @return the cloned FO node
  75. * @throws FOPException if there's a problem while cloning the node
  76. */
  77. public FONode clone(FONode cloneparent, boolean removeChildren)
  78. throws FOPException {
  79. FONode foNode = (FONode) clone();
  80. foNode.parent = cloneparent;
  81. foNode.siblings = null;
  82. return foNode;
  83. }
  84. /** {@inheritDoc} */
  85. protected Object clone() {
  86. try {
  87. return super.clone();
  88. } catch (CloneNotSupportedException e) {
  89. throw new AssertionError(); // Can't happen
  90. }
  91. }
  92. /**
  93. * Bind the given <code>PropertyList</code> to this node
  94. * Does nothing by default. Subclasses should override this method
  95. * in case they want to use the properties available on the
  96. * <code>PropertyList</code>.
  97. *
  98. * @param propertyList the <code>PropertyList</code>
  99. * @throws FOPException if there was an error when
  100. * processing the <code>PropertyList</code>
  101. */
  102. public void bind(PropertyList propertyList) throws FOPException {
  103. //nop
  104. }
  105. /**
  106. * Set the location information for this element
  107. * @param locator the org.xml.sax.Locator object
  108. */
  109. public void setLocator(Locator locator) {
  110. if (locator != null) {
  111. //Create a copy of the locator so the info is preserved when we need to
  112. //give pointers during layout.
  113. this.locator = new LocatorImpl(locator);
  114. }
  115. }
  116. /**
  117. * Returns the <code>Locator</code> containing the location information for this
  118. * element, or <code>null</code> if not available
  119. *
  120. * @return the location information for this element or <code>null</code>, if not available
  121. */
  122. public Locator getLocator() {
  123. return this.locator;
  124. }
  125. /**
  126. * Recursively goes up the FOTree hierarchy until the <code>fo:root</code>
  127. * is found, which returns the parent <code>FOEventHandler</code>.
  128. * <br>(see also: {@link org.apache.fop.fo.pagination.Root#getFOEventHandler()})
  129. *
  130. * @return the FOEventHandler object that is the parent of the FO Tree
  131. */
  132. public FOEventHandler getFOEventHandler() {
  133. return parent.getFOEventHandler();
  134. }
  135. /**
  136. * Returns the context class providing information used during FO tree building.
  137. * @return the builder context
  138. */
  139. public FOTreeBuilderContext getBuilderContext() {
  140. return parent.getBuilderContext();
  141. }
  142. /**
  143. * Indicates whether this node is a child of an fo:marker.
  144. * @return true if this node is a child of an fo:marker
  145. */
  146. protected boolean inMarker() {
  147. return getBuilderContext().inMarker();
  148. }
  149. /**
  150. * Returns the user agent that is associated with the
  151. * tree's <code>FOEventHandler</code>.
  152. *
  153. * @return the user agent
  154. */
  155. public FOUserAgent getUserAgent() {
  156. return getFOEventHandler().getUserAgent();
  157. }
  158. /**
  159. * Returns the logger for the node.
  160. *
  161. * @return the logger
  162. */
  163. public Log getLogger() {
  164. return log;
  165. }
  166. /**
  167. * Initialize the node with its name, location information, and attributes
  168. * The attributes must be used immediately as the sax attributes
  169. * will be altered for the next element.
  170. *
  171. * @param elementName element name (e.g., "fo:block")
  172. * @param locator Locator object (ignored by default)
  173. * @param attlist Collection of attributes passed to us from the parser.
  174. * @param pList the property list of the parent node
  175. * @throws FOPException for errors or inconsistencies in the attributes
  176. */
  177. public void processNode(String elementName, Locator locator,
  178. Attributes attlist, PropertyList pList) throws FOPException {
  179. if (log.isDebugEnabled()) {
  180. log.debug("Unhandled element: " + elementName
  181. + (locator != null ? " at " + getLocatorString(locator) : ""));
  182. }
  183. }
  184. /**
  185. * Create a property list for this node. Return null if the node does not
  186. * need a property list.
  187. *
  188. * @param pList the closest parent propertylist.
  189. * @param foEventHandler The FOEventHandler where the PropertyListMaker
  190. * instance can be found.
  191. * @return A new property list.
  192. * @throws FOPException if there's a problem during processing
  193. */
  194. protected PropertyList createPropertyList(
  195. PropertyList pList,
  196. FOEventHandler foEventHandler)
  197. throws FOPException {
  198. return null;
  199. }
  200. /**
  201. * Checks to make sure, during SAX processing of input document, that the
  202. * incoming node is valid for this (parent) node (e.g., checking to
  203. * see that <code>fo:table</code> is not an immediate child of <code>fo:root</code>)
  204. * called from {@link FOTreeBuilder#startElement(String, String, String, Attributes)}
  205. * before constructing the child {@link FObj}.
  206. *
  207. * @param loc location in the FO source file
  208. * @param namespaceURI namespace of incoming node
  209. * @param localName name of the incoming node (without namespace prefix)
  210. * @throws ValidationException if incoming node not valid for parent
  211. */
  212. protected void validateChildNode(
  213. Locator loc,
  214. String namespaceURI,
  215. String localName)
  216. throws ValidationException {
  217. //nop
  218. }
  219. /**
  220. * Static version of {@link FONode#validateChildNode(Locator, String, String)} that
  221. * can be used by subclasses that need to validate children against a different node
  222. * (for example: <code>fo:wrapper</code> needs to check if the incoming node is a
  223. * valid child to its parent)
  224. *
  225. * @param fo the {@link FONode} to validate against
  226. * @param loc location in the source file
  227. * @param namespaceURI namespace of the incoming node
  228. * @param localName name of the incoming node (without namespace prefix)
  229. * @throws ValidationException if the incoming node is not a valid child for the given FO
  230. */
  231. protected static void validateChildNode(
  232. FONode fo,
  233. Locator loc,
  234. String namespaceURI,
  235. String localName)
  236. throws ValidationException {
  237. fo.validateChildNode(loc, namespaceURI, localName);
  238. }
  239. /**
  240. * Adds characters. Does nothing by default. To be overridden in subclasses
  241. * that allow <code>#PCDATA</code> content.
  242. *
  243. * @param data array of characters containing text to be added
  244. * @param start starting array element to add
  245. * @param end ending array element to add
  246. * @param pList currently applicable PropertyList
  247. * @param locator location in the XSL-FO source file.
  248. * @throws FOPException if there's a problem during processing
  249. * @deprecated Please override {@link #characters(char[], int, int, PropertyList, Locator)}
  250. * instead!
  251. */
  252. protected void addCharacters(char[] data, int start, int end,
  253. PropertyList pList,
  254. Locator locator) throws FOPException {
  255. // ignore
  256. }
  257. /**
  258. * Adds characters. Does nothing by default. To be overridden in subclasses
  259. * that allow <code>#PCDATA</code> content.
  260. *
  261. * @param data array of characters containing text to be added
  262. * @param start starting array element to add
  263. * @param length number of elements to add
  264. * @param pList currently applicable PropertyList
  265. * @param locator location in the XSL-FO source file.
  266. * @throws FOPException if there's a problem during processing
  267. */
  268. protected void characters(char[] data, int start, int length,
  269. PropertyList pList,
  270. Locator locator) throws FOPException {
  271. addCharacters(data, start, start + length, pList, locator);
  272. }
  273. /**
  274. * Called after processNode() is called. Subclasses can do additional processing.
  275. *
  276. * @throws FOPException if there's a problem during processing
  277. */
  278. protected void startOfNode() throws FOPException {
  279. // do nothing by default
  280. }
  281. /**
  282. * Primarily used for making final content model validation checks
  283. * and/or informing the {@link FOEventHandler} that the end of this FO
  284. * has been reached.
  285. * The default implementation simply calls {@link #finalizeNode()}, without
  286. * sending any event to the {@link FOEventHandler}.
  287. * <br/><i>Note: the recommended way to override this method in subclasses is</i>
  288. * <br/><br/><code>super.endOfNode(); // invoke finalizeNode()
  289. * <br/>getFOEventHandler().endXXX(); // send endOfNode() notification</code>
  290. *
  291. * @throws FOPException if there's a problem during processing
  292. */
  293. protected void endOfNode() throws FOPException {
  294. this.finalizeNode();
  295. }
  296. /**
  297. * Adds a node as a child of this node. The default implementation of this method
  298. * just ignores any child node being added.
  299. *
  300. * @param child child node to be added to the childNodes of this node
  301. * @throws FOPException if there's a problem during processing
  302. */
  303. protected void addChildNode(FONode child) throws FOPException {
  304. // do nothing by default
  305. }
  306. /**
  307. * Removes a child node. Used by the child nodes to remove themselves, for
  308. * example table-body if it has no children.
  309. *
  310. * @param child child node to be removed
  311. */
  312. public void removeChild(FONode child) {
  313. //nop
  314. }
  315. /**
  316. * Finalize this node.
  317. * This method can be overridden by subclasses to perform finishing
  318. * tasks (cleanup, validation checks, ...) without triggering
  319. * endXXX() events in the {@link FOEventHandler}.
  320. * The method is called by the default {@link #endOfNode()}
  321. * implementation.
  322. *
  323. * @throws FOPException in case there was an error
  324. */
  325. public void finalizeNode() throws FOPException {
  326. // do nothing by default
  327. }
  328. /**
  329. * Return the parent node of this node
  330. *
  331. * @return the parent node of this node
  332. */
  333. public FONode getParent() {
  334. return this.parent;
  335. }
  336. /**
  337. * Return an iterator over all the child nodes of this node.
  338. *
  339. * @return the iterator over the FO's childnodes
  340. */
  341. public FONodeIterator getChildNodes() {
  342. return null;
  343. }
  344. /**
  345. * Return an iterator over the object's child nodes starting
  346. * at the passed node.
  347. *
  348. * @param childNode First node in the iterator
  349. * @return the iterator, or <code>null</code> if
  350. * the given node is not a child of this node.
  351. */
  352. public FONodeIterator getChildNodes(FONode childNode) {
  353. return null;
  354. }
  355. /**
  356. * Return a {@link CharIterator} over all characters in this node
  357. *
  358. * @return an iterator for the characters in this node
  359. */
  360. public CharIterator charIterator() {
  361. return new OneCharIterator(CharUtilities.CODE_EOT);
  362. }
  363. /**
  364. * Helper function to standardize the names of all namespace URI - local
  365. * name pairs in text messages.
  366. * For readability, using fo:, fox:, svg:, for those namespaces even
  367. * though that prefix may not have been chosen in the document.
  368. * @param namespaceURI URI of node found
  369. * (e.g., "http://www.w3.org/1999/XSL/Format")
  370. * @param localName local name of node, (e.g., "root" for "fo:root")
  371. * @return the prefix:localname, if fo/fox/svg, or a longer representation
  372. * with the unabbreviated URI otherwise.
  373. */
  374. public static String getNodeString(String namespaceURI, String localName) {
  375. if (namespaceURI.equals(FOElementMapping.URI)) {
  376. return "fo:" + localName;
  377. } else if (namespaceURI.equals(ExtensionElementMapping.URI)) {
  378. return "fox:" + localName;
  379. } else if (namespaceURI.equals(InternalElementMapping.URI)) {
  380. return "foi:" + localName; // used FOP internally for accessibility
  381. } else if (namespaceURI.equals(SVGElementMapping.URI)) {
  382. return "svg:" + localName;
  383. } else {
  384. return "(Namespace URI: \"" + namespaceURI + "\", "
  385. + "Local Name: \"" + localName + "\")";
  386. }
  387. }
  388. /**
  389. * Returns an instance of the FOValidationEventProducer.
  390. * @return an event producer for FO validation
  391. */
  392. protected FOValidationEventProducer getFOValidationEventProducer() {
  393. return FOValidationEventProducer.Provider.get(
  394. getUserAgent().getEventBroadcaster());
  395. }
  396. /**
  397. * Helper function to standardize "too many" error exceptions
  398. * (e.g., two fo:declarations within fo:root)
  399. * @param loc org.xml.sax.Locator object of the error (*not* parent node)
  400. * @param nsURI namespace URI of incoming invalid node
  401. * @param lName local name (i.e., no prefix) of incoming node
  402. * @throws ValidationException the validation error provoked by the method call
  403. */
  404. protected void tooManyNodesError(Locator loc, String nsURI, String lName)
  405. throws ValidationException {
  406. tooManyNodesError(loc, new QName(nsURI, lName));
  407. }
  408. /**
  409. * Helper function to standardize "too many" error exceptions
  410. * (e.g., two <code>fo:declarations</code> within <code>fo:root</code>)
  411. *
  412. * @param loc org.xml.sax.Locator object of the error (*not* parent node)
  413. * @param offendingNode the qualified name of the offending node
  414. * @throws ValidationException the validation error provoked by the method call
  415. */
  416. protected void tooManyNodesError(Locator loc, QName offendingNode)
  417. throws ValidationException {
  418. getFOValidationEventProducer().tooManyNodes(this, getName(), offendingNode, loc);
  419. }
  420. /**
  421. * Helper function to standardize "too many" error exceptions
  422. * (e.g., two fo:declarations within fo:root)
  423. * This overloaded method helps make the caller code better self-documenting
  424. * @param loc org.xml.sax.Locator object of the error (*not* parent node)
  425. * @param offendingNode incoming node that would cause a duplication.
  426. * @throws ValidationException the validation error provoked by the method call
  427. */
  428. protected void tooManyNodesError(Locator loc, String offendingNode)
  429. throws ValidationException {
  430. tooManyNodesError(loc, new QName(FO_URI, offendingNode));
  431. }
  432. /**
  433. * Helper function to standardize "out of order" exceptions
  434. * (e.g., <code>fo:layout-master-set</code> appearing after <code>fo:page-sequence</code>)
  435. *
  436. * @param loc org.xml.sax.Locator object of the error (*not* parent node)
  437. * @param tooLateNode string name of node that should be earlier in document
  438. * @param tooEarlyNode string name of node that should be later in document
  439. * @throws ValidationException the validation error provoked by the method call
  440. */
  441. protected void nodesOutOfOrderError(Locator loc, String tooLateNode,
  442. String tooEarlyNode) throws ValidationException {
  443. nodesOutOfOrderError(loc, tooLateNode, tooEarlyNode, false);
  444. }
  445. /**
  446. * Helper function to standardize "out of order" exceptions
  447. * (e.g., fo:layout-master-set appearing after fo:page-sequence)
  448. * @param loc org.xml.sax.Locator object of the error (*not* parent node)
  449. * @param tooLateNode string name of node that should be earlier in document
  450. * @param tooEarlyNode string name of node that should be later in document
  451. * @param canRecover indicates whether FOP can recover from this problem and continue working
  452. * @throws ValidationException the validation error provoked by the method call
  453. */
  454. protected void nodesOutOfOrderError(Locator loc, String tooLateNode,
  455. String tooEarlyNode, boolean canRecover) throws ValidationException {
  456. getFOValidationEventProducer().nodeOutOfOrder(this, getName(),
  457. tooLateNode, tooEarlyNode, canRecover, loc);
  458. }
  459. /**
  460. * Helper function to return "invalid child" exceptions
  461. * (e.g., <code>fo:block</code> appearing immediately under <code>fo:root</code>)
  462. *
  463. * @param loc org.xml.sax.Locator object of the error (*not* parent node)
  464. * @param nsURI namespace URI of incoming invalid node
  465. * @param lName local name (i.e., no prefix) of incoming node
  466. * @throws ValidationException the validation error provoked by the method call
  467. */
  468. protected void invalidChildError(Locator loc, String nsURI, String lName)
  469. throws ValidationException {
  470. invalidChildError(loc, getName(), nsURI, lName, null);
  471. }
  472. /**
  473. * Helper function to return "invalid child" exceptions with more
  474. * complex validation rules (i.e., needing more explanation of the problem)
  475. *
  476. * @param loc org.xml.sax.Locator object of the error (*not* parent node)
  477. * @param parentName the name of the parent element
  478. * @param nsURI namespace URI of incoming invalid node
  479. * @param lName local name (i.e., no prefix) of incoming node
  480. * @param ruleViolated name of the rule violated (used to lookup a resource in a bundle)
  481. * @throws ValidationException the validation error provoked by the method call
  482. */
  483. protected void invalidChildError(Locator loc, String parentName, String nsURI, String lName,
  484. String ruleViolated)
  485. throws ValidationException {
  486. getFOValidationEventProducer().invalidChild(this, parentName,
  487. new QName(nsURI, lName), ruleViolated, loc);
  488. }
  489. /**
  490. * Helper function to throw an error caused by missing mandatory child elements.
  491. * (e.g., <code>fo:layout-master-set</code> not having any <code>fo:page-master</code>
  492. * child element.
  493. *
  494. * @param contentModel The XSL Content Model for the fo: object or a similar description
  495. * indicating the necessary child elements.
  496. * @throws ValidationException the validation error provoked by the method call
  497. */
  498. protected void missingChildElementError(String contentModel)
  499. throws ValidationException {
  500. getFOValidationEventProducer().missingChildElement(this, getName(),
  501. contentModel, false, locator);
  502. }
  503. /**
  504. * Helper function to throw an error caused by missing mandatory child elements.
  505. * E.g., fo:layout-master-set not having any page-master child element.
  506. * @param contentModel The XSL Content Model for the fo: object or a similar description
  507. * indicating the necessary child elements.
  508. * @param canRecover indicates whether FOP can recover from this problem and continue working
  509. * @throws ValidationException the validation error provoked by the method call
  510. */
  511. protected void missingChildElementError(String contentModel, boolean canRecover)
  512. throws ValidationException {
  513. getFOValidationEventProducer().missingChildElement(this, getName(),
  514. contentModel, canRecover, locator);
  515. }
  516. /**
  517. * Helper function to throw an error caused by missing mandatory properties
  518. *
  519. * @param propertyName the name of the missing property.
  520. * @throws ValidationException the validation error provoked by the method call
  521. */
  522. protected void missingPropertyError(String propertyName)
  523. throws ValidationException {
  524. getFOValidationEventProducer().missingProperty(this, getName(), propertyName, locator);
  525. }
  526. /**
  527. * Helper function to return "Error(line#/column#)" string for
  528. * above exception messages
  529. *
  530. * @param loc org.xml.sax.Locator object
  531. * @return String opening error text
  532. */
  533. protected static String errorText(Locator loc) {
  534. return "Error(" + getLocatorString(loc) + "): ";
  535. }
  536. /**
  537. * Helper function to return "Warning(line#/column#)" string for
  538. * warning messages
  539. *
  540. * @param loc org.xml.sax.Locator object
  541. * @return String opening warning text
  542. */
  543. protected static String warningText(Locator loc) {
  544. return "Warning(" + getLocatorString(loc) + "): ";
  545. }
  546. /**
  547. * Helper function to format a Locator instance.
  548. *
  549. * @param loc org.xml.sax.Locator object
  550. * @return String the formatted text
  551. */
  552. public static String getLocatorString(Locator loc) {
  553. if (loc == null) {
  554. return "Unknown location";
  555. } else {
  556. return loc.getLineNumber() + "/" + loc.getColumnNumber();
  557. }
  558. }
  559. /**
  560. * Decorates a log or warning message with context information on the given node.
  561. *
  562. * @param text the original message
  563. * @param node the context node
  564. * @return the decorated text
  565. */
  566. public static String decorateWithContextInfo(String text, FONode node) {
  567. if (node != null) {
  568. StringBuffer sb = new StringBuffer(text);
  569. sb.append(" (").append(node.getContextInfo()).append(")");
  570. return sb.toString();
  571. } else {
  572. return text;
  573. }
  574. }
  575. /**
  576. * Returns a String containing as much context information as possible about a node. Call
  577. * this method only in exceptional conditions because this method may perform quite extensive
  578. * information gathering inside the FO tree.
  579. * @return a String containing context information
  580. */
  581. // [GA] remove deprecation - no alternative specified
  582. // @deprecated Not localized! Should rename getContextInfoAlt() to getContextInfo() when done!
  583. public String getContextInfo() {
  584. StringBuffer sb = new StringBuffer();
  585. if (getLocalName() != null) {
  586. sb.append(getName());
  587. sb.append(", ");
  588. }
  589. if (this.locator != null) {
  590. sb.append("location: ");
  591. sb.append(getLocatorString(this.locator));
  592. } else {
  593. String s = gatherContextInfo();
  594. if (s != null) {
  595. sb.append("\"");
  596. sb.append(s);
  597. sb.append("\"");
  598. } else {
  599. sb.append("no context info available");
  600. }
  601. }
  602. if (sb.length() > 80) {
  603. sb.setLength(80);
  604. }
  605. return sb.toString();
  606. }
  607. /**
  608. * Returns a String containing as some context information about a node. It does not take the
  609. * locator into consideration and returns null if no useful context information can be found.
  610. * Call this method only in exceptional conditions because this method may perform quite
  611. * extensive information gathering inside the FO tree. All text returned by this method that
  612. * is not extracted from document content needs to be locale-independent.
  613. * @return a String containing context information
  614. */
  615. protected String getContextInfoAlt() {
  616. String s = gatherContextInfo();
  617. if (s != null) {
  618. StringBuffer sb = new StringBuffer();
  619. if (getLocalName() != null) {
  620. sb.append(getName());
  621. sb.append(", ");
  622. }
  623. sb.append("\"");
  624. sb.append(s);
  625. sb.append("\"");
  626. return sb.toString();
  627. } else {
  628. return null;
  629. }
  630. }
  631. /** Function for AdvancedMessageFormat to retrieve context info from an FONode. */
  632. public static class GatherContextInfoFunction implements Function {
  633. /** {@inheritDoc} */
  634. public Object evaluate(Map params) {
  635. Object obj = params.get("source");
  636. if (obj instanceof PropertyList) {
  637. PropertyList propList = (PropertyList)obj;
  638. obj = propList.getFObj();
  639. }
  640. if (obj instanceof FONode) {
  641. FONode node = (FONode)obj;
  642. return node.getContextInfoAlt();
  643. }
  644. return null;
  645. }
  646. /** {@inheritDoc} */
  647. public Object getName() {
  648. return "gatherContextInfo";
  649. }
  650. }
  651. /**
  652. * Gathers context information for the getContextInfo() method.
  653. * @return the collected context information or null, if none is available
  654. */
  655. protected String gatherContextInfo() {
  656. return null;
  657. }
  658. /**
  659. * Returns the root node of this tree
  660. *
  661. * @return the root node
  662. */
  663. public Root getRoot() {
  664. return parent.getRoot();
  665. }
  666. /**
  667. * Returns the fully qualified name of the node
  668. *
  669. * @return the fully qualified name of this node
  670. */
  671. public String getName() {
  672. return getName(getNormalNamespacePrefix());
  673. }
  674. /**
  675. * Returns the fully qualified name of the node
  676. *
  677. * @param prefix the namespace prefix to build the name with (may be null)
  678. * @return the fully qualified name of this node
  679. */
  680. public String getName(String prefix) {
  681. if (prefix != null) {
  682. StringBuffer sb = new StringBuffer();
  683. sb.append(prefix).append(':').append(getLocalName());
  684. return sb.toString();
  685. } else {
  686. return getLocalName();
  687. }
  688. }
  689. /**
  690. * Returns the local name (i.e. without namespace prefix) of the node
  691. *
  692. * @return the local name of this node
  693. */
  694. public abstract String getLocalName();
  695. /**
  696. * Returns the normally used namespace prefix for this node
  697. *
  698. * @return the normally used namespace prefix for this kind of node (ex. "fo" for XSL-FO)
  699. */
  700. public abstract String getNormalNamespacePrefix();
  701. /**
  702. * Returns the namespace URI for this node
  703. *
  704. * @return the namespace URI for this node
  705. */
  706. public String getNamespaceURI() {
  707. return null;
  708. }
  709. /**
  710. * Returns the {@link Constants} class integer value of this node
  711. *
  712. * @return the integer enumeration of this FO (e.g. {@link Constants#FO_ROOT})
  713. * if a formatting object, {@link Constants#FO_UNKNOWN_NODE} otherwise
  714. */
  715. public int getNameId() {
  716. return Constants.FO_UNKNOWN_NODE;
  717. }
  718. /**
  719. * This method is overridden by extension elements and allows the extension element
  720. * to return a pass-through attachment which the parent formatting objects should simply
  721. * carry with them but otherwise ignore. This mechanism is used to pass non-standard
  722. * information from the FO tree through to the layout engine and the renderers.
  723. *
  724. * @return the extension attachment if one is created by the extension element, null otherwise.
  725. */
  726. public ExtensionAttachment getExtensionAttachment() {
  727. return null;
  728. }
  729. /**
  730. * This method is overridden by extension elements and allows the extension element to return
  731. * a {@link ContentHandlerFactory}. This factory can create ContentHandler implementations that
  732. * handle foreign XML content by either building up a specific DOM, a Java object or something
  733. * else.
  734. *
  735. * @return the <code>ContentHandlerFactory</code> or <code>null</code> if not applicable
  736. */
  737. public ContentHandlerFactory getContentHandlerFactory() {
  738. return null;
  739. }
  740. /**
  741. * Returns <code>true</code> if <code>fo:marker</code> is allowed as
  742. * a child node.
  743. * <br>To be overridden <i>only</i> in extension nodes that need it.
  744. *
  745. * @return true if markers are valid children
  746. */
  747. protected boolean canHaveMarkers() {
  748. int foId = getNameId();
  749. switch (foId) {
  750. case Constants.FO_BASIC_LINK:
  751. case Constants.FO_BIDI_OVERRIDE:
  752. case Constants.FO_BLOCK:
  753. case Constants.FO_BLOCK_CONTAINER:
  754. case Constants.FO_FLOW:
  755. case Constants.FO_INLINE:
  756. case Constants.FO_INLINE_CONTAINER:
  757. case Constants.FO_LIST_BLOCK:
  758. case Constants.FO_LIST_ITEM:
  759. case Constants.FO_LIST_ITEM_BODY:
  760. case Constants.FO_LIST_ITEM_LABEL:
  761. case Constants.FO_TABLE:
  762. case Constants.FO_TABLE_BODY:
  763. case Constants.FO_TABLE_HEADER:
  764. case Constants.FO_TABLE_FOOTER:
  765. case Constants.FO_TABLE_CELL:
  766. case Constants.FO_TABLE_AND_CAPTION:
  767. case Constants.FO_TABLE_CAPTION:
  768. case Constants.FO_WRAPPER:
  769. return true;
  770. default:
  771. return false;
  772. }
  773. }
  774. /**
  775. * This method is used when adding child nodes to a FO that already
  776. * contains at least one child. In this case, the new child becomes a
  777. * sibling to the previous one
  778. *
  779. * @param precedingSibling the previous child
  780. * @param followingSibling the new child
  781. */
  782. protected static void attachSiblings(FONode precedingSibling,
  783. FONode followingSibling) {
  784. if (precedingSibling.siblings == null) {
  785. precedingSibling.siblings = new FONode[2];
  786. }
  787. if (followingSibling.siblings == null) {
  788. followingSibling.siblings = new FONode[2];
  789. }
  790. precedingSibling.siblings[1] = followingSibling;
  791. followingSibling.siblings[0] = precedingSibling;
  792. }
  793. /**
  794. * Base iterator interface over a FO's children
  795. */
  796. public interface FONodeIterator extends ListIterator {
  797. /**
  798. * Returns the parent node for this iterator's list
  799. * of child nodes
  800. *
  801. * @return the parent node
  802. */
  803. FObj parentNode();
  804. /**
  805. * Convenience method with return type of FONode
  806. * (semantically equivalent to: <code>(FONode) next();</code>)
  807. *
  808. * @return the next node (if any), as a type FONode
  809. */
  810. FONode nextNode();
  811. /**
  812. * Convenience method with return type of FONode
  813. * (semantically equivalent to: <code>(FONode) previous();</code>)
  814. *
  815. * @return the previous node (if any), as a type FONode
  816. */
  817. FONode previousNode();
  818. /**
  819. * Returns the first node in the list, and decreases the index,
  820. * so that a subsequent call to <code>hasPrevious()</code> will
  821. * return <code>false</code>
  822. *
  823. * @return the first node in the list
  824. */
  825. FONode firstNode();
  826. /**
  827. * Returns the last node in the list, and advances the
  828. * current position, so that a subsequent call to <code>hasNext()</code>
  829. * will return <code>false</code>
  830. *
  831. * @return the last node in the list
  832. */
  833. FONode lastNode();
  834. }
  835. public void setPtr(String ptr) {
  836. throw new UnsupportedOperationException();
  837. }
  838. }