| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Component; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.instruct.AttributeSet; |
| 13 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 14 |
import net.sf.saxon.expr.instruct.UseAttributeSet; |
| 15 |
import net.sf.saxon.expr.parser.Optimizer; |
| 16 |
import net.sf.saxon.lib.NamespaceConstant; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.pattern.NodeKindTest; |
| 19 |
import net.sf.saxon.trace.LocationKind; |
| 20 |
import net.sf.saxon.trans.SymbolicName; |
| 21 |
import net.sf.saxon.trans.Visibility; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.tree.AttributeLocation; |
| 24 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 25 |
import net.sf.saxon.value.Whitespace; |
| 26 |
|
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class XSLAttributeSet extends StyleElement implements StylesheetComponent { |
| 35 |
|
| 36 |
private String nameAtt; |
| 37 |
|
| 38 |
|
| 39 |
private String useAtt; |
| 40 |
|
| 41 |
|
| 42 |
private String visibilityAtt; |
| 43 |
|
| 44 |
|
| 45 |
private SlotManager stackFrameMap; |
| 46 |
|
| 47 |
|
| 48 |
private List<ComponentDeclaration> attributeSetElements = new ArrayList<ComponentDeclaration>(); |
| 49 |
|
| 50 |
|
| 51 |
private StructuredQName[] useAttributeSetNames; |
| 52 |
|
| 53 |
private List<Expression> containedInstructions = new ArrayList<Expression>(); |
| 54 |
|
| 55 |
|
| 56 |
private boolean validated = false; |
| 57 |
private Visibility visibility; |
| 58 |
private boolean streamable = false; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public AttributeSet getActor() { |
| 65 |
return (AttributeSet)getPrincipalStylesheetModule().getStylesheetPackage().getComponent( |
| 66 |
new SymbolicName(StandardNames.XSL_ATTRIBUTE_SET, getObjectName())).getActor(); |
| 67 |
} |
| 68 |
|
| 69 |
public SymbolicName getSymbolicName() { |
| 70 |
return new SymbolicName(StandardNames.XSL_ATTRIBUTE_SET, getObjectName()); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public void checkCompatibility(Component component) throws XPathException { |
| 81 |
if (((AttributeSet)component.getActor()).isDeclaredStreamable() && !isDeclaredStreamable()) { |
| 82 |
compileError("The overridden attribute set is declared streamable, " + |
| 83 |
"so the overriding attribute set must also be declared streamable"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
@Override |
| 95 |
public boolean isDeclaration() { |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public StructuredQName getAttributeSetName() { |
| 106 |
return getObjectName(); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public boolean isDeclaredStreamable() { |
| 115 |
return streamable; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public void prepareAttributes() throws XPathException { |
| 120 |
useAtt = null; |
| 121 |
String streamableAtt = null; |
| 122 |
|
| 123 |
AttributeCollection atts = getAttributeList(); |
| 124 |
|
| 125 |
for (int a = 0; a < atts.getLength(); a++) { |
| 126 |
String f = atts.getQName(a); |
| 127 |
if (f.equals("name")) { |
| 128 |
nameAtt = Whitespace.trim(atts.getValue(a)); |
| 129 |
} else if (f.equals("use-attribute-sets")) { |
| 130 |
useAtt = atts.getValue(a); |
| 131 |
} else if (f.equals("streamable")) { |
| 132 |
streamableAtt = atts.getValue(a); |
| 133 |
} else if (f.equals("visibility")) { |
| 134 |
visibilityAtt = Whitespace.trim(atts.getValue(a)); |
| 135 |
} else { |
| 136 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (nameAtt == null) { |
| 141 |
reportAbsence("name"); |
| 142 |
setObjectName(new StructuredQName("", "", "attribute-set-error-name")); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
if (visibilityAtt == null) { |
| 147 |
visibility = Visibility.PRIVATE; |
| 148 |
} else { |
| 149 |
visibility = interpretVisibilityValue(visibilityAtt, ""); |
| 150 |
} |
| 151 |
|
| 152 |
if (streamableAtt != null) { |
| 153 |
streamable = processStreamableAtt(streamableAtt); |
| 154 |
} |
| 155 |
|
| 156 |
try { |
| 157 |
setObjectName(makeQName(nameAtt)); |
| 158 |
} catch (XPathException err) { |
| 159 |
setObjectName(new StructuredQName("", "", "attribute-set-error-name")); |
| 160 |
err.setLocation(new AttributeLocation(this, StructuredQName.fromClarkName("name"))); |
| 161 |
throw err; |
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public StructuredQName getObjectName() { |
| 175 |
StructuredQName o = super.getObjectName(); |
| 176 |
if (o == null) { |
| 177 |
try { |
| 178 |
prepareAttributes(); |
| 179 |
o = getObjectName(); |
| 180 |
} catch (XPathException err) { |
| 181 |
o = new StructuredQName("saxon", NamespaceConstant.SAXON, "badly-named-attribute-set"); |
| 182 |
setObjectName(o); |
| 183 |
} |
| 184 |
} |
| 185 |
return o; |
| 186 |
} |
| 187 |
|
| 188 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 189 |
|
| 190 |
if (validated) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
checkTopLevel("XTSE0010", true); |
| 195 |
|
| 196 |
stackFrameMap = getConfiguration().makeSlotManager(); |
| 197 |
|
| 198 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 199 |
NodeInfo child; |
| 200 |
while ((child = kids.next()) != null) { |
| 201 |
if (child instanceof XSLAttribute) { |
| 202 |
if (visibility == Visibility.ABSTRACT) { |
| 203 |
compileError("An abstract attribute-set must contain no xsl:attribute instructions"); |
| 204 |
} |
| 205 |
} else { |
| 206 |
compileError("Only xsl:attribute is allowed within xsl:attribute-set", "XTSE0010"); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (useAtt != null) { |
| 211 |
if (visibility == Visibility.ABSTRACT) { |
| 212 |
compileError("An abstract attribute-set must have no @use-attribute-sets attribute"); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
useAttributeSetNames = getUsedAttributeSets(useAtt); |
| 218 |
} |
| 219 |
|
| 220 |
validated = true; |
| 221 |
} |
| 222 |
|
| 223 |
public StructuredQName[] getUseAttributeSetNames() { |
| 224 |
return useAttributeSetNames; |
| 225 |
} |
| 226 |
|
| 227 |
public void index(ComponentDeclaration decl, PrincipalStylesheetModule top) throws XPathException { |
| 228 |
top.indexAttributeSet(decl); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public void checkCircularity(XSLAttributeSet origin) throws XPathException { |
| 242 |
if (this == origin) { |
| 243 |
compileError("The definition of the attribute set is circular", "XTSE0720"); |
| 244 |
} else { |
| 245 |
if (!validated) { |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
return; |
| 250 |
} |
| 251 |
if (attributeSetElements != null) { |
| 252 |
for (ComponentDeclaration attributeSetElement : attributeSetElements) { |
| 253 |
XSLAttributeSet element = (XSLAttributeSet) attributeSetElement.getSourceElement(); |
| 254 |
element.checkCircularity(origin); |
| 255 |
if (streamable && !element.streamable) { |
| 256 |
compileError("Attribute-set is declared streamable but references a non-streamable attribute set " + |
| 257 |
element.getAttributeSetName().getDisplayName(), "XTSE3430"); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public List<Expression> getContainedInstructions() { |
| 270 |
return containedInstructions; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public SlotManager getSlotManager() { |
| 278 |
return stackFrameMap; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException { |
| 289 |
|
| 290 |
if (isActionCompleted(ACTION_COMPILE)) { |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
if (useAtt != null) { |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
List<UseAttributeSet> invocations = UseAttributeSet.makeUseAttributeSetInstructions(useAttributeSetNames, this); |
| 299 |
if (!invocations.isEmpty()) { |
| 300 |
containedInstructions.add(UseAttributeSet.makeCompositeExpression(invocations)); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
for (StructuredQName name : useAttributeSetNames) { |
| 306 |
getPrincipalStylesheetModule().getAttributeSets(name, attributeSetElements); |
| 307 |
} |
| 308 |
|
| 309 |
for (ComponentDeclaration attributeSetElement : attributeSetElements) { |
| 310 |
((XSLAttributeSet) attributeSetElement.getSourceElement()).checkCircularity(this); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
if (streamable) { |
| 316 |
for (ComponentDeclaration attributeSetElement : attributeSetElements) { |
| 317 |
if (!((XSLAttributeSet) attributeSetElement.getSourceElement()).streamable) { |
| 318 |
compileError("Attribute set is declared streamable, " + |
| 319 |
"but references an attribute set that is not declared streamable", "XTSE0730"); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
XSLAttribute node; |
| 327 |
AxisIterator iter = iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 328 |
while ((node = (XSLAttribute) iter.next()) != null) { |
| 329 |
Expression inst = node.compile(compilation, decl); |
| 330 |
inst.setRetainedStaticContext(makeRetainedStaticContext()); |
| 331 |
inst = inst.simplify(); |
| 332 |
if (compilation.getCompilerInfo().isCompileWithTracing()) { |
| 333 |
inst = makeTraceInstruction(this, inst); |
| 334 |
} |
| 335 |
containedInstructions.add(inst); |
| 336 |
} |
| 337 |
|
| 338 |
setActionCompleted(ACTION_COMPILE); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public void optimize(ComponentDeclaration declaration) throws XPathException { |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public int getConstructType() { |
| 359 |
return StandardNames.XSL_ATTRIBUTE_SET; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public void generateByteCode(Optimizer opt) {} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
} |
| 384 |
|