KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
11 import java.util.Locale JavaDoc;
12
13 import org.apache.avalon.framework.logger.AbstractLogEnabled;
14 import org.apache.avalon.framework.component.Component;
15
16 public abstract class AbstractBundle extends AbstractLogEnabled implements Bundle, Component {
17
18     /** bundle info */
19     private BundleInfo bundleInfo;
20
21     /** bundle info mapper */
22     private BundleInfoMapper mapper;
23
24     /** Parent of the current bundle */
25     private Bundle parent = null;
26
27     /** Last modification time */
28     private long lastModified = -1;
29
30     /**
31      * Get the bundle info.
32      */

33     public BundleInfo getBundleInfo() {
34         return this.bundleInfo;
35     }
36
37     /**
38      * Set the bundle info.
39      */

40     public void setBundleInfo(BundleInfo bundleInfo) {
41         if (this.bundleInfo == null) this.bundleInfo = bundleInfo;
42     }
43
44     public BundleInfoMapper getMapper() {
45         return mapper;
46     }
47
48     public void setMapper(BundleInfoMapper mapper) {
49         if (this.mapper == null) this.mapper = mapper;
50     }
51
52     /**
53      * Get the parent bundle of the current bundle.
54      *
55      * @return the parent bundle
56      */

57     public Bundle getParent() {
58         return this.parent;
59     }
60
61     /**
62      * Set the parent bundle of the current bundle.
63      *
64      * @param parent the parent bundle
65      */

66     public void setParent(Bundle parent) {
67         if (this.parent == null) this.parent = parent;
68     }
69
70     /**
71      * Returns the last modification time of this bundle, in milliseconds.
72      *
73      * @return last modification time, -1 if N/A
74      */

75     public long getLastModified() {
76         return this.lastModified;
77     }
78
79     /**
80      * Sets the last modification time of this bundle, in milliseconds.
81      *
82      * @param lastModified last modification time
83      */

84     public void setLastModified(long lastModified) {
85         this.lastModified = lastModified;
86     }
87
88     /**
89      * Get value by key and substitute variables.
90      *
91      * @param key key
92      * @param variables map with variable values
93      * @return value with variable values substituted
94      * @exception MissingResourceException if value was not found
95      */

96     public String JavaDoc getString(String JavaDoc key, Map JavaDoc variables) {
97         return substitute(getString(key), variables);
98     }
99
100     /**
101      * Convert the "user view" of the lookup key to the
102      * "system view". Used to hide the implemented storage
103      * mechanism and/or XML file schema.
104      *
105      * The default implementation just returns the key "as-is".
106      *
107      * @param key user key
108      * @return system key
109      */

110     public String JavaDoc convertKey(String JavaDoc userKey) {
111         return userKey;
112     }
113
114     /**
115      * Substitute the "variables" in the string with the values
116      * provided in the map.
117      *
118      * @param value value where to search for variables
119      * @param dictionary map with variable values
120      * @return value with variable values substituted
121      */

122     public String JavaDoc substitute(String JavaDoc value, Map JavaDoc values) {
123         if (value == null || values == null) return value;
124         if (getLogger().isDebugEnabled()) getLogger().debug("Substituting value: " + value);
125
126         StringBuffer JavaDoc result = new StringBuffer JavaDoc(value.length());
127         int startPos = 0;
128         int endPos = 0;
129         int lastPos = value.length();
130         Object JavaDoc varValue = "";
131         String JavaDoc varKey = "", oldKey = "";
132         while (endPos < lastPos) {
133             startPos = endPos;
134             endPos = value.indexOf('{', startPos);
135             if (endPos == -1)
136                 endPos = lastPos;
137             result.append(value.substring(startPos, endPos));
138             if (endPos < lastPos)
139                 endPos++;
140             if (endPos < lastPos) {
141                 startPos = endPos;
142                 endPos = value.indexOf('}', startPos);
143                 if (endPos == -1)
144                     endPos = lastPos;
145                 oldKey = varKey;
146                 varKey = value.substring(startPos, endPos);
147                 if (!oldKey.equals(varKey))
148                     varValue = values.get(varKey);
149                 if (varValue != null) {
150                     if (getLogger().isDebugEnabled()) getLogger().debug("Substituting var: " + varKey + " --> " + varValue);
151                     result.append(varValue);
152                 }
153                 else {
154                     if (getLogger().isWarnEnabled()) getLogger().warn(bundleInfo + ": var not found: " + varKey);
155                     result.append('{').append(varKey).append('}');
156                 }
157                 if (endPos < lastPos)
158                     endPos++;
159             }
160         }
161         if (getLogger().isDebugEnabled()) getLogger().debug("Returning substituted value: " + result);
162         return result.toString();
163     }
164
165 }
166
Popular Tags