| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 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.ArrayList; |
| 17 |
import java.util.List; |
| 18 |
import java.util.StringTokenizer; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class XSLCatch extends StyleElement { |
| 28 |
|
| 29 |
private Expression select; |
| 30 |
private QNameTest nameTest; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public boolean isInstruction() { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public boolean mayContainSequenceConstructor() { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected boolean seesAvuncularVariables() { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
public void prepareAttributes() throws XPathException { |
| 63 |
|
| 64 |
String selectAtt = null; |
| 65 |
String errorAtt = null; |
| 66 |
|
| 67 |
AttributeCollection atts = getAttributeList(); |
| 68 |
|
| 69 |
for (int a = 0; a < atts.getLength(); a++) { |
| 70 |
String f = atts.getQName(a); |
| 71 |
if (f.equals("select")) { |
| 72 |
selectAtt = atts.getValue(a); |
| 73 |
select = makeExpression(selectAtt, a); |
| 74 |
} else if (f.equals("errors")) { |
| 75 |
errorAtt = atts.getValue(a); |
| 76 |
} else { |
| 77 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (errorAtt == null) { |
| 82 |
|
| 83 |
nameTest = AnyNodeTest.getInstance(); |
| 84 |
} else { |
| 85 |
List<QNameTest> tests = parseNameTests(errorAtt); |
| 86 |
if (tests.size() == 0) { |
| 87 |
compileError("xsl:catch/@errors must not be empty"); |
| 88 |
} |
| 89 |
if (tests.size() == 1) { |
| 90 |
nameTest = tests.get(0); |
| 91 |
} else { |
| 92 |
nameTest = new UnionQNameTest(tests); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private List<QNameTest> parseNameTests(String elements) throws XPathException { |
| 106 |
List<QNameTest> result = new ArrayList<QNameTest>(); |
| 107 |
StringTokenizer st = new StringTokenizer(elements, " \t\n\r", false); |
| 108 |
while (st.hasMoreTokens()) { |
| 109 |
String s = st.nextToken(); |
| 110 |
QNameTest nt; |
| 111 |
if (s.equals("*")) { |
| 112 |
nt = AnyNodeTest.getInstance(); |
| 113 |
result.add(nt); |
| 114 |
|
| 115 |
} else if (s.endsWith(":*")) { |
| 116 |
if (s.length() == 2) { |
| 117 |
compileError("No prefix before ':*'"); |
| 118 |
result.add(AnyNodeTest.getInstance()); |
| 119 |
} |
| 120 |
String prefix = s.substring(0, s.length() - 2); |
| 121 |
String uri = getURIForPrefix(prefix, false); |
| 122 |
nt = new NamespaceTest( |
| 123 |
getNamePool(), |
| 124 |
Type.ELEMENT, |
| 125 |
uri); |
| 126 |
result.add(nt); |
| 127 |
} else if (s.startsWith("*:")) { |
| 128 |
if (s.length() == 2) { |
| 129 |
compileErrorInAttribute("No local name after '*:'", "XTSE0010", "errors"); |
| 130 |
result.add(AnyNodeTest.getInstance()); |
| 131 |
} |
| 132 |
String localname = s.substring(2); |
| 133 |
nt = new LocalNameTest( |
| 134 |
getNamePool(), |
| 135 |
Type.ELEMENT, |
| 136 |
localname); |
| 137 |
result.add(nt); |
| 138 |
} else { |
| 139 |
String prefix; |
| 140 |
String localName; |
| 141 |
String uri; |
| 142 |
try { |
| 143 |
String[] parts = NameChecker.getQNameParts(s); |
| 144 |
prefix = parts[0]; |
| 145 |
if (parts[0].equals("")) { |
| 146 |
uri = ""; |
| 147 |
} else { |
| 148 |
uri = getURIForPrefix(prefix, false); |
| 149 |
if (uri == null) { |
| 150 |
undeclaredNamespaceError(prefix, "XTSE0280", "errors"); |
| 151 |
result.add(AnyNodeTest.getInstance()); |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
localName = parts[1]; |
| 156 |
} catch (QNameException err) { |
| 157 |
compileErrorInAttribute("Error code " + s + " is not a valid QName", "XTSE0280", "errors"); |
| 158 |
result.add(AnyNodeTest.getInstance()); |
| 159 |
break; |
| 160 |
} |
| 161 |
NamePool target = getNamePool(); |
| 162 |
int nameCode = target.allocateFingerprint(uri, localName); |
| 163 |
nt = new NameTest(Type.ELEMENT, nameCode, getNamePool()); |
| 164 |
result.add(nt); |
| 165 |
} |
| 166 |
} |
| 167 |
return result; |
| 168 |
} |
| 169 |
|
| 170 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 171 |
select = typeCheck("select", select); |
| 172 |
if (select != null && hasChildNodes()) { |
| 173 |
compileError("An xsl:catch element with a select attribute must be empty", "XTSE3150"); |
| 174 |
} |
| 175 |
if (!(getParent() instanceof XSLTry)) { |
| 176 |
compileError("xsl:catch may appear only as a child of xsl:try"); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 182 |
if (select == null) { |
| 183 |
select = compileSequenceConstructor(exec, decl, true); |
| 184 |
} |
| 185 |
((XSLTry) getParent()).addCatchClause(nameTest, select); |
| 186 |
return null; |
| 187 |
} |
| 188 |
|
| 189 |
} |