1 5 6 package org.infohazard.maverick.shunt; 7 8 import java.util.ArrayList ; 9 import java.util.Collections ; 10 import java.util.Enumeration ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.StringTokenizer ; 15 16 import javax.servlet.ServletConfig ; 17 import javax.servlet.http.HttpServletRequest ; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.infohazard.maverick.flow.ConfigException; 22 import org.infohazard.maverick.flow.NoSuitableModeException; 23 import org.infohazard.maverick.flow.Shunt; 24 import org.infohazard.maverick.flow.ShuntFactory; 25 import org.infohazard.maverick.flow.View; 26 import org.jdom.Element; 27 28 61 public class LanguageShuntFactory implements ShuntFactory 62 { 63 65 class LanguageShunt implements Shunt 66 { 67 69 protected Map modes = new HashMap (); 70 71 76 public void defineMode(String mode, View v) throws ConfigException 77 { 78 if (mode == null) 79 this.reallyDefineMode(null, v); 80 else 81 { 82 mode = mode.toLowerCase(); 83 84 StringTokenizer tokens = new StringTokenizer (mode, ","); 85 while (tokens.hasMoreTokens()) 86 { 87 mode = tokens.nextToken().trim(); 88 89 this.reallyDefineMode(mode, v); 90 } 91 } 92 } 93 94 97 protected void reallyDefineMode(String mode, View v) throws ConfigException 98 { 99 if (this.modes.containsKey(mode)) 100 throw new ConfigException("A duplicate mode (" + mode + ") was" 101 + " specified in a view. Note that language modes" 102 + " are case insensitive."); 103 104 this.modes.put(mode, v); 105 } 106 107 109 public View getView(HttpServletRequest request) throws NoSuitableModeException 110 { 111 String wholeHeader = request.getHeader("Accept-Language"); 113 if (wholeHeader == null) wholeHeader = ""; 115 else 116 wholeHeader = wholeHeader.toLowerCase(); 117 118 Enumeration tokens = new StringTokenizer (wholeHeader, ","); 120 121 List nextPass = null; 124 125 do 126 { 127 while (tokens.hasMoreElements()) 128 { 129 String lang = (String )tokens.nextElement(); 130 lang = lang.trim(); 131 132 View theView = (View)modes.get(lang); 133 if (theView != null) 134 { 135 log.debug("Using mode: " + lang); 136 137 return theView; 138 } 139 140 int dash = lang.lastIndexOf("-"); 141 if (dash >= 0) 142 { 143 if (nextPass == null) 145 nextPass = new ArrayList (); 146 147 nextPass.add(lang.substring(0, dash)); 149 } 150 } 151 152 if (nextPass != null) 154 { 155 tokens = Collections.enumeration(nextPass); 156 nextPass = null; 157 } 158 } 159 while (tokens.hasMoreElements()); 160 161 162 View defaultView = (View)modes.get(null); 164 if (defaultView != null) 165 return defaultView; 166 167 168 throw new NoSuitableModeException("No appropriate mode could be found for Accepts-Language \"" 170 + wholeHeader + "\". Possibilities were: " 171 + modes.keySet()); 172 } 173 } 174 175 178 private static Log log = LogFactory.getLog(LanguageShuntFactory.class); 179 180 183 public void init(Element factoryNode, ServletConfig servletCfg) 184 { 185 } 186 187 190 public Shunt createShunt() throws ConfigException 191 { 192 log.debug("Creating language shunt"); 193 194 return new LanguageShunt(); 195 } 196 } | Popular Tags |