| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 16 |
import net.sf.saxon.functions.NormalizeSpace_1; |
| 17 |
import net.sf.saxon.functions.SystemFunction; |
| 18 |
import net.sf.saxon.lib.ConversionRules; |
| 19 |
import net.sf.saxon.lib.Validation; |
| 20 |
import net.sf.saxon.om.NodeInfo; |
| 21 |
import net.sf.saxon.om.NodeName; |
| 22 |
import net.sf.saxon.om.StandardNames; |
| 23 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 24 |
import net.sf.saxon.trans.Err; |
| 25 |
import net.sf.saxon.trans.XPathException; |
| 26 |
import net.sf.saxon.tree.util.Orphan; |
| 27 |
import net.sf.saxon.type.*; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public final class FixedAttribute extends AttributeCreator { |
| 38 |
|
| 39 |
private NodeName nodeName; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public FixedAttribute(NodeName nodeName, |
| 51 |
int validationAction, |
| 52 |
SimpleType schemaType) { |
| 53 |
this.nodeName = nodeName; |
| 54 |
setSchemaType(schemaType); |
| 55 |
setValidationAction(validationAction); |
| 56 |
setOptions(0); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public int getInstructionNameCode() { |
| 64 |
return StandardNames.XSL_ATTRIBUTE; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
@Override |
| 75 |
public String getExpressionName() { |
| 76 |
return "att"; |
| 77 |
} |
| 78 |
|
| 79 |
public NodeName getAttributeName() { |
| 80 |
return nodeName; |
| 81 |
} |
| 82 |
|
| 83 |
public void localTypeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 84 |
|
| 85 |
if (nodeName.equals(StandardNames.XML_ID_NAME) && !getSelect().isCallOn(NormalizeSpace_1.class)) { |
| 86 |
Expression select = SystemFunction.makeCall( |
| 87 |
"normalize-space", getRetainedStaticContext(), getSelect()); |
| 88 |
setSelect(select); |
| 89 |
} |
| 90 |
|
| 91 |
Configuration config = visitor.getConfiguration(); |
| 92 |
final ConversionRules rules = config.getConversionRules(); |
| 93 |
SimpleType schemaType = getSchemaType(); |
| 94 |
String errorCode = "XTTE1540"; |
| 95 |
if (schemaType == null) { |
| 96 |
int validation = getValidationAction(); |
| 97 |
if (validation == Validation.STRICT) { |
| 98 |
SchemaDeclaration decl = config.getAttributeDeclaration(nodeName.getStructuredQName()); |
| 99 |
if (decl == null) { |
| 100 |
XPathException se = new XPathException( |
| 101 |
"Strict validation fails: there is no global attribute declaration for " + |
| 102 |
nodeName.getDisplayName()); |
| 103 |
se.setErrorCode("XTTE1510"); |
| 104 |
se.setLocation(getLocation()); |
| 105 |
throw se; |
| 106 |
} |
| 107 |
schemaType = (SimpleType) decl.getType(); |
| 108 |
errorCode = "XTTE1510"; |
| 109 |
} else if (validation == Validation.LAX) { |
| 110 |
SchemaDeclaration decl = config.getAttributeDeclaration(nodeName.getStructuredQName()); |
| 111 |
if (decl != null) { |
| 112 |
schemaType = (SimpleType) decl.getType(); |
| 113 |
errorCode = "XTTE1515"; |
| 114 |
} else { |
| 115 |
visitor.getStaticContext().issueWarning( |
| 116 |
"Lax validation has no effect: there is no global attribute declaration for " + |
| 117 |
nodeName.getDisplayName(), getLocation()); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
if (Literal.isAtomic(getSelect()) && schemaType != null && !schemaType.isNamespaceSensitive()) { |
| 124 |
CharSequence value = ((Literal) getSelect()).getValue().getStringValueCS(); |
| 125 |
ValidationFailure err = schemaType.validateContent( |
| 126 |
value, DummyNamespaceResolver.getInstance(), rules); |
| 127 |
if (err != null) { |
| 128 |
XPathException se = new XPathException("Attribute value " + Err.wrap(value, Err.VALUE) + |
| 129 |
" does not the match the required type " + |
| 130 |
schemaType.getDescription() + ". " + |
| 131 |
err.getMessage()); |
| 132 |
se.setErrorCode(errorCode); |
| 133 |
throw se; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if (getSelect() instanceof StringLiteral) { |
| 140 |
boolean special = false; |
| 141 |
CharSequence val = ((StringLiteral) getSelect()).getStringValue(); |
| 142 |
for (int k = 0; k < val.length(); k++) { |
| 143 |
char c = val.charAt(k); |
| 144 |
if ((int) c < 33 || (int) c > 126 || |
| 145 |
c == '<' || c == '>' || c == '&' || c == '\"' || c == '\'') { |
| 146 |
special = true; |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
if (!special) { |
| 151 |
setNoSpecialChars(); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public int getAttributeFingerprint() { |
| 163 |
return nodeName.getFingerprint(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public int getCardinality() { |
| 168 |
return StaticProperty.EXACTLY_ONE; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public Expression copy(RebindingMap rebindings) { |
| 182 |
FixedAttribute exp = new FixedAttribute(nodeName, getValidationAction(), getSchemaType()); |
| 183 |
ExpressionTool.copyLocationInfo(this, exp); |
| 184 |
exp.setSelect(getSelect().copy(rebindings)); |
| 185 |
exp.setInstruction(isInstruction()); |
| 186 |
return exp; |
| 187 |
} |
| 188 |
|
| 189 |
public NodeName evaluateNodeName(XPathContext context) { |
| 190 |
return nodeName; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException { |
| 202 |
int fp = nodeName.getFingerprint(); |
| 203 |
if (fp == StandardNames.XSI_TYPE || |
| 204 |
fp == StandardNames.XSI_SCHEMA_LOCATION || |
| 205 |
fp == StandardNames.XSI_NIL || |
| 206 |
fp == StandardNames.XSI_NO_NAMESPACE_SCHEMA_LOCATION) { |
| 207 |
return; |
| 208 |
} |
| 209 |
if (parentType instanceof SimpleType) { |
| 210 |
XPathException err = new XPathException("Attribute " + nodeName.getDisplayName() + |
| 211 |
" is not permitted in the content model of the simple type " + parentType.getDescription()); |
| 212 |
err.setIsTypeError(true); |
| 213 |
err.setLocation(getLocation()); |
| 214 |
if (getPackageData().getHostLanguage() == Configuration.XSLT) { |
| 215 |
err.setErrorCode("XTTE1510"); |
| 216 |
} else { |
| 217 |
err.setErrorCode("XQDY0027"); |
| 218 |
} |
| 219 |
throw err; |
| 220 |
} |
| 221 |
SchemaType type; |
| 222 |
try { |
| 223 |
type = ((ComplexType) parentType).getAttributeUseType(nodeName.getStructuredQName()); |
| 224 |
} catch (SchemaException e) { |
| 225 |
throw new XPathException(e); |
| 226 |
} |
| 227 |
if (type == null) { |
| 228 |
XPathException err = new XPathException("Attribute " + nodeName.getDisplayName() + |
| 229 |
" is not permitted in the content model of the complex type " + parentType.getDescription()); |
| 230 |
err.setIsTypeError(true); |
| 231 |
err.setLocation(getLocation()); |
| 232 |
if (getPackageData().getHostLanguage() == Configuration.XSLT) { |
| 233 |
err.setErrorCode("XTTE1510"); |
| 234 |
} else { |
| 235 |
err.setErrorCode("XQDY0027"); |
| 236 |
} |
| 237 |
throw err; |
| 238 |
} |
| 239 |
|
| 240 |
try { |
| 241 |
|
| 242 |
getSelect().checkPermittedContents(type, true); |
| 243 |
} catch (XPathException e) { |
| 244 |
e.maybeSetLocation(getLocation()); |
| 245 |
throw e; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
public NodeInfo evaluateItem(XPathContext context) throws XPathException { |
| 251 |
Orphan o = (Orphan) super.evaluateItem(context); |
| 252 |
assert o != null; |
| 253 |
validateOrphanAttribute(o, context); |
| 254 |
return o; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public void export(ExpressionPresenter out) throws XPathException { |
| 264 |
out.startElement("att", this); |
| 265 |
out.emitAttribute("name", nodeName.getDisplayName()); |
| 266 |
if (!nodeName.getStructuredQName().hasURI("")) { |
| 267 |
out.emitAttribute("nsuri", nodeName.getStructuredQName().getURI()); |
| 268 |
} |
| 269 |
if (getValidationAction() != Validation.SKIP && getValidationAction() != Validation.BY_TYPE) { |
| 270 |
out.emitAttribute("validation", Validation.toString(getValidationAction())); |
| 271 |
} |
| 272 |
if (getSchemaType() != null) { |
| 273 |
out.emitAttribute("type", getSchemaType().getStructuredQName()); |
| 274 |
} |
| 275 |
String flags = ""; |
| 276 |
if (isLocal()) { |
| 277 |
flags += "l"; |
| 278 |
} |
| 279 |
if (!flags.isEmpty()) { |
| 280 |
out.emitAttribute("flags", flags); |
| 281 |
} |
| 282 |
getSelect().export(out); |
| 283 |
out.endElement(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
@Override |
| 292 |
public String toShortString() { |
| 293 |
return "attr{" + nodeName.getDisplayName() + "=...}"; |
| 294 |
} |
| 295 |
} |
| 296 |
|