1 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 ; 37 import java.util.HashMap ; 38 import java.util.Locale ; 39 import java.util.Map ; 40 import java.util.ResourceBundle ; 41 import java.util.Enumeration ; 42 43 51 52 public class SnipRenderContext extends BaseRenderContext { 53 public static final String LANGUAGE_BUNDLE = "SnipRenderContext.language_bundle"; 54 public static final String HTTP_REQUEST = "SnipRenderContext.request"; 55 public static final String HTTP_PARAMS = "SnipRenderContext.params"; 56 public static final String USER = "SnipRenderContext.user"; 57 public static final String SNIP = "SnipRenderContext.snip"; 58 public static final String VIEWED = "SnipRenderContext.viewed_snip"; 59 public static final String CONTAINER = "SnipRenderContext.container"; 60 61 private Snip snip; 62 private SnipSpace space; 63 private Map attributes; 64 65 public SnipRenderContext(Snip snip, SnipSpace space) { 66 super(); 67 this.space = space; 68 this.snip = snip; 69 70 HttpServletRequest request = 71 (HttpServletRequest ) Application.get().getParameters().get("request"); 72 Locale 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 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 (); 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 request = (HttpServletRequest )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 key, Object value) { 111 if (null == attributes) { 112 initAttributes(); 113 } 114 attributes.put(key, value); 115 } 116 117 public Object getAttribute(Object key) { 118 if (null == attributes) { 119 initAttributes(); 120 } 121 return attributes.get(key); 122 } 123 124 public Map getAttributes() { 125 if (null == attributes) { 126 initAttributes(); 127 } 128 return attributes; 129 } 130 131 } 132 | Popular Tags |