1 8 package org.apache.avalon.excalibur.i18n; 9 10 import java.util.List ; 11 import java.util.LinkedList ; 12 import java.util.Locale ; 13 14 import org.apache.avalon.framework.configuration.Configuration; 15 import org.apache.avalon.framework.configuration.ConfigurationException; 16 import org.apache.avalon.framework.configuration.Configurable; 17 18 25 public class DefaultBundleMatcher implements BundleMatcher, Configurable { 26 27 private List bundleInfos = new LinkedList (); 28 private String type; 29 30 35 public String getType(BundleInfo bundleInfo) { 36 if (bundleInfos != null) { 37 for (int i = 0; i < bundleInfos.size(); i++) { 38 if (match((BundleInfo) bundleInfos.get(i), bundleInfo)) 39 return this.type; 40 } 41 return null; 42 } 43 return this.type; 44 } 45 46 51 public void configure(Configuration configuration) throws ConfigurationException { 52 this.type = configuration.getAttribute("type"); 53 Configuration[] bundleInfos = configuration.getChildren("bundle-info"); 54 for (int i = 0; i < bundleInfos.length; i++) { 55 56 Configurable bundleInfo = null; 57 String className = bundleInfos[i].getAttribute("class", null); 58 if (className != null) { 59 try { 60 bundleInfo = (Configurable) Class.forName(className).newInstance(); 61 } 62 catch (ClassCastException e) { 63 throw new ConfigurationException("bundleinfo implementation does not implement Configurable interface! " + 64 "All bundleinfo implementations passed to matchers have to be Configurable!", e); 65 } 66 catch (Exception e) { 67 throw new ConfigurationException("unable to load bundleinfo implementation", e); 68 } 69 } 70 if (bundleInfo == null) bundleInfo = new ConfigurableBundleInfo(); 71 bundleInfo.configure(bundleInfos[i]); 72 this.bundleInfos.add(bundleInfo); 73 } 74 } 75 76 protected boolean match(BundleInfo src, BundleInfo compareTo) { 77 return 78 match(src.getName(), compareTo.getName()) && 79 match(src.getLocale(), compareTo.getLocale()) && 80 match(src.getExtensionParameter(), compareTo.getExtensionParameter()); 81 } 82 83 protected boolean match(String str, String reference) { 84 if (str == null) return true; 85 if (reference == null) return false; 86 return str.equals(reference); 87 } 88 89 protected boolean match(Locale locale, Locale reference) { 90 if (locale == null) return true; 91 if (reference == null) return false; 92 return 93 match(locale.getLanguage(), reference.getLanguage()) && 94 match(locale.getCountry(), reference.getCountry()) && 95 match(locale.getVariant(), reference.getVariant()); 96 } 97 98 } 99 | Popular Tags |