| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.accum.Accumulator; |
| 12 |
import net.sf.saxon.expr.accum.AccumulatorRegistry; |
| 13 |
import net.sf.saxon.expr.sort.MergeInstr; |
| 14 |
import net.sf.saxon.lib.Validation; |
| 15 |
import net.sf.saxon.om.AttributeCollection; |
| 16 |
import net.sf.saxon.om.AxisInfo; |
| 17 |
import net.sf.saxon.om.NameChecker; |
| 18 |
import net.sf.saxon.om.NodeInfo; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 21 |
import net.sf.saxon.type.SchemaType; |
| 22 |
import net.sf.saxon.type.Type; |
| 23 |
import net.sf.saxon.value.Whitespace; |
| 24 |
|
| 25 |
import java.util.HashSet; |
| 26 |
import java.util.Set; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class XSLMergeSource extends StyleElement { |
| 34 |
|
| 35 |
|
| 36 |
private Expression forEachItem; |
| 37 |
private Expression forEachSource; |
| 38 |
private Expression select; |
| 39 |
private boolean sortBeforeMerge = false; |
| 40 |
private int mergeKeyCount = 0; |
| 41 |
private String sourceName; |
| 42 |
private int validationAction = Validation.STRIP; |
| 43 |
private SchemaType schemaType = null; |
| 44 |
private boolean streamable = false; |
| 45 |
private Set<Accumulator> accumulators = new HashSet<Accumulator>(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public boolean isInstruction() { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public boolean mayContainSequenceConstructor() { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Expression getForEachItem() { |
| 75 |
return forEachItem; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public Expression getForEachSource() { |
| 85 |
return forEachSource; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public Expression getSelect() { |
| 95 |
return select; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public boolean isSortBeforeMerge() { |
| 104 |
return sortBeforeMerge; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public String getSourceName() { |
| 113 |
return sourceName; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public int getValidationAction() { |
| 122 |
return validationAction; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public SchemaType getSchemaTypeAttribute() { |
| 131 |
return schemaType; |
| 132 |
} |
| 133 |
|
| 134 |
public MergeInstr.MergeSource makeMergeSource(MergeInstr mi, Expression select) { |
| 135 |
MergeInstr.MergeSource ms = new MergeInstr.MergeSource(mi); |
| 136 |
if (forEachItem != null) { |
| 137 |
ms.initForEachItem(mi, forEachItem); |
| 138 |
} |
| 139 |
if (forEachSource != null) { |
| 140 |
ms.initForEachStream(mi, forEachSource); |
| 141 |
} |
| 142 |
if (select != null) { |
| 143 |
this.select = select; |
| 144 |
ms.initRowSelect(mi, select); |
| 145 |
} |
| 146 |
ms.baseURI = getBaseURI(); |
| 147 |
ms.sourceName = sourceName; |
| 148 |
ms.validation = validationAction; |
| 149 |
ms.schemaType = schemaType; |
| 150 |
ms.setStreamable(streamable); |
| 151 |
ms.accumulators = accumulators; |
| 152 |
return ms; |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public Expression compile(Compilation exec, ComponentDeclaration decl) |
| 157 |
throws XPathException { |
| 158 |
return null; |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
protected void prepareAttributes() throws XPathException { |
| 163 |
AttributeCollection atts = getAttributeList(); |
| 164 |
|
| 165 |
String selectAtt = null; |
| 166 |
String forEachItemAtt = null; |
| 167 |
String forEachSourceAtt = null; |
| 168 |
String validationAtt = null; |
| 169 |
String typeAtt = null; |
| 170 |
String streamableAtt = null; |
| 171 |
String useAccumulatorsAtt = null; |
| 172 |
|
| 173 |
for (int a = 0; a < atts.getLength(); a++) { |
| 174 |
String f = atts.getQName(a); |
| 175 |
switch (f) { |
| 176 |
case "for-each-item": |
| 177 |
forEachItemAtt = atts.getValue(a); |
| 178 |
forEachItem = makeExpression(forEachItemAtt, a); |
| 179 |
break; |
| 180 |
case "for-each-source": |
| 181 |
forEachSourceAtt = atts.getValue(a); |
| 182 |
forEachSource = makeExpression(forEachSourceAtt, a); |
| 183 |
break; |
| 184 |
case "select": |
| 185 |
selectAtt = atts.getValue(a); |
| 186 |
select = makeExpression(selectAtt, a); |
| 187 |
break; |
| 188 |
case "sort-before-merge": |
| 189 |
sortBeforeMerge = processBooleanAttribute("sort-before-merge", atts.getValue(a)); |
| 190 |
break; |
| 191 |
case "name": |
| 192 |
String nameAtt = Whitespace.trim(atts.getValue(a)); |
| 193 |
if (NameChecker.isValidNCName(nameAtt)) { |
| 194 |
sourceName = nameAtt; |
| 195 |
} else { |
| 196 |
compileError("xsl:merge-source/@name (" + nameAtt + ") is not a valid NCName", "XTSE0020"); |
| 197 |
} |
| 198 |
break; |
| 199 |
case "validation": |
| 200 |
validationAtt = Whitespace.trim(atts.getValue(a)); |
| 201 |
break; |
| 202 |
case "type": |
| 203 |
typeAtt = Whitespace.trim(atts.getValue(a)); |
| 204 |
break; |
| 205 |
case "streamable": |
| 206 |
streamableAtt = atts.getValue(a); |
| 207 |
break; |
| 208 |
case "use-accumulators": |
| 209 |
useAccumulatorsAtt = Whitespace.trim(atts.getValue(a)); |
| 210 |
break; |
| 211 |
default: |
| 212 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if (sourceName == null) { |
| 218 |
sourceName = "saxon-merge-source-" + hashCode(); |
| 219 |
} |
| 220 |
|
| 221 |
if (forEachItemAtt != null) { |
| 222 |
if (forEachSourceAtt != null) { |
| 223 |
compileError("The for-each-item and for-each-source attributes must not both be present", "XTSE3195"); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if (selectAtt == null) { |
| 228 |
reportAbsence("select"); |
| 229 |
} |
| 230 |
|
| 231 |
if (validationAtt == null) { |
| 232 |
validationAction = getDefaultValidation(); |
| 233 |
} else { |
| 234 |
validationAction = validateValidationAttribute(validationAtt); |
| 235 |
} |
| 236 |
if (typeAtt != null) { |
| 237 |
if (!isSchemaAware()) { |
| 238 |
compileError("The @type attribute is available only with a schema-aware XSLT processor", "XTSE1660"); |
| 239 |
} |
| 240 |
schemaType = getSchemaType(typeAtt); |
| 241 |
validationAction = Validation.BY_TYPE; |
| 242 |
} |
| 243 |
|
| 244 |
if (typeAtt != null && validationAtt != null) { |
| 245 |
compileError("The @validation and @type attributes are mutually exclusive", "XTSE1505"); |
| 246 |
} |
| 247 |
|
| 248 |
if ((typeAtt != null || validationAtt != null) && forEachSourceAtt == null) { |
| 249 |
compileError("The @type and @validation attributes can be used only when @for-each-stream is specified", "XTSE0020"); |
| 250 |
} |
| 251 |
|
| 252 |
if (streamableAtt != null) { |
| 253 |
streamable = processStreamableAtt(streamableAtt); |
| 254 |
if (streamable && forEachSource == null) { |
| 255 |
compileError("Streaming on xsl:merge-source is possible only when @for-each-source is used", "XTSE3195"); |
| 256 |
} |
| 257 |
} else if (forEachSource != null) { |
| 258 |
streamable = false; |
| 259 |
} |
| 260 |
|
| 261 |
if (useAccumulatorsAtt == null) { |
| 262 |
useAccumulatorsAtt = ""; |
| 263 |
} |
| 264 |
|
| 265 |
AccumulatorRegistry registry = getPrincipalStylesheetModule().getStylesheetPackage().getAccumulatorRegistry(); |
| 266 |
accumulators = registry.getUsedAccumulators(useAccumulatorsAtt, this); |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 271 |
|
| 272 |
forEachItem = typeCheck("for-each-item", forEachItem); |
| 273 |
forEachSource = typeCheck("for-each-source", forEachSource); |
| 274 |
select = typeCheck("select", select); |
| 275 |
|
| 276 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 277 |
NodeInfo child; |
| 278 |
while ((child = kids.next()) != null) { |
| 279 |
if (child instanceof XSLMergeKey) { |
| 280 |
mergeKeyCount++; |
| 281 |
} else if (child.getNodeKind() == Type.TEXT) { |
| 282 |
|
| 283 |
if (!Whitespace.isWhite(child.getStringValueCS())) { |
| 284 |
compileError("No character data is allowed within xsl:merge-source", "XTSE0010"); |
| 285 |
} |
| 286 |
} else if (child instanceof StyleElement) { |
| 287 |
((StyleElement) child).compileError("No children other than xsl:merge-key are allowed within xsl:merge-source", "XTSE0010"); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (mergeKeyCount == 0) { |
| 292 |
compileError("xsl:merge-source must have exactly at least one xsl:merge-key child element", "XTSE0010"); |
| 293 |
} |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
} |