| 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.expr.StringLiteral; |
| 12 |
import net.sf.saxon.expr.instruct.ComputedAttribute; |
| 13 |
import net.sf.saxon.expr.instruct.FixedAttribute; |
| 14 |
import net.sf.saxon.expr.instruct.Instruction; |
| 15 |
import net.sf.saxon.lib.StandardURIChecker; |
| 16 |
import net.sf.saxon.lib.Validation; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.trans.Err; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 21 |
import net.sf.saxon.type.SchemaType; |
| 22 |
import net.sf.saxon.type.SimpleType; |
| 23 |
import net.sf.saxon.value.StringValue; |
| 24 |
import net.sf.saxon.value.Whitespace; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public final class XSLAttribute extends XSLLeafNodeConstructor { |
| 31 |
|
| 32 |
private Expression attributeName; |
| 33 |
private Expression separator; |
| 34 |
private Expression namespace = null; |
| 35 |
private int validationAction = Validation.PRESERVE; |
| 36 |
private SimpleType schemaType; |
| 37 |
|
| 38 |
public void prepareAttributes() throws XPathException { |
| 39 |
|
| 40 |
AttributeCollection atts = getAttributeList(); |
| 41 |
|
| 42 |
String nameAtt = null; |
| 43 |
String namespaceAtt = null; |
| 44 |
String selectAtt = null; |
| 45 |
String separatorAtt = null; |
| 46 |
String validationAtt = null; |
| 47 |
String typeAtt = null; |
| 48 |
|
| 49 |
for (int a = 0; a < atts.getLength(); a++) { |
| 50 |
String f = atts.getQName(a); |
| 51 |
if (f.equals("name")) { |
| 52 |
nameAtt = Whitespace.trim(atts.getValue(a)); |
| 53 |
attributeName = makeAttributeValueTemplate(nameAtt, a); |
| 54 |
} else if (f.equals("namespace")) { |
| 55 |
namespaceAtt = Whitespace.trim(atts.getValue(a)); |
| 56 |
namespace = makeAttributeValueTemplate(namespaceAtt, a); |
| 57 |
} else if (f.equals("select")) { |
| 58 |
selectAtt = atts.getValue(a); |
| 59 |
select = makeExpression(selectAtt, a); |
| 60 |
} else if (f.equals("separator")) { |
| 61 |
separatorAtt = atts.getValue(a); |
| 62 |
separator = makeAttributeValueTemplate(separatorAtt, a); |
| 63 |
} else if (f.equals("validation")) { |
| 64 |
validationAtt = Whitespace.trim(atts.getValue(a)); |
| 65 |
} else if (f.equals("type")) { |
| 66 |
typeAtt = Whitespace.trim(atts.getValue(a)); |
| 67 |
} else { |
| 68 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if (nameAtt == null) { |
| 73 |
reportAbsence("name"); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if (attributeName instanceof StringLiteral) { |
| 78 |
if (!NameChecker.isQName(((StringLiteral) attributeName).getStringValue())) { |
| 79 |
invalidAttributeName("Attribute name " + Err.wrap(nameAtt) + " is not a valid QName"); |
| 80 |
} |
| 81 |
if (nameAtt.equals("xmlns")) { |
| 82 |
if (namespace == null) { |
| 83 |
invalidAttributeName("Invalid attribute name: xmlns"); |
| 84 |
} |
| 85 |
} |
| 86 |
if (nameAtt.startsWith("xmlns:")) { |
| 87 |
if (namespaceAtt == null) { |
| 88 |
invalidAttributeName("Invalid attribute name: " + Err.wrap(nameAtt)); |
| 89 |
} else { |
| 90 |
|
| 91 |
nameAtt = nameAtt.substring(6); |
| 92 |
attributeName = new StringLiteral(nameAtt); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
if (namespaceAtt != null) { |
| 99 |
if (namespace instanceof StringLiteral) { |
| 100 |
if (!StandardURIChecker.getInstance().isValidURI(((StringLiteral) namespace).getStringValue())) { |
| 101 |
compileError("The value of the namespace attribute must be a valid URI", "XTDE0865"); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if (separatorAtt == null) { |
| 107 |
if (selectAtt == null) { |
| 108 |
separator = new StringLiteral(StringValue.EMPTY_STRING); |
| 109 |
} else { |
| 110 |
separator = new StringLiteral(StringValue.SINGLE_SPACE); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (validationAtt != null) { |
| 115 |
validationAction = validateValidationAttribute(validationAtt); |
| 116 |
} else { |
| 117 |
validationAction = getDefaultValidation(); |
| 118 |
} |
| 119 |
|
| 120 |
if (typeAtt != null) { |
| 121 |
if (!isSchemaAware()) { |
| 122 |
compileError( |
| 123 |
"The @type attribute is available only with a schema-aware XSLT processor", "XTSE1660"); |
| 124 |
} else { |
| 125 |
SchemaType type = getSchemaType(typeAtt); |
| 126 |
if (type == null) { |
| 127 |
compileError("Unknown attribute type " + typeAtt, "XTSE1520"); |
| 128 |
} else { |
| 129 |
if (type.isSimpleType()) { |
| 130 |
schemaType = (SimpleType) type; |
| 131 |
} else { |
| 132 |
compileError("Type annotation for attributes must be a simple type", "XTSE1530"); |
| 133 |
} |
| 134 |
} |
| 135 |
validationAction = Validation.BY_TYPE; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (typeAtt != null && validationAtt != null) { |
| 140 |
compileError("The validation and type attributes are mutually exclusive", "XTSE1505"); |
| 141 |
validationAction = getDefaultValidation(); |
| 142 |
schemaType = null; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
private void invalidAttributeName(String message) throws XPathException { |
| 147 |
compileErrorInAttribute(message, "XTDE0850", "name"); |
| 148 |
|
| 149 |
attributeName = new StringLiteral("saxon-error-attribute"); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 154 |
if (schemaType != null) { |
| 155 |
if (schemaType.isNamespaceSensitive()) { |
| 156 |
compileErrorInAttribute("Validation at attribute level must not specify a " + |
| 157 |
"namespace-sensitive type (xs:QName or xs:NOTATION)", "XTTE1545", "type"); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
attributeName = typeCheck("name", attributeName); |
| 162 |
namespace = typeCheck("namespace", namespace); |
| 163 |
select = typeCheck("select", select); |
| 164 |
separator = typeCheck("separator", separator); |
| 165 |
|
| 166 |
super.validate(decl); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
protected String getErrorCodeForSelectPlusContent() { |
| 176 |
return "XTSE0840"; |
| 177 |
} |
| 178 |
|
| 179 |
public Instruction compile(Compilation compilation, ComponentDeclaration decl) throws XPathException { |
| 180 |
NamespaceResolver nsContext = null; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if (attributeName instanceof StringLiteral ) { |
| 185 |
String qName = Whitespace.trim(((StringLiteral) attributeName).getStringValue()); |
| 186 |
String[] parts; |
| 187 |
try { |
| 188 |
parts = NameChecker.getQNameParts(qName); |
| 189 |
} catch (QNameException e) { |
| 190 |
|
| 191 |
return null; |
| 192 |
} |
| 193 |
|
| 194 |
if (namespace == null) { |
| 195 |
String nsuri = ""; |
| 196 |
if (!parts[0].equals("")) { |
| 197 |
nsuri = getURIForPrefix(parts[0], false); |
| 198 |
if (nsuri == null) { |
| 199 |
undeclaredNamespaceError(parts[0], "XTDE0860", "name"); |
| 200 |
return null; |
| 201 |
} |
| 202 |
} |
| 203 |
NodeName attributeName = new FingerprintedQName(parts[0], nsuri, parts[1]); |
| 204 |
attributeName.obtainFingerprint(getNamePool()); |
| 205 |
FixedAttribute inst = new FixedAttribute(attributeName, |
| 206 |
validationAction, |
| 207 |
schemaType); |
| 208 |
inst.setInstruction(true); |
| 209 |
inst.setLocation(allocateLocation()); |
| 210 |
compileContent(compilation, decl, inst, separator); |
| 211 |
return inst; |
| 212 |
} else if (namespace instanceof StringLiteral) { |
| 213 |
String nsuri = ((StringLiteral) namespace).getStringValue(); |
| 214 |
if (nsuri.equals("")) { |
| 215 |
parts[0] = ""; |
| 216 |
} else if (parts[0].equals("")) { |
| 217 |
|
| 218 |
|
| 219 |
AxisIterator iter = iterateAxis(AxisInfo.NAMESPACE); |
| 220 |
NodeInfo ns; |
| 221 |
while ((ns = iter.next()) != null) { |
| 222 |
if (ns.getStringValue().equals(nsuri)) { |
| 223 |
parts[0] = ns.getLocalPart(); |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (parts[0].equals("")) { |
| 229 |
String p = getNamePool().suggestPrefixForURI( |
| 230 |
((StringLiteral) namespace).getStringValue()); |
| 231 |
if (p != null) { |
| 232 |
parts[0] = p; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
if (parts[0].equals("")) { |
| 238 |
parts[0] = "ns0"; |
| 239 |
} |
| 240 |
} |
| 241 |
NodeName nodeName = new FingerprintedQName(parts[0], nsuri, parts[1]); |
| 242 |
nodeName.obtainFingerprint(getNamePool()); |
| 243 |
FixedAttribute inst = new FixedAttribute(nodeName, validationAction, schemaType); |
| 244 |
inst.setInstruction(true); |
| 245 |
compileContent(compilation, decl, inst, separator); |
| 246 |
return inst; |
| 247 |
} |
| 248 |
} else { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if (namespace == null) { |
| 253 |
nsContext = makeNamespaceContext(); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
ComputedAttribute inst = new ComputedAttribute(attributeName, |
| 258 |
namespace, |
| 259 |
nsContext, |
| 260 |
validationAction, |
| 261 |
schemaType, |
| 262 |
false); |
| 263 |
|
| 264 |
inst.setInstruction(true); |
| 265 |
inst.setLocation(allocateLocation()); |
| 266 |
compileContent(compilation, decl, inst, separator); |
| 267 |
return inst; |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|