KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > gettext > expr > PluralExpr


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.expr;
31
32 import com.caucho.quercus.env.StringValue;
33
34 /**
35  * Represents a gettext plural expression.
36  */

37 public class PluralExpr
38 {
39   private PluralExprParser _parser;
40
41   private Expr _npluralsExpr;
42   private Expr _pluralExpr;
43
44   private PluralExpr(CharSequence JavaDoc expr)
45   {
46     _parser = new PluralExprParser(expr);
47   }
48
49   private void init()
50   {
51     if (_parser != null) {
52       _npluralsExpr = _parser.getNpluralsExpr();
53       _pluralExpr = _parser.getPluralExpr();
54       _parser = null;
55     }
56   }
57
58   /**
59    * Returns a PluralExpr from the metadata.
60    *
61    * @param metadata contains the plural expression
62    * @return PluralExpr
63    */

64   public static PluralExpr getPluralExpr(StringValue metaData)
65   {
66     String JavaDoc pluralForms = "Plural-Forms:";
67     int i = metaData.indexOf(pluralForms);
68
69     if (i < 0)
70       return new PluralExpr("nplurals=2; plural=n!=1");
71
72     i += pluralForms.length();
73     int j = metaData.indexOf('\n', i);
74
75     if (j < 0)
76       return new PluralExpr(metaData.substring(i));
77     else
78       return new PluralExpr(metaData.substring(i, j));
79   }
80
81   /**
82    * Returns evaluated plural expression
83    *
84    * @param expr
85    * @param quantity number of items
86    */

87   public static int eval(CharSequence JavaDoc expr, int quantity)
88   {
89     return new PluralExpr(expr).eval(quantity);
90   }
91
92   /**
93    * Evaluates this plural expression.
94    */

95   public int eval(int quantity)
96   {
97     init();
98
99     return validate(quantity);
100   }
101
102   /**
103    * Returns a valid plural form index.
104    */

105   private int validate(int quantity)
106   {
107     int pluralForm;
108     int numOfPlurals;
109
110     if (_pluralExpr == null)
111       pluralForm = -1;
112     else
113       pluralForm = _pluralExpr.eval(quantity);
114
115     if (_npluralsExpr == null)
116       numOfPlurals = -1;
117     else
118       numOfPlurals = _npluralsExpr.eval(quantity);
119
120     if (numOfPlurals < 1 || pluralForm < 0)
121     {
122       if (quantity == 1)
123         return 0;
124       else
125         return 1;
126     }
127
128     // pluralForm is a 0-based index
129
if (pluralForm >= numOfPlurals)
130       return 0;
131
132     return pluralForm;
133   }
134 }
135
Popular Tags