Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RendererContext.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //FOP
  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 String mime;
  30. private AbstractRenderer renderer;
  31. private FOUserAgent userAgent;
  32. private Map props = new java.util.HashMap();
  33. /**
  34. * Contructor for this class. It takes a MIME type as parameter.
  35. *
  36. * @param renderer The current renderer
  37. * @param m The MIME type of the output that's generated.
  38. */
  39. public RendererContext(AbstractRenderer renderer, String m) {
  40. this.renderer = renderer;
  41. this.mime = m;
  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. /**
  102. * Base class for a wrapper around RendererContext to access its properties in a type-safe,
  103. * renderer-specific way.
  104. */
  105. public static class RendererContextWrapper {
  106. /** The wrapped RendererContext */
  107. protected RendererContext context;
  108. /**
  109. * Main constructor
  110. * @param context the RendererContent instance
  111. */
  112. public RendererContextWrapper(RendererContext context) {
  113. this.context = context;
  114. }
  115. /** @return the user agent */
  116. public FOUserAgent getUserAgent() {
  117. return context.getUserAgent();
  118. }
  119. /** @return the currentXPosition */
  120. public int getCurrentXPosition() {
  121. return ((Integer)context.getProperty(RendererContextConstants.XPOS)).intValue();
  122. }
  123. /** @return the currentYPosition */
  124. public int getCurrentYPosition() {
  125. return ((Integer)context.getProperty(RendererContextConstants.YPOS)).intValue();
  126. }
  127. /** @return the width of the image */
  128. public int getWidth() {
  129. return ((Integer)context.getProperty(RendererContextConstants.WIDTH)).intValue();
  130. }
  131. /** @return the height of the image */
  132. public int getHeight() {
  133. return ((Integer)context.getProperty(RendererContextConstants.HEIGHT)).intValue();
  134. }
  135. /** @return the foreign attributes */
  136. public Map getForeignAttributes() {
  137. return (Map)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES);
  138. }
  139. }
  140. }