KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > java > XslNumber


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xsl.java;
30
31 import com.caucho.java.JavaWriter;
32 import com.caucho.xml.QName;
33 import com.caucho.xpath.Expr;
34 import com.caucho.xpath.pattern.AbstractPattern;
35 import com.caucho.xsl.XslNumberFormat;
36 import com.caucho.xsl.XslParseException;
37
38 /**
39  * Returns the value of an expression.
40  */

41 public class XslNumber extends XslNode {
42   private String JavaDoc _value;
43   private String JavaDoc _count;
44   private String JavaDoc _from;
45   private String JavaDoc _level = "single";
46   private String JavaDoc _format;
47   private String JavaDoc _letter;
48   private String JavaDoc _lang;
49   private String JavaDoc _groupingSeparator;
50   private String JavaDoc _groupingSize = "";
51
52   /**
53    * Returns the tag name.
54    */

55   public String JavaDoc getTagName()
56   {
57     return "xsl:number";
58   }
59
60   /**
61    * Adds an attribute.
62    */

63   public void addAttribute(QName name, String JavaDoc value)
64     throws XslParseException
65   {
66     if ("value".equals(name.getName())) {
67       _value = value;
68     }
69     else if ("count".equals(name.getName())) {
70       _count = value;
71     }
72     else if ("from".equals(name.getName())) {
73       _from = value;
74     }
75     else if ("level".equals(name.getName())) {
76       _level = value;
77     }
78     else if ("format".equals(name.getName())) {
79       _format = value;
80     }
81     else if ("letter-value".equals(name.getName())) {
82       _letter = value;
83     }
84     else if ("lang".equals(name.getName())) {
85       _lang = value;
86     }
87     else if ("grouping-separator".equals(name.getName())) {
88       _groupingSeparator = value;
89     }
90     else if ("grouping-size".equals(name.getName())) {
91       _groupingSize = value;
92     }
93     else
94       super.addAttribute(name, value);
95   }
96
97   /**
98    * Ends the attributes.
99    */

100   public void endAttributes()
101     throws XslParseException
102   {
103   }
104
105   /**
106    * Generates the code for the tag
107    *
108    * @param out the output writer for the generated java.
109    */

110   public void generate(JavaWriter out)
111     throws Exception JavaDoc
112   {
113     int size = 0;
114
115     for (int i = 0; i < _groupingSize.length(); i++) {
116     char ch = _groupingSize.charAt(i);
117     if (ch >= '0' && ch <= '9')
118         size = 10 * size + ch - '0';
119     }
120
121     boolean isAlphabetic = true;
122     if (! "alphabetic".equals(_letter))
123       isAlphabetic = false;
124
125     AbstractPattern countPattern = null;
126     if (_count != null)
127       countPattern = parseMatch(_count);
128
129     AbstractPattern fromPattern = null;
130     if (_from != null)
131       fromPattern = parseMatch(_from);
132
133     if (_level == null || _level.equals("single")) {
134       _level = "single";
135     }
136     else if (_level.equals("multiple")) {
137     }
138     else if (_level.equals("any")) {
139     }
140     else
141       throw error(L.l("xsl:number can't understand level=`{0}'",
142                       _level));
143
144     XslNumberFormat xslFormat;
145     xslFormat = new XslNumberFormat(_format, _lang, isAlphabetic,
146                                     _groupingSeparator, size);
147
148     if (_value != null)
149       printNumber(out, parseExpr(_value), xslFormat);
150     else
151       printNumber(out, _level, countPattern, fromPattern, xslFormat);
152   }
153
154   void printNumber(JavaWriter out, Expr expr, XslNumberFormat format)
155     throws Exception JavaDoc
156   {
157     int index = _gen.addExpr(expr);
158     
159     out.print("exprNumber(out, node, env, _exprs[" + index + "]");
160     out.print(", _xsl_formats[" + _gen.addFormat(format) + "]");
161     out.println(");");
162   }
163
164   void printNumber(JavaWriter out, String JavaDoc level,
165                    AbstractPattern countPattern,
166                    AbstractPattern fromPattern,
167            XslNumberFormat format)
168     throws Exception JavaDoc
169   {
170     if (level.equals("single"))
171       out.print("singleNumber(out, ");
172     else if (level.equals("multiple"))
173       out.print("multiNumber(out, ");
174     else if (level.equals("any"))
175       out.print("anyNumber(out, ");
176     else
177       throw error(L.l("xsl:number cannot understand level='{0}'",
178               level));
179
180     out.print("node, env, ");
181     printPattern(out, countPattern);
182     out.print(", ");
183     printPattern(out, fromPattern);
184     out.print(", _xsl_formats[" + _gen.addFormat(format) + "]");
185     out.println(");");
186   }
187
188   void printPattern(JavaWriter out, AbstractPattern pattern)
189     throws Exception JavaDoc
190   {
191     if (pattern == null)
192       out.print("null");
193     else {
194       out.print("_match_patterns[" + _gen.addMatch(pattern) + "]");
195     }
196   }
197 }
198
Popular Tags