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.6KB

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