Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FONode.java 33KB

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