| 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.event.Receiver; |
| 12 |
import net.sf.saxon.event.ReceiverOptions; |
| 13 |
import net.sf.saxon.expr.*; |
| 14 |
import net.sf.saxon.expr.parser.*; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.lib.StandardURIChecker; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.pattern.NodeKindTest; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.ItemType; |
| 22 |
import net.sf.saxon.value.AnyURIValue; |
| 23 |
import net.sf.saxon.value.AtomicValue; |
| 24 |
import net.sf.saxon.value.SequenceType; |
| 25 |
import net.sf.saxon.value.Whitespace; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class NamespaceConstructor extends SimpleNodeConstructor { |
| 32 |
|
| 33 |
private Operand nameOp; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public NamespaceConstructor(Expression name) { |
| 42 |
nameOp = new Operand(this, name, OperandRole.SINGLE_ATOMIC); |
| 43 |
} |
| 44 |
|
| 45 |
public Expression getNameExp() { |
| 46 |
return nameOp.getChildExpression(); |
| 47 |
} |
| 48 |
|
| 49 |
public void setNameExp(Expression nameExp) { |
| 50 |
nameOp.setChildExpression(nameExp); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public Iterable<Operand> operands() { |
| 55 |
return operandList(selectOp, nameOp); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public int getInstructionNameCode() { |
| 63 |
return StandardNames.XSL_NAMESPACE; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
public ItemType getItemType() { |
| 68 |
return NodeKindTest.NAMESPACE; |
| 69 |
} |
| 70 |
|
| 71 |
public int getCardinality() { |
| 72 |
return StaticProperty.EXACTLY_ONE; |
| 73 |
} |
| 74 |
|
| 75 |
public void localTypeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 76 |
StaticContext env = visitor.getStaticContext(); |
| 77 |
nameOp.typeCheck(visitor, contextItemType); |
| 78 |
|
| 79 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "namespace/name", 0); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
setNameExp(env.getConfiguration().getTypeChecker(false).staticTypeCheck( |
| 84 |
getNameExp(), SequenceType.OPTIONAL_ATOMIC, role, visitor)); |
| 85 |
adoptChildExpression(getNameExp()); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if (getNameExp() instanceof Literal) { |
| 90 |
evaluatePrefix(env.makeEarlyEvaluationContext()); |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public Expression copy(RebindingMap rebindings) { |
| 104 |
NamespaceConstructor exp = new NamespaceConstructor(getNameExp().copy(rebindings)); |
| 105 |
exp.setSelect(getSelect().copy(rebindings)); |
| 106 |
ExpressionTool.copyLocationInfo(this, exp); |
| 107 |
return exp; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
public NodeName evaluateNodeName(XPathContext context) throws XPathException { |
| 112 |
String prefix = evaluatePrefix(context); |
| 113 |
return new NoNamespaceName(prefix); |
| 114 |
} |
| 115 |
|
| 116 |
private String evaluatePrefix(XPathContext context) throws XPathException { |
| 117 |
AtomicValue value = (AtomicValue) getNameExp().evaluateItem(context); |
| 118 |
if (value == null) { |
| 119 |
return ""; |
| 120 |
} |
| 121 |
if (!(value instanceof net.sf.saxon.value.StringValue) || value instanceof AnyURIValue) { |
| 122 |
|
| 123 |
XPathException err = new XPathException( |
| 124 |
"Namespace prefix is not an xs:string or xs:untypedAtomic", "XPTY0004", getLocation()); |
| 125 |
err.setIsTypeError(true); |
| 126 |
throw dynamicError(getLocation(), err, context); |
| 127 |
} |
| 128 |
String prefix = Whitespace.trim(value.getStringValueCS()); |
| 129 |
if (!(prefix.isEmpty() || NameChecker.isValidNCName(prefix))) { |
| 130 |
String errorCode = isXSLT() ? "XTDE0920" : "XQDY0074"; |
| 131 |
XPathException err = new XPathException("Namespace prefix is invalid: " + prefix, errorCode, getLocation()); |
| 132 |
throw dynamicError(getLocation(), err, context); |
| 133 |
} |
| 134 |
|
| 135 |
if (prefix.equals("xmlns")) { |
| 136 |
String errorCode = isXSLT() ? "XTDE0920" : "XQDY0101"; |
| 137 |
XPathException err = new XPathException("Namespace prefix 'xmlns' is not allowed", errorCode, getLocation()); |
| 138 |
throw dynamicError(getLocation(), err, context); |
| 139 |
} |
| 140 |
return prefix; |
| 141 |
} |
| 142 |
|
| 143 |
public void processValue(CharSequence value, XPathContext context) throws XPathException { |
| 144 |
String prefix = evaluatePrefix(context); |
| 145 |
String uri = value.toString(); |
| 146 |
checkPrefixAndUri(prefix, uri, context); |
| 147 |
|
| 148 |
NamespaceBinding nscode = new NamespaceBinding(prefix, uri); |
| 149 |
Receiver out = context.getReceiver(); |
| 150 |
out.namespace(nscode, ReceiverOptions.REJECT_DUPLICATES); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public NodeInfo evaluateItem(XPathContext context) throws XPathException { |
| 161 |
NodeInfo node = (NodeInfo) super.evaluateItem(context); |
| 162 |
assert node != null; |
| 163 |
String prefix = node.getLocalPart(); |
| 164 |
String uri = node.getStringValue(); |
| 165 |
checkPrefixAndUri(prefix, uri, context); |
| 166 |
return node; |
| 167 |
} |
| 168 |
|
| 169 |
private void checkPrefixAndUri(String prefix, String uri, XPathContext context) throws XPathException { |
| 170 |
if (prefix.equals("xml") != uri.equals(NamespaceConstant.XML)) { |
| 171 |
String errorCode = isXSLT() ? "XTDE0925" : "XQDY0101"; |
| 172 |
XPathException err = new XPathException("Namespace prefix 'xml' and namespace uri " + NamespaceConstant.XML + |
| 173 |
" must only be used together", errorCode, getLocation()); |
| 174 |
throw dynamicError(getLocation(), err, context); |
| 175 |
} |
| 176 |
|
| 177 |
if (uri.isEmpty()) { |
| 178 |
String errorCode = isXSLT() ? "XTDE0930" : "XQDY0101"; |
| 179 |
XPathException err = new XPathException("Namespace URI is an empty string", errorCode, getLocation()); |
| 180 |
throw dynamicError(getLocation(), err, context); |
| 181 |
} |
| 182 |
|
| 183 |
if (uri.equals(NamespaceConstant.XMLNS)) { |
| 184 |
String errorCode = isXSLT() ? "XTDE0905" : "XQDY0101"; |
| 185 |
XPathException err = new XPathException("A namespace node cannot have the reserved namespace " + |
| 186 |
NamespaceConstant.XMLNS, errorCode, getLocation()); |
| 187 |
throw dynamicError(getLocation(), err, context); |
| 188 |
} |
| 189 |
|
| 190 |
if (context.getConfiguration().getXsdVersion() == Configuration.XSD10 && |
| 191 |
!StandardURIChecker.getInstance().isValidURI(uri)) { |
| 192 |
XPathException de = new XPathException( |
| 193 |
"The string value of the constructed namespace node must be a valid URI", "XTDE0905", getLocation()); |
| 194 |
throw dynamicError(getLocation(), de, context); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public void export(ExpressionPresenter out) throws XPathException { |
| 205 |
out.startElement("namespace", this); |
| 206 |
String flags = ""; |
| 207 |
if (isLocal()) { |
| 208 |
flags += "l"; |
| 209 |
} |
| 210 |
if (!flags.isEmpty()) { |
| 211 |
out.emitAttribute("flags", flags); |
| 212 |
} |
| 213 |
out.setChildRole("name"); |
| 214 |
getNameExp().export(out); |
| 215 |
out.setChildRole("select"); |
| 216 |
getSelect().export(out); |
| 217 |
out.endElement(); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
} |