Debian Codegraph @

net/sf/saxon/style/XSLAnalyzeString.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.AnalyzeString;
13 import net.sf.saxon.om.AttributeCollection;
14 import net.sf.saxon.om.AxisInfo;
15 import net.sf.saxon.om.NodeInfo;
16 import net.sf.saxon.regex.RegularExpression;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.iter.AxisIterator;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 /**
24 * An xsl:analyze-string elements in the stylesheet. New at XSLT 2.0<BR>
25 */
26
27 public class XSLAnalyzeString extends StyleElement {
28
29 /*@Nullable*/ private Expression select;
30 private Expression regex;
31 private Expression flags;
32 private StyleElement matching;
33 private StyleElement nonMatching;
34 private RegularExpression pattern;
35
36 /**
37 * Determine whether this node is an instruction.
38 *
39 * @return true - it is an instruction
40 */
41
42 public boolean isInstruction() {
43 return true;
44 }
45
46 /**
47 * Determine whether this type of element is allowed to contain an xsl:fallback
48 * instruction
49 */
50
51 public boolean mayContainFallback() {
52 return true;
53 }
54
55
56 public void prepareAttributes() throws XPathException {
57 String selectAtt = null;
58 String regexAtt = null;
59 String flagsAtt = null;
60
61 AttributeCollection atts = getAttributeList();
62
63 for (int a = 0; a < atts.getLength(); a++) {
64 String f = atts.getQName(a);
65 if (f.equals("regex")) {
66 regexAtt = atts.getValue(a);
67 regex = makeAttributeValueTemplate(regexAtt, a);
68
69 } else if (f.equals("select")) {
70 selectAtt = atts.getValue(a);
71 select = makeExpression(selectAtt, a);
72 } else if (f.equals("flags")) {
73 flagsAtt = atts.getValue(a); // not trimmed, see bugzilla 4315
74 flags = makeAttributeValueTemplate(flagsAtt, a);
75 } else {
76 checkUnknownAttribute(atts.getNodeName(a));
77 }
78 }
79
80 if (selectAtt == null) {
81 reportAbsence("select");
82 select = makeExpression(".", -1); // for error recovery
83 }
84
85 if (regexAtt == null) {
86 reportAbsence("regex");
87 regex = makeAttributeValueTemplate("xxx", -1); // for error recovery
88 }
89
90 if (flagsAtt == null) {
91 flagsAtt = "";
92 flags = makeAttributeValueTemplate("", -1);
93 }
94
95
96 if (regex instanceof StringLiteral && flags instanceof StringLiteral) {
97 try {
98 final String regex = ((StringLiteral) this.regex).getStringValue();
99 final String flagstr = ((StringLiteral) flags).getStringValue();
100
101 List<String> warnings = new ArrayList<String>();
102 pattern = getConfiguration().compileRegularExpression(
103 regex, flagstr, getEffectiveVersion() >= 30 ? "XP30" : "XP20", warnings);
104 for (String w : warnings) {
105 issueWarning(w, this);
106 }
107 } catch (XPathException err) {
108 if ("FORX0001".equals(err.getErrorCodeLocalPart())) {
109 invalidFlags("Error in regular expression flags: " + err.getMessage());
110 } else {
111 invalidRegex("Error in regular expression: " + err.getMessage());
112 }
113 }
114 }
115
116 }
117
118 private void invalidRegex(String message) throws XPathException {
119 compileErrorInAttribute(message, "XTDE1140", "regex");
120 // prevent it being reported more than once
121 pattern = getConfiguration().compileRegularExpression("x", "", "XP20", null);
122 }
123
124 private void invalidFlags(String message) throws XPathException {
125 compileErrorInAttribute(message, "XTDE1145", "flags");
126 // prevent it being reported more than once
127 pattern = getConfiguration().compileRegularExpression("x", "", "XP20", null);
128 }
129
130 public void validate(ComponentDeclaration decl) throws XPathException {
131 //checkWithinTemplate();
132
133 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
134 boolean foundFallback = false;
135 while (true) {
136 NodeInfo curr = kids.next();
137 if (curr == null) {
138 break;
139 }
140 if (curr instanceof XSLFallback) {
141 foundFallback = true;
142 } else if (curr instanceof XSLMatchingSubstring) {
143 boolean b = curr.getLocalPart().equals("matching-substring");
144 if (b) {
145 if (matching != null || nonMatching != null || foundFallback) {
146 compileError("xsl:matching-substring element must come first", "XTSE0010");
147 }
148 matching = (StyleElement) curr;
149 } else {
150 if (nonMatching != null || foundFallback) {
151 compileError("xsl:non-matching-substring cannot appear here", "XTSE0010");
152 }
153 nonMatching = (StyleElement) curr;
154 }
155 } else {
156 compileError("Only xsl:matching-substring and xsl:non-matching-substring are allowed here", "XTSE0010");
157 }
158 }
159
160 if (matching == null && nonMatching == null) {
161 compileError("At least one xsl:matching-substring or xsl:non-matching-substring element must be present",
162 "XTSE1130");
163 }
164
165 select = typeCheck("select", select);
166 regex = typeCheck("regex", regex);
167 flags = typeCheck("flags", flags);
168
169 }
170
171 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
172 Expression matchingBlock = null;
173 if (matching != null) {
174 matchingBlock = matching.compileSequenceConstructor(exec, decl, false);
175 }
176
177 Expression nonMatchingBlock = null;
178 if (nonMatching != null) {
179 nonMatchingBlock = nonMatching.compileSequenceConstructor(exec, decl, false);
180 }
181
182 try {
183 return new AnalyzeString(select,
184 regex,
185 flags,
186 matchingBlock == null ? null : matchingBlock.simplify(),
187 nonMatchingBlock == null ? null : nonMatchingBlock.simplify(),
188 pattern);
189 } catch (XPathException e) {
190 compileError(e);
191 return null;
192 }
193 }
194
195
196 }
197