1 9 10 package com.opensymphony.module.sitemesh.mapper; 11 12 import com.opensymphony.module.sitemesh.Config; 13 import com.opensymphony.module.sitemesh.Decorator; 14 import com.opensymphony.module.sitemesh.DecoratorMapper; 15 import com.opensymphony.module.sitemesh.Page; 16 17 import javax.servlet.http.HttpServletRequest ; 18 import java.io.File ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 45 public final class LanguageDecoratorMapper extends AbstractDecoratorMapper { 46 private Map map = null; 47 48 public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException { 49 super.init(config, properties, parent); 50 map = new HashMap (); 51 initMap(properties); 52 } 53 54 public Decorator getDecorator(HttpServletRequest request, Page page) { 55 try { 56 Decorator result = null; 57 final Decorator d = super.getDecorator(request, page); 58 String path = modifyPath(d.getPage(), getExt(request.getHeader("Accept-Language"))); 59 60 File decFile = new File (config.getServletContext().getRealPath(path)); 61 62 if (decFile.isFile()) { 63 result = new DefaultDecorator(d.getName(), path, null) { 64 public String getInitParameter(String paramName) { 65 return d.getInitParameter(paramName); 66 } 67 }; 68 } 69 return result == null ? super.getDecorator(request, page) : result; 70 } 71 catch (NullPointerException e) { 72 return super.getDecorator(request, page); 73 } 74 } 75 76 77 private String getExt(String acceptLanguage) { 78 Iterator i = map.entrySet().iterator(); 79 while (i.hasNext()) { 80 Map.Entry entry = (Map.Entry ) i.next(); 81 82 if (acceptLanguage.substring(0, 2).equals(entry.getKey())) { 86 return (String ) entry.getValue(); 87 } 88 89 } 96 return null; 97 } 98 99 100 private static String modifyPath(String path, String ext) { 101 int dot = path.indexOf('.'); 102 if (dot > -1) { 103 return path.substring(0, dot) + '-' + ext + path.substring(dot); 104 } 105 else { 106 return path + '-' + ext; 107 } 108 } 109 110 111 private void initMap(Properties props) { 112 Iterator i = props.entrySet().iterator(); 113 while (i.hasNext()) { 114 Map.Entry entry = (Map.Entry ) i.next(); 115 String key = (String ) entry.getKey(); 116 if (key.startsWith("match.")) { 117 String match = key.substring(6); 118 map.put(match, entry.getValue()); 119 } 120 } 121 } 122 } | Popular Tags |