Debian Codegraph @

net/sf/saxon/expr/instruct/ProcessingInstruction.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.instruct;
9
10 import net.sf.saxon.event.Receiver;
11 import net.sf.saxon.expr.*;
12 import net.sf.saxon.expr.parser.*;
13 import net.sf.saxon.om.NameChecker;
14 import net.sf.saxon.om.NoNamespaceName;
15 import net.sf.saxon.om.NodeName;
16 import net.sf.saxon.om.StandardNames;
17 import net.sf.saxon.pattern.NodeKindTest;
18 import net.sf.saxon.trace.ExpressionPresenter;
19 import net.sf.saxon.trans.Err;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.type.ItemType;
22 import net.sf.saxon.value.*;
23
24
25 /**
26 * An xsl:processing-instruction element in the stylesheet, or a processing-instruction
27 * constructor in a query
28 */
29
30 public class ProcessingInstruction extends SimpleNodeConstructor {
31
32 private Operand nameOp;
33
34 /**
35 * Create an xsl:processing-instruction instruction
36 *
37 * @param name the expression used to compute the name of the generated
38 * processing-instruction
39 */
40
41 public ProcessingInstruction(Expression name) {
42 nameOp = new Operand(this, name, OperandRole.SINGLE_ATOMIC);
43 }
44
45
46 public Expression getNameExp() {
47 return nameOp.getChildExpression();
48 }
49
50 public void setNameExp(Expression nameExp) {
51 nameOp.setChildExpression(nameExp);
52 }
53
54 @Override
55 public Iterable<Operand> operands() {
56 return operandList(selectOp, nameOp);
57 }
58
59 /**
60 * Get the name of this instruction for diagnostic and tracing purposes
61 *
62 * @return the string "xsl:processing-instruction"
63 */
64
65 public int getInstructionNameCode() {
66 return StandardNames.XSL_PROCESSING_INSTRUCTION;
67 }
68
69 /*@NotNull*/
70 public ItemType getItemType() {
71 return NodeKindTest.PROCESSING_INSTRUCTION;
72 }
73
74 public int getCardinality() {
75 return StaticProperty.EXACTLY_ONE;
76 }
77
78 /**
79 * Copy an expression. This makes a deep copy.
80 *
81 * @return the copy of the original expression
82 * @param rebindings
83 */
84
85 /*@NotNull*/
86 public Expression copy(RebindingMap rebindings) {
87 ProcessingInstruction exp = new ProcessingInstruction(getNameExp().copy(rebindings));
88 ExpressionTool.copyLocationInfo(this, exp);
89 exp.setSelect(getSelect().copy(rebindings));
90 return exp;
91 }
92
93 public void localTypeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
94 StaticContext env = visitor.getStaticContext();
95 nameOp.typeCheck(visitor, contextItemType);
96
97 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "processing-instruction/name", 0);
98 // See bug 2110. XQuery does not use the function conversion rules here, and disallows xs:anyURI.
99 // In XSLT the name is an AVT so we automatically get a string; in XQuery we'll use the standard
100 // mechanism to get an atomic value, and then check the type "by hand" at run time.
101 setNameExp(visitor.getConfiguration().getTypeChecker(false).staticTypeCheck(
102 getNameExp(), SequenceType.SINGLE_ATOMIC, role, visitor));
103 Expression nameExp = getNameExp();
104 adoptChildExpression(nameExp);
105
106 // Do early checking of name if known statically
107
108 if (nameExp instanceof Literal && ((Literal)nameExp).getValue() instanceof AtomicValue) {
109 AtomicValue val = (AtomicValue) ((Literal) nameExp).getValue();
110 checkName(val, env.makeEarlyEvaluationContext());
111 }
112
113 // Do early checking of content if known statically
114
115 if (getSelect() instanceof Literal) {
116 String s = ((Literal) getSelect()).getValue().getStringValue();
117 String s2 = checkContent(s, env.makeEarlyEvaluationContext());
118 if (!s2.equals(s)) {
119 setSelect(new StringLiteral(s2));
120 }
121 }
122 }
123
124 public int getDependencies() {
125 return getNameExp().getDependencies() | super.getDependencies();
126 }
127
128
129 /**
130 * Process the value of the node, to create the new node.
131 *
132 * @param value the string value of the new node
133 * @param context the dynamic evaluation context
134 * @throws XPathException
135 */
136
137 public void processValue(CharSequence value, XPathContext context) throws XPathException {
138 String expandedName = evaluateName(context);
139 if (expandedName != null) {
140 String data = checkContent(value.toString(), context);
141 Receiver out = context.getReceiver();
142 out.processingInstruction(expandedName, data, getLocation(), 0);
143 }
144 }
145
146 /**
147 * Check the content of the node, and adjust it if necessary
148 *
149 * @param data the supplied content
150 * @return the original content, unless adjustments are needed
151 * @throws XPathException if the content is invalid
152 */
153
154 protected String checkContent(String data, XPathContext context) throws XPathException {
155 if (isXSLT()) {
156 return checkContentXSLT(data);
157 } else {
158 try {
159 return checkContentXQuery(data);
160 } catch (XPathException err) {
161 err.setXPathContext(context);
162 err.setLocation(getLocation());
163 throw err;
164 }
165 }
166 }
167
168 /**
169 * Check the content of the node, and adjust it if necessary, using the XSLT rules
170 *
171 * @param data the supplied content
172 * @return the original content, unless adjustments are needed
173 */
174
175 public static String checkContentXSLT(String data) {
176 int hh;
177 while ((hh = data.indexOf("?>")) >= 0) {
178 data = data.substring(0, hh + 1) + ' ' + data.substring(hh + 1);
179 }
180 return Whitespace.removeLeadingWhitespace(data).toString();
181 }
182
183 /**
184 * Check the content of the node, and adjust it if necessary, using the XQuery rules
185 *
186 * @param data the supplied content
187 * @return the original content, unless adjustments are needed
188 * @throws XPathException if the content is invalid
189 */
190
191 public static String checkContentXQuery(String data) throws XPathException {
192 if (data.contains("?>")) {
193 throw new XPathException("Invalid characters (?>) in processing instruction", "XQDY0026");
194 }
195 return Whitespace.removeLeadingWhitespace(data).toString();
196 }
197
198 public NodeName evaluateNodeName(XPathContext context) throws XPathException {
199 String expandedName = evaluateName(context);
200 return new NoNamespaceName(expandedName);
201 }
202
203 /**
204 * Evaluate the name of the processing instruction.
205 *
206 * @param context the dynamic evaluation context
207 * @return the name of the processing instruction (an NCName), or null, indicating an invalid name
208 * @throws XPathException if evaluation fails, or if the recoverable error is treated as fatal
209 */
210 private String evaluateName(XPathContext context) throws XPathException {
211 AtomicValue av = (AtomicValue) getNameExp().evaluateItem(context);
212 if (av instanceof StringValue && !(av instanceof AnyURIValue)) {
213 // Always true under XSLT
214 return checkName(av, context);
215 } else {
216 XPathException e = new XPathException("Processing instruction name is not a string");
217 e.setXPathContext(context);
218 e.setErrorCode("XPTY0004");
219 throw dynamicError(getLocation(), e, context);
220 }
221 }
222
223 private String checkName(AtomicValue name, XPathContext context) throws XPathException {
224 if (name instanceof StringValue && !(name instanceof AnyURIValue)) {
225 String expandedName = Whitespace.trim(name.getStringValue());
226 if (!NameChecker.isValidNCName(expandedName)) {
227 XPathException e = new XPathException("Processing instruction name " + Err.wrap(expandedName) + " is not a valid NCName");
228 e.setXPathContext(context);
229 e.setErrorCode(isXSLT() ? "XTDE0890" : "XQDY0041");
230 throw dynamicError(getLocation(), e, context);
231 }
232 if (expandedName.equalsIgnoreCase("xml")) {
233 XPathException e = new XPathException("Processing instructions cannot be named 'xml' in any combination of upper/lower case");
234 e.setXPathContext(context);
235 e.setErrorCode(isXSLT() ? "XTDE0890" : "XQDY0064");
236 throw dynamicError(getLocation(), e, context);
237 }
238 return expandedName;
239 } else {
240 XPathException e = new XPathException("Processing instruction name " + Err.wrap(name.getStringValue()) +
241 " is not of type xs:string or xs:untypedAtomic");
242 e.setXPathContext(context);
243 e.setErrorCode("XPTY0004");
244 e.setIsTypeError(true);
245 throw dynamicError(getLocation(), e, context);
246 }
247 }
248
249 /**
250 * Diagnostic print of expression structure. The abstract expression tree
251 * is written to the supplied output destination.
252 */
253
254 public void export(ExpressionPresenter out) throws XPathException {
255 out.startElement("procInst", this);
256 String flags = "";
257 if (isLocal()) {
258 flags += "l";
259 }
260 if (!flags.isEmpty()) {
261 out.emitAttribute("flags", flags);
262 }
263 out.setChildRole("name");
264 getNameExp().export(out);
265 out.setChildRole("select");
266 getSelect().export(out);
267 out.endElement();
268 }
269
270
271 }
272