KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > RuleManager


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 JavaDoc;
10
11 import java.util.Hashtable JavaDoc;
12 import java.util.Enumeration JavaDoc;
13
14 /**
15   * <B>RuleManager</B> maintains a set of template rules, one set for each mode
16   * @version 10 December 1999: carved out of the old Controller class
17   * @author Michael H. Kay (mhkay@iclway.co.uk)
18   */

19   
20 public class RuleManager {
21    
22     private Mode defaultMode; // node handlers with default mode
23
private Hashtable JavaDoc modes; // tables of node handlers for non-default modes
24
private NamePool namePool;
25     private StandaloneContext standaloneContext;
26
27     /**
28     * create a RuleManager and initialise variables
29     */

30
31     public RuleManager(NamePool pool) {
32         namePool = pool;
33         resetHandlers();
34     }
35
36     /**
37     * Set the standalone context for XPath expressions and patterns. This is
38     * used only for expressions and patterns occurring outside the context
39     * of a stylesheet.
40     */

41     
42     public void setStandaloneContext(StandaloneContext context) {
43         standaloneContext = context;
44     }
45
46     /**
47     * Get the standalone context for XPath expressions and patterns. This is
48     * used only for expressions and patterns occurring outside the context
49     * of a stylesheet.
50     * @return the StandaloneContext associated with this RuleManager. Creates a new
51     * one if none has been set explicitly.
52     */

53     
54     public StandaloneContext getStandaloneContext() {
55         if (standaloneContext==null) {
56             standaloneContext = new StandaloneContext(namePool);
57         }
58         return standaloneContext;
59     }
60
61     /**
62     * Set up a new table of handlers.
63     */

64
65     public void resetHandlers() {
66         defaultMode = new Mode();
67         modes = new Hashtable JavaDoc();
68     }
69
70     /**
71     * Get the Mode object for a named mode. If there is not one already registered.
72     * a new Mode is created.
73     * @param modeNameCode The name code of the mode. Supply -1 to get the default
74     * mode.
75     */

76
77     public Mode getMode(int modeNameCode) {
78         if (modeNameCode==-1) {
79             return defaultMode;
80         }
81         Integer JavaDoc modekey = new Integer JavaDoc(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     /**
92       * Register a handler for a particular pattern. This is a convenience interface
93       * that calls setHandler(pattern, eh, mode, precedence) with default mode and precedence.
94       * @param pattern A match pattern
95       * @param eh The NodeHandler to be used
96       * @see NodeHandler
97       * @see Pattern
98       */

99         
100     public void setHandler(String JavaDoc pattern, NodeHandler eh) throws XPathException {
101         Pattern match = Pattern.make(pattern, getStandaloneContext());
102         setHandler(match, eh, defaultMode, 0);
103     }
104
105     /**
106       * Register a handler for a particular pattern. The priority of the rule
107       * is the default priority for the pattern, which depends on the syntax of
108       * the pattern suppllied.
109       * @param pattern A match pattern
110       * @param eh The ElementHandler to be used
111       * @param mode The processing mode
112       * @param precedence The import precedence (use 0 by default)
113       */

114         
115     public void setHandler(Pattern pattern, NodeHandler eh, Mode mode, int precedence) {
116         // for a union pattern, register the parts separately (each with its own priority)
117
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     /**
132       * Register a handler for a particular pattern.
133       * @param pattern Must be a valid Pattern.
134       * @param eh The ElementHandler to be used
135       * @param mode The processing mode to which this element handler applies
136       * @param precedence The import precedence of this rule
137       * @param priority The priority of the rule: if an element matches several patterns, the
138       * one with highest priority is used
139       * @see NodeHandler
140       * @see Pattern
141       */

142
143     public void setHandler(Pattern pattern, NodeHandler eh,
144                  Mode mode, int precedence, double priority) {
145      
146         // for a union pattern, register the parts separately
147
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     /**
160       * Find the handler registered for a particular node in default mode.
161       * @param node The NodeInfo for the relevant node
162       * @return The handler that will process this
163       * node. Returns the default handler for the type of node if there is no specific
164       * one registered.
165       */

166
167     public NodeHandler getHandler (NodeInfo node, Context c) throws TransformerException JavaDoc {
168         return getHandler(node, defaultMode, c);
169     }
170
171     /**
172       * Find the handler registered for a particular node in a specific mode.
173       * @param node The NodeInfo for the relevant node
174       * @param mode The processing mode
175       * @return The handler that will process this node
176       * Returns null if there is no specific handler registered.
177       */

178
179     public NodeHandler getHandler (NodeInfo node, Mode mode, Context c)
180     throws TransformerException JavaDoc {
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     /**
194     * Get a handler whose import precedence is in a particular range. This is used to support
195     * the xsl:apply-imports function
196     */

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     /**
205     * Get a list of all registered modes
206     * @return an Enumeration of all modes in use, excluding the default (unnamed) mode
207     */

208
209     public Enumeration JavaDoc getAllModes() {
210         return modes.keys();
211     }
212
213 }
214
215 //
216
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
217
// you may not use this file except in compliance with the License. You may obtain a copy of the
218
// License at http://www.mozilla.org/MPL/
219
//
220
// Software distributed under the License is distributed on an "AS IS" basis,
221
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
222
// See the License for the specific language governing rights and limitations under the License.
223
//
224
// The Original Code is: all this file.
225
//
226
// The Initial Developer of the Original Code is
227
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
228
//
229
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
230
//
231
// Contributor(s): none.
232
//
233
Popular Tags