KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > i18n > DefaultBundleMatcher


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.i18n;
9
10 import java.util.List JavaDoc;
11 import java.util.LinkedList JavaDoc;
12 import java.util.Locale JavaDoc;
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 /**
19  * Used to map bundle information to string representation (e.g. URI),
20  * to find the relevant bundle.
21  *
22  * @author <a HREF="mailto:neeme@apache.org">Neeme Praks</a>
23  * @version CVS $Revision: 1.5 $ $Date: 2002/01/02 19:04:56 $ $Author: neeme $
24  */

25 public class DefaultBundleMatcher implements BundleMatcher, Configurable {
26
27     private List JavaDoc bundleInfos = new LinkedList JavaDoc();
28     private String JavaDoc type;
29
30     /**
31      * Get the string form of the bundle, based on bundle info.
32      *
33      * @return the string form
34      */

35     public String JavaDoc 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     /**
47      * Configure the component.
48      *
49      * @param configuration the configuration
50      */

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 JavaDoc className = bundleInfos[i].getAttribute("class", null);
58             if (className != null) {
59                 try {
60                     bundleInfo = (Configurable) Class.forName(className).newInstance();
61                 }
62                 catch (ClassCastException JavaDoc 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 JavaDoc 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 JavaDoc str, String JavaDoc 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 JavaDoc locale, Locale JavaDoc 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