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.

IFContext.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.render.intermediate;
  19. import java.util.Collections;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. import org.apache.xmlgraphics.util.QName;
  23. import org.apache.fop.accessibility.StructureTreeElement;
  24. import org.apache.fop.apps.FOUserAgent;
  25. /**
  26. * This class provides a context object that is valid for a single processing run to create
  27. * an output file using the intermediate format. It allows access to the user agent and other
  28. * context information, such as foreign attributes for certain elements in the intermediate
  29. * format.
  30. * <p>
  31. * Foreign attributes are usually specific to a particular output format implementation. Most
  32. * implementations will just ignore all foreign attributes for most elements. That's why the
  33. * main IF interfaces are not burdened with this.
  34. */
  35. public class IFContext {
  36. private FOUserAgent userAgent;
  37. /** foreign attributes: Map<QName, Object> */
  38. private Map foreignAttributes = Collections.EMPTY_MAP;
  39. private Locale language;
  40. private StructureTreeElement structureTreeElement;
  41. private String id = "";
  42. /**
  43. * Main constructor.
  44. * @param ua the user agent
  45. */
  46. public IFContext(FOUserAgent ua) {
  47. setUserAgent(ua);
  48. }
  49. /**
  50. * Set the user agent.
  51. * @param ua the user agent
  52. */
  53. public void setUserAgent(FOUserAgent ua) {
  54. if (this.userAgent != null) {
  55. throw new IllegalStateException("The user agent was already set");
  56. }
  57. this.userAgent = ua;
  58. }
  59. /**
  60. * Returns the associated user agent.
  61. * @return the user agent
  62. */
  63. public FOUserAgent getUserAgent() {
  64. return this.userAgent;
  65. }
  66. /**
  67. * Returns the currently applicable foreign attributes.
  68. * @return a Map<QName, Object>
  69. */
  70. public Map getForeignAttributes() {
  71. return this.foreignAttributes;
  72. }
  73. /**
  74. * Returns a foreign attribute.
  75. * @param qName the qualified name of the foreign attribute
  76. * @return the value of the foreign attribute or null if the attribute isn't specified
  77. */
  78. public Object getForeignAttribute(QName qName) {
  79. return this.foreignAttributes.get(qName);
  80. }
  81. /**
  82. * Sets the currently applicable foreign attributes.
  83. * @param foreignAttributes a Map<QName, Object> or null to reset
  84. */
  85. public void setForeignAttributes(Map foreignAttributes) {
  86. if (foreignAttributes != null) {
  87. this.foreignAttributes = foreignAttributes;
  88. } else {
  89. //Make sure there is always at least an empty map so we don't have to check
  90. //in the implementation code
  91. this.foreignAttributes = Collections.EMPTY_MAP;
  92. }
  93. }
  94. /**
  95. * Resets the foreign attributes to "no foreign attributes".
  96. */
  97. public void resetForeignAttributes() {
  98. setForeignAttributes(null);
  99. }
  100. /**
  101. * Sets the currently applicable language.
  102. * @param lang the language
  103. */
  104. public void setLanguage(Locale lang) {
  105. this.language = lang;
  106. }
  107. /**
  108. * Returns the currently applicable language.
  109. * @return the language (or null if the language is undefined)
  110. */
  111. public Locale getLanguage() {
  112. return this.language;
  113. }
  114. /**
  115. * Sets the structure tree element to which the subsequently painted marks
  116. * will correspond. This method is used when accessibility features are
  117. * enabled.
  118. *
  119. * @param structureTreeElement the structure tree element
  120. */
  121. public void setStructureTreeElement(StructureTreeElement structureTreeElement) {
  122. this.structureTreeElement = structureTreeElement;
  123. }
  124. /**
  125. * Resets the current structure tree element.
  126. * @see #setStructureTreeElement(StructureTreeElement)
  127. */
  128. public void resetStructureTreeElement() {
  129. setStructureTreeElement(null);
  130. }
  131. /**
  132. * Returns the current structure tree element.
  133. * @return the structure tree element (or null if no element is active)
  134. * @see #setStructureTreeElement(StructureTreeElement)
  135. */
  136. public StructureTreeElement getStructureTreeElement() {
  137. return this.structureTreeElement;
  138. }
  139. /**
  140. * Sets the ID of the object enclosing the content that will follow.
  141. *
  142. * @param id the ID of the nearest ancestor object for which the id property was set
  143. */
  144. void setID(String id) {
  145. assert id != null;
  146. this.id = id;
  147. }
  148. /**
  149. * Returns the ID of the object enclosing the current content.
  150. *
  151. * @return the ID of the nearest ancestor object for which the id property was set
  152. */
  153. String getID() {
  154. return id;
  155. }
  156. }