| 1 |
package gg.jte.convert.jsp.converter; |
| 2 |
|
| 3 |
import gg.jte.convert.ConverterOutput; |
| 4 |
import gg.jte.convert.CustomTagConverter; |
| 5 |
import gg.jte.convert.jsp.BodyConverter; |
| 6 |
import org.apache.jasper.JasperException; |
| 7 |
import org.apache.jasper.compiler.JtpConverter; |
| 8 |
import org.apache.jasper.compiler.JtpCustomTag; |
| 9 |
import org.xml.sax.Attributes; |
| 10 |
|
| 11 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 12 |
|
| 13 |
import static gg.jte.convert.jsp.converter.JspExpressionConverter.convertAttributeValue; |
| 14 |
|
| 15 |
public class JspJteConverter implements CustomTagConverter { |
| 16 |
|
| 17 |
@Override |
| 18 |
public void convert(JtpConverter converter, JtpCustomTag tag, ConverterOutput output, BodyConverter bodyConverter) throws JasperException { |
| 19 |
String jteTagPath = tag.getAttribute("jte"); |
| 20 |
Attributes attributes = tag.getAttributes(); |
| 21 |
|
| 22 |
String pathWithoutExtension = jteTagPath.substring(0, jteTagPath.length() - 4); |
| 23 |
String tagCall = pathWithoutExtension.replace('/', '.'); |
| 24 |
int tagStartPos = output.getCurrentLineCharCount(); |
| 25 |
StringBuilder sb = new StringBuilder(tagStartPos); |
| 26 |
for (int i = 0; i < tagStartPos; ++i) { |
| 27 |
sb.append(output.getIndentationChar()); |
| 28 |
} |
| 29 |
final String tagStartIndent = sb.toString(); // Used in lambda, must be final |
| 30 |
output.append("@template.").append(tagCall).append("("); |
| 31 |
|
| 32 |
final AtomicBoolean first = new AtomicBoolean(true); // Used in lambda, must be final |
| 33 |
Runnable handleParameterStart = () -> { |
| 34 |
if (!first.get()) { |
| 35 |
output.append(","); |
| 36 |
} |
| 37 |
output.newLine(tagStartIndent); |
| 38 |
output.indent(1); |
| 39 |
first.set(false); |
| 40 |
}; |
| 41 |
|
| 42 |
for (int i = 0; i < attributes.getLength(); i++) { |
| 43 |
String localName = attributes.getLocalName(i); |
| 44 |
if ("jte".equals(localName)) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
handleParameterStart.run(); |
| 48 |
output.append(localName).append(" = ").append(convertAttributeValue(attributes.getValue(i))); |
| 49 |
} |
| 50 |
|
| 51 |
if (tag.hasBody()) { |
| 52 |
handleParameterStart.run(); |
| 53 |
output.append("bodyContent = @`"); |
| 54 |
bodyConverter.convert(); |
| 55 |
output.append("`"); |
| 56 |
} |
| 57 |
|
| 58 |
if (!first.get()) { |
| 59 |
output.newLine(tagStartIndent); |
| 60 |
} |
| 61 |
output.append(")"); |
| 62 |
} |
| 63 |
} |