001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl.java;
030:
031: import com.caucho.java.JavaWriter;
032: import com.caucho.xml.QName;
033: import com.caucho.xpath.Expr;
034: import com.caucho.xpath.pattern.AbstractPattern;
035: import com.caucho.xsl.XslNumberFormat;
036: import com.caucho.xsl.XslParseException;
037:
038: /**
039: * Returns the value of an expression.
040: */
041: public class XslNumber extends XslNode {
042: private String _value;
043: private String _count;
044: private String _from;
045: private String _level = "single";
046: private String _format;
047: private String _letter;
048: private String _lang;
049: private String _groupingSeparator;
050: private String _groupingSize = "";
051:
052: /**
053: * Returns the tag name.
054: */
055: public String getTagName() {
056: return "xsl:number";
057: }
058:
059: /**
060: * Adds an attribute.
061: */
062: public void addAttribute(QName name, String value)
063: throws XslParseException {
064: if ("value".equals(name.getName())) {
065: _value = value;
066: } else if ("count".equals(name.getName())) {
067: _count = value;
068: } else if ("from".equals(name.getName())) {
069: _from = value;
070: } else if ("level".equals(name.getName())) {
071: _level = value;
072: } else if ("format".equals(name.getName())) {
073: _format = value;
074: } else if ("letter-value".equals(name.getName())) {
075: _letter = value;
076: } else if ("lang".equals(name.getName())) {
077: _lang = value;
078: } else if ("grouping-separator".equals(name.getName())) {
079: _groupingSeparator = value;
080: } else if ("grouping-size".equals(name.getName())) {
081: _groupingSize = value;
082: } else
083: super .addAttribute(name, value);
084: }
085:
086: /**
087: * Ends the attributes.
088: */
089: public void endAttributes() throws XslParseException {
090: }
091:
092: /**
093: * Generates the code for the tag
094: *
095: * @param out the output writer for the generated java.
096: */
097: public void generate(JavaWriter out) throws Exception {
098: int size = 0;
099:
100: for (int i = 0; i < _groupingSize.length(); i++) {
101: char ch = _groupingSize.charAt(i);
102: if (ch >= '0' && ch <= '9')
103: size = 10 * size + ch - '0';
104: }
105:
106: boolean isAlphabetic = true;
107: if (!"alphabetic".equals(_letter))
108: isAlphabetic = false;
109:
110: AbstractPattern countPattern = null;
111: if (_count != null)
112: countPattern = parseMatch(_count);
113:
114: AbstractPattern fromPattern = null;
115: if (_from != null)
116: fromPattern = parseMatch(_from);
117:
118: if (_level == null || _level.equals("single")) {
119: _level = "single";
120: } else if (_level.equals("multiple")) {
121: } else if (_level.equals("any")) {
122: } else
123: throw error(L.l("xsl:number can't understand level=`{0}'",
124: _level));
125:
126: XslNumberFormat xslFormat;
127: xslFormat = new XslNumberFormat(_format, _lang, isAlphabetic,
128: _groupingSeparator, size);
129:
130: if (_value != null)
131: printNumber(out, parseExpr(_value), xslFormat);
132: else
133: printNumber(out, _level, countPattern, fromPattern,
134: xslFormat);
135: }
136:
137: void printNumber(JavaWriter out, Expr expr, XslNumberFormat format)
138: throws Exception {
139: int index = _gen.addExpr(expr);
140:
141: out.print("exprNumber(out, node, env, _exprs[" + index + "]");
142: out.print(", _xsl_formats[" + _gen.addFormat(format) + "]");
143: out.println(");");
144: }
145:
146: void printNumber(JavaWriter out, String level,
147: AbstractPattern countPattern, AbstractPattern fromPattern,
148: XslNumberFormat format) throws Exception {
149: if (level.equals("single"))
150: out.print("singleNumber(out, ");
151: else if (level.equals("multiple"))
152: out.print("multiNumber(out, ");
153: else if (level.equals("any"))
154: out.print("anyNumber(out, ");
155: else
156: throw error(L.l("xsl:number cannot understand level='{0}'",
157: level));
158:
159: out.print("node, env, ");
160: printPattern(out, countPattern);
161: out.print(", ");
162: printPattern(out, fromPattern);
163: out.print(", _xsl_formats[" + _gen.addFormat(format) + "]");
164: out.println(");");
165: }
166:
167: void printPattern(JavaWriter out, AbstractPattern pattern)
168: throws Exception {
169: if (pattern == null)
170: out.print("null");
171: else {
172: out
173: .print("_match_patterns[" + _gen.addMatch(pattern)
174: + "]");
175: }
176: }
177: }
|