1 package org.jahia.taglibs.resourcebundle; 2 3 4 import java.io.IOException ; 5 import java.text.MessageFormat ; 6 import java.util.Locale ; 7 import java.util.MissingResourceException ; 8 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpSession ; 11 import javax.servlet.jsp.JspException ; 12 import javax.servlet.jsp.JspWriter ; 13 import javax.servlet.jsp.tagext.TagSupport ; 14 15 import org.jahia.engines.EngineMessage; 16 import org.jahia.params.ParamBean; 17 import org.jahia.resourcebundle.JahiaResourceBundle; 18 19 20 31 public class MessageTag extends TagSupport { 32 33 private static org.apache.log4j.Logger logger = 34 org.apache.log4j.Logger.getLogger(MessageTag.class); 35 36 private static final String CLASS_NAME = MessageTag.class.getName(); 37 38 private String key = null; 39 private String name = null; 40 private boolean display = true; 41 private String property = null; 42 43 public void setKey(String key) { 44 if ( key == null ) 45 key = ""; 46 this.key = key; 47 } 48 49 public String getName() { 50 return name; 51 } 52 public void setName(String name) { 53 this.name = name; 54 } 55 public boolean isDisplay() { 56 return display; 57 } 58 public void setDisplay(boolean display) { 59 this.display = display; 60 } 61 public String getProperty() { 62 return property; 63 } 64 public void setProperty(String property) { 65 this.property = property; 66 } 67 68 public int doStartTag() { 69 70 HttpServletRequest request = (HttpServletRequest )pageContext.getRequest(); 71 String resValue = null; 72 73 Locale currentLocale = request.getLocale(); 74 HttpSession session = pageContext.getSession(); 75 if (session != null) { 76 if (session.getAttribute(ParamBean.SESSION_LOCALE) != null) { 77 currentLocale = (Locale ) session.getAttribute(ParamBean. 78 SESSION_LOCALE); 79 } 80 } 81 82 try { 83 84 String keyName = null; 85 if (key != null) { 86 resValue = JahiaResourceBundle 87 .getMessageResource(key, 88 currentLocale); 89 } else if (name != null) { 90 EngineMessage message = (EngineMessage) pageContext.findAttribute(name); 91 if (message != null) { 92 String keyValue = JahiaResourceBundle 93 .getMessageResource(message.getKey(), 94 currentLocale); 95 if (message.getValues() != null) { 96 MessageFormat msgFormat = new MessageFormat (keyValue); 97 msgFormat.setLocale(currentLocale); 98 resValue = msgFormat.format(message.getValues()); 99 } else { 100 resValue = keyValue; 101 } 102 103 } else { 104 logger.error("Couldn't find any EngineMessage bean with name " + name + "!"); 105 } 106 } 107 108 if (key != null) { 109 } 110 111 } catch ( MissingResourceException mre ) { 112 logger.warn("Couldn't find resource : ", mre); 113 } 114 115 if (resValue == null) { 116 resValue = ""; 117 } 118 119 try { 120 JspWriter out = pageContext.getOut(); 121 out.print( resValue ); 122 } catch (IOException ioe) { 123 logger.error("IO error while displaying message : ", ioe); 124 } 125 126 return SKIP_BODY; 127 128 } 129 130 public int doEndTag() throws JspException { 131 key = null; 134 name = null; 135 property = null; 136 display = true; 137 return EVAL_PAGE; 138 } 139 140 141 } 142 | Popular Tags |