| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans.rules; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContextMajor; |
| 11 |
import net.sf.saxon.expr.instruct.TemplateRule; |
| 12 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 13 |
import net.sf.saxon.om.Item; |
| 14 |
import net.sf.saxon.pattern.Pattern; |
| 15 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class Rule { |
| 23 |
protected Pattern pattern; |
| 24 |
protected RuleTarget action; |
| 25 |
protected int precedence; |
| 26 |
protected int minImportPrecedence; |
| 27 |
protected double priority; |
| 28 |
|
| 29 |
protected Rule next; |
| 30 |
protected int sequence; |
| 31 |
protected int part; |
| 32 |
|
| 33 |
private boolean alwaysMatches; |
| 34 |
|
| 35 |
private int rank; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public Rule() {} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public Rule( Pattern p, RuleTarget o, int prec, int min, double prio, int seq, int part) { |
| 52 |
pattern = p; |
| 53 |
action = o; |
| 54 |
precedence = prec; |
| 55 |
minImportPrecedence = min; |
| 56 |
priority = prio; |
| 57 |
next = null; |
| 58 |
sequence = seq; |
| 59 |
this.part = part; |
| 60 |
o.registerRule(this); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
protected void copyFrom(Rule r, boolean copyChain) { |
| 71 |
pattern = r.pattern.copy(new RebindingMap()); |
| 72 |
action = r.action instanceof TemplateRule ? ((TemplateRule) r.action).copy() : r.action; |
| 73 |
precedence = r.precedence; |
| 74 |
minImportPrecedence = r.minImportPrecedence; |
| 75 |
priority = r.priority; |
| 76 |
sequence = r.sequence; |
| 77 |
part = r.part; |
| 78 |
if (r.next == null || !copyChain) { |
| 79 |
next = null; |
| 80 |
} else { |
| 81 |
next = r.next.copy(true); |
| 82 |
} |
| 83 |
action.registerRule(this); |
| 84 |
} |
| 85 |
|
| 86 |
public Rule copy(boolean copyChain) { |
| 87 |
Rule r2 = new Rule(); |
| 88 |
r2.copyFrom(this, copyChain); |
| 89 |
return r2; |
| 90 |
} |
| 91 |
|
| 92 |
public int getSequence() { |
| 93 |
return sequence; |
| 94 |
} |
| 95 |
|
| 96 |
public int getPartNumber() { |
| 97 |
return part; |
| 98 |
} |
| 99 |
|
| 100 |
public void setAction( RuleTarget action) { |
| 101 |
this.action = action; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
public RuleTarget getAction() { |
| 106 |
return action; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
public Rule getNext() { |
| 111 |
return next; |
| 112 |
} |
| 113 |
|
| 114 |
public void setNext( Rule next) { |
| 115 |
this.next = next; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public Pattern getPattern() { |
| 120 |
return pattern; |
| 121 |
} |
| 122 |
|
| 123 |
public void setPattern(Pattern pattern) { |
| 124 |
this.pattern = pattern; |
| 125 |
} |
| 126 |
|
| 127 |
public int getPrecedence() { |
| 128 |
return precedence; |
| 129 |
} |
| 130 |
|
| 131 |
public int getMinImportPrecedence() { |
| 132 |
return minImportPrecedence; |
| 133 |
} |
| 134 |
|
| 135 |
public double getPriority() { |
| 136 |
return priority; |
| 137 |
} |
| 138 |
|
| 139 |
public void setAlwaysMatches(boolean matches) { |
| 140 |
alwaysMatches = matches; |
| 141 |
} |
| 142 |
|
| 143 |
public boolean isAlwaysMatches() { |
| 144 |
return alwaysMatches; |
| 145 |
} |
| 146 |
|
| 147 |
public void setRank(int rank) { |
| 148 |
this.rank = rank; |
| 149 |
} |
| 150 |
|
| 151 |
public int getRank() { |
| 152 |
return rank; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public void export(ExpressionPresenter out, boolean modeStreamable) throws XPathException { |
| 162 |
RuleTarget target = getAction(); |
| 163 |
TemplateRule template = null; |
| 164 |
if (target instanceof TemplateRule) { |
| 165 |
template = (TemplateRule) target; |
| 166 |
int s = out.startElement("templateRule"); |
| 167 |
out.emitAttribute("prec", getPrecedence() + ""); |
| 168 |
out.emitAttribute("prio", getPriority() + ""); |
| 169 |
out.emitAttribute("seq", getSequence() + ""); |
| 170 |
if (part != 0) { |
| 171 |
out.emitAttribute("part", "" + part); |
| 172 |
} |
| 173 |
out.emitAttribute("rank", "" + getRank()); |
| 174 |
out.emitAttribute("minImp", getMinImportPrecedence() + ""); |
| 175 |
out.emitAttribute("slots", template.getStackFrameMap().getNumberOfVariables() + ""); |
| 176 |
template.explainProperties(out); |
| 177 |
exportOtherProperties(out); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
out.setChildRole("match"); |
| 197 |
getPattern().export(out); |
| 198 |
if (template.getBody() != null) { |
| 199 |
out.setChildRole("action"); |
| 200 |
template.getBody().export(out); |
| 201 |
} |
| 202 |
int e = out.endElement(); |
| 203 |
if (s != e) { |
| 204 |
throw new IllegalStateException( |
| 205 |
"exported expression tree unbalanced in template at line " + |
| 206 |
(template != null ? |
| 207 |
template.getLineNumber() + " of " + template.getSystemId() : "")); |
| 208 |
} |
| 209 |
} else { |
| 210 |
target.export(out); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public void exportOtherProperties(ExpressionPresenter out) throws XPathException {} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public int compareRank(Rule other) { |
| 232 |
return rank - other.rank; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public int compareComputedRank(Rule other) { |
| 245 |
if (precedence == other.precedence) { |
| 246 |
return Double.compare(priority, other.priority); |
| 247 |
} else if (precedence < other.precedence) { |
| 248 |
return -1; |
| 249 |
} else { |
| 250 |
return +1; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
public boolean matches(Item item, XPathContextMajor context) throws XPathException { |
| 255 |
return alwaysMatches || pattern.matches(item, context); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
} |