1 package net.sf.saxon.trans; 2 import net.sf.saxon.expr.XPathContext; 3 import net.sf.saxon.instruct.Template; 4 import net.sf.saxon.om.NodeInfo; 5 import net.sf.saxon.pattern.Pattern; 6 import net.sf.saxon.pattern.UnionPattern; 7 8 import java.io.Serializable ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 12 17 18 public class RuleManager implements Serializable { 19 20 private Mode defaultMode; private HashMap modes; private Mode omniMode = null; 24 27 28 public RuleManager() { 29 resetHandlers(); 30 } 31 32 35 36 public void resetHandlers() { 37 defaultMode = new Mode(Mode.DEFAULT_MODE); 38 modes = new HashMap (5); 39 } 40 41 48 49 public Mode getMode(int modeNameCode) { 50 if (modeNameCode==Mode.DEFAULT_MODE) { 51 return defaultMode; 52 } 53 if (modeNameCode==Mode.ALL_MODES) { 54 if (omniMode==null) { 55 omniMode = new Mode(Mode.NAMED_MODE); 56 } 57 return omniMode; 58 } 59 Integer modekey = new Integer (modeNameCode & 0xfffff); 60 Mode m = (Mode)modes.get(modekey); 61 if (m==null) { 62 m = new Mode(omniMode); 63 modes.put(modekey, m); 64 } 67 return m; 68 } 69 70 79 80 public void setHandler(Pattern pattern, Template eh, Mode mode, int precedence) { 81 if (pattern instanceof UnionPattern) { 83 UnionPattern up = (UnionPattern)pattern; 84 Pattern p1 = up.getLHS(); 85 Pattern p2 = up.getRHS(); 86 setHandler(p1, eh, mode, precedence); 87 setHandler(p2, eh, mode, precedence); 88 return; 89 } 90 91 double priority = pattern.getDefaultPriority(); 92 setHandler(pattern, eh, mode, precedence, priority); 93 } 94 95 96 106 107 public void setHandler(Pattern pattern, Template eh, 108 Mode mode, int precedence, double priority) { 109 110 if (pattern instanceof UnionPattern) { 112 UnionPattern up = (UnionPattern)pattern; 113 Pattern p1 = up.getLHS(); 114 Pattern p2 = up.getRHS(); 115 setHandler(p1, eh, mode, precedence, priority); 116 setHandler(p2, eh, mode, precedence, priority); 117 return; 118 } 119 mode.addRule(pattern, eh, precedence, priority); 120 121 if (mode==omniMode) { 124 defaultMode.addRule(pattern, eh, precedence, priority); 125 Iterator iter = modes.values().iterator(); 126 while (iter.hasNext()) { 127 Mode m = (Mode)iter.next(); 128 m.addRule(pattern, eh, precedence, priority); 129 } 130 } 131 } 132 133 141 142 public Template getTemplateRule (NodeInfo node, Mode mode, XPathContext c) throws XPathException { 143 144 if (mode==null) { 145 mode = defaultMode; 146 } 147 148 return (Template)mode.getRule(node, c); 149 } 150 151 162 163 public Template getTemplateRule (NodeInfo node, Mode mode, int min, int max, XPathContext c) 164 throws XPathException { 165 if (mode==null) { 166 mode = defaultMode; 167 } 168 return (Template)mode.getRule(node, min, max, c); 169 } 170 171 180 181 public Template getNextMatchHandler(NodeInfo node, Mode mode, Template currentHandler, XPathContext c) 182 throws XPathException { 183 if (mode==null) { 184 mode = defaultMode; 185 } 186 return (Template)mode.getNextMatchRule(node, currentHandler, c); 187 } 188 } 189 190 | Popular Tags |