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.xsl.Sort;
035: import com.caucho.xsl.XslParseException;
036:
037: /**
038: * Represents the xsl:sort element.
039: */
040: public class XslSort extends XslNode {
041: private String _select;
042: private String _lang;
043: private String _order;
044: private String _caseOrder;
045: private String _dataType;
046:
047: /**
048: * Returns the tag name.
049: */
050: public String getTagName() {
051: return "xsl:sort";
052: }
053:
054: /**
055: * Adds an attribute.
056: */
057: public void addAttribute(QName name, String value)
058: throws XslParseException {
059: if (name.getName().equals("select")) {
060: _select = value;
061: } else if (name.getName().equals("case-order")) {
062: if (value.equals("upper-first")
063: || value.equals("lower-first"))
064: _caseOrder = value;
065: else
066: throw error(L
067: .l(
068: "'{0}' is not a valid case-order for xsl:sort.",
069: value));
070: } else if (name.getName().equals("order")) {
071: _order = value;
072: } else if (name.getName().equals("data-type")) {
073: _dataType = value;
074: } else if (name.getName().equals("xsl:lang")
075: || name.getName().equals("lang")) {
076: _lang = value;
077: } else
078: super .addAttribute(name, value);
079: }
080:
081: /**
082: * Ends the attributes.
083: */
084: public void endAttributes() throws XslParseException {
085: if (_select == null)
086: throw error(L
087: .l("<xsl:sort> requires a 'select' attribute."));
088: }
089:
090: /**
091: * Generates the sort value.
092: */
093: public Sort generateSort() throws Exception {
094: Expr expr = parseExpr(_select);
095:
096: Expr isAscending = constructBoolean(_order, "ascending");
097: Expr caseOrder = constructBoolean(_caseOrder, "upper-first");
098:
099: if (_caseOrder == null)
100: caseOrder = null;
101:
102: boolean isText = !"number".equals(_dataType);
103:
104: Sort sort;
105:
106: if (_lang == null) {
107: sort = Sort.create(expr, isAscending, isText);
108: } else {
109: String lang = _lang;
110:
111: if (lang.startsWith("{") && lang.endsWith("}"))
112: lang = lang.substring(1, lang.length() - 1);
113: else
114: lang = "'" + lang + "'";
115:
116: sort = Sort.create(expr, isAscending, parseExpr(lang));
117: }
118:
119: sort.setCaseOrder(caseOrder);
120:
121: return sort;
122: }
123:
124: /**
125: * Tests for a boolean EL value.
126: */
127: private Expr constructBoolean(String test, String match)
128: throws XslParseException {
129: if (test == null) {
130: return parseExpr("true()");
131: } else if (test.startsWith("{") && test.endsWith("}")) {
132: test = test.substring(1, test.length() - 1);
133:
134: return parseExpr(test + " = '" + match + "'");
135: } else if (test.equals(match))
136: return parseExpr("true()");
137: else
138: return parseExpr("false()");
139: }
140:
141: /**
142: * Generates the code for the tag
143: *
144: * @param out the output writer for the generated java.
145: */
146: public void generate(JavaWriter out) throws Exception {
147: throw error(L
148: .l("<xsl:sort> must be a child of <xsl:for-each> or <xsl:apply-templates>"));
149: }
150: }
|