| 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.Controller; |
| 12 |
import net.sf.saxon.event.Receiver; |
| 13 |
import net.sf.saxon.event.ReceiverOptions; |
| 14 |
import net.sf.saxon.expr.StaticProperty; |
| 15 |
import net.sf.saxon.expr.XPathContext; |
| 16 |
import net.sf.saxon.lib.ConversionRules; |
| 17 |
import net.sf.saxon.lib.Validation; |
| 18 |
import net.sf.saxon.om.NameOfNode; |
| 19 |
import net.sf.saxon.om.NodeName; |
| 20 |
import net.sf.saxon.om.StandardNames; |
| 21 |
import net.sf.saxon.pattern.NodeKindTest; |
| 22 |
import net.sf.saxon.trans.Err; |
| 23 |
import net.sf.saxon.trans.XPathException; |
| 24 |
import net.sf.saxon.tree.util.Orphan; |
| 25 |
import net.sf.saxon.type.*; |
| 26 |
import net.sf.saxon.value.Whitespace; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract class AttributeCreator extends SimpleNodeConstructor implements ValidatingInstruction { |
| 33 |
|
| 34 |
|
| 35 |
SimpleType schemaType = null; |
| 36 |
private int validationAction; |
| 37 |
private int options; |
| 38 |
private boolean isInstruction; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public void setInstruction(boolean inst) { |
| 46 |
isInstruction = inst; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
@Override |
| 57 |
public boolean isInstruction() { |
| 58 |
return isInstruction; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public void setSchemaType( SimpleType type) { |
| 69 |
schemaType = type; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public SimpleType getSchemaType() { |
| 81 |
return schemaType; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void setValidationAction(int action) { |
| 91 |
validationAction = action; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public int getValidationAction() { |
| 101 |
return validationAction; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void setOptions(int options) { |
| 114 |
this.options = options; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public void setRejectDuplicates() { |
| 123 |
options |= ReceiverOptions.REJECT_DUPLICATES; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public void setNoSpecialChars() { |
| 132 |
options |= ReceiverOptions.NO_SPECIAL_CHARS; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public int getOptions() { |
| 142 |
return options; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
@Override |
| 154 |
public int computeSpecialProperties() { |
| 155 |
int p = super.computeSpecialProperties(); |
| 156 |
if (getValidationAction() == Validation.SKIP) { |
| 157 |
p |= StaticProperty.ALL_NODES_UNTYPED; |
| 158 |
} |
| 159 |
return p; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public ItemType getItemType() { |
| 170 |
return NodeKindTest.ATTRIBUTE; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public void processValue(CharSequence value, XPathContext context) throws XPathException { |
| 182 |
NodeName attName = evaluateNodeName(context); |
| 183 |
Receiver out = context.getReceiver(); |
| 184 |
int opt = getOptions(); |
| 185 |
SimpleType ann; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
SimpleType schemaType = getSchemaType(); |
| 192 |
int validationAction = getValidationAction(); |
| 193 |
if (schemaType != null) { |
| 194 |
ann = schemaType; |
| 195 |
|
| 196 |
ValidationFailure err = schemaType.validateContent( |
| 197 |
value, DummyNamespaceResolver.getInstance(), context.getConfiguration().getConversionRules()); |
| 198 |
if (err != null) { |
| 199 |
ValidationFailure ve = new ValidationFailure( |
| 200 |
"Attribute value " + Err.wrap(value, Err.VALUE) + |
| 201 |
" does not match the required type " + |
| 202 |
schemaType.getDescription() + ". " + |
| 203 |
err.getMessage()); |
| 204 |
ve.setSchemaType(schemaType); |
| 205 |
ve.setErrorCode("XTTE1540"); |
| 206 |
throw ve.makeException(); |
| 207 |
} |
| 208 |
} else if (validationAction == Validation.STRICT || |
| 209 |
validationAction == Validation.LAX) { |
| 210 |
try { |
| 211 |
Configuration config = context.getConfiguration(); |
| 212 |
ann = config.validateAttribute(attName.getStructuredQName(), value, validationAction); |
| 213 |
} catch (ValidationException e) { |
| 214 |
XPathException err = XPathException.makeXPathException(e); |
| 215 |
err.maybeSetErrorCode(validationAction == Validation.STRICT ? "XTTE1510" : "XTTE1515"); |
| 216 |
err.setXPathContext(context); |
| 217 |
err.maybeSetLocation(getLocation()); |
| 218 |
err.setIsTypeError(true); |
| 219 |
throw err; |
| 220 |
} |
| 221 |
} else { |
| 222 |
ann = BuiltInAtomicType.UNTYPED_ATOMIC; |
| 223 |
} |
| 224 |
if (attName.equals(StandardNames.XML_ID_NAME)) { |
| 225 |
value = Whitespace.collapseWhitespace(value); |
| 226 |
} |
| 227 |
|
| 228 |
try { |
| 229 |
out.attribute(attName, ann, value, getLocation(), opt); |
| 230 |
} catch (XPathException err) { |
| 231 |
throw dynamicError(getLocation(), err, context); |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
protected void validateOrphanAttribute(Orphan orphan, XPathContext context) throws XPathException { |
| 246 |
ConversionRules rules = context.getConfiguration().getConversionRules(); |
| 247 |
SimpleType schemaType = getSchemaType(); |
| 248 |
int validationAction = getValidationAction(); |
| 249 |
if (schemaType != null) { |
| 250 |
ValidationFailure err = schemaType.validateContent( |
| 251 |
orphan.getStringValueCS(), DummyNamespaceResolver.getInstance(), rules); |
| 252 |
if (err != null) { |
| 253 |
err.setMessage("Attribute value " + Err.wrap(orphan.getStringValueCS(), Err.VALUE) + |
| 254 |
" does not the match the required type " + |
| 255 |
schemaType.getDescription() + ". " + |
| 256 |
err.getMessage()); |
| 257 |
err.setErrorCode("XTTE1555"); |
| 258 |
err.setLocator(getLocation()); |
| 259 |
throw err.makeException(); |
| 260 |
} |
| 261 |
orphan.setTypeAnnotation(schemaType); |
| 262 |
if (schemaType.isNamespaceSensitive()) { |
| 263 |
throw new XPathException("Cannot validate a parentless attribute whose content is namespace-sensitive", "XTTE1545"); |
| 264 |
} |
| 265 |
} else if (validationAction == Validation.STRICT || validationAction == Validation.LAX) { |
| 266 |
try { |
| 267 |
final Controller controller = context.getController(); |
| 268 |
assert controller != null; |
| 269 |
SimpleType ann = controller.getConfiguration().validateAttribute( |
| 270 |
NameOfNode.makeName(orphan).getStructuredQName(), orphan.getStringValueCS(), validationAction); |
| 271 |
orphan.setTypeAnnotation(ann); |
| 272 |
} catch (ValidationException e) { |
| 273 |
XPathException err = XPathException.makeXPathException(e); |
| 274 |
err.setErrorCodeQName(e.getErrorCodeQName()); |
| 275 |
err.setXPathContext(context); |
| 276 |
err.setLocation(getLocation()); |
| 277 |
err.setIsTypeError(true); |
| 278 |
throw err; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|