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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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[] PROPERTY_LIST_TABLE
  40. = FOPropertyMapping.getGenericMappings();
  41. /** pointer to the descendant subtree */
  42. protected FONode firstChild;
  43. /** pointer to the end of the descendant subtree */
  44. protected FONode lastChild;
  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 PROPERTY_LIST_TABLE[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. lastChild = child;
  176. } else {
  177. if (lastChild == null) {
  178. FONode prevChild = firstChild;
  179. while (prevChild.siblings != null
  180. && prevChild.siblings[1] != null) {
  181. prevChild = prevChild.siblings[1];
  182. }
  183. FONode.attachSiblings(prevChild, child);
  184. } else {
  185. FONode.attachSiblings(lastChild, child);
  186. lastChild = child;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. /**
  193. * Used by RetrieveMarker during Marker-subtree cloning
  194. * @param child the (cloned) child node
  195. * @param parent the (cloned) parent node
  196. * @throws FOPException when the child could not be added to the parent
  197. */
  198. protected static void addChildTo(FONode child, FONode parent)
  199. throws FOPException {
  200. parent.addChildNode(child);
  201. }
  202. /** {@inheritDoc} */
  203. public void removeChild(FONode child) {
  204. FONode nextChild = null;
  205. if (child.siblings != null) {
  206. nextChild = child.siblings[1];
  207. }
  208. if (child == firstChild) {
  209. firstChild = nextChild;
  210. if (firstChild != null) {
  211. firstChild.siblings[0] = null;
  212. }
  213. } else {
  214. FONode prevChild = child.siblings[0];
  215. prevChild.siblings[1] = nextChild;
  216. if (nextChild != null) {
  217. nextChild.siblings[0] = prevChild;
  218. }
  219. }
  220. if (child == lastChild) {
  221. if (child.siblings != null) {
  222. lastChild = siblings[0];
  223. } else {
  224. lastChild = null;
  225. }
  226. }
  227. }
  228. /**
  229. * Find the nearest parent, grandparent, etc. FONode that is also an FObj
  230. * @return FObj the nearest ancestor FONode that is an FObj
  231. */
  232. public FObj findNearestAncestorFObj() {
  233. FONode par = parent;
  234. while (par != null && !(par instanceof FObj)) {
  235. par = par.parent;
  236. }
  237. return (FObj) par;
  238. }
  239. /**
  240. * Check if this formatting object generates reference areas.
  241. * @return true if generates reference areas
  242. * TODO see if needed
  243. */
  244. public boolean generatesReferenceAreas() {
  245. return false;
  246. }
  247. /** {@inheritDoc} */
  248. public FONodeIterator getChildNodes() {
  249. if (hasChildren()) {
  250. return new FObjIterator(this);
  251. }
  252. return null;
  253. }
  254. /**
  255. * Indicates whether this formatting object has children.
  256. * @return true if there are children
  257. */
  258. public boolean hasChildren() {
  259. return this.firstChild != null;
  260. }
  261. /**
  262. * Return an iterator over the object's childNodes starting
  263. * at the passed-in node (= first call to iterator.next() will
  264. * return childNode)
  265. * @param childNode First node in the iterator
  266. * @return A ListIterator or null if childNode isn't a child of
  267. * this FObj.
  268. */
  269. public FONodeIterator getChildNodes(FONode childNode) {
  270. FONodeIterator it = getChildNodes();
  271. if (it != null) {
  272. if (firstChild == childNode) {
  273. return it;
  274. } else {
  275. while (it.hasNext()
  276. && it.nextNode().siblings[1] != childNode) {
  277. //nop
  278. }
  279. if (it.hasNext()) {
  280. return it;
  281. } else {
  282. return null;
  283. }
  284. }
  285. }
  286. return null;
  287. }
  288. /**
  289. * Notifies a FObj that one of it's children is removed.
  290. * This method is subclassed by Block to clear the
  291. * firstInlineChild variable in case it doesn't generate
  292. * any areas (see addMarker()).
  293. * @param node the node that was removed
  294. */
  295. void notifyChildRemoval(FONode node) {
  296. //nop
  297. }
  298. /**
  299. * Add the marker to this formatting object.
  300. * If this object can contain markers it checks that the marker
  301. * has a unique class-name for this object and that it is
  302. * the first child.
  303. * @param marker Marker to add.
  304. */
  305. protected void addMarker(Marker marker) {
  306. String mcname = marker.getMarkerClassName();
  307. if (firstChild != null) {
  308. // check for empty childNodes
  309. for (Iterator iter = getChildNodes(); iter.hasNext();) {
  310. FONode node = (FONode) iter.next();
  311. if (node instanceof FObj
  312. || (node instanceof FOText
  313. && ((FOText) node).willCreateArea())) {
  314. getFOValidationEventProducer().markerNotInitialChild(this, getName(),
  315. mcname, locator);
  316. return;
  317. } else if (node instanceof FOText) {
  318. iter.remove();
  319. notifyChildRemoval(node);
  320. }
  321. }
  322. }
  323. if (markers == null) {
  324. markers = new java.util.HashMap();
  325. }
  326. if (!markers.containsKey(mcname)) {
  327. markers.put(mcname, marker);
  328. } else {
  329. getFOValidationEventProducer().markerNotUniqueForSameParent(this, getName(),
  330. mcname, locator);
  331. }
  332. }
  333. /**
  334. * @return true if there are any Markers attached to this object
  335. */
  336. public boolean hasMarkers() {
  337. return markers != null && !markers.isEmpty();
  338. }
  339. /**
  340. * @return the collection of Markers attached to this object
  341. */
  342. public Map getMarkers() {
  343. return markers;
  344. }
  345. /** {@inheritDoc} */
  346. protected String getContextInfoAlt() {
  347. StringBuffer sb = new StringBuffer();
  348. if (getLocalName() != null) {
  349. sb.append(getName());
  350. sb.append(", ");
  351. }
  352. if (hasId()) {
  353. sb.append("id=").append(getId());
  354. return sb.toString();
  355. }
  356. String s = gatherContextInfo();
  357. if (s != null) {
  358. sb.append("\"");
  359. if (s.length() < 32) {
  360. sb.append(s);
  361. } else {
  362. sb.append(s.substring(0, 32));
  363. sb.append("...");
  364. }
  365. sb.append("\"");
  366. return sb.toString();
  367. } else {
  368. return null;
  369. }
  370. }
  371. /** {@inheritDoc} */
  372. protected String gatherContextInfo() {
  373. if (getLocator() != null) {
  374. return super.gatherContextInfo();
  375. } else {
  376. ListIterator iter = getChildNodes();
  377. if (iter == null) {
  378. return null;
  379. }
  380. StringBuffer sb = new StringBuffer();
  381. while (iter.hasNext()) {
  382. FONode node = (FONode) iter.next();
  383. String s = node.gatherContextInfo();
  384. if (s != null) {
  385. if (sb.length() > 0) {
  386. sb.append(", ");
  387. }
  388. sb.append(s);
  389. }
  390. }
  391. return (sb.length() > 0 ? sb.toString() : null);
  392. }
  393. }
  394. /**
  395. * Convenience method for validity checking. Checks if the
  396. * incoming node is a member of the "%block;" parameter entity
  397. * as defined in Sect. 6.2 of the XSL 1.0 & 1.1 Recommendations
  398. *
  399. * @param nsURI namespace URI of incoming node
  400. * @param lName local name (i.e., no prefix) of incoming node
  401. * @return true if a member, false if not
  402. */
  403. protected boolean isBlockItem(String nsURI, String lName) {
  404. return (FO_URI.equals(nsURI)
  405. && ("block".equals(lName)
  406. || "table".equals(lName)
  407. || "table-and-caption".equals(lName)
  408. || "block-container".equals(lName)
  409. || "list-block".equals(lName)
  410. || "float".equals(lName)
  411. || isNeutralItem(nsURI, lName)));
  412. }
  413. /**
  414. * Convenience method for validity checking. Checks if the
  415. * incoming node is a member of the "%inline;" parameter entity
  416. * as defined in Sect. 6.2 of the XSL 1.0 & 1.1 Recommendations
  417. *
  418. * @param nsURI namespace URI of incoming node
  419. * @param lName local name (i.e., no prefix) of incoming node
  420. * @return true if a member, false if not
  421. */
  422. protected boolean isInlineItem(String nsURI, String lName) {
  423. return (FO_URI.equals(nsURI)
  424. && ("bidi-override".equals(lName)
  425. || "character".equals(lName)
  426. || "external-graphic".equals(lName)
  427. || "instream-foreign-object".equals(lName)
  428. || "inline".equals(lName)
  429. || "inline-container".equals(lName)
  430. || "leader".equals(lName)
  431. || "page-number".equals(lName)
  432. || "page-number-citation".equals(lName)
  433. || "page-number-citation-last".equals(lName)
  434. || "basic-link".equals(lName)
  435. || ("multi-toggle".equals(lName)
  436. && (getNameId() == FO_MULTI_CASE
  437. || findAncestor(FO_MULTI_CASE) > 0))
  438. || ("footnote".equals(lName)
  439. && !isOutOfLineFODescendant)
  440. || isNeutralItem(nsURI, lName)));
  441. }
  442. /**
  443. * Convenience method for validity checking. Checks if the
  444. * incoming node is a member of the "%block;" parameter entity
  445. * or "%inline;" parameter entity
  446. * @param nsURI namespace URI of incoming node
  447. * @param lName local name (i.e., no prefix) of incoming node
  448. * @return true if a member, false if not
  449. */
  450. protected boolean isBlockOrInlineItem(String nsURI, String lName) {
  451. return (isBlockItem(nsURI, lName) || isInlineItem(nsURI, lName));
  452. }
  453. /**
  454. * Convenience method for validity checking. Checks if the
  455. * incoming node is a member of the neutral item list
  456. * as defined in Sect. 6.2 of the XSL 1.0 & 1.1 Recommendations
  457. * @param nsURI namespace URI of incoming node
  458. * @param lName local name (i.e., no prefix) of incoming node
  459. * @return true if a member, false if not
  460. */
  461. boolean isNeutralItem(String nsURI, String lName) {
  462. return (FO_URI.equals(nsURI)
  463. && ("multi-switch".equals(lName)
  464. || "multi-properties".equals(lName)
  465. || "wrapper".equals(lName)
  466. || (!isOutOfLineFODescendant && "float".equals(lName))
  467. || "retrieve-marker".equals(lName)
  468. || "retrieve-table-marker".equals(lName)));
  469. }
  470. /**
  471. * Convenience method for validity checking. Checks if the
  472. * current node has an ancestor of a given name.
  473. * @param ancestorID ID of node name to check for (e.g., FO_ROOT)
  474. * @return number of levels above FO where ancestor exists,
  475. * -1 if not found
  476. */
  477. protected int findAncestor(int ancestorID) {
  478. int found = 1;
  479. FONode temp = getParent();
  480. while (temp != null) {
  481. if (temp.getNameId() == ancestorID) {
  482. return found;
  483. }
  484. found += 1;
  485. temp = temp.getParent();
  486. }
  487. return -1;
  488. }
  489. /**
  490. * Clears the list of child nodes.
  491. */
  492. public void clearChildNodes() {
  493. this.firstChild = null;
  494. }
  495. /** @return the "id" property. */
  496. public String getId() {
  497. return id;
  498. }
  499. /** @return whether this object has an id set */
  500. public boolean hasId() {
  501. return (id != null && id.length() > 0);
  502. }
  503. /** {@inheritDoc} */
  504. public String getNamespaceURI() {
  505. return FOElementMapping.URI;
  506. }
  507. /** {@inheritDoc} */
  508. public String getNormalNamespacePrefix() {
  509. return "fo";
  510. }
  511. /**
  512. * Add a new extension attachment to this FObj.
  513. * (see org.apache.fop.fo.FONode for details)
  514. *
  515. * @param attachment the attachment to add.
  516. */
  517. void addExtensionAttachment(ExtensionAttachment attachment) {
  518. if (attachment == null) {
  519. throw new NullPointerException(
  520. "Parameter attachment must not be null");
  521. }
  522. if (extensionAttachments == null) {
  523. extensionAttachments = new java.util.ArrayList<ExtensionAttachment>();
  524. }
  525. if (log.isDebugEnabled()) {
  526. log.debug("ExtensionAttachment of category "
  527. + attachment.getCategory() + " added to "
  528. + getName() + ": " + attachment);
  529. }
  530. extensionAttachments.add(attachment);
  531. }
  532. /** @return the extension attachments of this FObj. */
  533. public List/*<ExtensionAttachment>*/ getExtensionAttachments() {
  534. if (extensionAttachments == null) {
  535. return Collections.EMPTY_LIST;
  536. } else {
  537. return extensionAttachments;
  538. }
  539. }
  540. /** @return true if this FObj has extension attachments */
  541. public boolean hasExtensionAttachments() {
  542. return extensionAttachments != null;
  543. }
  544. /**
  545. * Adds a foreign attribute to this FObj.
  546. * @param attributeName the attribute name as a QName instance
  547. * @param value the attribute value
  548. */
  549. public void addForeignAttribute(QName attributeName, String value) {
  550. /* TODO: Handle this over FOP's property mechanism so we can use
  551. * inheritance.
  552. */
  553. if (attributeName == null) {
  554. throw new NullPointerException("Parameter attributeName must not be null");
  555. }
  556. if (foreignAttributes == null) {
  557. foreignAttributes = new java.util.HashMap<QName, String>();
  558. }
  559. foreignAttributes.put(attributeName, value);
  560. }
  561. /** @return the map of foreign attributes */
  562. public Map getForeignAttributes() {
  563. if (foreignAttributes == null) {
  564. return Collections.EMPTY_MAP;
  565. } else {
  566. return foreignAttributes;
  567. }
  568. }
  569. /** {@inheritDoc} */
  570. public String toString() {
  571. return (super.toString() + "[@id=" + this.id + "]");
  572. }
  573. /** Basic {@link FONode.FONodeIterator} implementation */
  574. public class FObjIterator implements FONodeIterator {
  575. private static final int F_NONE_ALLOWED = 0;
  576. private static final int F_SET_ALLOWED = 1;
  577. private static final int F_REMOVE_ALLOWED = 2;
  578. private FONode currentNode;
  579. private final FObj parentNode;
  580. private int currentIndex;
  581. private int flags = F_NONE_ALLOWED;
  582. FObjIterator(FObj parent) {
  583. this.parentNode = parent;
  584. this.currentNode = parent.firstChild;
  585. this.currentIndex = 0;
  586. this.flags = F_NONE_ALLOWED;
  587. }
  588. /** {@inheritDoc} */
  589. public FObj parentNode() {
  590. return parentNode;
  591. }
  592. /** {@inheritDoc} */
  593. public Object next() {
  594. if (currentNode != null) {
  595. if (currentIndex != 0) {
  596. if (currentNode.siblings != null
  597. && currentNode.siblings[1] != null) {
  598. currentNode = currentNode.siblings[1];
  599. } else {
  600. throw new NoSuchElementException();
  601. }
  602. }
  603. currentIndex++;
  604. flags |= (F_SET_ALLOWED | F_REMOVE_ALLOWED);
  605. return currentNode;
  606. } else {
  607. throw new NoSuchElementException();
  608. }
  609. }
  610. /** {@inheritDoc} */
  611. public Object previous() {
  612. if (currentNode.siblings != null
  613. && currentNode.siblings[0] != null) {
  614. currentIndex--;
  615. currentNode = currentNode.siblings[0];
  616. flags |= (F_SET_ALLOWED | F_REMOVE_ALLOWED);
  617. return currentNode;
  618. } else {
  619. throw new NoSuchElementException();
  620. }
  621. }
  622. /** {@inheritDoc} */
  623. public void set(Object o) {
  624. if ((flags & F_SET_ALLOWED) == F_SET_ALLOWED) {
  625. FONode newNode = (FONode) o;
  626. if (currentNode == parentNode.firstChild) {
  627. parentNode.firstChild = newNode;
  628. } else {
  629. FONode.attachSiblings(currentNode.siblings[0], newNode);
  630. }
  631. if (currentNode.siblings != null
  632. && currentNode.siblings[1] != null) {
  633. FONode.attachSiblings(newNode, currentNode.siblings[1]);
  634. }
  635. if (currentNode == parentNode.lastChild) {
  636. parentNode.lastChild = newNode;
  637. }
  638. } else {
  639. throw new IllegalStateException();
  640. }
  641. }
  642. /** {@inheritDoc} */
  643. public void add(Object o) {
  644. FONode newNode = (FONode) o;
  645. if (currentIndex == -1) {
  646. if (currentNode != null) {
  647. FONode.attachSiblings(newNode, currentNode);
  648. }
  649. parentNode.firstChild = newNode;
  650. currentIndex = 0;
  651. currentNode = newNode;
  652. if (parentNode.lastChild == null) {
  653. parentNode.lastChild = newNode;
  654. }
  655. } else {
  656. if (currentNode.siblings != null
  657. && currentNode.siblings[1] != null) {
  658. FONode.attachSiblings((FONode) o, currentNode.siblings[1]);
  659. }
  660. FONode.attachSiblings(currentNode, (FONode) o);
  661. if (currentNode == parentNode.lastChild) {
  662. parentNode.lastChild = newNode;
  663. }
  664. }
  665. flags &= F_NONE_ALLOWED;
  666. }
  667. /** {@inheritDoc} */
  668. public boolean hasNext() {
  669. return (currentNode != null)
  670. && ((currentIndex == 0)
  671. || (currentNode.siblings != null
  672. && currentNode.siblings[1] != null));
  673. }
  674. /** {@inheritDoc} */
  675. public boolean hasPrevious() {
  676. return (currentIndex != 0)
  677. || (currentNode.siblings != null
  678. && currentNode.siblings[0] != null);
  679. }
  680. /** {@inheritDoc} */
  681. public int nextIndex() {
  682. return currentIndex + 1;
  683. }
  684. /** {@inheritDoc} */
  685. public int previousIndex() {
  686. return currentIndex - 1;
  687. }
  688. /** {@inheritDoc} */
  689. public void remove() {
  690. if ((flags & F_REMOVE_ALLOWED) == F_REMOVE_ALLOWED) {
  691. parentNode.removeChild(currentNode);
  692. if (currentIndex == 0) {
  693. //first node removed
  694. currentNode = parentNode.firstChild;
  695. } else if (currentNode.siblings != null
  696. && currentNode.siblings[0] != null) {
  697. currentNode = currentNode.siblings[0];
  698. currentIndex--;
  699. } else {
  700. currentNode = null;
  701. }
  702. flags &= F_NONE_ALLOWED;
  703. } else {
  704. throw new IllegalStateException();
  705. }
  706. }
  707. /** {@inheritDoc} */
  708. public FONode lastNode() {
  709. while (currentNode != null
  710. && currentNode.siblings != null
  711. && currentNode.siblings[1] != null) {
  712. currentNode = currentNode.siblings[1];
  713. currentIndex++;
  714. }
  715. return currentNode;
  716. }
  717. /** {@inheritDoc} */
  718. public FONode firstNode() {
  719. currentNode = parentNode.firstChild;
  720. currentIndex = 0;
  721. return currentNode;
  722. }
  723. /** {@inheritDoc} */
  724. public FONode nextNode() {
  725. return (FONode) next();
  726. }
  727. /** {@inheritDoc} */
  728. public FONode previousNode() {
  729. return (FONode) previous();
  730. }
  731. }
  732. }