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.

FObj.java 29KB

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