| 1 |
package gg.jte.convert.jsp; |
| 2 |
|
| 3 |
import gg.jte.convert.IoUtils; |
| 4 |
import gg.jte.convert.ConverterOutput; |
| 5 |
import gg.jte.convert.cc.CamelCaseConverter; |
| 6 |
import gg.jte.convert.jsp.converter.*; |
| 7 |
import org.apache.jasper.compiler.JtpConverter; |
| 8 |
|
| 9 |
import java.io.IOException; |
| 10 |
import java.io.UncheckedIOException; |
| 11 |
import java.net.MalformedURLException; |
| 12 |
import java.net.URL; |
| 13 |
import java.nio.file.Files; |
| 14 |
import java.nio.file.Path; |
| 15 |
import java.nio.file.Paths; |
| 16 |
import java.util.*; |
| 17 |
import java.util.function.Consumer; |
| 18 |
import java.util.regex.Matcher; |
| 19 |
import java.util.regex.Pattern; |
| 20 |
import java.util.stream.Stream; |
| 21 |
|
| 22 |
public class JspToJteConverter { |
| 23 |
|
| 24 |
private static final Pattern UNCONVERTED_TAG_REFERENCES = Pattern.compile("<[a-zA-Z0-9\\-]+:[a-zA-Z0-9\\-]+\\b"); |
| 25 |
|
| 26 |
public static void convertFromIntelliJPlugin(String[] commandLineArgs, JspToJteConverter converter, Consumer<Converter> parserSetup) { |
| 27 |
if (commandLineArgs.length < 1) { |
| 28 |
throw new IllegalArgumentException("Missing JSP file, it should be the first argument on the command line."); |
| 29 |
} |
| 30 |
|
| 31 |
Path jspFile = Paths.get(commandLineArgs[0]); |
| 32 |
jspFile = converter.jspRoot.toAbsolutePath().relativize(jspFile); |
| 33 |
|
| 34 |
converter.convertTag(jspFile.toString(), parserSetup); |
| 35 |
} |
| 36 |
|
| 37 |
private final Path jspRoot; |
| 38 |
private final Path jteRoot; |
| 39 |
private final String jteTag; |
| 40 |
|
| 41 |
public JspToJteConverter(Path jspRoot, Path jteRoot, String jteTag) { |
| 42 |
this.jspRoot = jspRoot; |
| 43 |
this.jteRoot = jteRoot; |
| 44 |
this.jteTag = jteTag; |
| 45 |
} |
| 46 |
|
| 47 |
public void convertTag(String jspTag, Consumer<Converter> parserSetup) { |
| 48 |
Path jspTagFile = jspRoot.resolve(jspTag); |
| 49 |
|
| 50 |
String relativeFilePath = jspRoot.relativize(jspTagFile).toString(); |
| 51 |
Converter converter = new JtpConverter( |
| 52 |
relativeFilePath, |
| 53 |
readFile(jspTagFile).replace("\r", "").getBytes(), |
| 54 |
getResourceBase(), |
| 55 |
relativeFilePath.endsWith(".tag"), |
| 56 |
new ConverterOutput() |
| 57 |
); |
| 58 |
|
| 59 |
converter.register("c:if", new JspIfConverter()); |
| 60 |
converter.register("c:forEach", new JspForEachConverter()); |
| 61 |
converter.register("c:choose", new JspChooseConverter()); |
| 62 |
converter.register("c:when", new JspWhenConverter()); |
| 63 |
converter.register("c:otherwise", new JspOtherwiseConverter()); |
| 64 |
converter.register("c:set", new JspSetConverter()); |
| 65 |
|
| 66 |
converter.register("fmt:message", new JstlFmtMessageConverter()); |
| 67 |
converter.register("fmt:param", new JstlFmtParamConverter()); |
| 68 |
|
| 69 |
converter.register(this.jteTag, new gg.jte.convert.jsp.converter.JspJteConverter()); |
| 70 |
|
| 71 |
if (getNotConvertedTags() != null) { |
| 72 |
for (String notConvertedTag : getNotConvertedTags()) { |
| 73 |
converter.register(notConvertedTag, new JspNoopConverter()); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (parserSetup != null) { |
| 78 |
parserSetup.accept(converter); |
| 79 |
} |
| 80 |
|
| 81 |
String jte = converter.convert(); |
| 82 |
|
| 83 |
System.out.println(jte); |
| 84 |
|
| 85 |
checkDependencies(jte); |
| 86 |
|
| 87 |
String jteTemplate = suggestJteFile(jspTag.replace('\\', '/')); |
| 88 |
Path jteTemplateFile = jteRoot.resolve(jteTemplate); |
| 89 |
IoUtils.writeFile(jteTemplateFile, jte); |
| 90 |
|
| 91 |
replaceUsages(jspTagFile, jteTemplateFile); |
| 92 |
} |
| 93 |
|
| 94 |
@SuppressWarnings("unused") |
| 95 |
public void replaceUsages(String jspTag, String jteTag) { |
| 96 |
replaceUsages(jspRoot.resolve(jspTag), jteRoot.resolve(jteTag)); |
| 97 |
} |
| 98 |
|
| 99 |
public void replaceUsages(Path jspTag, Path jteTag) { |
| 100 |
String oldJspTagPrefix = extractTagPrefix(jspTag); |
| 101 |
String oldJspTagClosing = "</" + oldJspTagPrefix.substring(1) + ">"; |
| 102 |
String newJteFile = jteRoot.relativize(jteTag).toString().replace('\\', '/'); |
| 103 |
|
| 104 |
IoUtils.deleteFile(jspTag); |
| 105 |
|
| 106 |
try (Stream<Path> stream = Files.walk(jspRoot)) { |
| 107 |
stream |
| 108 |
.filter(Files::isRegularFile) |
| 109 |
.filter(p -> !Files.isDirectory(p)) |
| 110 |
.filter(p -> { |
| 111 |
String fileName = p.toString(); |
| 112 |
return fileName.endsWith(".jsp") || fileName.endsWith(".jsp.inc") || fileName.endsWith(".tag"); |
| 113 |
}).forEach(jspFile -> replaceUsages(jspFile, oldJspTagPrefix, oldJspTagClosing, newJteFile)); |
| 114 |
} catch (IOException e) { |
| 115 |
throw new UncheckedIOException(e); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
protected String readFile(Path jspTag) { |
| 120 |
return IoUtils.readFile(jspTag); |
| 121 |
} |
| 122 |
|
| 123 |
private void replaceUsages(Path jspFile, String oldJspTagPrefix, String oldJspTagClosing, String newJteFile) { |
| 124 |
boolean modified = false; |
| 125 |
|
| 126 |
StringBuilder jspContent = new StringBuilder(IoUtils.readFile(jspFile)); |
| 127 |
|
| 128 |
int lastIndex = 0; |
| 129 |
|
| 130 |
do { |
| 131 |
lastIndex = jspContent.indexOf(oldJspTagPrefix, lastIndex); |
| 132 |
if (lastIndex >= 0 && lastIndex < jspContent.length()) { |
| 133 |
char character = jspContent.charAt(lastIndex + oldJspTagPrefix.length()); |
| 134 |
if (Character.isWhitespace(character) || character == '/' || character == '>') { |
| 135 |
jspContent.replace(lastIndex, lastIndex + oldJspTagPrefix.length(), "<" + jteTag + " jte=\"" + newJteFile + "\""); |
| 136 |
modified = true; |
| 137 |
} else { |
| 138 |
++lastIndex; |
| 139 |
} |
| 140 |
} else { |
| 141 |
break; |
| 142 |
} |
| 143 |
} while (true); |
| 144 |
|
| 145 |
lastIndex = 0; |
| 146 |
|
| 147 |
do { |
| 148 |
lastIndex = jspContent.indexOf(oldJspTagClosing, lastIndex); |
| 149 |
if (lastIndex >= 0 && lastIndex < jspContent.length()) { |
| 150 |
jspContent.replace(lastIndex, lastIndex + oldJspTagClosing.length(), "</" + jteTag + ">"); |
| 151 |
modified = true; |
| 152 |
} else { |
| 153 |
break; |
| 154 |
} |
| 155 |
} while (true); |
| 156 |
|
| 157 |
if (modified) { |
| 158 |
IoUtils.writeFile(jspFile, jspContent.toString()); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private String extractTagPrefix(Path jspTag) { |
| 163 |
String fileName = jspTag.getFileName().toString(); |
| 164 |
String tagName = fileName.substring(0, fileName.indexOf('.')); |
| 165 |
|
| 166 |
String namespace = jspTag.getParent().getFileName().toString(); |
| 167 |
if ("tags".equals(namespace)) { |
| 168 |
namespace = "include"; |
| 169 |
} |
| 170 |
|
| 171 |
return "<" + namespace + ":" + tagName; |
| 172 |
} |
| 173 |
|
| 174 |
protected URL getResourceBase() { |
| 175 |
try { |
| 176 |
return Paths.get("").toUri().toURL(); |
| 177 |
} catch (MalformedURLException e) { |
| 178 |
throw new UncheckedIOException(e); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public String suggestJteFile(String jspFile) { |
| 183 |
int fileSeparatorIndex = jspFile.indexOf('.'); |
| 184 |
if (fileSeparatorIndex == -1) { |
| 185 |
throw new IllegalArgumentException("JSP file without file extension " + jspFile); |
| 186 |
} |
| 187 |
|
| 188 |
String jspFileWithoutExtension = jspFile.substring(0, fileSeparatorIndex); |
| 189 |
|
| 190 |
StringBuilder jteFile = new StringBuilder(); |
| 191 |
|
| 192 |
jteFile.append(jspFileWithoutExtension); |
| 193 |
CamelCaseConverter.convertTo(jteFile); |
| 194 |
jteFile.append(".jte"); |
| 195 |
|
| 196 |
return jteFile.toString(); |
| 197 |
} |
| 198 |
|
| 199 |
protected void checkDependencies(String jteCode) { |
| 200 |
List<String> unresolvedJspTags = findUnresolvedJspTags(jteCode); |
| 201 |
if (unresolvedJspTags.isEmpty()) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
Set<String> notConvertedTagsSet = getNotConvertedTagsAsSet(); |
| 206 |
|
| 207 |
Set<String> errors = new LinkedHashSet<>(); |
| 208 |
for (String unresolvedJspTag : unresolvedJspTags) { |
| 209 |
if (!notConvertedTagsSet.contains(unresolvedJspTag)) { |
| 210 |
errors.add("The tag " + unresolvedJspTag + "/> is used by this tag and not converted to jte yet. You should convert " + unresolvedJspTag + "/> first. If this is a tag that should be always converted by hand, implement getNotConvertedTags() and add it there."); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (!errors.isEmpty()) { |
| 215 |
throw new UnsupportedOperationException(String.join("\n", errors)); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
private Set<String> getNotConvertedTagsAsSet() { |
| 220 |
String[] notConvertedTags = getNotConvertedTags(); |
| 221 |
Set<String> notConvertedTagsSet = new HashSet<>(); |
| 222 |
if (notConvertedTags != null) { |
| 223 |
for (String notConvertedTag : notConvertedTags) { |
| 224 |
notConvertedTagsSet.add("<" + notConvertedTag); |
| 225 |
} |
| 226 |
} |
| 227 |
return notConvertedTagsSet; |
| 228 |
} |
| 229 |
|
| 230 |
protected List<String> findUnresolvedJspTags(String jteCode) { |
| 231 |
List<String> result = new ArrayList<>(); |
| 232 |
|
| 233 |
Matcher matcher = UNCONVERTED_TAG_REFERENCES.matcher(jteCode); |
| 234 |
|
| 235 |
while (matcher.find()) { |
| 236 |
result.add(matcher.group()); |
| 237 |
} |
| 238 |
|
| 239 |
return result; |
| 240 |
} |
| 241 |
|
| 242 |
protected String[] getNotConvertedTags() { |
| 243 |
return null; |
| 244 |
} |
| 245 |
} |