Debian Codegraph @

net/sf/saxon/style/XSLNamespace.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.style;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.StringLiteral;
12 import net.sf.saxon.expr.instruct.NamespaceConstructor;
13 import net.sf.saxon.om.AxisInfo;
14 import net.sf.saxon.om.NodeInfo;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.AxisIterator;
17 import net.sf.saxon.type.Type;
18 import net.sf.saxon.value.StringValue;
19
20 /**
21 * An xsl:namespace element in the stylesheet. (XSLT 2.0)
22 */
23
24 public class XSLNamespace extends XSLLeafNodeConstructor {
25
26 /*@Nullable*/ Expression name;
27
28 public void prepareAttributes() throws XPathException {
29 name = prepareAttributesNameAndSelect();
30 }
31
32 public void validate(ComponentDeclaration decl) throws XPathException {
33 name = typeCheck("name", name);
34 select = typeCheck("select", select);
35 int countChildren = 0;
36 NodeInfo firstChild = null;
37 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
38 while (true) {
39 NodeInfo child = kids.next();
40 if (child == null) {
41 break;
42 }
43 if (child instanceof XSLFallback) {
44 continue;
45 }
46 if (select != null) {
47 String errorCode = getErrorCodeForSelectPlusContent();
48 compileError("An " + getDisplayName() + " element with a select attribute must be empty", errorCode);
49 }
50 countChildren++;
51 if (firstChild == null) {
52 firstChild = child;
53 } else {
54 break;
55 }
56 }
57
58 if (select == null) {
59 if (countChildren == 0) {
60 // there are no child nodes and no select attribute
61 select = new StringLiteral(StringValue.EMPTY_STRING);
62 select.setRetainedStaticContext(makeRetainedStaticContext());
63 } else if (countChildren == 1) {
64 // there is exactly one child node
65 if (firstChild.getNodeKind() == Type.TEXT) {
66 // it is a text node: optimize for this case
67 select = new StringLiteral(firstChild.getStringValueCS());
68 select.setRetainedStaticContext(makeRetainedStaticContext());
69 }
70 }
71 }
72 }
73
74 /**
75 * Get the error code to be returned when the element has a select attribute but is not empty.
76 *
77 * @return the error code defined for this condition, for this particular instruction
78 */
79
80 protected String getErrorCodeForSelectPlusContent() {
81 return "XTSE0910";
82 }
83
84 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
85 NamespaceConstructor inst = new NamespaceConstructor(name);
86 compileContent(exec, decl, inst, new StringLiteral(StringValue.SINGLE_SPACE));
87 return inst;
88 }
89
90 }
91