| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Component; |
| 11 |
import net.sf.saxon.expr.ComponentBinding; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.om.Item; |
| 14 |
import net.sf.saxon.om.NamePool; |
| 15 |
import net.sf.saxon.style.StylesheetPackage; |
| 16 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 17 |
import net.sf.saxon.trans.rules.*; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.HashSet; |
| 21 |
import java.util.List; |
| 22 |
import java.util.Set; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class CompoundMode extends Mode { |
| 29 |
|
| 30 |
private Mode base; |
| 31 |
private SimpleMode overrides; |
| 32 |
private int overridingPrecedence; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public CompoundMode(Mode base, SimpleMode overrides) { |
| 41 |
super(base.getModeName()); |
| 42 |
if (!base.getModeName().equals(overrides.getModeName())) { |
| 43 |
throw new AssertionError("Base and overriding modes must have the same name"); |
| 44 |
} |
| 45 |
if (base.getModeName().equals(Mode.UNNAMED_MODE_NAME)) { |
| 46 |
throw new AssertionError("Cannot override an unnamed mode"); |
| 47 |
} |
| 48 |
if (base.getModeName().equals(Mode.OMNI_MODE)) { |
| 49 |
throw new AssertionError("Cannot override mode='#all'"); |
| 50 |
} |
| 51 |
this.base = base; |
| 52 |
this.overrides = overrides; |
| 53 |
this.mustBeTyped = base.mustBeTyped; |
| 54 |
this.mustBeUntyped = base.mustBeUntyped; |
| 55 |
this.overridingPrecedence = base.getMaxPrecedence() + 1; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public BuiltInRuleSet getBuiltInRuleSet() { |
| 67 |
return base.getBuiltInRuleSet(); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
@Override |
| 75 |
public SimpleMode getActivePart() { |
| 76 |
return overrides; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@Override |
| 86 |
public boolean isEmpty() { |
| 87 |
return base.isEmpty() && overrides.isEmpty(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
public int getMaxPrecedence() { |
| 95 |
return overridingPrecedence; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public int getMaxRank() { |
| 104 |
return overrides.getMaxRank(); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void computeRankings(int start) throws XPathException { |
| 117 |
overrides.computeRankings(base.getMaxRank() + 1); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
@Override |
| 127 |
public void processRules(RuleAction action) throws XPathException { |
| 128 |
overrides.processRules(action); |
| 129 |
base.processRules(action); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
@Override |
| 140 |
public Set<String> getExplicitNamespaces(NamePool pool) { |
| 141 |
HashSet<String> r = new HashSet<String>(); |
| 142 |
r.addAll(base.getExplicitNamespaces(pool)); |
| 143 |
r.addAll(overrides.getExplicitNamespaces(pool)); |
| 144 |
return r; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public void allocateAllBindingSlots(final StylesheetPackage pack) { |
| 154 |
if (!bindingSlotsAllocated) { |
| 155 |
List<ComponentBinding> baseBindings = base.getDeclaringComponent().getComponentBindings(); |
| 156 |
List<ComponentBinding> newBindings = new ArrayList<ComponentBinding>(baseBindings); |
| 157 |
Component comp = getDeclaringComponent(); |
| 158 |
comp.setComponentBindings(newBindings); |
| 159 |
SimpleMode.forceAllocateAllBindingSlots(pack, overrides, newBindings); |
| 160 |
bindingSlotsAllocated = true; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public Rule getRule(Item item, XPathContext context) throws XPathException { |
| 174 |
Rule r = overrides.getRule(item, context); |
| 175 |
if (r == null) { |
| 176 |
r = base.getRule(item, context); |
| 177 |
} |
| 178 |
return r; |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public int getStackFrameSlotsNeeded() { |
| 183 |
return Math.max(base.getStackFrameSlotsNeeded(), overrides.getStackFrameSlotsNeeded()); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
@Override |
| 196 |
public Rule getRule(Item item, XPathContext context, SimpleMode.RuleFilter filter) throws XPathException { |
| 197 |
Rule r = overrides.getRule(item, context, filter); |
| 198 |
if (r == null) { |
| 199 |
r = base.getRule(item, context, filter); |
| 200 |
} |
| 201 |
return r; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
@Override |
| 211 |
public void exportTemplateRules(ExpressionPresenter presenter) throws XPathException { |
| 212 |
overrides.exportTemplateRules(presenter); |
| 213 |
base.exportTemplateRules(presenter); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
@Override |
| 223 |
public void explainTemplateRules(ExpressionPresenter presenter) throws XPathException { |
| 224 |
overrides.explainTemplateRules(presenter); |
| 225 |
base.explainTemplateRules(presenter); |
| 226 |
} |
| 227 |
} |