| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans.rules; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.Component; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.instruct.TemplateRule; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 15 |
import net.sf.saxon.expr.parser.Token; |
| 16 |
import net.sf.saxon.om.Item; |
| 17 |
import net.sf.saxon.om.StructuredQName; |
| 18 |
import net.sf.saxon.pattern.*; |
| 19 |
import net.sf.saxon.style.StylesheetModule; |
| 20 |
import net.sf.saxon.style.StylesheetPackage; |
| 21 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 22 |
import net.sf.saxon.trans.*; |
| 23 |
|
| 24 |
import java.util.Collection; |
| 25 |
import java.util.HashMap; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public final class RuleManager { |
| 32 |
|
| 33 |
private StylesheetPackage stylesheetPackage; |
| 34 |
private Configuration config; |
| 35 |
private SimpleMode unnamedMode; |
| 36 |
private HashMap<StructuredQName, Mode> modes; |
| 37 |
|
| 38 |
private SimpleMode omniMode = null; |
| 39 |
private int recoveryPolicy; |
| 40 |
private boolean unnamedModeExplicit; |
| 41 |
private CompilerInfo compilerInfo; |
| 42 |
private int nextSequenceNumber = 0; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public RuleManager(StylesheetPackage pack) { |
| 49 |
this(pack, pack.getConfiguration().getDefaultXsltCompilerInfo()); |
| 50 |
} |
| 51 |
|
| 52 |
public RuleManager(StylesheetPackage pack, CompilerInfo compilerInfo) { |
| 53 |
this.stylesheetPackage = pack; |
| 54 |
this.config = pack.getConfiguration(); |
| 55 |
this.compilerInfo = compilerInfo; |
| 56 |
this.unnamedMode = config.makeMode(Mode.UNNAMED_MODE_NAME, this.compilerInfo); |
| 57 |
Component c = unnamedMode.makeDeclaringComponent(Visibility.PRIVATE, stylesheetPackage); |
| 58 |
c.setVisibility(Visibility.PRIVATE, VisibilityProvenance.DEFAULTED); |
| 59 |
this.stylesheetPackage.addComponent(c); |
| 60 |
this.unnamedMode.setRecoveryPolicy(recoveryPolicy); |
| 61 |
this.modes = new HashMap<>(5); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public void setUnnamedModeExplicit(boolean declared) { |
| 71 |
unnamedModeExplicit = declared; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public boolean isUnnamedModeExplicit() { |
| 81 |
return unnamedModeExplicit; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void setCompilerInfo(CompilerInfo compilerInfo) { |
| 90 |
this.compilerInfo = compilerInfo; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public void setRecoveryPolicy(int policy) { |
| 104 |
recoveryPolicy = policy; |
| 105 |
unnamedMode.setRecoveryPolicy(policy); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public int getRecoveryPolicy() { |
| 118 |
return recoveryPolicy; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public Collection<Mode> getAllNamedModes() { |
| 128 |
return modes.values(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public SimpleMode getUnnamedMode() { |
| 139 |
return unnamedMode; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public Mode obtainMode(StructuredQName modeName, boolean createIfAbsent) { |
| 158 |
if (modeName == null || modeName.equals(Mode.UNNAMED_MODE_NAME)) { |
| 159 |
return unnamedMode; |
| 160 |
} |
| 161 |
if (modeName.equals(Mode.OMNI_MODE)) { |
| 162 |
if (omniMode == null) { |
| 163 |
omniMode = config.makeMode(modeName, compilerInfo); |
| 164 |
omniMode.setRecoveryPolicy(recoveryPolicy); |
| 165 |
} |
| 166 |
return omniMode; |
| 167 |
} |
| 168 |
|
| 169 |
Mode m = modes.get(modeName); |
| 170 |
if (m == null && createIfAbsent) { |
| 171 |
m = config.makeMode(modeName, compilerInfo); |
| 172 |
m.setRecoveryPolicy(recoveryPolicy); |
| 173 |
modes.put(modeName, m); |
| 174 |
Component c = m.makeDeclaringComponent(Visibility.PRIVATE, stylesheetPackage); |
| 175 |
c.setVisibility(Visibility.PRIVATE, VisibilityProvenance.DEFAULTED); |
| 176 |
stylesheetPackage.addComponent(c); |
| 177 |
} |
| 178 |
return m; |
| 179 |
} |
| 180 |
|
| 181 |
public void registerMode(Mode mode) { |
| 182 |
modes.put(mode.getModeName(), mode); |
| 183 |
} |
| 184 |
|
| 185 |
public boolean existsOmniMode() { |
| 186 |
return omniMode != null; |
| 187 |
} |
| 188 |
|
| 189 |
public int allocateSequenceNumber() { |
| 190 |
return nextSequenceNumber++; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public int registerRule(Pattern pattern, TemplateRule eh, |
| 212 |
Mode mode, StylesheetModule module, double priority, int position, int part) { |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if (pattern instanceof UnionPattern) { |
| 220 |
UnionPattern up = (UnionPattern) pattern; |
| 221 |
Pattern p1 = up.getLHS(); |
| 222 |
Pattern p2 = up.getRHS(); |
| 223 |
int lhsParts = registerRule(p1, eh, mode, module, priority, position, part); |
| 224 |
int rhsParts = registerRule(p2, eh, mode, module, priority, position, lhsParts); |
| 225 |
return lhsParts + rhsParts; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
if (pattern instanceof NodeTestPattern && |
| 230 |
pattern.getItemType() instanceof CombinedNodeTest && |
| 231 |
((CombinedNodeTest) pattern.getItemType()).getOperator() == Token.UNION) { |
| 232 |
CombinedNodeTest cnt = (CombinedNodeTest) pattern.getItemType(); |
| 233 |
NodeTest[] nt = cnt.getComponentNodeTests(); |
| 234 |
final NodeTestPattern nt0 = new NodeTestPattern(nt[0]); |
| 235 |
ExpressionTool.copyLocationInfo(pattern, nt0); |
| 236 |
int lhsParts = registerRule(nt0, eh, mode, module, priority, position, part); |
| 237 |
final NodeTestPattern nt1 = new NodeTestPattern(nt[1]); |
| 238 |
ExpressionTool.copyLocationInfo(pattern, nt1); |
| 239 |
int rhsParts = registerRule(nt1, eh, mode, module, priority, position, lhsParts); |
| 240 |
return lhsParts + rhsParts; |
| 241 |
} |
| 242 |
if (Double.isNaN(priority)) { |
| 243 |
priority = pattern.getDefaultPriority(); |
| 244 |
} else { |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
part = 0; |
| 249 |
} |
| 250 |
|
| 251 |
if (mode instanceof SimpleMode) { |
| 252 |
((SimpleMode) mode).addRule(pattern, eh, module, module.getPrecedence(), priority, position, part); |
| 253 |
} else { |
| 254 |
mode.getActivePart().addRule(pattern, eh, module, mode.getMaxPrecedence(), priority, position, part); |
| 255 |
} |
| 256 |
return 1; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public Rule getTemplateRule(Item item, Mode mode, int min, int max, XPathContext c) |
| 273 |
throws XPathException { |
| 274 |
|
| 275 |
if (mode == null) { |
| 276 |
mode = unnamedMode; |
| 277 |
} |
| 278 |
return mode.getRule(item, min, max, c); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public void computeRankings() throws XPathException { |
| 290 |
unnamedMode.computeRankings(0); |
| 291 |
for (Mode mode : modes.values()) { |
| 292 |
mode.computeRankings(0); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public void invertStreamableTemplates() throws XPathException { |
| 303 |
unnamedMode.invertStreamableTemplates(); |
| 304 |
for (Mode mode : modes.values()) { |
| 305 |
mode.getActivePart().invertStreamableTemplates(); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public void checkConsistency() throws XPathException { |
| 316 |
unnamedMode.checkForConflictingProperties(); |
| 317 |
for (Mode mode : modes.values()) { |
| 318 |
mode.getActivePart().checkForConflictingProperties(); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public void explainTemplateRules(ExpressionPresenter presenter) throws XPathException { |
| 329 |
unnamedMode.explain(presenter); |
| 330 |
for (Mode mode : modes.values()) { |
| 331 |
mode.explain(presenter); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public void optimizeRules() { |
| 341 |
unnamedMode.optimizeRules(); |
| 342 |
for (Mode mode : modes.values()) { |
| 343 |
mode.getActivePart().optimizeRules(); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|