| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 12 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 13 |
import net.sf.saxon.expr.sort.CodepointCollator; |
| 14 |
import net.sf.saxon.expr.sort.SortKeyDefinition; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.lib.StringCollator; |
| 17 |
import net.sf.saxon.om.AttributeCollection; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.type.StringConverter; |
| 20 |
import net.sf.saxon.type.ValidationFailure; |
| 21 |
import net.sf.saxon.value.SequenceType; |
| 22 |
import net.sf.saxon.value.StringValue; |
| 23 |
import net.sf.saxon.value.Whitespace; |
| 24 |
|
| 25 |
import java.net.URI; |
| 26 |
import java.net.URISyntaxException; |
| 27 |
|
| 28 |
public abstract class XSLSortOrMergeKey extends StyleElement { |
| 29 |
|
| 30 |
protected SortKeyDefinition sortKeyDefinition; |
| 31 |
protected Expression select; |
| 32 |
protected Expression order; |
| 33 |
protected Expression dataType = null; |
| 34 |
protected Expression caseOrder; |
| 35 |
protected Expression lang; |
| 36 |
protected Expression collationName; |
| 37 |
protected Expression stable; |
| 38 |
protected boolean useDefaultCollation = true; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public boolean mayContainSequenceConstructor() { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
protected String getErrorCode() { |
| 52 |
return "XTSE1015"; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 57 |
if (select != null && hasChildNodes()) { |
| 58 |
compileError("An " + getDisplayName() + " element with a select attribute must be empty", getErrorCode()); |
| 59 |
} |
| 60 |
if (select == null && !hasChildNodes()) { |
| 61 |
select = new ContextItemExpression(); |
| 62 |
select.setRetainedStaticContext(getStaticContext().makeRetainedStaticContext()); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
if (useDefaultCollation) { |
| 68 |
collationName = new StringLiteral(getDefaultCollationName()); |
| 69 |
} |
| 70 |
|
| 71 |
StringCollator stringCollator = null; |
| 72 |
if (collationName instanceof StringLiteral) { |
| 73 |
String collationString = ((StringLiteral) collationName).getStringValue(); |
| 74 |
try { |
| 75 |
URI collationURI = new URI(collationString); |
| 76 |
if (!collationURI.isAbsolute()) { |
| 77 |
URI base = new URI(getBaseURI()); |
| 78 |
collationURI = base.resolve(collationURI); |
| 79 |
collationString = collationURI.toString(); |
| 80 |
} |
| 81 |
} catch (URISyntaxException err) { |
| 82 |
compileError("Collation name '" + collationString + "' is not a valid URI"); |
| 83 |
collationString = NamespaceConstant.CODEPOINT_COLLATION_URI; |
| 84 |
} |
| 85 |
stringCollator = findCollation(collationString, getBaseURI()); |
| 86 |
if (stringCollator == null) { |
| 87 |
compileError("Collation " + collationString + " has not been defined", "XTDE1035"); |
| 88 |
stringCollator = CodepointCollator.getInstance(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
select = typeCheck("select", select); |
| 93 |
order = typeCheck("order", order); |
| 94 |
caseOrder = typeCheck("case-order", caseOrder); |
| 95 |
lang = typeCheck("lang", lang); |
| 96 |
dataType = typeCheck("data-type", dataType); |
| 97 |
collationName = typeCheck("collation", collationName); |
| 98 |
|
| 99 |
|
| 100 |
if (select != null) { |
| 101 |
try { |
| 102 |
RoleDiagnostic role = |
| 103 |
new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, getDisplayName() + "//select", 0); |
| 104 |
select = getConfiguration().getTypeChecker(false).staticTypeCheck(select, |
| 105 |
SequenceType.ATOMIC_SEQUENCE, |
| 106 |
role, makeExpressionVisitor()); |
| 107 |
} catch (XPathException err) { |
| 108 |
compileError(err); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
sortKeyDefinition = new SortKeyDefinition(); |
| 113 |
sortKeyDefinition.setOrder(order); |
| 114 |
sortKeyDefinition.setCaseOrder(caseOrder); |
| 115 |
sortKeyDefinition.setLanguage(lang); |
| 116 |
sortKeyDefinition.setSortKey(select, true); |
| 117 |
sortKeyDefinition.setDataTypeExpression(dataType); |
| 118 |
sortKeyDefinition.setCollationNameExpression(collationName); |
| 119 |
sortKeyDefinition.setCollation(stringCollator); |
| 120 |
sortKeyDefinition.setBaseURI(getBaseURI()); |
| 121 |
sortKeyDefinition.setStable(stable); |
| 122 |
sortKeyDefinition.setBackwardsCompatible(xPath10ModeIsEnabled()); |
| 123 |
} |
| 124 |
|
| 125 |
protected Expression getStable() { |
| 126 |
return stable; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
protected void prepareAttributes() throws XPathException { |
| 131 |
AttributeCollection atts = getAttributeList(); |
| 132 |
|
| 133 |
String selectAtt = null; |
| 134 |
String orderAtt = null; |
| 135 |
String dataTypeAtt = null; |
| 136 |
String caseOrderAtt = null; |
| 137 |
String langAtt = null; |
| 138 |
String collationAtt = null; |
| 139 |
String stableAtt = null; |
| 140 |
|
| 141 |
for (int a = 0; a < atts.getLength(); a++) { |
| 142 |
String f = atts.getQName(a); |
| 143 |
if (f.equals("select")) { |
| 144 |
selectAtt = atts.getValue(a); |
| 145 |
select = makeExpression(selectAtt, a); |
| 146 |
} else if (f.equals("order")) { |
| 147 |
orderAtt = Whitespace.trim(atts.getValue(a)); |
| 148 |
order = makeAttributeValueTemplate(orderAtt, a); |
| 149 |
} else if (f.equals("data-type")) { |
| 150 |
dataTypeAtt = Whitespace.trim(atts.getValue(a)); |
| 151 |
dataType = makeAttributeValueTemplate(dataTypeAtt, a); |
| 152 |
} else if (f.equals("case-order")) { |
| 153 |
caseOrderAtt = Whitespace.trim(atts.getValue(a)); |
| 154 |
caseOrder = makeAttributeValueTemplate(caseOrderAtt, a); |
| 155 |
} else if (f.equals("lang")) { |
| 156 |
langAtt = Whitespace.trim(atts.getValue(a)); |
| 157 |
lang = makeAttributeValueTemplate(langAtt, a); |
| 158 |
} else if (f.equals("collation")) { |
| 159 |
collationAtt = Whitespace.trim(atts.getValue(a)); |
| 160 |
collationName = makeAttributeValueTemplate(collationAtt, a); |
| 161 |
} else if (f.equals("stable")) { |
| 162 |
stableAtt = Whitespace.trim(atts.getValue(a)); |
| 163 |
stable = makeAttributeValueTemplate(stableAtt, a); |
| 164 |
} else { |
| 165 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (orderAtt == null) { |
| 170 |
order = new StringLiteral("ascending"); |
| 171 |
} else { |
| 172 |
checkAttributeValue("order", orderAtt, true, new String[]{"ascending", "descending"}); |
| 173 |
} |
| 174 |
|
| 175 |
if (dataTypeAtt == null) { |
| 176 |
dataType = null; |
| 177 |
} |
| 178 |
|
| 179 |
if (caseOrderAtt == null) { |
| 180 |
caseOrder = new StringLiteral("#default"); |
| 181 |
} else { |
| 182 |
checkAttributeValue("case-order", caseOrderAtt, true, new String[]{"lower-first", "upper-first"}); |
| 183 |
useDefaultCollation = false; |
| 184 |
} |
| 185 |
|
| 186 |
if (langAtt == null || langAtt.equals("")) { |
| 187 |
lang = new StringLiteral(StringValue.EMPTY_STRING); |
| 188 |
} else { |
| 189 |
useDefaultCollation = false; |
| 190 |
if (lang instanceof StringLiteral) { |
| 191 |
String s = ((StringLiteral) lang).getStringValue(); |
| 192 |
if (s.length() != 0) { |
| 193 |
ValidationFailure vf = StringConverter.StringToLanguage.INSTANCE.validate(s); |
| 194 |
if (vf != null) { |
| 195 |
compileError("The lang attribute must be a valid language code", "XTDE0030"); |
| 196 |
lang = new StringLiteral(StringValue.EMPTY_STRING); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (stableAtt == null) { |
| 203 |
stable = null; |
| 204 |
} else { |
| 205 |
checkAttributeValue("stable", stableAtt, true, StyleElement.YES_NO); |
| 206 |
} |
| 207 |
|
| 208 |
if (collationAtt != null) { |
| 209 |
useDefaultCollation = false; |
| 210 |
} |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 217 |
if (select == null) { |
| 218 |
Expression b = compileSequenceConstructor(exec, decl, true); |
| 219 |
if (b == null) { |
| 220 |
b = Literal.makeEmptySequence(); |
| 221 |
b.setRetainedStaticContext(makeRetainedStaticContext()); |
| 222 |
} |
| 223 |
try { |
| 224 |
Expression atomizedSortKey = Atomizer.makeAtomizer(b, null); |
| 225 |
atomizedSortKey = atomizedSortKey.simplify(); |
| 226 |
ExpressionTool.copyLocationInfo(b, atomizedSortKey); |
| 227 |
sortKeyDefinition.setSortKey(atomizedSortKey, true); |
| 228 |
select = atomizedSortKey; |
| 229 |
} catch (XPathException e) { |
| 230 |
compileError(e); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
sortKeyDefinition = (SortKeyDefinition)sortKeyDefinition.simplify(); |
| 236 |
|
| 237 |
return null; |
| 238 |
} |
| 239 |
|
| 240 |
public SortKeyDefinition getSortKeyDefinition() { |
| 241 |
return sortKeyDefinition; |
| 242 |
} |
| 243 |
|
| 244 |
} |
| 245 |
|