| 1 34 35 package com.micronova.jsp.tag; 36 37 import com.micronova.util.*; 38 import java.util.*; 39 import javax.servlet.jsp.*; 40 import javax.servlet.jsp.jstl.fmt.LocaleSupport; 41 42 public class MessageMap extends MapBean 43 { 44 protected PageContext _pageContext; 45 protected String _baseName; 46 protected String _defaultValue; 47 48 public MessageMap(PageContext pageContext) 49 { 50 super(); 51 52 _pageContext = pageContext; 53 _baseName = null; 54 _defaultValue = ""; 55 } 56 57 public void setBaseName(String baseName) 58 { 59 _baseName = baseName; 60 } 61 62 public void setDefaultValue(String defaultValue) 63 { 64 _defaultValue = defaultValue; 65 } 66 67 public Object evaluate(String expression, Class objectClass) throws Exception  68 { 69 return EL.evaluateExpression(_pageContext, "resourceMapExpression", expression.replaceAll("@\\{", "\\$\\{"), objectClass); 70 } 71 72 73 74 public Object getObject(Object client, Object key) 75 { 76 PageContext pageContext = _pageContext; 77 String baseName = _baseName; 78 79 Object object = null; 80 81 try 82 { 83 String keyString = key.toString(); 84 85 List partList = StringUtil.split(keyString, ':'); 86 87 int partCount = partList.size(); 88 89 if (partCount < 2) 90 { 91 String name = (String )evaluate(keyString, String .class); 92 93 if (baseName != null) 94 { 95 object = LocaleSupport.getLocalizedMessage(pageContext, name, baseName); 96 } 97 else 98 { 99 object = LocaleSupport.getLocalizedMessage(pageContext, name); 100 } 101 } 102 else 103 { 104 String name = (String )EL.evaluateExpression(pageContext, "resourceMapExpression", partList.get(0).toString(), String .class); 105 106 Object [] objectList = new Object [partCount - 1]; 107 108 for (int i = partCount; --i >= 1; ) 109 { 110 objectList[i - 1] = evaluate(partList.get(i).toString(), Object .class); 111 } 112 113 if (baseName != null) 114 { 115 object = LocaleSupport.getLocalizedMessage(pageContext, name, objectList, baseName); 116 } 117 else 118 { 119 object = LocaleSupport.getLocalizedMessage(pageContext, name, objectList); 120 121 } 122 } 123 124 String defaultValue = _defaultValue; 125 126 if (!("*".equals(defaultValue))) 127 { 128 if (object.equals("???" + keyString + "???")) 129 { 130 object = defaultValue; 131 } 132 } 133 } 134 catch (Exception e) 135 { 136 throw new RuntimeException (e); 137 } 138 139 return object; 140 } 141 142 public Object putObject(Object client, Object key, Object value) 143 { 144 if ("baseName".equals(key)) 145 { 146 setBaseName(value.toString()); 147 } 148 else if ("defaultValue".equals(key)) 149 { 150 setDefaultValue(value.toString()); 151 } 152 153 return null; 154 } 155 } 156 | Popular Tags |