Debian Codegraph @

net/sf/saxon/om/StylesheetSpaceStrippingRule.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.om;
9
10
11 import net.sf.saxon.event.ProxyReceiver;
12 import net.sf.saxon.event.Receiver;
13 import net.sf.saxon.event.Stripper;
14 import net.sf.saxon.trace.ExpressionPresenter;
15 import net.sf.saxon.type.SchemaType;
16
17 import java.util.Arrays;
18
19 /**
20 * A whitespace stripping rule that strips whitespace according to the rules defined for XSLT stylesheets
21 */
22
23 public class StylesheetSpaceStrippingRule implements SpaceStrippingRule {
24
25 // Any child of one of the following elements is removed from the tree,
26 // regardless of any xml:space attributes. Note that this array must be in numeric
27 // order for binary chop to work correctly.
28
29 private static final int[] specials = {
30 StandardNames.XSL_ANALYZE_STRING,
31 StandardNames.XSL_APPLY_IMPORTS,
32 StandardNames.XSL_APPLY_TEMPLATES,
33 StandardNames.XSL_ATTRIBUTE_SET,
34 StandardNames.XSL_CALL_TEMPLATE,
35 StandardNames.XSL_CHARACTER_MAP,
36 StandardNames.XSL_CHOOSE,
37 StandardNames.XSL_EVALUATE,
38 StandardNames.XSL_MERGE,
39 StandardNames.XSL_MERGE_SOURCE,
40 StandardNames.XSL_NEXT_ITERATION,
41 StandardNames.XSL_NEXT_MATCH,
42 StandardNames.XSL_STYLESHEET,
43 StandardNames.XSL_TRANSFORM
44 };
45
46 private NamePool namePool;
47
48 public StylesheetSpaceStrippingRule(NamePool pool) {
49 this.namePool = pool;
50 }
51
52 /**
53 * Decide whether an element is in the set of white-space preserving element types
54 * @param elementName identifies the element being tested
55 * @param schemaType not used
56 */
57
58 public int isSpacePreserving(/*@NotNull*/ NodeName elementName, SchemaType schemaType) {
59 int fingerprint = elementName.obtainFingerprint(namePool);
60 if (fingerprint == (StandardNames.XSL_TEXT & NamePool.FP_MASK)) {
61 return Stripper.ALWAYS_PRESERVE;
62 }
63
64 if (Arrays.binarySearch(specials, fingerprint) >= 0) {
65 return Stripper.ALWAYS_STRIP;
66 }
67
68 return Stripper.STRIP_DEFAULT;
69 }
70
71 /**
72 * Make a filter to implement these space-stripping rules, or null if no filtering
73 * is necessary
74 *
75 * @return a filter in the form of a ProxyReceiver, or null
76 */
77 @Override
78 public ProxyReceiver makeStripper(Receiver next) {
79 return new Stripper(this, next);
80 }
81
82 /**
83 * Export this rule as part of an exported stylesheet
84 *
85 * @param presenter the output handler
86 */
87 public void export(ExpressionPresenter presenter) {
88 // no action
89 }
90 }
91