| 1 |
package org.apache.jasper.compiler; |
| 2 |
|
| 3 |
import gg.jte.convert.CustomTagConverter; |
| 4 |
import gg.jte.convert.ConverterOutput; |
| 5 |
import gg.jte.convert.jsp.Converter; |
| 6 |
import gg.jte.convert.jsp.JspElementType; |
| 7 |
import gg.jte.convert.jsp.converter.JspAttributeConverter; |
| 8 |
import gg.jte.convert.jsp.converter.JspExpressionConverter; |
| 9 |
import org.apache.jasper.JasperException; |
| 10 |
|
| 11 |
import java.io.IOException; |
| 12 |
import java.net.URL; |
| 13 |
import java.util.*; |
| 14 |
import java.util.regex.Pattern; |
| 15 |
|
| 16 |
import static gg.jte.convert.jsp.converter.JspExpressionConverter.convertAttributeValue; |
| 17 |
|
| 18 |
public class JtpConverter extends Node.Visitor implements Converter { |
| 19 |
private static final Pattern NEW_LINE_CLEANUP = Pattern.compile("\n{3,}"); |
| 20 |
private static final Pattern NEW_LINE_WHITESPACE_CLEANUP = Pattern.compile("\n([\\t ]+)\n"); |
| 21 |
|
| 22 |
private final String relativeFilePath; |
| 23 |
private final byte[] input; |
| 24 |
private final URL resourceBase; |
| 25 |
private final boolean tagFile; |
| 26 |
private final ConverterOutput output; |
| 27 |
private final Map<String, CustomTagConverter> converters = new HashMap<>(); |
| 28 |
private final Set<String> inlinedIncludes = new HashSet<>(); |
| 29 |
private final Map<String, EnumSet<JspElementType>> suppressions = new HashMap<>(); |
| 30 |
private final Set<String> imports = new TreeSet<>(); |
| 31 |
private final Set<String> params = new TreeSet<>(); |
| 32 |
|
| 33 |
private String prefix; |
| 34 |
private String lineSeparator = "\n"; |
| 35 |
|
| 36 |
public JtpConverter(String relativeFilePath, byte[] input, URL resourceBase, boolean tagFile, ConverterOutput output) { |
| 37 |
this.relativeFilePath = relativeFilePath; |
| 38 |
this.input = input; |
| 39 |
this.resourceBase = resourceBase; |
| 40 |
this.tagFile = tagFile; |
| 41 |
this.output = output; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public void register(String tagName, CustomTagConverter converter) { |
| 46 |
this.converters.put(tagName, converter); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void addInlinedInclude(String path) { |
| 51 |
inlinedIncludes.add(path); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void addSuppressions(String path, EnumSet<JspElementType> suppressions) { |
| 56 |
this.suppressions.put(path, suppressions); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void setLineSeparator(String lineSeparator) { |
| 61 |
this.lineSeparator = lineSeparator; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public void setIndentationCount(int indentationCount) { |
| 66 |
output.setIndentationCount(indentationCount); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void setIndentationChar(char indentationChar) { |
| 71 |
output.setIndentationChar(indentationChar); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public String convert() { |
| 76 |
try { |
| 77 |
Node.Nodes nodes = JtpParser.parse(relativeFilePath, input, resourceBase, tagFile); |
| 78 |
|
| 79 |
nodes.visit(this); |
| 80 |
|
| 81 |
return cleanResult(output.trim().prepend(createPrefix()).toString()); |
| 82 |
} catch (JasperException | IOException e) { |
| 83 |
throw new RuntimeException(e); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private String createPrefix() { |
| 88 |
StringBuilder result = new StringBuilder(prefix == null ? "" : prefix); |
| 89 |
|
| 90 |
for (String type : imports) { |
| 91 |
result.append("@import ").append(type).append('\n'); |
| 92 |
} |
| 93 |
|
| 94 |
if (result.length() > 0) { |
| 95 |
result.append('\n'); |
| 96 |
} |
| 97 |
|
| 98 |
for (String param : params) { |
| 99 |
result.append("@param ").append(param).append('\n'); |
| 100 |
} |
| 101 |
|
| 102 |
return result.toString(); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void setPrefix(String prefix) { |
| 107 |
this.prefix = prefix; |
| 108 |
} |
| 109 |
|
| 110 |
public void addImport(String className) { |
| 111 |
imports.add(className); |
| 112 |
} |
| 113 |
|
| 114 |
public void addParam(String param) { |
| 115 |
params.add(param); |
| 116 |
} |
| 117 |
|
| 118 |
private String cleanResult(String result) { |
| 119 |
result = NEW_LINE_WHITESPACE_CLEANUP.matcher(result).replaceAll("\n"); |
| 120 |
result = NEW_LINE_CLEANUP.matcher(result).replaceAll("\n\n"); |
| 121 |
result = result.replace("\n", lineSeparator); |
| 122 |
return result; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void visit(Node.TemplateText n) { |
| 127 |
if (output.isTrimWhitespace()) { |
| 128 |
output.append(n.getText().trim()); |
| 129 |
} else { |
| 130 |
output.append(n.getText()); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public void visit(Node.UseBean n) throws JasperException { |
| 136 |
if (isSuppressed(n)) { |
| 137 |
return; |
| 138 |
} |
| 139 |
super.visit(n); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void visit(Node.ELExpression n) { |
| 144 |
if (!output.isInsideScript()) { |
| 145 |
output.append("${"); |
| 146 |
} |
| 147 |
|
| 148 |
output.append(convertAttributeValue("${" + n.getText() + "}")); |
| 149 |
|
| 150 |
if (!output.isInsideScript()) { |
| 151 |
output.append("}"); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void visit(Node.PageDirective n) { |
| 157 |
// noop |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void visit(Node.Root n) throws JasperException { |
| 162 |
visitBody(n); |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public void visit(Node.TaglibDirective n) { |
| 167 |
// noop |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public void visit(Node.TagDirective n) { |
| 172 |
// noop |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public void visit(Node.SetProperty n) { |
| 177 |
String name = n.getAttributeValue("name"); |
| 178 |
String value = n.getAttributeValue("value"); |
| 179 |
String property = n.getAttributeValue("property"); |
| 180 |
|
| 181 |
output.append("!{"); |
| 182 |
output.append(name).append(".set"); |
| 183 |
output.append(Character.toUpperCase(property.charAt(0)) + property.substring(1)); |
| 184 |
output.append("("); |
| 185 |
output.append(JspExpressionConverter.convertAttributeValue(value)); |
| 186 |
output.append(");}"); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public void visit(Node.AttributeDirective n) { |
| 191 |
new JspAttributeConverter().convert(this, new JtpAttribute(n), output); |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void visit(Node.Declaration n) throws JasperException { |
| 196 |
if (isSuppressed(n)) { |
| 197 |
return; |
| 198 |
} |
| 199 |
super.visit(n); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public void visit(Node.CustomTag n) throws JasperException { |
| 204 |
if (isSuppressed(n)) { |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
CustomTagConverter converter = converters.get(n.getQName()); |
| 209 |
if (converter == null) { |
| 210 |
throw new RuntimeException("Missing converter for custom tag: <" + n.getQName() + " />"); |
| 211 |
} |
| 212 |
|
| 213 |
JtpCustomTag tag = new JtpCustomTag(n); |
| 214 |
|
| 215 |
output.pushTrimWhitespace(converter.isTrimWhitespace()); |
| 216 |
converter.convert(this, tag, output, () -> tag.visitBody(this)); |
| 217 |
output.popTrimWhitespace(); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public void visit(Node.Comment n) { |
| 222 |
if (isSuppressed(n)) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
output.append("<%--").append(n.getText()).append("--%>"); |
| 227 |
|
| 228 |
if (output.isTrimWhitespace()) { |
| 229 |
output.setAllowNextWhitespace(); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public void visit(Node.Scriptlet n) throws JasperException { |
| 235 |
if (isSuppressed(n)) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
super.visit(n); |
| 240 |
} |
| 241 |
|
| 242 |
public static JspElementType getType(Node n) { |
| 243 |
return JspElementType.valueOf(n.getClass().getSimpleName()); |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public void visit(Node.IncludeDirective n) throws JasperException { |
| 248 |
String file = n.getAttributeValue("file"); |
| 249 |
if (!inlinedIncludes.contains(file)) { |
| 250 |
throw new UnsupportedOperationException("Includes are not supported. You should convert it to a tag first, or suppress with addInlinedInclude(\"" + file + "\")"); |
| 251 |
} |
| 252 |
|
| 253 |
visitBody(n); |
| 254 |
} |
| 255 |
|
| 256 |
@Override |
| 257 |
public void visit(Node.DoBodyAction n) { |
| 258 |
addImport("gg.jte.Content"); |
| 259 |
addParam("Content bodyContent"); |
| 260 |
output.append("${bodyContent}"); |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
protected void doVisit(Node n) { |
| 265 |
throw new RuntimeException("Unsupported feature detected: " + n.getClass().getSimpleName()); |
| 266 |
} |
| 267 |
|
| 268 |
private boolean isSuppressed(Node n) { |
| 269 |
EnumSet<JspElementType> suppressedTypes = suppressions.get(n.getStart().getFile()); |
| 270 |
if (suppressedTypes == null) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
JspElementType type = getType(n); |
| 275 |
return suppressedTypes.contains(type); |
| 276 |
} |
| 277 |
} |