KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > RendererContext


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
18 /* $Id: RendererContext.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.render;
21
22 //Java
23
import java.util.Map JavaDoc;
24
25 //FOP
26
import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.fop.apps.FOUserAgent;
28
29 /**
30  * The Render Context for external handlers. This provides a rendering context
31  * so that external handlers can get information to be able to render to the
32  * render target.
33  */

34 public class RendererContext {
35
36     private String JavaDoc mime;
37     private AbstractRenderer renderer;
38     private FOUserAgent userAgent;
39     private Map JavaDoc props = new java.util.HashMap JavaDoc();
40
41     /**
42      * Contructor for this class. It takes a MIME type as parameter.
43      *
44      * @param renderer The current renderer
45      * @param m The MIME type of the output that's generated.
46      */

47     public RendererContext(AbstractRenderer renderer, String JavaDoc m) {
48         this.renderer = renderer;
49         this.mime = m;
50     }
51
52     /**
53      * @return Returns the renderer.
54      */

55     public AbstractRenderer getRenderer() {
56         return renderer;
57     }
58     
59     /**
60      * Returns the MIME type associated with this RendererContext.
61      *
62      * @return The MIME type (ex. application/pdf)
63      */

64     public String JavaDoc getMimeType() {
65         return mime;
66     }
67
68     /**
69      * Sets the user agent.
70      *
71      * @param ua The user agent
72      */

73     public void setUserAgent(FOUserAgent ua) {
74         userAgent = ua;
75     }
76
77     /**
78      * Returns the user agent.
79      *
80      * @return The user agent
81      */

82     public FOUserAgent getUserAgent() {
83         return userAgent;
84     }
85
86     /**
87      * Sets a property on the RendererContext.
88      *
89      * @param name The key of the property
90      * @param val The value of the property
91      */

92     public void setProperty(String JavaDoc name, Object JavaDoc val) {
93         props.put(name, val);
94     }
95
96     /**
97      * Returns a property from the RendererContext.
98      *
99      * @param prop The key of the property to return.
100      * @return The requested value, <code>null</code> if it doesn't exist.
101      */

102     public Object JavaDoc getProperty(String JavaDoc prop) {
103         return props.get(prop);
104     }
105
106     /**
107      * Wrap the render context to allow easier access to its values.
108      *
109      * @param context the renderer context
110      * @return the generic renderer context wrapper
111      */

112     public static RendererContextWrapper wrapRendererContext(RendererContext context) {
113         RendererContextWrapper wrapper = new RendererContextWrapper(context);
114         return wrapper;
115     }
116
117     /**
118      * Base class for a wrapper around RendererContext to access its properties in a type-safe,
119      * renderer-specific way.
120      */

121     public static class RendererContextWrapper {
122
123         /** The wrapped RendererContext */
124         protected RendererContext context;
125         
126         /**
127          * Main constructor
128          * @param context the RendererContent instance
129          */

130         public RendererContextWrapper(RendererContext context) {
131             this.context = context;
132         }
133         
134         /** @return the user agent */
135         public FOUserAgent getUserAgent() {
136             return context.getUserAgent();
137         }
138
139         /** @return the currentXPosition */
140         public int getCurrentXPosition() {
141             return ((Integer JavaDoc)context.getProperty(RendererContextConstants.XPOS)).intValue();
142         }
143
144         /** @return the currentYPosition */
145         public int getCurrentYPosition() {
146             return ((Integer JavaDoc)context.getProperty(RendererContextConstants.YPOS)).intValue();
147         }
148
149         /** @return the width of the image */
150         public int getWidth() {
151             return ((Integer JavaDoc)context.getProperty(RendererContextConstants.WIDTH)).intValue();
152         }
153
154         /** @return the height of the image */
155         public int getHeight() {
156             return ((Integer JavaDoc)context.getProperty(RendererContextConstants.HEIGHT)).intValue();
157         }
158
159         /** @return the handler configuration */
160         public Configuration getHandlerConfiguration() {
161             return (Configuration)context.getProperty(
162                     RendererContextConstants.HANDLER_CONFIGURATION);
163         }
164
165         /** @return the foreign attributes */
166         public Map JavaDoc getForeignAttributes() {
167             return (Map JavaDoc)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES);
168         }
169         
170     }
171 }
172
173
Popular Tags