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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. private String location;
  43. /**
  44. * Main constructor.
  45. * @param ua the user agent
  46. */
  47. public IFContext(FOUserAgent ua) {
  48. setUserAgent(ua);
  49. }
  50. /**
  51. * Set the user agent.
  52. * @param ua the user agent
  53. */
  54. public void setUserAgent(FOUserAgent ua) {
  55. if (this.userAgent != null) {
  56. throw new IllegalStateException("The user agent was already set");
  57. }
  58. this.userAgent = ua;
  59. }
  60. /**
  61. * Returns the associated user agent.
  62. * @return the user agent
  63. */
  64. public FOUserAgent getUserAgent() {
  65. return this.userAgent;
  66. }
  67. /**
  68. * Returns the currently applicable foreign attributes.
  69. * @return a Map<QName, Object>
  70. */
  71. public Map getForeignAttributes() {
  72. return this.foreignAttributes;
  73. }
  74. /**
  75. * Returns a foreign attribute.
  76. * @param qName the qualified name of the foreign attribute
  77. * @return the value of the foreign attribute or null if the attribute isn't specified
  78. */
  79. public Object getForeignAttribute(QName qName) {
  80. return this.foreignAttributes.get(qName);
  81. }
  82. /**
  83. * Sets the currently applicable foreign attributes.
  84. * @param foreignAttributes a Map<QName, Object> or null to reset
  85. */
  86. public void setForeignAttributes(Map foreignAttributes) {
  87. if (foreignAttributes != null) {
  88. this.foreignAttributes = foreignAttributes;
  89. } else {
  90. //Make sure there is always at least an empty map so we don't have to check
  91. //in the implementation code
  92. this.foreignAttributes = Collections.EMPTY_MAP;
  93. }
  94. }
  95. /**
  96. * Resets the foreign attributes to "no foreign attributes".
  97. */
  98. public void resetForeignAttributes() {
  99. setForeignAttributes(null);
  100. }
  101. /**
  102. * Sets the currently applicable language.
  103. * @param lang the language
  104. */
  105. public void setLanguage(Locale lang) {
  106. this.language = lang;
  107. }
  108. /**
  109. * Returns the currently applicable language.
  110. * @return the language (or null if the language is undefined)
  111. */
  112. public Locale getLanguage() {
  113. return this.language;
  114. }
  115. /**
  116. * Sets the structure tree element to which the subsequently painted marks
  117. * will correspond. This method is used when accessibility features are
  118. * enabled.
  119. *
  120. * @param structureTreeElement the structure tree element
  121. */
  122. public void setStructureTreeElement(StructureTreeElement structureTreeElement) {
  123. this.structureTreeElement = structureTreeElement;
  124. }
  125. /**
  126. * Resets the current structure tree element.
  127. * @see #setStructureTreeElement(StructureTreeElement)
  128. */
  129. public void resetStructureTreeElement() {
  130. setStructureTreeElement(null);
  131. }
  132. /**
  133. * Returns the current structure tree element.
  134. * @return the structure tree element (or null if no element is active)
  135. * @see #setStructureTreeElement(StructureTreeElement)
  136. */
  137. public StructureTreeElement getStructureTreeElement() {
  138. return this.structureTreeElement;
  139. }
  140. /**
  141. * Sets the ID of the object enclosing the content that will follow.
  142. *
  143. * @param id the ID of the nearest ancestor object for which the id property was set
  144. */
  145. void setID(String id) {
  146. assert id != null;
  147. this.id = id;
  148. }
  149. /**
  150. * Returns the ID of the object enclosing the current content.
  151. *
  152. * @return the ID of the nearest ancestor object for which the id property was set
  153. */
  154. String getID() {
  155. return id;
  156. }
  157. /**
  158. * Sets the location of the object enclosing the current content.
  159. *
  160. * @location the line and column location of the object in the source FO file
  161. */
  162. public void setLocation(String location) {
  163. this.location = location;
  164. }
  165. /**
  166. * Returns the location of the object enclosing the current content.
  167. *
  168. * @return the line and column location of the object in the source FO file,
  169. * {@code null} if that information is not available
  170. */
  171. public String getLocation() {
  172. return location;
  173. }
  174. }