001: package net.sf.saxon.style;
002:
003: import net.sf.saxon.event.Stripper;
004: import net.sf.saxon.om.NamePool;
005: import net.sf.saxon.om.NodeInfo;
006: import net.sf.saxon.trans.XPathException;
007:
008: import java.util.Arrays;
009:
010: /**
011: * The StylesheetStripper refines the Stripper class to do stripping of
012: * whitespace nodes on a stylesheet. This is handled specially (a) because
013: * it is done at compile time, so there is no Controller available, and (b)
014: * because the rules are very simple
015: * @author Michael H. Kay
016: */
017:
018: public class StylesheetStripper extends Stripper {
019:
020: // Any child of one of the following elements is removed from the tree,
021: // regardless of any xml:space attributes. Note that this array must be in numeric
022: // order for binary chop to work correctly.
023:
024: private static final int[] specials = {
025: StandardNames.XSL_ANALYZE_STRING,
026: StandardNames.XSL_APPLY_IMPORTS,
027: StandardNames.XSL_APPLY_TEMPLATES,
028: StandardNames.XSL_ATTRIBUTE_SET,
029: StandardNames.XSL_CALL_TEMPLATE,
030: StandardNames.XSL_CHARACTER_MAP, StandardNames.XSL_CHOOSE,
031: StandardNames.XSL_NEXT_MATCH, StandardNames.XSL_STYLESHEET,
032: StandardNames.XSL_TRANSFORM };
033:
034: public Stripper getAnother() {
035: StylesheetStripper s = new StylesheetStripper();
036: return s;
037: }
038:
039: /**
040: * Set the rules appropriate for whitespace-stripping in a stylesheet
041: */
042:
043: public void setStylesheetRules(NamePool namePool) {
044: // xsl_text = namePool.getFingerprint(NamespaceConstant.XSLT, "text");
045: // specials[0] = namePool.getFingerprint(NamespaceConstant.XSLT, "analyze-string");
046: // specials[1] = namePool.getFingerprint(NamespaceConstant.XSLT, "apply-imports");
047: // specials[2] = namePool.getFingerprint(NamespaceConstant.XSLT, "apply-templates");
048: // specials[3] = namePool.getFingerprint(NamespaceConstant.XSLT, "attribute-set");
049: // specials[4] = namePool.getFingerprint(NamespaceConstant.XSLT, "call-template");
050: // specials[5] = namePool.getFingerprint(NamespaceConstant.XSLT, "character-map");
051: // specials[6] = namePool.getFingerprint(NamespaceConstant.XSLT, "choose");
052: // specials[7] = namePool.getFingerprint(NamespaceConstant.XSLT, "next-match");
053: // specials[8] = namePool.getFingerprint(NamespaceConstant.XSLT, "stylesheet");
054: // specials[9] = namePool.getFingerprint(NamespaceConstant.XSLT, "transform");
055: }
056:
057: /**
058: * Decide whether an element is in the set of white-space preserving element types
059: * @param nameCode identifies the element being tested
060: */
061:
062: public byte isSpacePreserving(int nameCode) {
063: int fp = nameCode & 0xfffff;
064: if (fp == StandardNames.XSL_TEXT) {
065: return ALWAYS_PRESERVE;
066: }
067: ;
068:
069: if (Arrays.binarySearch(specials, fp) >= 0) {
070: return ALWAYS_STRIP;
071: }
072: // for (int i = 0; i < specials.length; i++) {
073: // if (fp == specials[i]) {
074: // return ALWAYS_STRIP;
075: // }
076: // }
077: return STRIP_DEFAULT;
078: }
079:
080: /**
081: * Decide whether an element is in the set of white-space preserving element types.
082: * This version of the method is useful in cases where getting the namecode of the
083: * element is potentially expensive, e.g. with DOM nodes.
084: * @param element Identifies the element whose whitespace is possibly to
085: * be preserved
086: * @return true if the element is in the set of white-space preserving element types
087: */
088:
089: public byte isSpacePreserving(NodeInfo element) {
090: return isSpacePreserving(element.getNameCode());
091: }
092:
093: /**
094: * Handle a text node
095: */
096:
097: public void characters(CharSequence chars, int locationId,
098: int properties) throws XPathException {
099: // assume adjacent chunks of text are already concatenated
100: super .characters(chars, locationId, properties);
101: }
102:
103: } // end of class StylesheetStripper
104:
105: //
106: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
107: // you may not use this file except in compliance with the License. You may obtain a copy of the
108: // License at http://www.mozilla.org/MPL/
109: //
110: // Software distributed under the License is distributed on an "AS IS" basis,
111: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
112: // See the License for the specific language governing rights and limitations under the License.
113: //
114: // The Original Code is: all this file.
115: //
116: // The Initial Developer of the Original Code is Michael H. Kay.
117: //
118: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
119: //
120: // Contributor(s): none.
121: //
|