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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. import org.apache.fop.fo.Constants;
  26. /**
  27. * This class provides a context object that is valid for a single processing run to create
  28. * an output file using the intermediate format. It allows access to the user agent and other
  29. * context information, such as foreign attributes for certain elements in the intermediate
  30. * format.
  31. * <p>
  32. * Foreign attributes are usually specific to a particular output format implementation. Most
  33. * implementations will just ignore all foreign attributes for most elements. That's why the
  34. * main IF interfaces are not burdened with this.
  35. */
  36. public class IFContext implements PageIndexContext {
  37. private FOUserAgent userAgent;
  38. /** foreign attributes: Map<QName, Object> */
  39. private Map foreignAttributes = Collections.EMPTY_MAP;
  40. private Locale language;
  41. private StructureTreeElement structureTreeElement;
  42. private String id = "";
  43. private String location;
  44. private boolean hyphenated;
  45. private int pageIndex = -1;
  46. private int pageNumber = -1;
  47. private RegionType regionType;
  48. /**
  49. * Main constructor.
  50. * @param ua the user agent
  51. */
  52. public IFContext(FOUserAgent ua) {
  53. setUserAgent(ua);
  54. }
  55. /**
  56. * Set the user agent.
  57. * @param ua the user agent
  58. */
  59. public void setUserAgent(FOUserAgent ua) {
  60. if (this.userAgent != null) {
  61. throw new IllegalStateException("The user agent was already set");
  62. }
  63. this.userAgent = ua;
  64. }
  65. /**
  66. * Returns the associated user agent.
  67. * @return the user agent
  68. */
  69. public FOUserAgent getUserAgent() {
  70. return this.userAgent;
  71. }
  72. /**
  73. * Returns the currently applicable foreign attributes.
  74. * @return a Map&lt;QName, Object&gt;
  75. */
  76. public Map getForeignAttributes() {
  77. return this.foreignAttributes;
  78. }
  79. /**
  80. * Returns a foreign attribute.
  81. * @param qName the qualified name of the foreign attribute
  82. * @return the value of the foreign attribute or null if the attribute isn't specified
  83. */
  84. public Object getForeignAttribute(QName qName) {
  85. return this.foreignAttributes.get(qName);
  86. }
  87. /**
  88. * Sets the currently applicable foreign attributes.
  89. * @param foreignAttributes a Map&lt;QName, Object&gt; or null to reset
  90. */
  91. public void setForeignAttributes(Map foreignAttributes) {
  92. if (foreignAttributes != null) {
  93. this.foreignAttributes = foreignAttributes;
  94. } else {
  95. //Make sure there is always at least an empty map so we don't have to check
  96. //in the implementation code
  97. this.foreignAttributes = Collections.EMPTY_MAP;
  98. }
  99. }
  100. /**
  101. * Resets the foreign attributes to "no foreign attributes".
  102. */
  103. public void resetForeignAttributes() {
  104. setForeignAttributes(null);
  105. }
  106. /**
  107. * Sets the currently applicable language.
  108. * @param lang the language
  109. */
  110. public void setLanguage(Locale lang) {
  111. this.language = lang;
  112. }
  113. /**
  114. * Returns the currently applicable language.
  115. * @return the language (or null if the language is undefined)
  116. */
  117. public Locale getLanguage() {
  118. return this.language;
  119. }
  120. /**
  121. * Sets the structure tree element to which the subsequently painted marks
  122. * will correspond. This method is used when accessibility features are
  123. * enabled.
  124. *
  125. * @param structureTreeElement the structure tree element
  126. */
  127. public void setStructureTreeElement(StructureTreeElement structureTreeElement) {
  128. this.structureTreeElement = structureTreeElement;
  129. }
  130. /**
  131. * Resets the current structure tree element.
  132. * @see #setStructureTreeElement(StructureTreeElement)
  133. */
  134. public void resetStructureTreeElement() {
  135. setStructureTreeElement(null);
  136. }
  137. /**
  138. * Returns the current structure tree element.
  139. * @return the structure tree element (or null if no element is active)
  140. * @see #setStructureTreeElement(StructureTreeElement)
  141. */
  142. public StructureTreeElement getStructureTreeElement() {
  143. return this.structureTreeElement;
  144. }
  145. /**
  146. * Sets the ID of the object enclosing the content that will follow.
  147. *
  148. * @param id the ID of the nearest ancestor object for which the id property was set
  149. */
  150. void setID(String id) {
  151. assert id != null;
  152. this.id = id;
  153. }
  154. /**
  155. * Returns the ID of the object enclosing the current content.
  156. *
  157. * @return the ID of the nearest ancestor object for which the id property was set
  158. */
  159. String getID() {
  160. return id;
  161. }
  162. /**
  163. * Sets the location of the object enclosing the current content.
  164. *
  165. * location the line and column location of the object in the source FO file
  166. */
  167. public void setLocation(String location) {
  168. this.location = location;
  169. }
  170. /**
  171. * Returns the location of the object enclosing the current content.
  172. *
  173. * @return the line and column location of the object in the source FO file,
  174. * {@code null} if that information is not available
  175. */
  176. public String getLocation() {
  177. return location;
  178. }
  179. /**
  180. * Records that the last text in the currently processed text area is hyphenated.
  181. */
  182. public void setHyphenated(boolean hyphenated) {
  183. this.hyphenated = hyphenated;
  184. }
  185. /**
  186. * Returns {@code true} if the last text in the currently processed text area is hyphenated.
  187. */
  188. public boolean isHyphenated() {
  189. return hyphenated;
  190. }
  191. /**
  192. * Record current page index.
  193. * @param pageIndex a zero based page index or -1 (no page)
  194. */
  195. public void setPageIndex(int pageIndex) {
  196. this.pageIndex = pageIndex;
  197. }
  198. /**
  199. * Obtain current page index.
  200. * @return a zero based page index or -1 (no page)
  201. */
  202. public int getPageIndex() {
  203. return this.pageIndex;
  204. }
  205. public int getPageNumber() {
  206. return pageNumber;
  207. }
  208. public void setPageNumber(int pageNumber) {
  209. this.pageNumber = pageNumber;
  210. }
  211. private enum RegionType {
  212. Footer,
  213. Header
  214. }
  215. public String getRegionType() {
  216. if (regionType != null) {
  217. return regionType.name();
  218. }
  219. return null;
  220. }
  221. public void setRegionType(String type) {
  222. regionType = null;
  223. if (type != null) {
  224. regionType = RegionType.valueOf(type);
  225. }
  226. }
  227. public void setRegionType(int type) {
  228. regionType = null;
  229. if (type == Constants.FO_REGION_AFTER) {
  230. regionType = RegionType.Footer;
  231. } else if (type == Constants.FO_REGION_BEFORE) {
  232. regionType = RegionType.Header;
  233. }
  234. }
  235. }