KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > DefaultPropertiesModel


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DefaultPropertiesModel.java,v 1.13 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp;
21
22 import org.apache.log4j.*;
23 import java.util.*;
24
25 import org.enhydra.barracuda.plankton.l10n.*;
26
27
28
29 /**
30  * This class provides a default implementation of a TemplateModel
31  * that looks up its keys in a properties file
32  */

33 public class DefaultPropertiesModel extends AbstractTemplateModel {
34
35     //public vars
36
protected static final Logger logger = Logger.getLogger(DefaultPropertiesModel.class.getName());
37
38     private final String JavaDoc DEFAULT_RESOURCE = "DefaultResource";
39
40     protected String JavaDoc modelName = null;
41     protected String JavaDoc propFileName = null;
42
43
44     //--------------- Constructors ---------------------------
45
public DefaultPropertiesModel() {
46         super();
47     }
48     
49     public DefaultPropertiesModel(String JavaDoc ipropFileName) {
50         setPropFileName(ipropFileName);
51     }
52
53     
54     //--------------- DefaultPropertiesModel -----------------
55
public void setPropFileName(String JavaDoc ipropFileName) {
56         //set the prop file name
57
propFileName = ipropFileName;
58
59         //set the model name based on the prop file name. While the
60
//prop file name is fully qualified, the model name will just be
61
//set to the last portion of the prop file name
62
if (propFileName!=null) {
63             int pos = propFileName.lastIndexOf(".");
64             if (pos>-1) modelName = propFileName.substring(pos+1);
65             else modelName = propFileName;
66         }
67     }
68     
69     
70
71     //--------------- AbstractTemplateModel ------------------
72
public String JavaDoc getName() {
73         return modelName;
74     }
75
76 // public Object getItem(String key, ViewContext vc) {
77
public Object JavaDoc getItem(String JavaDoc key) {
78         //By default, we're going to try and return the key value from
79
//the underlying properties file. To do this, we first need to
80
//try and figure out what format our view expects. We get this
81
//information by looking in the ViewCapabilities file. If the
82
//VC info is not set, just assume default locale...
83

84         //figure out the target locale
85
ViewContext vc = getViewContext();
86         Locale targetLocale = vc.getViewCapabilities().getClientLocale();
87     
88         //get the appropriate resource bundle (we don't need to worry about
89
//caching the resource bundle since the ResourceBundle class already
90
//does that for us (cool!))
91
//jrk_20020414.1_start
92
//If this class is not within the same class loader as the resource
93
//that is being referred to, it won't find it and will throw a
94
//java.util.MissingResourceException. So, we provide a class loader by saying:
95
//Thread.currentThread().getContextClassLoader()
96
//This should fix some classloading issues in Engines with multiple
97
//class loaders (eg.. Tomcat-3.3.x and Tomcat-4.x.x)
98
//ResourceBundle rb = ResourceBundle.getBundle(propFileName, targetLocale);
99
ResourceBundle rb = ResourceBundle.getBundle(propFileName, targetLocale, Thread.currentThread().getContextClassLoader());
100 //jrk_20020414.1_end
101

102         //now try looking up the value
103
return Localize.getString(rb, key);
104     }
105 }
Popular Tags