Debian Codegraph @

net/sf/saxon/style/XSLContextItem.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.StaticProperty;
11 import net.sf.saxon.om.AttributeCollection;
12 import net.sf.saxon.om.AxisInfo;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.AnyItemType;
15 import net.sf.saxon.type.ItemType;
16 import net.sf.saxon.type.Type;
17 import net.sf.saxon.value.SequenceType;
18 import net.sf.saxon.value.Whitespace;
19
20
21 /**
22 * An xsl:context-item element in the stylesheet. <br>
23 */
24
25 public class XSLContextItem extends StyleElement {
26
27 private ItemType requiredType = AnyItemType.getInstance();
28 private boolean mayBeOmitted = true;
29 private boolean absentFocus = false;
30
31
32 public void prepareAttributes() throws XPathException {
33
34 String asAtt = null;
35 String useAtt = null;
36
37 AttributeCollection atts = getAttributeList();
38
39 for (int a = 0; a < atts.getLength(); a++) {
40 String f = atts.getQName(a);
41 switch (f) {
42 case "as":
43 asAtt = Whitespace.trim(atts.getValue(a));
44 break;
45 case "use":
46 useAtt = Whitespace.trim(atts.getValue(a));
47 break;
48 default:
49 checkUnknownAttribute(atts.getNodeName(a));
50 break;
51 }
52 }
53 if (asAtt != null) {
54 SequenceType st;
55 try {
56 st = makeSequenceType(asAtt);
57 } catch (XPathException e) {
58 st = SequenceType.SINGLE_ITEM;
59 compileErrorInAttribute(e.getMessage(), e.getErrorCodeLocalPart(), "as");
60 }
61 if (st.getCardinality() != StaticProperty.EXACTLY_ONE) {
62 compileError("The xsl:context-item/@use attribute must be an item type (no occurrence indicator allowed)", "XTSE0020");
63 return;
64 }
65 requiredType = st.getPrimaryType();
66 }
67 if (useAtt != null) {
68 switch (useAtt) {
69 case "required":
70 mayBeOmitted = false;
71 break;
72 case "optional":
73 // no action, this is the default
74 break;
75 case "absent":
76 absentFocus = true;
77 break;
78 default:
79 invalidAttribute("use", "required|optional|absent");
80 break;
81 }
82 }
83 if (asAtt != null && absentFocus) {
84 compileError("The 'as' attribute must be omitted when use='absent' is specified", "XTSE3089");
85 }
86 }
87
88 /**
89 * Check that the stylesheet element is valid. This is called once for each element, after
90 * the entire tree has been built. As well as validation, it can perform first-time
91 * initialisation. The default implementation does nothing; it is normally overriden
92 * in subclasses.
93 *
94 * @param decl the declaration to be validated
95 * @throws XPathException if any error is found during validation
96 */
97
98 public void validate(ComponentDeclaration decl) throws XPathException {
99 if (!(getParent() instanceof XSLTemplate)) {
100 compileError("xsl:context-item can appear only as a child of xsl:template");
101 return;
102 }
103 if (mayBeOmitted && ((XSLTemplate) getParent()).getTemplateName() == null) {
104 compileError("xsl:context-item appearing in an xsl:template declaration with no name attribute must specify use=required",
105 "XTSE0020");
106 }
107 ((XSLTemplate)getParent()).setContextItemRequirements(requiredType, mayBeOmitted, absentFocus);
108 iterateAxis(AxisInfo.PRECEDING_SIBLING).forEachOrFail(prec -> {
109 if (prec.getNodeKind() != Type.TEXT || !Whitespace.isWhite(prec.getStringValueCS())) {
110 compileError("xsl:context-item must be the first child of xsl:template");
111 }
112 });
113 }
114
115 public ItemType getRequiredContextItemType() {
116 return requiredType;
117 }
118
119 public boolean isMayBeOmitted() {
120 return mayBeOmitted;
121 }
122
123 public boolean isAbsentFocus() {
124 return absentFocus;
125 }
126
127
128 }