| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.event.Stripper; |
| 11 |
import net.sf.saxon.om.*; |
| 12 |
import net.sf.saxon.pattern.*; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.type.Type; |
| 15 |
|
| 16 |
import java.util.StringTokenizer; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class XSLPreserveSpace extends StyleElement { |
| 23 |
|
| 24 |
private String elements; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
@Override |
| 34 |
public boolean isDeclaration() { |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
public void prepareAttributes() throws XPathException { |
| 39 |
|
| 40 |
AttributeCollection atts = getAttributeList(); |
| 41 |
|
| 42 |
for (int a = 0; a < atts.getLength(); a++) { |
| 43 |
String f = atts.getQName(a); |
| 44 |
if (f.equals("elements")) { |
| 45 |
elements = atts.getValue(a); |
| 46 |
} else { |
| 47 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 48 |
} |
| 49 |
} |
| 50 |
if (elements == null) { |
| 51 |
reportAbsence("elements"); |
| 52 |
elements = "*"; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 57 |
checkEmpty(); |
| 58 |
checkTopLevel("XTSE0010", false); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void index(ComponentDeclaration decl, PrincipalStylesheetModule top) { |
| 63 |
if (getFingerprint() == StandardNames.XSL_STRIP_SPACE) { |
| 64 |
if (getFingerprint() == StandardNames.XSL_STRIP_SPACE) { |
| 65 |
String elements = getAttributeValue("", "elements"); |
| 66 |
if (elements != null && !elements.trim().isEmpty()) { |
| 67 |
top.getStylesheetPackage().setStripsWhitespace(true); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) { |
| 74 |
Stripper.StripRuleTarget preserve = |
| 75 |
getFingerprint() == StandardNames.XSL_PRESERVE_SPACE ? Stripper.PRESERVE : Stripper.STRIP; |
| 76 |
PrincipalStylesheetModule psm = getCompilation().getPrincipalStylesheetModule(); |
| 77 |
SpaceStrippingRule stripperRules = psm.getStylesheetPackage().getStripperRules(); |
| 78 |
if (!(stripperRules instanceof SelectedElementsSpaceStrippingRule)) { |
| 79 |
stripperRules = new SelectedElementsSpaceStrippingRule(true); |
| 80 |
psm.getStylesheetPackage().setStripperRules(stripperRules); |
| 81 |
} |
| 82 |
|
| 83 |
SelectedElementsSpaceStrippingRule rules = (SelectedElementsSpaceStrippingRule) stripperRules; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
StringTokenizer st = new StringTokenizer(elements, " \t\n\r", false); |
| 88 |
try { |
| 89 |
while (st.hasMoreTokens()) { |
| 90 |
String s = st.nextToken(); |
| 91 |
NodeTest nt; |
| 92 |
if (s.equals("*")) { |
| 93 |
nt = NodeKindTest.ELEMENT; |
| 94 |
rules.addRule(nt, preserve, decl.getModule(), decl.getSourceElement().getLineNumber()); |
| 95 |
|
| 96 |
} else if (s.startsWith("Q{")) { |
| 97 |
int brace = s.indexOf('}'); |
| 98 |
if (brace < 0) { |
| 99 |
compileError("No closing '}' in EQName"); |
| 100 |
} else if (brace == s.length() - 1) { |
| 101 |
compileError("Missing local part in EQName"); |
| 102 |
} else { |
| 103 |
String uri = s.substring(2, brace); |
| 104 |
String local = s.substring(brace + 1); |
| 105 |
if (local.equals("*")) { |
| 106 |
nt = new NamespaceTest(getNamePool(), Type.ELEMENT, uri); |
| 107 |
} else { |
| 108 |
nt = new NameTest(Type.ELEMENT, uri, local, getNamePool()); |
| 109 |
} |
| 110 |
rules.addRule(nt, preserve, decl.getModule(), decl.getSourceElement().getLineNumber()); |
| 111 |
} |
| 112 |
|
| 113 |
} else if (s.endsWith(":*")) { |
| 114 |
if (s.length() == 2) { |
| 115 |
compileError("No prefix before ':*'"); |
| 116 |
} |
| 117 |
String prefix = s.substring(0, s.length() - 2); |
| 118 |
String uri = getURIForPrefix(prefix, false); |
| 119 |
if (uri == null) { |
| 120 |
undeclaredNamespaceError(prefix, "XTSE0280", "elements"); |
| 121 |
} |
| 122 |
nt = new NamespaceTest(getNamePool(), Type.ELEMENT, uri); |
| 123 |
rules.addRule(nt, preserve, decl.getModule(), decl.getSourceElement().getLineNumber()); |
| 124 |
|
| 125 |
} else if (s.startsWith("*:")) { |
| 126 |
if (s.length() == 2) { |
| 127 |
compileErrorInAttribute("No local name after '*:'", "XTSE0010", "elements"); |
| 128 |
} |
| 129 |
String localname = s.substring(2); |
| 130 |
nt = new LocalNameTest(getNamePool(), Type.ELEMENT, localname); |
| 131 |
rules.addRule(nt, preserve, decl.getModule(), decl.getSourceElement().getLineNumber()); |
| 132 |
|
| 133 |
} else { |
| 134 |
String prefix; |
| 135 |
String localName; |
| 136 |
String uri; |
| 137 |
try { |
| 138 |
String[] parts = NameChecker.getQNameParts(s); |
| 139 |
prefix = parts[0]; |
| 140 |
if (parts[0].equals("")) { |
| 141 |
uri = getDefaultXPathNamespace(); |
| 142 |
} else { |
| 143 |
uri = getURIForPrefix(prefix, false); |
| 144 |
if (uri == null) { |
| 145 |
undeclaredNamespaceError(prefix, "XTSE0280", "elements"); |
| 146 |
} |
| 147 |
} |
| 148 |
localName = parts[1]; |
| 149 |
} catch (QNameException err) { |
| 150 |
compileError("Element name " + s + " is not a valid QName", "XTSE0280"); |
| 151 |
return; |
| 152 |
} |
| 153 |
NamePool target = getNamePool(); |
| 154 |
int nameCode = target.allocateFingerprint(uri, localName); |
| 155 |
nt = new NameTest(Type.ELEMENT, nameCode, getNamePool()); |
| 156 |
rules.addRule(nt, preserve, decl.getModule(), decl.getSourceElement().getLineNumber()); |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
} catch (XPathException e) { |
| 161 |
e.maybeSetLocation(allocateLocation()); |
| 162 |
compileError(e); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
} |
| 168 |
|