| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
public final class Comment extends SimpleNodeConstructor { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public Comment() { |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public int getInstructionNameCode() { |
| 42 |
return StandardNames.XSL_COMMENT; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public ItemType getItemType() { |
| 47 |
return NodeKindTest.COMMENT; |
| 48 |
} |
| 49 |
|
| 50 |
public int getCardinality() { |
| 51 |
return StaticProperty.EXACTLY_ONE; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 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 |
|
| 162 |
|
| 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 |
} |