1 13 package com.tonbeller.wcf.format; 14 15 import java.text.MessageFormat ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.Locale ; 19 20 21 24 public abstract class FormatHandlerSupport implements FormatHandler { 25 protected String name; 26 protected String pattern; 27 protected String errorMessage; 28 protected ArrayList patterns = new ArrayList (); 29 protected Locale locale; 30 31 34 public void addPattern(Pattern p) { 35 patterns.add(p); 36 } 37 38 45 protected String findPattern(String userPattern) { 46 47 if (userPattern != null && userPattern.length() > 0) 48 return userPattern; 49 50 Iterator it = patterns.iterator(); 51 52 while (it.hasNext()) { 53 Pattern p = (Pattern) it.next(); 54 55 if (locale.getLanguage().equals(p.getLanguage())) { 56 return p.getPattern(); 57 } 58 } 59 60 return this.getPattern(); 61 } 62 63 protected String getErrorMessage(String userInput) { 64 String errorMessage = null; 65 66 Iterator it = patterns.iterator(); 67 68 while (it.hasNext()) { 69 Pattern p = (Pattern) it.next(); 70 71 if (locale.getLanguage().equals(p.getLanguage())) { 72 errorMessage = p.getErrorMessage(); 73 } 74 } 75 76 if (errorMessage == null) { 78 errorMessage = getErrorMessage(); 79 } 80 81 if (errorMessage == null) { 82 return userInput; 83 } 84 85 return MessageFormat.format(errorMessage, new Object [] { userInput }); 86 } 87 88 92 public String getName() { 93 return name; 94 } 95 96 100 public String getPattern() { 101 return pattern; 102 } 103 104 108 public void setName(String name) { 109 this.name = name; 110 } 111 112 116 public void setPattern(String pattern) { 117 this.pattern = pattern; 118 } 119 120 124 public String getErrorMessage() { 125 return errorMessage; 126 } 127 128 132 public void setErrorMessage(String errorMessage) { 133 this.errorMessage = errorMessage; 134 } 135 136 140 public Locale getLocale() { 141 return locale; 142 } 143 144 148 public void setLocale(Locale locale) { 149 this.locale = locale; 150 } 151 } | Popular Tags |