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

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