1 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 40 public class XslSort extends XslNode { 41 private String _select; 42 private String _lang; 43 private String _order; 44 private String _caseOrder; 45 private String _dataType; 46 47 50 public String getTagName() 51 { 52 return "xsl:sort"; 53 } 54 57 public void addAttribute(QName name, String 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 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 98 public Sort generateSort() 99 throws Exception 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 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 135 private Expr constructBoolean(String test, String 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 157 public void generate(JavaWriter out) 158 throws Exception 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 |