Debian Codegraph @

net/sf/saxon/expr/instruct/Comment.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.ContextItemStaticInfo;
13 import net.sf.saxon.expr.parser.ExpressionTool;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.parser.RebindingMap;
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.XPathException;
20 import net.sf.saxon.type.ItemType;
21
22
23 /**
24 * An instruction representing an xsl:comment element in the stylesheet.
25 */
26
27 public final class Comment extends SimpleNodeConstructor {
28
29 /**
30 * Construct the instruction
31 */
32
33 public Comment() {
34 }
35
36 /**
37 * Get the instruction name, for diagnostics and tracing
38 * return the string "xsl:comment"
39 */
40
41 public int getInstructionNameCode() {
42 return StandardNames.XSL_COMMENT;
43 }
44
45 /*@NotNull*/
46 public ItemType getItemType() {
47 return NodeKindTest.COMMENT;
48 }
49
50 public int getCardinality() {
51 return StaticProperty.EXACTLY_ONE;
52 }
53
54
55 /**
56 * Copy an expression. This makes a deep copy.
57 *
58 * @return the copy of the original expression
59 * @param rebindings
60 */
61
62 /*@NotNull*/
63 public Expression copy(RebindingMap rebindings) {
64 Comment exp = new Comment();
65 ExpressionTool.copyLocationInfo(this, exp);
66 exp.setSelect(getSelect().copy(rebindings));
67 return exp;
68 }
69
70 public void localTypeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
71 // Do early checking of content if known statically
72
73 if (getSelect() instanceof Literal) {
74 String s = ((Literal) getSelect()).getValue().getStringValue();
75 String s2 = checkContent(s, visitor.getStaticContext().makeEarlyEvaluationContext());
76 if (!s2.equals(s)) {
77 setSelect(new StringLiteral(s2));
78 }
79 }
80 }
81
82
83 /**
84 * Process the value of the node, to create the new node.
85 *
86 * @param value the string value of the new node
87 * @param context the dynamic evaluation context
88 * @throws XPathException if a dynamic error occurs
89 */
90
91 public void processValue(CharSequence value, XPathContext context) throws XPathException {
92 String comment = checkContent(value.toString(), context);
93 Receiver out = context.getReceiver();
94 out.comment(comment, getLocation(), 0);
95 }
96
97
98 /**
99 * Check the content of the node, and adjust it if necessary
100 *
101 * @param comment the supplied content
102 * @param context the dynamic context
103 * @return the original content, unless adjustments are needed
104 * @throws XPathException if the content is invalid
105 */
106
107 @Override
108 protected String checkContent(String comment, XPathContext context) throws XPathException {
109 if (isXSLT()) {
110 return checkContentXSLT(comment);
111 } else {
112 try {
113 return checkContentXQuery(comment);
114 } catch (XPathException err) {
115 err.setXPathContext(context);
116 err.setLocation(getLocation());
117 throw err;
118 }
119 }
120 }
121
122 /**
123 * Check the content of the comment according to the XSLT rules (which fix it if it is wrong)
124 *
125 * @param comment the proposed text of the comment
126 * @return the adjusted text of the comment
127 */
128
129 public static String checkContentXSLT(String comment) {
130 int hh;
131 while ((hh = comment.indexOf("--")) >= 0) {
132 comment = comment.substring(0, hh + 1) + ' ' + comment.substring(hh + 1);
133 }
134 if (comment.length() > 0 && comment.charAt(comment.length() - 1) == '-') {
135 comment = comment + ' ';
136 }
137 return comment;
138 }
139
140 /**
141 * Check the content of the comment according to the XQuery rules (which throw an error if it is wrong)
142 *
143 * @param comment the proposed text of the comment
144 * @return the adjusted text of the comment (always the same as the original if there is no error)
145 * @throws net.sf.saxon.trans.XPathException
146 * if the content is invalid
147 */
148
149 public static String checkContentXQuery(String comment) throws XPathException {
150 if (comment.contains("--")) {
151 throw new XPathException("Invalid characters (--) in comment", "XQDY0072");
152 }
153 if (comment.length() > 0 && comment.charAt(comment.length() - 1) == '-') {
154 throw new XPathException("Comment cannot end in '-'", "XQDY0072");
155 }
156 return comment;
157 }
158
159
160 /**
161 * Diagnostic print of expression structure. The abstract expression tree
162 * is written to the supplied output destination.
163 */
164
165 public void export(ExpressionPresenter out) throws XPathException {
166 out.startElement("comment", this);
167 String flags = "";
168 if (isLocal()) {
169 flags += "l";
170 }
171 if (!flags.isEmpty()) {
172 out.emitAttribute("flags", flags);
173 }
174 getSelect().export(out);
175 out.endElement();
176 }
177
178 }