KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > StylesheetStripper


1 package net.sf.saxon.style;
2 import net.sf.saxon.event.Stripper;
3 import net.sf.saxon.om.NamePool;
4 import net.sf.saxon.om.NodeInfo;
5 import net.sf.saxon.trans.XPathException;
6
7 import java.util.Arrays JavaDoc;
8
9 /**
10   * The StylesheetStripper refines the Stripper class to do stripping of
11   * whitespace nodes on a stylesheet. This is handled specially (a) because
12   * it is done at compile time, so there is no Controller available, and (b)
13   * because the rules are very simple
14   * @author Michael H. Kay
15   */

16
17 public class StylesheetStripper extends Stripper
18 {
19
20
21     // Any child of one of the following elements is removed from the tree,
22
// regardless of any xml:space attributes. Note that this array must be in numeric
23
// order for binary chop to work correctly.
24

25     private static final int[] specials = {
26         StandardNames.XSL_ANALYZE_STRING,
27         StandardNames.XSL_APPLY_IMPORTS,
28         StandardNames.XSL_APPLY_TEMPLATES,
29         StandardNames.XSL_ATTRIBUTE_SET,
30         StandardNames.XSL_CALL_TEMPLATE,
31         StandardNames.XSL_CHARACTER_MAP,
32         StandardNames.XSL_CHOOSE,
33         StandardNames.XSL_NEXT_MATCH,
34         StandardNames.XSL_STYLESHEET,
35         StandardNames.XSL_TRANSFORM
36     };
37
38     public Stripper getAnother() {
39         StylesheetStripper s = new StylesheetStripper();
40         return s;
41     }
42
43     /**
44     * Set the rules appropriate for whitespace-stripping in a stylesheet
45     */

46
47     public void setStylesheetRules(NamePool namePool) {
48 // xsl_text = namePool.getFingerprint(NamespaceConstant.XSLT, "text");
49
// specials[0] = namePool.getFingerprint(NamespaceConstant.XSLT, "analyze-string");
50
// specials[1] = namePool.getFingerprint(NamespaceConstant.XSLT, "apply-imports");
51
// specials[2] = namePool.getFingerprint(NamespaceConstant.XSLT, "apply-templates");
52
// specials[3] = namePool.getFingerprint(NamespaceConstant.XSLT, "attribute-set");
53
// specials[4] = namePool.getFingerprint(NamespaceConstant.XSLT, "call-template");
54
// specials[5] = namePool.getFingerprint(NamespaceConstant.XSLT, "character-map");
55
// specials[6] = namePool.getFingerprint(NamespaceConstant.XSLT, "choose");
56
// specials[7] = namePool.getFingerprint(NamespaceConstant.XSLT, "next-match");
57
// specials[8] = namePool.getFingerprint(NamespaceConstant.XSLT, "stylesheet");
58
// specials[9] = namePool.getFingerprint(NamespaceConstant.XSLT, "transform");
59
}
60
61     /**
62     * Decide whether an element is in the set of white-space preserving element types
63     * @param nameCode identifies the element being tested
64     */

65
66     public byte isSpacePreserving(int nameCode) {
67         int fp = nameCode & 0xfffff;
68         if (fp == StandardNames.XSL_TEXT) {
69             return ALWAYS_PRESERVE;
70         };
71
72         if (Arrays.binarySearch(specials, fp) >= 0) {
73             return ALWAYS_STRIP;
74         }
75 // for (int i = 0; i < specials.length; i++) {
76
// if (fp == specials[i]) {
77
// return ALWAYS_STRIP;
78
// }
79
// }
80
return STRIP_DEFAULT;
81     }
82
83     /**
84     * Decide whether an element is in the set of white-space preserving element types.
85      * This version of the method is useful in cases where getting the namecode of the
86      * element is potentially expensive, e.g. with DOM nodes.
87     * @param element Identifies the element whose whitespace is possibly to
88      * be preserved
89     * @return true if the element is in the set of white-space preserving element types
90     */

91
92     public byte isSpacePreserving(NodeInfo element) {
93         return isSpacePreserving(element.getNameCode());
94     }
95
96     /**
97     * Handle a text node
98     */

99
100     public void characters (CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
101         // assume adjacent chunks of text are already concatenated
102
super.characters(chars, locationId, properties);
103     }
104
105
106 } // end of class StylesheetStripper
107

108 //
109
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
110
// you may not use this file except in compliance with the License. You may obtain a copy of the
111
// License at http://www.mozilla.org/MPL/
112
//
113
// Software distributed under the License is distributed on an "AS IS" basis,
114
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
115
// See the License for the specific language governing rights and limitations under the License.
116
//
117
// The Original Code is: all this file.
118
//
119
// The Initial Developer of the Original Code is Michael H. Kay.
120
//
121
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
122
//
123
// Contributor(s): none.
124
//
125
Popular Tags