| 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.instruct.GlobalContextRequirement; |
| 12 |
import net.sf.saxon.om.*; |
| 13 |
import net.sf.saxon.pattern.NodeKindTest; |
| 14 |
import net.sf.saxon.trans.*; |
| 15 |
import net.sf.saxon.trans.rules.RuleManager; |
| 16 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 17 |
import net.sf.saxon.type.Type; |
| 18 |
import net.sf.saxon.value.Whitespace; |
| 19 |
|
| 20 |
import java.util.ArrayList; |
| 21 |
import java.util.HashSet; |
| 22 |
import java.util.List; |
| 23 |
import java.util.Set; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class XSLUsePackage extends StyleElement { |
| 29 |
|
| 30 |
private String nameAtt = null; |
| 31 |
private PackageVersionRanges versionRanges = null; |
| 32 |
private StylesheetPackage usedPackage; |
| 33 |
private List<XSLAccept> acceptors = null; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public void findUsedPackage(CompilerInfo info) throws XPathException { |
| 40 |
if (usedPackage == null) { |
| 41 |
if (nameAtt == null) { |
| 42 |
nameAtt = Whitespace.trim(getAttributeValue("", "name")); |
| 43 |
} |
| 44 |
usedPackage = info.getPackageLibrary().findPackage(nameAtt, getPackageVersionRanges()).loadedPackage; |
| 45 |
if (usedPackage == null) { |
| 46 |
compileError("Package " + getAttributeValue("name") + " could not be found", "XTSE3000"); |
| 47 |
|
| 48 |
usedPackage = getConfiguration().makeStylesheetPackage(); |
| 49 |
usedPackage.setJustInTimeCompilation(info.isJustInTimeCompilation()); |
| 50 |
} |
| 51 |
GlobalContextRequirement gcr = usedPackage.getContextItemRequirements(); |
| 52 |
if (gcr != null && !gcr.isMayBeOmitted()) { |
| 53 |
compileError("Package " + getAttributeValue("name") + |
| 54 |
" requires a global context item, so it cannot be used as a library package", "XTTE0590"); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public StylesheetPackage getUsedPackage() { |
| 68 |
return usedPackage; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public PackageVersionRanges getPackageVersionRanges() throws XPathException { |
| 82 |
if (versionRanges == null) { |
| 83 |
try { |
| 84 |
prepareAttributes(); |
| 85 |
} catch (XPathException e) { |
| 86 |
versionRanges = new PackageVersionRanges("*"); |
| 87 |
} |
| 88 |
} |
| 89 |
return versionRanges; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
protected void prepareAttributes() throws XPathException { |
| 95 |
AttributeCollection atts = getAttributeList(); |
| 96 |
String ranges = "*"; |
| 97 |
for (int a = 0; a < atts.getLength(); a++) { |
| 98 |
String f = atts.getQName(a); |
| 99 |
if (f.equals("name")) { |
| 100 |
nameAtt = Whitespace.trim(atts.getValue(a)); |
| 101 |
} else if (f.equals("package-version")) { |
| 102 |
ranges = Whitespace.trim(atts.getValue(a)).replaceAll("\\\\", ""); |
| 103 |
} else { |
| 104 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 105 |
} |
| 106 |
} |
| 107 |
versionRanges = new PackageVersionRanges(ranges); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public boolean isDeclaration() { |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 116 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 117 |
NodeInfo child; |
| 118 |
while ((child = kids.next()) != null) { |
| 119 |
if (child.getNodeKind() == Type.TEXT) { |
| 120 |
compileError("Character content is not allowed as a child of xsl:use-package"); |
| 121 |
} else if (child instanceof XSLAccept || child instanceof XSLOverride) { |
| 122 |
|
| 123 |
} else { |
| 124 |
compileError("Child element " + Err.wrap(child.getDisplayName(), Err.ELEMENT) + |
| 125 |
" is not allowed as a child of xsl:use-package", "XTSE0010"); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public Set<SymbolicName> getExplicitAcceptedComponentNames() throws XPathException { |
| 131 |
Set<SymbolicName> explicitAccepts = new HashSet<SymbolicName>(); |
| 132 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 133 |
NodeInfo child; |
| 134 |
while ((child = kids.next()) != null) { |
| 135 |
if (child instanceof XSLAccept) { |
| 136 |
Set<ComponentTest> explicitComponentTests = ((XSLAccept) child).getExplicitComponentTests(); |
| 137 |
for (ComponentTest test : explicitComponentTests) { |
| 138 |
SymbolicName name = test.getSymbolicNameIfExplicit(); |
| 139 |
explicitAccepts.add(name); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
return explicitAccepts; |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void postValidate() throws XPathException { |
| 148 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 149 |
NodeInfo curr; |
| 150 |
while ((curr = kids.next()) != null) { |
| 151 |
if (curr instanceof XSLOverride || curr instanceof XSLAccept) { |
| 152 |
((StyleElement) curr).postValidate(); |
| 153 |
} |
| 154 |
} |
| 155 |
Set<SymbolicName> accepts = getExplicitAcceptedComponentNames(); |
| 156 |
Set<SymbolicName> overrides = getNamedOverrides(); |
| 157 |
accepts.retainAll(overrides); |
| 158 |
if (!accepts.isEmpty()) { |
| 159 |
StringBuilder duplicates = new StringBuilder(); |
| 160 |
boolean first = true; |
| 161 |
for (SymbolicName name : accepts) { |
| 162 |
if (first) { |
| 163 |
first = false; |
| 164 |
} else { |
| 165 |
duplicates.append(", "); |
| 166 |
} |
| 167 |
duplicates.append(name.toString()); |
| 168 |
} |
| 169 |
compileError("Cannot accept and override the same component (" + duplicates + ")", "XTSE3051"); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public List<XSLAccept> getAcceptors() { |
| 180 |
if (this.acceptors == null) { |
| 181 |
acceptors = new ArrayList<XSLAccept>(); |
| 182 |
AxisIterator useKids = iterateAxis(AxisInfo.CHILD); |
| 183 |
NodeInfo decl; |
| 184 |
while ((decl = useKids.next()) != null) { |
| 185 |
if (decl instanceof XSLAccept) { |
| 186 |
acceptors.add((XSLAccept) decl); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
return acceptors; |
| 191 |
} |
| 192 |
|
| 193 |
; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public void gatherNamedOverrides(PrincipalStylesheetModule module, |
| 207 |
List<ComponentDeclaration> topLevel, |
| 208 |
Set<SymbolicName> overrides) |
| 209 |
throws XPathException { |
| 210 |
if (usedPackage == null) { |
| 211 |
return; |
| 212 |
} |
| 213 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 214 |
NodeInfo override; |
| 215 |
while ((override = kids.next()) != null) { |
| 216 |
if (override instanceof XSLOverride) { |
| 217 |
AxisIterator overridings = override.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 218 |
NodeInfo overridingDeclaration; |
| 219 |
while ((overridingDeclaration = overridings.next()) != null) { |
| 220 |
if (overridingDeclaration instanceof StylesheetComponent) { |
| 221 |
ComponentDeclaration decl = new ComponentDeclaration(module, (StyleElement) overridingDeclaration); |
| 222 |
topLevel.add(decl); |
| 223 |
SymbolicName name = ((StylesheetComponent) overridingDeclaration).getSymbolicName(); |
| 224 |
if (name != null) { |
| 225 |
overrides.add(name); |
| 226 |
} else if (overridingDeclaration instanceof XSLTemplate && overridingDeclaration.getAttributeValue("", "match") != null) { |
| 227 |
StructuredQName[] modeNames = ((XSLTemplate)overridingDeclaration).getModeNames(); |
| 228 |
for (StructuredQName m : modeNames) { |
| 229 |
overrides.add(new SymbolicName(StandardNames.XSL_MODE, m)); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
public Set<SymbolicName> getNamedOverrides() |
| 239 |
throws XPathException { |
| 240 |
Set<SymbolicName> overrides = new HashSet<SymbolicName>(); |
| 241 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 242 |
NodeInfo override; |
| 243 |
while ((override = kids.next()) != null) { |
| 244 |
if (override instanceof XSLOverride) { |
| 245 |
AxisIterator overridings = override.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 246 |
NodeInfo overridingDeclaration; |
| 247 |
while ((overridingDeclaration = overridings.next()) != null) { |
| 248 |
if (overridingDeclaration instanceof StylesheetComponent) { |
| 249 |
SymbolicName name = ((StylesheetComponent) overridingDeclaration).getSymbolicName(); |
| 250 |
if (name != null) { |
| 251 |
overrides.add(name); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
return overrides; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public void gatherRuleOverrides(PrincipalStylesheetModule module, Set<SymbolicName> overrides) |
| 274 |
throws XPathException { |
| 275 |
StylesheetPackage thisPackage = module.getStylesheetPackage(); |
| 276 |
RuleManager ruleManager = module.getRuleManager(); |
| 277 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 278 |
Set<SymbolicName> overriddenModes = new HashSet<SymbolicName>(); |
| 279 |
|
| 280 |
|
| 281 |
NodeInfo override; |
| 282 |
while ((override = kids.next()) != null) { |
| 283 |
if (override instanceof XSLOverride) { |
| 284 |
AxisIterator overridings = override.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 285 |
NodeInfo overridingDeclaration; |
| 286 |
while ((overridingDeclaration = overridings.next()) != null) { |
| 287 |
if (overridingDeclaration instanceof XSLTemplate && overridingDeclaration.getAttributeValue("", "match") != null) { |
| 288 |
StructuredQName[] modeNames = ((XSLTemplate) overridingDeclaration).getModeNames(); |
| 289 |
for (StructuredQName modeName : modeNames) { |
| 290 |
if (modeName.equals(Mode.OMNI_MODE)) { |
| 291 |
((StyleElement) overridingDeclaration).compileError( |
| 292 |
"The mode name #all must not appear in an overriding template rule", "XTSE3440"); |
| 293 |
continue; |
| 294 |
} |
| 295 |
SymbolicName symbolicName = new SymbolicName(StandardNames.XSL_MODE, modeName); |
| 296 |
overrides.add(symbolicName); |
| 297 |
overriddenModes.add(symbolicName); |
| 298 |
Component.M derivedComponent = (Component.M)thisPackage.getComponent(symbolicName); |
| 299 |
|
| 300 |
if (derivedComponent == null) { |
| 301 |
((StyleElement) overridingDeclaration).compileError( |
| 302 |
"Mode " + modeName.getDisplayName() + " is not defined in the used package", "XTSE3060"); |
| 303 |
continue; |
| 304 |
} |
| 305 |
|
| 306 |
if (derivedComponent.getBaseComponent() == null) { |
| 307 |
((StyleElement) overridingDeclaration).compileError( |
| 308 |
"Mode " + modeName.getDisplayName() + |
| 309 |
" cannot be overridden because it is local to this package", "XTSE3440"); |
| 310 |
continue; |
| 311 |
} |
| 312 |
|
| 313 |
Component.M usedComponent = (Component.M)derivedComponent.getBaseComponent(); |
| 314 |
|
| 315 |
if (usedComponent.getVisibility() == Visibility.FINAL) { |
| 316 |
((StyleElement) overridingDeclaration).compileError( |
| 317 |
"Cannot define overriding template rules in mode " + modeName.getDisplayName() + |
| 318 |
" because it has visibility=final", "XTSE3060"); |
| 319 |
continue; |
| 320 |
} |
| 321 |
|
| 322 |
Mode usedMode = usedComponent.getActor(); |
| 323 |
if (usedComponent.getVisibility() != Visibility.PUBLIC) { |
| 324 |
((StyleElement) overridingDeclaration).compileError( |
| 325 |
"Cannot override template rules in mode " + modeName.getDisplayName() + |
| 326 |
", because the mode is not public", "XTSE3060"); |
| 327 |
continue; |
| 328 |
} |
| 329 |
if (derivedComponent.getActor() == usedMode) { |
| 330 |
SimpleMode overridingMode = new SimpleMode(modeName); |
| 331 |
CompoundMode newCompoundMode = new CompoundMode(usedMode, overridingMode); |
| 332 |
newCompoundMode.setDeclaringComponent(derivedComponent); |
| 333 |
ruleManager.registerMode(newCompoundMode); |
| 334 |
derivedComponent.setActor(newCompoundMode); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
RuleManager usedPackageRuleManager = usedPackage.getRuleManager(); |
| 346 |
if (usedPackageRuleManager != null) { |
| 347 |
for (Mode m : usedPackageRuleManager.getAllNamedModes()) { |
| 348 |
SymbolicName sn = m.getSymbolicName(); |
| 349 |
if (!overriddenModes.contains(sn)) { |
| 350 |
Component c = thisPackage.getComponent(sn); |
| 351 |
if (c != null && c.getVisibility() != Visibility.PRIVATE) { |
| 352 |
ruleManager.registerMode((Mode) c.getActor()); |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
} |
| 361 |
|