KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > gettext > GettextResource


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Nam Nguyen
28  */

29
30 package com.caucho.quercus.lib.gettext;
31
32 import com.caucho.vfs.BasicDependencyContainer;
33 import com.caucho.quercus.QuercusModuleException;
34 import com.caucho.quercus.env.Env;
35 import com.caucho.quercus.env.StringValue;
36 import com.caucho.quercus.env.UnicodeValue;
37 import com.caucho.quercus.lib.gettext.expr.PluralExpr;
38 import com.caucho.vfs.Depend;
39 import com.caucho.vfs.Path;
40
41 import java.io.IOException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Locale JavaDoc;
45
46 /**
47  * Represents a container for gettext translations.
48  */

49 class GettextResource
50 {
51   protected Path _pathPO;
52   private Path _pathMO;
53   private Path _currentPath;
54
55   private BasicDependencyContainer _depend;
56
57   private PluralExpr _pluralExpr;
58
59   private HashMap JavaDoc<UnicodeValue, ArrayList JavaDoc<UnicodeValue>> _translations;
60
61   protected GettextResource(Env env,
62                               Path root,
63                               Locale JavaDoc locale,
64                               CharSequence JavaDoc category,
65                               CharSequence JavaDoc domain)
66   {
67     StringBuilder JavaDoc sb = new StringBuilder JavaDoc(locale.toString());
68     sb.append('/');
69     sb.append(category);
70     sb.append('/');
71     sb.append(domain);
72     sb.append(".po");
73
74     _pathPO = lookupPath(env, root, sb.toString());
75
76     sb.setCharAt(sb.length() - 2, 'm');
77     _pathMO = lookupPath(env, root, sb.toString());
78
79     init();
80   }
81
82   private Path lookupPath(Env env, Path root, String JavaDoc relPath)
83   {
84     return root.lookup(relPath);
85   }
86
87   private void init()
88   {
89     if (_pathPO != null && _pathPO.exists())
90       _currentPath = _pathPO;
91     else if (_pathMO != null && _pathMO.exists())
92       _currentPath = _pathMO;
93     else
94       return;
95
96     try {
97       GettextParser parser;
98
99       if (_depend == null)
100         _depend = new BasicDependencyContainer();
101
102       _depend.add(new Depend(_currentPath));
103
104       if (_currentPath == _pathPO)
105         parser = new POFileParser(_currentPath);
106       else
107         parser = new MOFileParser(_currentPath);
108
109       _pluralExpr = parser.getPluralExpr();
110       _translations = parser.readTranslations();
111
112       parser.close();
113
114     } catch (IOException JavaDoc e) {
115       throw new QuercusModuleException(e.getMessage());
116     }
117   }
118
119   /**
120    * Returns the translation for this singular key.
121    *
122    * @param key
123    */

124   protected UnicodeValue getTranslation(StringValue key)
125   {
126     if (isModified())
127       init();
128
129     return getTranslationImpl(key, 0);
130   }
131
132   private boolean isModified()
133   {
134     if (_depend == null)
135       return true;
136
137     return _depend.isModified();
138   }
139
140   /**
141    * Returns the translation for this plural key.
142    *
143    * @param key
144    * @param quantity
145    */

146   protected UnicodeValue getTranslation(StringValue key, int quantity)
147   {
148     if (isModified())
149       init();
150
151     if (_pluralExpr != null)
152       return getTranslationImpl(key, _pluralExpr.eval(quantity));
153     else
154       return null;
155   }
156
157   /**
158    * Returns the translation for this key at the specified index in the array.
159    *
160    * @param key to find translation of
161    * @param index in the array for this key
162    *
163    * @return translated string, else null on error.
164    */

165   protected UnicodeValue getTranslationImpl(StringValue key, int index)
166   {
167     if (_translations == null)
168       return null;
169
170     ArrayList JavaDoc<UnicodeValue> pluralForms = _translations.get(key);
171
172     if (pluralForms == null || pluralForms.size() == 0)
173       return null;
174
175     if (index < pluralForms.size())
176       return pluralForms.get(index);
177     else
178       return pluralForms.get(0);
179   }
180 }
181
Popular Tags