KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > context > SnipRenderContext


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.render.context;
27
28 import org.radeox.api.engine.context.RenderContext;
29 import org.radeox.engine.context.BaseRenderContext;
30 import org.radeox.util.i18n.ResourceManager;
31 import org.snipsnap.app.Application;
32 import org.snipsnap.container.Components;
33 import org.snipsnap.snip.Snip;
34 import org.snipsnap.snip.SnipSpace;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Locale JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.ResourceBundle JavaDoc;
41 import java.util.Enumeration JavaDoc;
42
43 /**
44  * SnipRenderContext implements RenderContext and is used to
45  * give special SnipSnap parameters to a SnipSnap aware
46  * Rendering Engine
47  *
48  * @author Stephan J. Schmidt
49  * @version $Id: SnipRenderContext.java 1606 2004-05-17 10:56:18Z leo $
50  */

51
52 public class SnipRenderContext extends BaseRenderContext {
53   public static final String JavaDoc LANGUAGE_BUNDLE = "SnipRenderContext.language_bundle";
54   public static final String JavaDoc HTTP_REQUEST = "SnipRenderContext.request";
55   public static final String JavaDoc HTTP_PARAMS = "SnipRenderContext.params";
56   public static final String JavaDoc USER = "SnipRenderContext.user";
57   public static final String JavaDoc SNIP = "SnipRenderContext.snip";
58   public static final String JavaDoc VIEWED = "SnipRenderContext.viewed_snip";
59   public static final String JavaDoc CONTAINER = "SnipRenderContext.container";
60
61   private Snip snip;
62   private SnipSpace space;
63   private Map JavaDoc attributes;
64
65   public SnipRenderContext(Snip snip, SnipSpace space) {
66     super();
67     this.space = space;
68     this.snip = snip;
69
70     HttpServletRequest JavaDoc request =
71       (HttpServletRequest JavaDoc) Application.get().getParameters().get("request");
72     Locale JavaDoc locale = Application.get().getConfiguration().getLocale();
73     if(null != request && null != request.getLocale()) {
74       ResourceManager.get().setLocale(request.getLocale(), request.getLocales());
75     } else {
76       ResourceManager.get().setLocale(locale, null);
77     }
78     set(SnipRenderContext.LANGUAGE_BUNDLE, ResourceManager.getBundle("i18n.messages"));
79     set(SnipRenderContext.LANGUAGE_LOCALE, ResourceManager.getLocale("i18n.messages"));
80   }
81
82   /**
83    * Gets the current snip for which the RenderEngine is called
84    *
85    * @return snip Snip for which the RenderEngine is called
86    */

87   public Snip getSnip() {
88     return this.snip;
89   }
90
91   public SnipSpace getSpace() {
92     return space;
93   }
94
95   public void setSpace(SnipSpace space) {
96     this.space = space;
97   }
98
99   private void initAttributes() {
100     attributes = new HashMap JavaDoc();
101     attributes.put(SNIP, snip);
102     attributes.put(USER, Application.get().getUser());
103     attributes.put(VIEWED, Application.get().getParameters().get("viewed"));
104     attributes.put(CONTAINER, Components.getContainer());
105     HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)Application.get().getParameters().get("request");
106     attributes.put(HTTP_REQUEST, request);
107     attributes.put(HTTP_PARAMS, Application.get().getParameters());
108   }
109
110   public void setAttribute(Object JavaDoc key, Object JavaDoc value) {
111     if (null == attributes) {
112       initAttributes();
113     }
114     attributes.put(key, value);
115   }
116
117   public Object JavaDoc getAttribute(Object JavaDoc key) {
118     if (null == attributes) {
119       initAttributes();
120     }
121     return attributes.get(key);
122   }
123
124   public Map JavaDoc getAttributes() {
125     if (null == attributes) {
126       initAttributes();
127     }
128     return attributes;
129   }
130
131 }
132
Popular Tags