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.

AreaTreeObject.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.area;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.xmlgraphics.util.QName;
  25. import org.apache.fop.fo.extensions.ExtensionAttachment;
  26. /**
  27. * Abstract base class for all area tree objects.
  28. */
  29. public abstract class AreaTreeObject implements Cloneable {
  30. /** Foreign attributes */
  31. protected Map<QName, String> foreignAttributes;
  32. /** Extension attachments */
  33. protected List<ExtensionAttachment> extensionAttachments;
  34. /** {@inheritDoc} */
  35. public Object clone() throws CloneNotSupportedException {
  36. AreaTreeObject ato = (AreaTreeObject) super.clone();
  37. if (foreignAttributes != null) {
  38. ato.foreignAttributes = (Map) ((HashMap) foreignAttributes).clone();
  39. }
  40. if (extensionAttachments != null) {
  41. ato.extensionAttachments = (List) ((ArrayList) extensionAttachments).clone();
  42. }
  43. return ato;
  44. }
  45. /**
  46. * Sets a foreign attribute.
  47. * @param name the qualified name of the attribute
  48. * @param value the attribute value
  49. */
  50. public void setForeignAttribute(QName name, String value) {
  51. if (this.foreignAttributes == null) {
  52. this.foreignAttributes = new HashMap<QName, String>();
  53. }
  54. this.foreignAttributes.put(name, value);
  55. }
  56. /**
  57. * Add foreign attributes from a Map.
  58. *
  59. * @param atts a Map with attributes (keys: QName, values: String)
  60. */
  61. public void setForeignAttributes(Map<QName, String> atts) {
  62. if (atts == null || atts.size() == 0) {
  63. return;
  64. }
  65. for (Map.Entry<QName, String> e : atts.entrySet()) {
  66. setForeignAttribute(e.getKey(), e.getValue());
  67. }
  68. }
  69. /**
  70. * Returns the value of a foreign attribute on the area.
  71. * @param name the qualified name of the attribute
  72. * @return the attribute value or null if it isn't set
  73. */
  74. public String getForeignAttributeValue(QName name) {
  75. if (this.foreignAttributes != null) {
  76. return this.foreignAttributes.get(name);
  77. } else {
  78. return null;
  79. }
  80. }
  81. /** @return the foreign attributes associated with this area */
  82. public Map<QName, String> getForeignAttributes() {
  83. if (this.foreignAttributes != null) {
  84. return Collections.unmodifiableMap(this.foreignAttributes);
  85. } else {
  86. return Collections.emptyMap();
  87. }
  88. }
  89. private void prepareExtensionAttachmentContainer() {
  90. if (this.extensionAttachments == null) {
  91. this.extensionAttachments = new ArrayList<ExtensionAttachment>();
  92. }
  93. }
  94. /**
  95. * Adds a new ExtensionAttachment instance to this page.
  96. * @param attachment the ExtensionAttachment
  97. */
  98. public void addExtensionAttachment(ExtensionAttachment attachment) {
  99. prepareExtensionAttachmentContainer();
  100. extensionAttachments.add(attachment);
  101. }
  102. /**
  103. * Set extension attachments from a List
  104. * @param extensionAttachments a List with extension attachments
  105. */
  106. public void setExtensionAttachments(List<ExtensionAttachment> extensionAttachments) {
  107. prepareExtensionAttachmentContainer();
  108. this.extensionAttachments.addAll(extensionAttachments);
  109. }
  110. /** @return the extension attachments associated with this area */
  111. public List<ExtensionAttachment> getExtensionAttachments() {
  112. if (this.extensionAttachments != null) {
  113. return Collections.unmodifiableList(this.extensionAttachments);
  114. } else {
  115. return Collections.emptyList();
  116. }
  117. }
  118. /**
  119. * Indicates whether this area tree object has any extension attachments.
  120. * @return true if there are extension attachments
  121. */
  122. public boolean hasExtensionAttachments() {
  123. return this.extensionAttachments != null && !this.extensionAttachments.isEmpty();
  124. }
  125. }