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 26KB

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