1 package com.icl.saxon; 2 import com.icl.saxon.expr.StandaloneContext; 3 import com.icl.saxon.expr.XPathException; 4 import com.icl.saxon.pattern.Pattern; 5 import com.icl.saxon.pattern.UnionPattern; 6 import com.icl.saxon.output.*; 7 import com.icl.saxon.om.*; 8 9 import javax.xml.transform.TransformerException ; 10 11 import java.util.Hashtable ; 12 import java.util.Enumeration ; 13 14 19 20 public class RuleManager { 21 22 private Mode defaultMode; private Hashtable modes; private NamePool namePool; 25 private StandaloneContext standaloneContext; 26 27 30 31 public RuleManager(NamePool pool) { 32 namePool = pool; 33 resetHandlers(); 34 } 35 36 41 42 public void setStandaloneContext(StandaloneContext context) { 43 standaloneContext = context; 44 } 45 46 53 54 public StandaloneContext getStandaloneContext() { 55 if (standaloneContext==null) { 56 standaloneContext = new StandaloneContext(namePool); 57 } 58 return standaloneContext; 59 } 60 61 64 65 public void resetHandlers() { 66 defaultMode = new Mode(); 67 modes = new Hashtable (); 68 } 69 70 76 77 public Mode getMode(int modeNameCode) { 78 if (modeNameCode==-1) { 79 return defaultMode; 80 } 81 Integer modekey = new Integer (modeNameCode & 0xfffff); 82 Mode m = (Mode)modes.get(modekey); 83 if (m==null) { 84 m = new Mode(); 85 m.setNameCode(modeNameCode); 86 modes.put(modekey, m); 87 } 88 return m; 89 } 90 91 99 100 public void setHandler(String pattern, NodeHandler eh) throws XPathException { 101 Pattern match = Pattern.make(pattern, getStandaloneContext()); 102 setHandler(match, eh, defaultMode, 0); 103 } 104 105 114 115 public void setHandler(Pattern pattern, NodeHandler eh, Mode mode, int precedence) { 116 if (pattern instanceof UnionPattern) { 118 UnionPattern up = (UnionPattern)pattern; 119 Pattern p1 = up.getLHS(); 120 Pattern p2 = up.getRHS(); 121 setHandler(p1, eh, mode, precedence); 122 setHandler(p2, eh, mode, precedence); 123 return; 124 } 125 126 double priority = pattern.getDefaultPriority(); 127 setHandler(pattern, eh, mode, precedence, priority); 128 } 129 130 131 142 143 public void setHandler(Pattern pattern, NodeHandler eh, 144 Mode mode, int precedence, double priority) { 145 146 if (pattern instanceof UnionPattern) { 148 UnionPattern up = (UnionPattern)pattern; 149 Pattern p1 = up.getLHS(); 150 Pattern p2 = up.getRHS(); 151 setHandler(p1, eh, mode, precedence, priority); 152 setHandler(p2, eh, mode, precedence, priority); 153 return; 154 } 155 156 mode.addRule(pattern, eh, precedence, priority); 157 } 158 159 166 167 public NodeHandler getHandler (NodeInfo node, Context c) throws TransformerException { 168 return getHandler(node, defaultMode, c); 169 } 170 171 178 179 public NodeHandler getHandler (NodeInfo node, Mode mode, Context c) 180 throws TransformerException { 181 182 if (mode==null) { 183 mode = defaultMode; 184 } 185 186 NodeHandler eh = (NodeHandler)mode.getRule(node, c); 187 188 if (eh!=null) return eh; 189 190 return null; 191 } 192 193 197 198 public NodeHandler getHandler (NodeInfo node, Mode mode, int min, int max, Context c) 199 throws XPathException { 200 if (mode==null) mode = defaultMode; 201 return (NodeHandler)mode.getRule(node, min, max, c); 202 } 203 204 208 209 public Enumeration getAllModes() { 210 return modes.keys(); 211 } 212 213 } 214 215 | Popular Tags |