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.

RendererContext.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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;
  19. //Java
  20. import java.util.Map;
  21. import org.apache.fop.apps.FOUserAgent;
  22. /**
  23. * The Render Context for external handlers. This provides a rendering context
  24. * so that external handlers can get information to be able to render to the
  25. * render target.
  26. */
  27. public class RendererContext {
  28. private final String mime;
  29. private final AbstractRenderer renderer;
  30. private FOUserAgent userAgent;
  31. private final Map<String, Object> props = new java.util.HashMap<String, Object>();
  32. /**
  33. * Constructor for this class. It takes a MIME type as parameter.
  34. *
  35. * @param renderer the current renderer
  36. * @param mime the MIME type of the output that's generated.
  37. */
  38. public RendererContext(AbstractRenderer renderer, String mime) {
  39. this.renderer = renderer;
  40. this.mime = mime;
  41. }
  42. /**
  43. * @return Returns the renderer.
  44. */
  45. public AbstractRenderer getRenderer() {
  46. return renderer;
  47. }
  48. /**
  49. * Returns the MIME type associated with this RendererContext.
  50. *
  51. * @return The MIME type (ex. application/pdf)
  52. */
  53. public String getMimeType() {
  54. return mime;
  55. }
  56. /**
  57. * Sets the user agent.
  58. *
  59. * @param ua The user agent
  60. */
  61. public void setUserAgent(FOUserAgent ua) {
  62. userAgent = ua;
  63. }
  64. /**
  65. * Returns the user agent.
  66. *
  67. * @return The user agent
  68. */
  69. public FOUserAgent getUserAgent() {
  70. return userAgent;
  71. }
  72. /**
  73. * Sets a property on the RendererContext.
  74. *
  75. * @param name The key of the property
  76. * @param val The value of the property
  77. */
  78. public void setProperty(String name, Object val) {
  79. props.put(name, val);
  80. }
  81. /**
  82. * Returns a property from the RendererContext.
  83. *
  84. * @param prop The key of the property to return.
  85. * @return The requested value, <code>null</code> if it doesn't exist.
  86. */
  87. public Object getProperty(String prop) {
  88. return props.get(prop);
  89. }
  90. /**
  91. * Wrap the render context to allow easier access to its values.
  92. *
  93. * @param context the renderer context
  94. * @return the generic renderer context wrapper
  95. */
  96. public static RendererContextWrapper wrapRendererContext(RendererContext context) {
  97. RendererContextWrapper wrapper = new RendererContextWrapper(context);
  98. return wrapper;
  99. }
  100. /** {@inheritDoc} **/
  101. public String toString() {
  102. StringBuffer stringBuffer = new StringBuffer("RendererContext{\n");
  103. for (Object o : props.keySet()) {
  104. String key = (String) o;
  105. Object value = props.get(key);
  106. stringBuffer.append("\t" + key + "=" + value + "\n");
  107. }
  108. stringBuffer.append("}");
  109. return stringBuffer.toString();
  110. }
  111. /**
  112. * Base class for a wrapper around RendererContext to access its properties in a type-safe,
  113. * renderer-specific way.
  114. */
  115. public static class RendererContextWrapper {
  116. /** The wrapped RendererContext */
  117. protected RendererContext context;
  118. /**
  119. * Main constructor
  120. * @param context the RendererContent instance
  121. */
  122. public RendererContextWrapper(RendererContext context) {
  123. this.context = context;
  124. }
  125. /** @return the user agent */
  126. public FOUserAgent getUserAgent() {
  127. return context.getUserAgent();
  128. }
  129. /** @return the currentXPosition */
  130. public int getCurrentXPosition() {
  131. return (Integer) context.getProperty(RendererContextConstants.XPOS);
  132. }
  133. /** @return the currentYPosition */
  134. public int getCurrentYPosition() {
  135. return (Integer) context.getProperty(RendererContextConstants.YPOS);
  136. }
  137. /** @return the width of the image */
  138. public int getWidth() {
  139. return (Integer) context.getProperty(RendererContextConstants.WIDTH);
  140. }
  141. /** @return the height of the image */
  142. public int getHeight() {
  143. return (Integer) context.getProperty(RendererContextConstants.HEIGHT);
  144. }
  145. /** @return the foreign attributes */
  146. public Map getForeignAttributes() {
  147. return (Map)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES);
  148. }
  149. /** {@inheritDoc} */
  150. public String toString() {
  151. return "RendererContextWrapper{"
  152. + "userAgent=" + getUserAgent()
  153. + "x=" + getCurrentXPosition()
  154. + "y=" + getCurrentYPosition()
  155. + "width=" + getWidth()
  156. + "height=" + getHeight()
  157. + "foreignAttributes=" + getForeignAttributes()
  158. + "}";
  159. }
  160. }
  161. }