| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.Location; |
| 11 |
import net.sf.saxon.lib.NamespaceConstant; |
| 12 |
import net.sf.saxon.om.FingerprintedQName; |
| 13 |
import net.sf.saxon.om.NodeName; |
| 14 |
import net.sf.saxon.om.SpaceStrippingRule; |
| 15 |
import net.sf.saxon.om.StandardNames; |
| 16 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 17 |
import net.sf.saxon.trans.rules.Rule; |
| 18 |
import net.sf.saxon.trans.rules.RuleTarget; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.type.ComplexType; |
| 21 |
import net.sf.saxon.type.SchemaType; |
| 22 |
import net.sf.saxon.type.SimpleType; |
| 23 |
import net.sf.saxon.type.Untyped; |
| 24 |
import net.sf.saxon.value.Whitespace; |
| 25 |
|
| 26 |
import java.util.Arrays; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class Stripper extends ProxyReceiver { |
| 40 |
|
| 41 |
public final static StripRuleTarget STRIP = new StripRuleTarget() { |
| 42 |
}; |
| 43 |
public final static StripRuleTarget PRESERVE = new StripRuleTarget() { |
| 44 |
}; |
| 45 |
protected SpaceStrippingRule rule; |
| 46 |
|
| 47 |
public Stripper( SpaceStrippingRule rule, Receiver next) { |
| 48 |
super(next); |
| 49 |
assert rule != null; |
| 50 |
this.rule = rule; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private byte[] stripStack = new byte[100]; |
| 62 |
private int top = 0; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public Stripper getAnother(Receiver next) { |
| 74 |
return new Stripper(rule, next); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private int isSpacePreserving(NodeName name, SchemaType type) throws XPathException { |
| 91 |
return rule.isSpacePreserving(name, type); |
| 92 |
} |
| 93 |
|
| 94 |
public static final byte ALWAYS_PRESERVE = 0x01; |
| 95 |
public static final byte ALWAYS_STRIP = 0x02; |
| 96 |
public static final byte STRIP_DEFAULT = 0x00; |
| 97 |
public static final byte PRESERVE_PARENT = 0x04; |
| 98 |
public static final byte SIMPLE_CONTENT = 0x08; |
| 99 |
public static final byte ASSERTIONS_EXIST = 0x10; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public void open() throws XPathException { |
| 107 |
|
| 108 |
top = 0; |
| 109 |
stripStack[top] = ALWAYS_PRESERVE; |
| 110 |
super.open(); |
| 111 |
} |
| 112 |
|
| 113 |
public void startElement(NodeName elemName, SchemaType type, Location location, int properties) throws XPathException { |
| 114 |
|
| 115 |
nextReceiver.startElement(elemName, type, location, properties); |
| 116 |
|
| 117 |
byte preserveParent = stripStack[top]; |
| 118 |
byte preserve = (byte) (preserveParent & (PRESERVE_PARENT | ASSERTIONS_EXIST)); |
| 119 |
|
| 120 |
int elementStrip = isSpacePreserving(elemName, type); |
| 121 |
if (elementStrip == ALWAYS_PRESERVE) { |
| 122 |
preserve |= ALWAYS_PRESERVE; |
| 123 |
} else if (elementStrip == ALWAYS_STRIP) { |
| 124 |
preserve |= ALWAYS_STRIP; |
| 125 |
} |
| 126 |
if (type != Untyped.getInstance()) { |
| 127 |
if (preserve == 0) { |
| 128 |
|
| 129 |
if (type.isSimpleType() || ((ComplexType) type).isSimpleContent()) { |
| 130 |
preserve |= SIMPLE_CONTENT; |
| 131 |
} |
| 132 |
} |
| 133 |
if (type instanceof ComplexType && ((ComplexType) type).hasAssertions()) { |
| 134 |
preserve |= ASSERTIONS_EXIST; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
top++; |
| 141 |
if (top >= stripStack.length) { |
| 142 |
stripStack = Arrays.copyOf(stripStack, top * 2); |
| 143 |
} |
| 144 |
stripStack[top] = preserve; |
| 145 |
} |
| 146 |
|
| 147 |
public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) |
| 148 |
throws XPathException { |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if (nameCode.equals(XML_SPACE)) { |
| 153 |
if (Whitespace.normalizeWhitespace(value).equals("preserve")) { |
| 154 |
stripStack[top] |= PRESERVE_PARENT; |
| 155 |
} else { |
| 156 |
stripStack[top] &= ~PRESERVE_PARENT; |
| 157 |
} |
| 158 |
} |
| 159 |
nextReceiver.attribute(nameCode, typeCode, value, locationId, properties); |
| 160 |
} |
| 161 |
|
| 162 |
private static NodeName XML_SPACE = new FingerprintedQName("xml", NamespaceConstant.XML, "space", StandardNames.XML_SPACE); |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public void endElement() throws XPathException { |
| 169 |
nextReceiver.endElement(); |
| 170 |
top--; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 178 |
|
| 179 |
|
| 180 |
if (((((stripStack[top] & (ALWAYS_PRESERVE | PRESERVE_PARENT | SIMPLE_CONTENT | ASSERTIONS_EXIST)) != 0) && |
| 181 |
(stripStack[top] & ALWAYS_STRIP) == 0) |
| 182 |
|| !Whitespace.isWhite(chars)) |
| 183 |
&& chars.length() > 0) { |
| 184 |
nextReceiver.characters(chars, locationId, properties); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public boolean usesTypeAnnotations() { |
| 197 |
return true; |
| 198 |
} |
| 199 |
|
| 200 |
public static class StripRuleTarget implements RuleTarget { |
| 201 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
public void registerRule(Rule rule) { |
| 206 |
|
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|