KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > trans > RuleManager


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 JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 /**
13   * <B>RuleManager</B> maintains a set of template rules, one set for each mode
14   * @version 10 December 1999: carved out of the old Controller class
15   * @author Michael H. Kay
16   */

17
18 public class RuleManager implements Serializable JavaDoc {
19
20     private Mode defaultMode; // node handlers with default mode
21
private HashMap JavaDoc modes; // tables of node handlers for non-default modes
22
private Mode omniMode = null; // node handlers that specify mode="all"
23

24     /**
25     * create a RuleManager and initialise variables.
26     */

27
28     public RuleManager() {
29         resetHandlers();
30     }
31
32     /**
33     * Set up a new table of handlers.
34     */

35
36     public void resetHandlers() {
37         defaultMode = new Mode(Mode.DEFAULT_MODE);
38         modes = new HashMap JavaDoc(5);
39     }
40
41     /**
42     * Get the Mode object for a named mode. If there is not one already registered.
43     * a new Mode is created.
44     * @param modeNameCode The name code of the mode. Supply Mode.DEFAULT_MODE to get the default
45     * mode or Mode.ALL_MODES to get the Mode object containing "mode=all" rules
46      * @return the Mode with this name
47     */

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 JavaDoc modekey = new Integer JavaDoc(modeNameCode & 0xfffff);
60         Mode m = (Mode)modes.get(modekey);
61         if (m==null) {
62             m = new Mode(omniMode);
63             modes.put(modekey, m);
64             // when creating a specific mode, copy all the rules currently held
65
// in the omniMode, as these apply to all modes
66
}
67         return m;
68     }
69
70     /**
71       * Register a handler for a particular pattern. The priority of the rule
72       * is the default priority for the pattern, which depends on the syntax of
73       * the pattern suppllied.
74       * @param pattern A match pattern
75       * @param eh The ElementHandler to be used
76       * @param mode The processing mode
77       * @param precedence The import precedence (use 0 by default)
78       */

79
80     public void setHandler(Pattern pattern, Template eh, Mode mode, int precedence) {
81         // for a union pattern, register the parts separately (each with its own priority)
82
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     /**
97       * Register a handler for a particular pattern.
98       * @param pattern Must be a valid Pattern.
99       * @param eh The Template to be used
100       * @param mode The processing mode to which this element handler applies
101       * @param precedence The import precedence of this rule
102       * @param priority The priority of the rule: if an element matches several patterns, the
103       * one with highest priority is used
104       * @see Pattern
105       */

106
107     public void setHandler(Pattern pattern, Template eh,
108                  Mode mode, int precedence, double priority) {
109
110         // for a union pattern, register the parts separately
111
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 adding a rule to the omniMode (mode='all') add it to all
122
// the other modes as well
123
if (mode==omniMode) {
124             defaultMode.addRule(pattern, eh, precedence, priority);
125             Iterator JavaDoc 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      /**
134       * Find the template rule registered for a particular node in a specific mode.
135       * @param node The NodeInfo for the relevant node
136       * @param mode The processing mode
137       * @param c The controller for this transformation
138       * @return The template rule that will process this node
139       * Returns null if there is no specific handler registered.
140       */

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     /**
152      * Get a template rule whose import precedence is in a particular range. This is used to support
153      * the xsl:apply-imports function
154      * @param node The node to be matched
155      * @param mode The mode for which a rule is required
156      * @param min The minimum import precedence that the rule must have
157      * @param max The maximum import precedence that the rule must have
158      * @param c The Controller for the transformation
159      * @return The template rule to be invoked
160      * @throws XPathException
161      */

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     /**
172      * Get the next-match handler after the current one
173      * @param node The node to be matched
174      * @param mode The processing mode
175      * @param currentHandler The current template rule
176      * @param c The dynamic context for the transformation
177      * @return The template rule to be executed
178      * @throws XPathException
179      */

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 //
191
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
192
// you may not use this file except in compliance with the License. You may obtain a copy of the
193
// License at http://www.mozilla.org/MPL/
194
//
195
// Software distributed under the License is distributed on an "AS IS" basis,
196
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
197
// See the License for the specific language governing rights and limitations under the License.
198
//
199
// The Original Code is: all this file.
200
//
201
// The Initial Developer of the Original Code is Michael H. Kay.
202
//
203
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
204
//
205
// Contributor(s): none.
206
//
207
Popular Tags