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

FObj.java 30KB

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