Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FObj.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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 java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import java.util.NoSuchElementException;
  26. import java.util.Set;
  27. import org.xml.sax.Attributes;
  28. import org.xml.sax.Locator;
  29. import org.apache.xmlgraphics.util.QName;
  30. import org.apache.fop.apps.FOPException;
  31. import org.apache.fop.fo.extensions.ExtensionAttachment;
  32. import org.apache.fop.fo.flow.ChangeBar;
  33. import org.apache.fop.fo.flow.Marker;
  34. import org.apache.fop.fo.pagination.PageSequence;
  35. import org.apache.fop.fo.properties.Property;
  36. import org.apache.fop.fo.properties.PropertyMaker;
  37. /**
  38. * Base class for representation of formatting objects and their processing.
  39. * All standard formatting object classes extend this class.
  40. */
  41. public abstract class FObj extends FONode implements Constants {
  42. /** the list of property makers */
  43. private static final PropertyMaker[] PROPERTY_LIST_TABLE
  44. = FOPropertyMapping.getGenericMappings();
  45. /** pointer to the descendant subtree */
  46. protected FONode firstChild;
  47. /** pointer to the end of the descendant subtree */
  48. protected FONode lastChild;
  49. /** The list of extension attachments, null if none */
  50. private List<ExtensionAttachment> extensionAttachments;
  51. /** The map of foreign attributes, null if none */
  52. private Map<QName, String> foreignAttributes;
  53. /** Used to indicate if this FO is either an Out Of Line FO (see rec)
  54. * or a descendant of one. Used during FO validation.
  55. */
  56. private boolean isOutOfLineFODescendant;
  57. /** Markers added to this element. */
  58. private Map<String, Marker> markers;
  59. private int bidiLevel = -1;
  60. // The value of properties relevant for all fo objects
  61. private String id;
  62. private String layer;
  63. // End of property values
  64. /**
  65. * Create a new formatting object.
  66. *
  67. * @param parent the parent node
  68. */
  69. public FObj(FONode parent) {
  70. super(parent);
  71. // determine if isOutOfLineFODescendant should be set
  72. if (parent != null && parent instanceof FObj) {
  73. if (((FObj) parent).getIsOutOfLineFODescendant()) {
  74. isOutOfLineFODescendant = true;
  75. } else {
  76. int foID = getNameId();
  77. if (foID == FO_FLOAT || foID == FO_FOOTNOTE
  78. || foID == FO_FOOTNOTE_BODY) {
  79. isOutOfLineFODescendant = true;
  80. }
  81. }
  82. }
  83. }
  84. /** {@inheritDoc} */
  85. public FONode clone(FONode parent, boolean removeChildren)
  86. throws FOPException {
  87. FObj fobj = (FObj) super.clone(parent, removeChildren);
  88. if (removeChildren) {
  89. fobj.firstChild = null;
  90. }
  91. return fobj;
  92. }
  93. /**
  94. * Returns the PropertyMaker for a given property ID.
  95. * @param propId the property ID
  96. * @return the requested Property Maker
  97. */
  98. public static PropertyMaker getPropertyMakerFor(int propId) {
  99. return PROPERTY_LIST_TABLE[propId];
  100. }
  101. /** {@inheritDoc} */
  102. public void processNode(String elementName, Locator locator,
  103. Attributes attlist, PropertyList pList)
  104. throws FOPException {
  105. setLocator(locator);
  106. pList.addAttributesToList(attlist);
  107. if (!inMarker() || "marker".equals(elementName)) {
  108. bind(pList);
  109. }
  110. warnOnUnknownProperties(attlist, elementName, pList);
  111. }
  112. private void warnOnUnknownProperties(Attributes attlist, String objName, PropertyList propertyList)
  113. throws FOPException {
  114. Map<String, Property> unknowns = propertyList.getUnknownPropertyValues();
  115. for (Entry<String, Property> entry : unknowns.entrySet()) {
  116. FOValidationEventProducer producer = FOValidationEventProducer.Provider.get(getUserAgent()
  117. .getEventBroadcaster());
  118. producer.warnOnInvalidPropertyValue(this, objName,
  119. getAttributeNameForValue(attlist, entry.getValue(), propertyList), entry.getKey(), null,
  120. getLocator());
  121. }
  122. }
  123. private String getAttributeNameForValue(Attributes attList, Property value, PropertyList propertyList)
  124. throws FOPException {
  125. for (int i = 0; i < attList.getLength(); i++) {
  126. String attributeName = attList.getQName(i);
  127. String attributeValue = attList.getValue(i);
  128. Property prop = propertyList.getPropertyForAttribute(attList, attributeName, attributeValue);
  129. if (prop != null && prop.equals(value)) {
  130. return attributeName;
  131. }
  132. }
  133. return "unknown";
  134. }
  135. /**
  136. * Create a default property list for this element.
  137. * {@inheritDoc}
  138. */
  139. protected PropertyList createPropertyList(PropertyList parent,
  140. FOEventHandler foEventHandler) throws FOPException {
  141. return getBuilderContext().getPropertyListMaker().make(this, parent);
  142. }
  143. /**
  144. * Bind property values from the property list to the FO node.
  145. * Must be overridden in all FObj subclasses that have properties
  146. * applying to it.
  147. * @param pList the PropertyList where the properties can be found.
  148. * @throws FOPException if there is a problem binding the values
  149. */
  150. public void bind(PropertyList pList) throws FOPException {
  151. id = pList.get(PR_ID).getString();
  152. layer = pList.get(PR_X_LAYER).getString();
  153. }
  154. /**
  155. * {@inheritDoc}
  156. * @throws FOPException FOP Exception
  157. */
  158. public void startOfNode() throws FOPException {
  159. if (id != null) {
  160. checkId(id);
  161. }
  162. PageSequence pageSequence = getRoot().getLastPageSequence();
  163. if (pageSequence != null && pageSequence.hasChangeBars()) {
  164. startOfNodeChangeBarList = pageSequence.getClonedChangeBarList();
  165. }
  166. }
  167. /**
  168. * {@inheritDoc}
  169. * @throws FOPException FOP Exception
  170. */
  171. public void endOfNode() throws FOPException {
  172. List<ChangeBar> endOfNodeChangeBarList = null;
  173. PageSequence pageSequence = getRoot().getLastPageSequence();
  174. if (pageSequence != null) {
  175. endOfNodeChangeBarList = pageSequence.getClonedChangeBarList();
  176. }
  177. if (startOfNodeChangeBarList != null && endOfNodeChangeBarList != null) {
  178. nodeChangeBarList = new LinkedList<ChangeBar>(endOfNodeChangeBarList);
  179. nodeChangeBarList.retainAll(startOfNodeChangeBarList);
  180. if (nodeChangeBarList.isEmpty()) {
  181. nodeChangeBarList = null;
  182. }
  183. startOfNodeChangeBarList = null;
  184. }
  185. super.endOfNode();
  186. }
  187. /**
  188. * Setup the id for this formatting object.
  189. * Most formatting objects can have an id that can be referenced.
  190. * This methods checks that the id isn't already used by another FO
  191. *
  192. * @param id the id to check
  193. * @throws ValidationException if the ID is already defined elsewhere
  194. * (strict validation only)
  195. */
  196. private void checkId(String id) throws ValidationException {
  197. if (!inMarker() && !id.equals("")) {
  198. Set<String> idrefs = getBuilderContext().getIDReferences();
  199. if (!idrefs.contains(id)) {
  200. idrefs.add(id);
  201. } else {
  202. getFOValidationEventProducer().idNotUnique(this, getName(), id, true, locator);
  203. }
  204. }
  205. }
  206. /**
  207. * Returns Out Of Line FO Descendant indicator.
  208. * @return true if Out of Line FO or Out Of Line descendant, false otherwise
  209. */
  210. boolean getIsOutOfLineFODescendant() {
  211. return isOutOfLineFODescendant;
  212. }
  213. /** {@inheritDoc}*/
  214. protected void addChildNode(FONode child) throws FOPException {
  215. if (child.getNameId() == FO_MARKER) {
  216. addMarker((Marker) child);
  217. } else {
  218. ExtensionAttachment attachment = child.getExtensionAttachment();
  219. if (attachment != null) {
  220. /* This removes the element from the normal children,
  221. * so no layout manager is being created for them
  222. * as they are only additional information.
  223. */
  224. addExtensionAttachment(attachment);
  225. } else {
  226. if (firstChild == null) {
  227. firstChild = child;
  228. lastChild = child;
  229. } else {
  230. if (lastChild == null) {
  231. FONode prevChild = firstChild;
  232. while (prevChild.siblings != null
  233. && prevChild.siblings[1] != null) {
  234. prevChild = prevChild.siblings[1];
  235. }
  236. FONode.attachSiblings(prevChild, child);
  237. } else {
  238. FONode.attachSiblings(lastChild, child);
  239. lastChild = child;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /**
  246. * Used by RetrieveMarker during Marker-subtree cloning
  247. * @param child the (cloned) child node
  248. * @param parent the (cloned) parent node
  249. * @throws FOPException when the child could not be added to the parent
  250. */
  251. protected static void addChildTo(FONode child, FONode parent)
  252. throws FOPException {
  253. parent.addChildNode(child);
  254. }
  255. /** {@inheritDoc} */
  256. public void removeChild(FONode child) {
  257. FONode nextChild = null;
  258. if (child.siblings != null) {
  259. nextChild = child.siblings[1];
  260. }
  261. if (child == firstChild) {
  262. firstChild = nextChild;
  263. if (firstChild != null) {
  264. firstChild.siblings[0] = null;
  265. }
  266. } else if (child.siblings != null) {
  267. FONode prevChild = child.siblings[0];
  268. prevChild.siblings[1] = nextChild;
  269. if (nextChild != null) {
  270. nextChild.siblings[0] = prevChild;
  271. }
  272. }
  273. if (child == lastChild) {
  274. if (child.siblings != null) {
  275. lastChild = siblings[0];
  276. } else {
  277. lastChild = null;
  278. }
  279. }
  280. }
  281. /**
  282. * Find the nearest parent, grandparent, etc. FONode that is also an FObj
  283. * @return FObj the nearest ancestor FONode that is an FObj
  284. */
  285. public FObj findNearestAncestorFObj() {
  286. FONode par = parent;
  287. while (par != null && !(par instanceof FObj)) {
  288. par = par.parent;
  289. }
  290. return (FObj) par;
  291. }
  292. /**
  293. * Check if this formatting object generates reference areas.
  294. * @return true if generates reference areas
  295. * TODO see if needed
  296. */
  297. public boolean generatesReferenceAreas() {
  298. return false;
  299. }
  300. /** {@inheritDoc} */
  301. public FONodeIterator getChildNodes() {
  302. if (hasChildren()) {
  303. return new FObjIterator(this);
  304. }
  305. return null;
  306. }
  307. /**
  308. * Indicates whether this formatting object has children.
  309. * @return true if there are children
  310. */
  311. public boolean hasChildren() {
  312. return this.firstChild != null;
  313. }
  314. /**
  315. * Return an iterator over the object's childNodes starting
  316. * at the passed-in node (= first call to iterator.next() will
  317. * return childNode)
  318. * @param childNode First node in the iterator
  319. * @return A FONodeIterator or null if childNode isn't a child of
  320. * this FObj.
  321. */
  322. public FONodeIterator getChildNodes(FONode childNode) {
  323. FONodeIterator it = getChildNodes();
  324. if (it != null) {
  325. if (firstChild == childNode) {
  326. return it;
  327. } else {
  328. while (it.hasNext()
  329. && it.next().siblings[1] != childNode) {
  330. //nop
  331. }
  332. if (it.hasNext()) {
  333. return it;
  334. } else {
  335. return null;
  336. }
  337. }
  338. }
  339. return null;
  340. }
  341. /**
  342. * Notifies a FObj that one of it's children is removed.
  343. * This method is subclassed by Block to clear the
  344. * firstInlineChild variable in case it doesn't generate
  345. * any areas (see addMarker()).
  346. * @param node the node that was removed
  347. */
  348. void notifyChildRemoval(FONode node) {
  349. //nop
  350. }
  351. /**
  352. * Add the marker to this formatting object.
  353. * If this object can contain markers it checks that the marker
  354. * has a unique class-name for this object and that it is
  355. * the first child.
  356. * @param marker Marker to add.
  357. */
  358. protected void addMarker(Marker marker) {
  359. String mcname = marker.getMarkerClassName();
  360. if (firstChild != null) {
  361. // check for empty childNodes
  362. for (FONodeIterator iter = getChildNodes(); iter.hasNext();) {
  363. FONode node = iter.next();
  364. if (node instanceof FObj
  365. || (node instanceof FOText
  366. && ((FOText) node).willCreateArea())) {
  367. getFOValidationEventProducer().markerNotInitialChild(this, getName(),
  368. mcname, locator);
  369. return;
  370. } else if (node instanceof FOText) {
  371. iter.remove();
  372. notifyChildRemoval(node);
  373. }
  374. }
  375. }
  376. if (markers == null) {
  377. markers = new HashMap<String, Marker>();
  378. }
  379. if (!markers.containsKey(mcname)) {
  380. markers.put(mcname, marker);
  381. } else {
  382. getFOValidationEventProducer().markerNotUniqueForSameParent(this, getName(),
  383. mcname, locator);
  384. }
  385. }
  386. /**
  387. * @return true if there are any Markers attached to this object
  388. */
  389. public boolean hasMarkers() {
  390. return markers != null && !markers.isEmpty();
  391. }
  392. /**
  393. * @return the collection of Markers attached to this object
  394. */
  395. public Map<String, Marker> getMarkers() {
  396. return markers;
  397. }
  398. /** {@inheritDoc} */
  399. protected String getContextInfoAlt() {
  400. StringBuilder sb = new StringBuilder();
  401. if (getLocalName() != null) {
  402. sb.append(getName());
  403. sb.append(", ");
  404. }
  405. if (hasId()) {
  406. sb.append("id=").append(getId());
  407. return sb.toString();
  408. }
  409. String s = gatherContextInfo();
  410. if (s != null) {
  411. sb.append("\"");
  412. if (s.length() < 32) {
  413. sb.append(s);
  414. } else {
  415. sb.append(s.substring(0, 32));
  416. sb.append("...");
  417. }
  418. sb.append("\"");
  419. return sb.toString();
  420. } else {
  421. return null;
  422. }
  423. }
  424. /** {@inheritDoc} */
  425. protected String gatherContextInfo() {
  426. if (getLocator() != null) {
  427. return super.gatherContextInfo();
  428. } else {
  429. FONodeIterator iter = getChildNodes();
  430. if (iter == null) {
  431. return null;
  432. }
  433. StringBuilder sb = new StringBuilder();
  434. while (iter.hasNext()) {
  435. FONode node = iter.next();
  436. String s = node.gatherContextInfo();
  437. if (s != null) {
  438. if (sb.length() > 0) {
  439. sb.append(", ");
  440. }
  441. sb.append(s);
  442. }
  443. }
  444. return (sb.length() > 0 ? sb.toString() : null);
  445. }
  446. }
  447. /**
  448. * Convenience method for validity checking. Checks if the
  449. * incoming node is a member of the "%block;" parameter entity
  450. * as defined in Sect. 6.2 of the XSL 1.0 &amp; 1.1 Recommendations
  451. *
  452. * @param nsURI namespace URI of incoming node
  453. * @param lName local name (i.e., no prefix) of incoming node
  454. * @return true if a member, false if not
  455. */
  456. protected boolean isBlockItem(String nsURI, String lName) {
  457. return (FO_URI.equals(nsURI)
  458. && ("block".equals(lName)
  459. || "table".equals(lName)
  460. || "table-and-caption".equals(lName)
  461. || "block-container".equals(lName)
  462. || "list-block".equals(lName)
  463. || "float".equals(lName)
  464. || isNeutralItem(nsURI, lName)));
  465. }
  466. /**
  467. * Convenience method for validity checking. Checks if the
  468. * incoming node is a member of the "%inline;" parameter entity
  469. * as defined in Sect. 6.2 of the XSL 1.0 &amp; 1.1 Recommendations
  470. *
  471. * @param nsURI namespace URI of incoming node
  472. * @param lName local name (i.e., no prefix) of incoming node
  473. * @return true if a member, false if not
  474. */
  475. protected boolean isInlineItem(String nsURI, String lName) {
  476. return (FO_URI.equals(nsURI)
  477. && ("bidi-override".equals(lName)
  478. || "change-bar-begin".equals(lName)
  479. || "change-bar-end".equals(lName)
  480. || "character".equals(lName)
  481. || "external-graphic".equals(lName)
  482. || "instream-foreign-object".equals(lName)
  483. || "inline".equals(lName)
  484. || "inline-container".equals(lName)
  485. || "leader".equals(lName)
  486. || "page-number".equals(lName)
  487. || "page-number-citation".equals(lName)
  488. || "page-number-citation-last".equals(lName)
  489. || "basic-link".equals(lName)
  490. || ("multi-toggle".equals(lName)
  491. && (getNameId() == FO_MULTI_CASE
  492. || findAncestor(FO_MULTI_CASE) > 0))
  493. || ("footnote".equals(lName)
  494. && !isOutOfLineFODescendant)
  495. || isNeutralItem(nsURI, lName)));
  496. }
  497. /**
  498. * Convenience method for validity checking. Checks if the
  499. * incoming node is a member of the "%block;" parameter entity
  500. * or "%inline;" parameter entity
  501. * @param nsURI namespace URI of incoming node
  502. * @param lName local name (i.e., no prefix) of incoming node
  503. * @return true if a member, false if not
  504. */
  505. protected boolean isBlockOrInlineItem(String nsURI, String lName) {
  506. return (isBlockItem(nsURI, lName) || isInlineItem(nsURI, lName));
  507. }
  508. /**
  509. * Convenience method for validity checking. Checks if the
  510. * incoming node is a member of the neutral item list
  511. * as defined in Sect. 6.2 of the XSL 1.0 &amp; 1.1 Recommendations
  512. * @param nsURI namespace URI of incoming node
  513. * @param lName local name (i.e., no prefix) of incoming node
  514. * @return true if a member, false if not
  515. */
  516. protected boolean isNeutralItem(String nsURI, String lName) {
  517. return (FO_URI.equals(nsURI)
  518. && ("multi-switch".equals(lName)
  519. || "multi-properties".equals(lName)
  520. || "wrapper".equals(lName)
  521. || (!isOutOfLineFODescendant && "float".equals(lName))
  522. || "retrieve-marker".equals(lName)
  523. || "retrieve-table-marker".equals(lName)));
  524. }
  525. /**
  526. * Convenience method for validity checking. Checks if the
  527. * current node has an ancestor of a given name.
  528. * @param ancestorID ID of node name to check for (e.g., FO_ROOT)
  529. * @return number of levels above FO where ancestor exists,
  530. * -1 if not found
  531. */
  532. protected int findAncestor(int ancestorID) {
  533. int found = 1;
  534. FONode temp = getParent();
  535. while (temp != null) {
  536. if (temp.getNameId() == ancestorID) {
  537. return found;
  538. }
  539. found += 1;
  540. temp = temp.getParent();
  541. }
  542. return -1;
  543. }
  544. /**
  545. * Clears the list of child nodes.
  546. */
  547. public void clearChildNodes() {
  548. this.firstChild = null;
  549. }
  550. /** @return the "id" property. */
  551. public String getId() {
  552. return id;
  553. }
  554. /** @return whether this object has an id set */
  555. public boolean hasId() {
  556. return (id != null && id.length() > 0);
  557. }
  558. /** @return the "layer" property. */
  559. public String getLayer() {
  560. return layer;
  561. }
  562. /** @return whether this object has an layer set */
  563. public boolean hasLayer() {
  564. return (layer != null && layer.length() > 0);
  565. }
  566. /** {@inheritDoc} */
  567. public String getNamespaceURI() {
  568. return FOElementMapping.URI;
  569. }
  570. /** {@inheritDoc} */
  571. public String getNormalNamespacePrefix() {
  572. return "fo";
  573. }
  574. /** {@inheritDoc} */
  575. public boolean isBidiRangeBlockItem() {
  576. String ns = getNamespaceURI();
  577. String ln = getLocalName();
  578. return !isNeutralItem(ns, ln) && isBlockItem(ns, ln);
  579. }
  580. /**
  581. * Recursively set resolved bidirectional level of FO (and its ancestors) if
  582. * and only if it is non-negative and if either the current value is reset (-1)
  583. * or the new value is less than the current value.
  584. * @param bidiLevel a non-negative bidi embedding level
  585. */
  586. public void setBidiLevel(int bidiLevel) {
  587. assert bidiLevel >= 0;
  588. if ((this.bidiLevel < 0) || (bidiLevel < this.bidiLevel)) {
  589. this.bidiLevel = bidiLevel;
  590. if ((parent != null) && !isBidiPropagationBoundary()) {
  591. FObj foParent = (FObj) parent;
  592. int parentBidiLevel = foParent.getBidiLevel();
  593. if ((parentBidiLevel < 0) || (bidiLevel < parentBidiLevel)) {
  594. foParent.setBidiLevel(bidiLevel);
  595. }
  596. }
  597. }
  598. }
  599. /**
  600. * Obtain resolved bidirectional level of FO.
  601. * @return either a non-negative bidi embedding level or -1
  602. * in case no bidi levels have been assigned
  603. */
  604. public int getBidiLevel() {
  605. return bidiLevel;
  606. }
  607. /**
  608. * Obtain resolved bidirectional level of FO or nearest FO
  609. * ancestor that has a resolved level.
  610. * @return either a non-negative bidi embedding level or -1
  611. * in case no bidi levels have been assigned to this FO or
  612. * any ancestor
  613. */
  614. public int getBidiLevelRecursive() {
  615. for (FONode fn = this; fn != null; fn = fn.getParent()) {
  616. if (fn instanceof FObj) {
  617. int level = ((FObj) fn).getBidiLevel();
  618. if (level >= 0) {
  619. return level;
  620. }
  621. }
  622. if (isBidiInheritanceBoundary()) {
  623. break;
  624. }
  625. }
  626. return -1;
  627. }
  628. protected boolean isBidiBoundary(boolean propagate) {
  629. return false;
  630. }
  631. private boolean isBidiInheritanceBoundary() {
  632. return isBidiBoundary(false);
  633. }
  634. private boolean isBidiPropagationBoundary() {
  635. return isBidiBoundary(true);
  636. }
  637. /**
  638. * Add a new extension attachment to this FObj.
  639. * (see org.apache.fop.fo.FONode for details)
  640. *
  641. * @param attachment the attachment to add.
  642. */
  643. void addExtensionAttachment(ExtensionAttachment attachment) {
  644. if (attachment == null) {
  645. throw new NullPointerException(
  646. "Parameter attachment must not be null");
  647. }
  648. if (extensionAttachments == null) {
  649. extensionAttachments = new java.util.ArrayList<ExtensionAttachment>();
  650. }
  651. if (log.isDebugEnabled()) {
  652. log.debug("ExtensionAttachment of category "
  653. + attachment.getCategory() + " added to "
  654. + getName() + ": " + attachment);
  655. }
  656. extensionAttachments.add(attachment);
  657. }
  658. /** @return the extension attachments of this FObj. */
  659. public List<ExtensionAttachment> getExtensionAttachments() {
  660. if (extensionAttachments == null) {
  661. return Collections.EMPTY_LIST;
  662. } else {
  663. return extensionAttachments;
  664. }
  665. }
  666. /** @return true if this FObj has extension attachments */
  667. public boolean hasExtensionAttachments() {
  668. return extensionAttachments != null;
  669. }
  670. /**
  671. * Adds a foreign attribute to this FObj.
  672. * @param attributeName the attribute name as a QName instance
  673. * @param value the attribute value
  674. */
  675. public void addForeignAttribute(QName attributeName, String value) {
  676. /* TODO: Handle this over FOP's property mechanism so we can use
  677. * inheritance.
  678. */
  679. if (attributeName == null) {
  680. throw new NullPointerException("Parameter attributeName must not be null");
  681. }
  682. if (foreignAttributes == null) {
  683. foreignAttributes = new java.util.HashMap<QName, String>();
  684. }
  685. foreignAttributes.put(attributeName, value);
  686. }
  687. /** @return the map of foreign attributes */
  688. public Map getForeignAttributes() {
  689. if (foreignAttributes == null) {
  690. return Collections.EMPTY_MAP;
  691. } else {
  692. return foreignAttributes;
  693. }
  694. }
  695. /** {@inheritDoc} */
  696. public String toString() {
  697. return (super.toString() + "[@id=" + this.id + "]");
  698. }
  699. /** Basic {@link FONode.FONodeIterator} implementation */
  700. public static class FObjIterator implements FONodeIterator {
  701. private static final int F_NONE_ALLOWED = 0;
  702. private static final int F_SET_ALLOWED = 1;
  703. private static final int F_REMOVE_ALLOWED = 2;
  704. private FONode currentNode;
  705. private final FObj parentNode;
  706. private int currentIndex;
  707. private int flags = F_NONE_ALLOWED;
  708. FObjIterator(FObj parent) {
  709. this.parentNode = parent;
  710. this.currentNode = parent.firstChild;
  711. this.currentIndex = 0;
  712. this.flags = F_NONE_ALLOWED;
  713. }
  714. /** {@inheritDoc} */
  715. public FObj parent() {
  716. return parentNode;
  717. }
  718. /** {@inheritDoc} */
  719. public FONode next() {
  720. if (currentNode != null) {
  721. if (currentIndex != 0) {
  722. if (currentNode.siblings != null
  723. && currentNode.siblings[1] != null) {
  724. currentNode = currentNode.siblings[1];
  725. } else {
  726. throw new NoSuchElementException();
  727. }
  728. }
  729. currentIndex++;
  730. flags |= (F_SET_ALLOWED | F_REMOVE_ALLOWED);
  731. return currentNode;
  732. } else {
  733. throw new NoSuchElementException();
  734. }
  735. }
  736. /** {@inheritDoc} */
  737. public FONode previous() {
  738. if (currentNode.siblings != null
  739. && currentNode.siblings[0] != null) {
  740. currentIndex--;
  741. currentNode = currentNode.siblings[0];
  742. flags |= (F_SET_ALLOWED | F_REMOVE_ALLOWED);
  743. return currentNode;
  744. } else {
  745. throw new NoSuchElementException();
  746. }
  747. }
  748. /** {@inheritDoc} */
  749. public void set(FONode newNode) {
  750. if ((flags & F_SET_ALLOWED) == F_SET_ALLOWED) {
  751. if (currentNode == parentNode.firstChild) {
  752. parentNode.firstChild = newNode;
  753. } else {
  754. FONode.attachSiblings(currentNode.siblings[0], newNode);
  755. }
  756. if (currentNode.siblings != null
  757. && currentNode.siblings[1] != null) {
  758. FONode.attachSiblings(newNode, currentNode.siblings[1]);
  759. }
  760. if (currentNode == parentNode.lastChild) {
  761. parentNode.lastChild = newNode;
  762. }
  763. } else {
  764. throw new IllegalStateException();
  765. }
  766. }
  767. /** {@inheritDoc} */
  768. public void add(FONode newNode) {
  769. if (currentIndex == -1) {
  770. if (currentNode != null) {
  771. FONode.attachSiblings(newNode, currentNode);
  772. }
  773. parentNode.firstChild = newNode;
  774. currentIndex = 0;
  775. currentNode = newNode;
  776. if (parentNode.lastChild == null) {
  777. parentNode.lastChild = newNode;
  778. }
  779. } else {
  780. if (currentNode.siblings != null
  781. && currentNode.siblings[1] != null) {
  782. FONode.attachSiblings(newNode, currentNode.siblings[1]);
  783. }
  784. FONode.attachSiblings(currentNode, newNode);
  785. if (currentNode == parentNode.lastChild) {
  786. parentNode.lastChild = newNode;
  787. }
  788. }
  789. flags &= F_NONE_ALLOWED;
  790. }
  791. /** {@inheritDoc} */
  792. public boolean hasNext() {
  793. return (currentNode != null)
  794. && ((currentIndex == 0)
  795. || (currentNode.siblings != null
  796. && currentNode.siblings[1] != null));
  797. }
  798. /** {@inheritDoc} */
  799. public boolean hasPrevious() {
  800. return (currentIndex != 0)
  801. || (currentNode.siblings != null
  802. && currentNode.siblings[0] != null);
  803. }
  804. /** {@inheritDoc} */
  805. public int nextIndex() {
  806. return currentIndex + 1;
  807. }
  808. /** {@inheritDoc} */
  809. public int previousIndex() {
  810. return currentIndex - 1;
  811. }
  812. /** {@inheritDoc} */
  813. public void remove() {
  814. if ((flags & F_REMOVE_ALLOWED) == F_REMOVE_ALLOWED) {
  815. parentNode.removeChild(currentNode);
  816. if (currentIndex == 0) {
  817. //first node removed
  818. currentNode = parentNode.firstChild;
  819. } else if (currentNode.siblings != null
  820. && currentNode.siblings[0] != null) {
  821. currentNode = currentNode.siblings[0];
  822. currentIndex--;
  823. } else {
  824. currentNode = null;
  825. }
  826. flags &= F_NONE_ALLOWED;
  827. } else {
  828. throw new IllegalStateException();
  829. }
  830. }
  831. /** {@inheritDoc} */
  832. public FONode last() {
  833. while (currentNode != null
  834. && currentNode.siblings != null
  835. && currentNode.siblings[1] != null) {
  836. currentNode = currentNode.siblings[1];
  837. currentIndex++;
  838. }
  839. return currentNode;
  840. }
  841. /** {@inheritDoc} */
  842. public FONode first() {
  843. currentNode = parentNode.firstChild;
  844. currentIndex = 0;
  845. return currentNode;
  846. }
  847. }
  848. }