KickJava   Java API By Example, From Geeks To Geeks.

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


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.xsl.Sort;
35 import com.caucho.xsl.XslParseException;
36
37 /**
38  * Represents the xsl:sort element.
39  */

40 public class XslSort extends XslNode {
41   private String JavaDoc _select;
42   private String JavaDoc _lang;
43   private String JavaDoc _order;
44   private String JavaDoc _caseOrder;
45   private String JavaDoc _dataType;
46
47   /**
48    * Returns the tag name.
49    */

50   public String JavaDoc getTagName()
51   {
52     return "xsl:sort";
53   }
54   /**
55    * Adds an attribute.
56    */

57   public void addAttribute(QName name, String JavaDoc value)
58     throws XslParseException
59   {
60     if (name.getName().equals("select")) {
61       _select = value;
62     }
63     else if (name.getName().equals("case-order")) {
64       if (value.equals("upper-first") ||
65       value.equals("lower-first"))
66     _caseOrder = value;
67       else
68     throw error(L.l("'{0}' is not a valid case-order for xsl:sort.",
69             value));
70     }
71     else if (name.getName().equals("order")) {
72       _order = value;
73     }
74     else if (name.getName().equals("data-type")) {
75       _dataType = value;
76     }
77     else if (name.getName().equals("xsl:lang") ||
78          name.getName().equals("lang")) {
79       _lang = value;
80     }
81     else
82       super.addAttribute(name, value);
83   }
84
85   /**
86    * Ends the attributes.
87    */

88   public void endAttributes()
89     throws XslParseException
90   {
91     if (_select == null)
92       throw error(L.l("<xsl:sort> requires a 'select' attribute."));
93   }
94
95   /**
96    * Generates the sort value.
97    */

98   public Sort generateSort()
99     throws Exception JavaDoc
100   {
101     Expr expr = parseExpr(_select);
102
103     Expr isAscending = constructBoolean(_order, "ascending");
104     Expr caseOrder = constructBoolean(_caseOrder, "upper-first");
105
106     if (_caseOrder == null)
107       caseOrder = null;
108       
109     boolean isText = ! "number".equals(_dataType);
110
111     Sort sort;
112     
113     if (_lang == null) {
114       sort = Sort.create(expr, isAscending, isText);
115     }
116     else {
117       String JavaDoc lang = _lang;
118       
119       if (lang.startsWith("{") && lang.endsWith("}"))
120     lang = lang.substring(1, lang.length() - 1);
121       else
122     lang = "'" + lang + "'";
123
124       sort = Sort.create(expr, isAscending, parseExpr(lang));
125     }
126
127     sort.setCaseOrder(caseOrder);
128
129     return sort;
130   }
131
132   /**
133    * Tests for a boolean EL value.
134    */

135   private Expr constructBoolean(String JavaDoc test, String JavaDoc match)
136     throws XslParseException
137   {
138     if (test == null) {
139       return parseExpr("true()");
140     }
141     else if (test.startsWith("{") && test.endsWith("}")) {
142       test = test.substring(1, test.length() - 1);
143         
144       return parseExpr(test + " = '" + match + "'");
145     }
146     else if (test.equals(match))
147       return parseExpr("true()");
148     else
149       return parseExpr("false()");
150   }
151
152   /**
153    * Generates the code for the tag
154    *
155    * @param out the output writer for the generated java.
156    */

157   public void generate(JavaWriter out)
158     throws Exception JavaDoc
159   {
160     throw error(L.l("<xsl:sort> must be a child of <xsl:for-each> or <xsl:apply-templates>"));
161   }
162 }
163
Popular Tags