Skip to content

Commit 863d7f6

Browse files
Release/0.6.1 (#3)
* Update version to 0.6.1. * Suppress automatic module warnings in module-info.java files. * Remove scope attribute when transforming Equal/NotEqual tags. * Build out unit tests for preprocessing transformers. * Fix regex matching logic in InlineBeanWriteTagTransformer. * Build out support for additional command-line switches. * Use a custom JSoup jar with Tag.convertTag() to preserve tag features when changing names.
1 parent 5e9f82a commit 863d7f6

File tree

22 files changed

+200
-57
lines changed

22 files changed

+200
-57
lines changed

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/jspServices/StrutsToSpringToolkit.jspServices.test.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

buildSrc/src/main/groovy/com.rombalabs.strutstospringtoolkit.java-common-conventions.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ plugins {
77
id 'java'
88
}
99

10-
version = '0.5.0-alpha.3'
10+
version = '0.6.1'
1111

1212
repositories {
1313
// Use Maven Central for resolving dependencies.
1414
mavenCentral()
15+
flatDir {
16+
dirs("../../jsoup/target")
17+
}
1518
}
16-
1719
dependencies {
1820
constraints {
1921
// Define dependency versions as constraints

jspConverter/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tasks.named('jar', Jar) {
1717
dependencies {
1818
implementation 'org.apache.logging.log4j:log4j-core'
1919
implementation 'org.apache.logging.log4j:log4j-api'
20-
20+
implementation 'commons-io:commons-io'
2121
implementation project(':jspServices')
2222
}
2323

jspConverter/src/main/java/com/rombalabs/strutstospringtoolkit/jspconverter/App.java

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import com.rombalabs.strutstospringtoolkit.jspservices.StrutsProcessor;
55
import org.apache.logging.log4j.LogManager;
66
import org.apache.logging.log4j.Logger;
7+
import org.apache.commons.io.FileUtils;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.util.Arrays;
11+
12+
import static java.lang.System.exit;
713

814
/*
915
* This Java source file was generated by the Gradle 'init' task.
@@ -12,30 +18,72 @@ public class App {
1218

1319
private static final Logger logger = LogManager.getLogger();
1420

15-
public static void main(String[] args) {
21+
public static void main(String[] args) throws IOException {
1622

1723
boolean rewriteInPlace = false;
24+
boolean preprocessOnly = false;
25+
boolean jsoupOnly = false;
26+
1827
if(args.length < 1)
1928
{
20-
logger.fatal("Please provide a file name as an argument.");
21-
return;
29+
printUsage();
30+
exit(1);
2231
}
2332

24-
if(args.length == 2)
25-
{
26-
if (args[1].equals("--replace")) {
27-
rewriteInPlace = true;
33+
//TODO This is gross, use a proper CLI arguments library!
34+
for (String arg : Arrays.stream(args).skip(1).toArray(String[]::new)) {
35+
switch (arg) {
36+
case "--replace":
37+
rewriteInPlace = true;
38+
break;
39+
40+
case "--preprocessOnly":
41+
preprocessOnly = true;
42+
break;
43+
44+
case "--jsoupOnly":
45+
jsoupOnly = true;
46+
break;
47+
48+
default:
49+
logger.fatal("Invalid argument: " + arg);
50+
printUsage();
51+
exit(1);
52+
return;
2853
}
29-
else {
30-
logger.fatal("Invalid argument.");
31-
return;
54+
}
55+
56+
String inputFile = args[0];
57+
var outputPath = inputFile.replace(".jsp", "_converted.jsp");
58+
logger.info("Storing output in " + (rewriteInPlace ? "temporary " : "") + "location: " + outputPath);
59+
if (!jsoupOnly) {
60+
var preProcessor = new PreProcessor();
61+
preProcessor.processFile(inputFile, outputPath);
62+
}
63+
64+
if (!preprocessOnly) {
65+
if (jsoupOnly) {
66+
outputPath = inputFile;
3267
}
68+
var strutsProcessor = new StrutsProcessor();
69+
strutsProcessor.processFile(outputPath, outputPath);
3370
}
3471

35-
var preProcessor = new PreProcessor();
36-
var strutsProcessor = new StrutsProcessor();
72+
if (rewriteInPlace) {
73+
logger.info("Rewriting existing input file...");
74+
File outputFile = new File(outputPath);
75+
FileUtils.copyFile(outputFile, new File(inputFile));
76+
FileUtils.delete(outputFile);
77+
}
78+
79+
}
3780

38-
var outputFile = preProcessor.processFile(args[0], rewriteInPlace);
39-
strutsProcessor.processFile(outputFile, true);
81+
private static void printUsage() {
82+
System.out.println("Usage:");
83+
System.out.println("jspConverter [FILE] [OPTIONS]\n");
84+
System.out.println("Options:");
85+
System.out.println("\t--replace\tOverwrites the input file");
86+
System.out.println("\t--preprocessOnly\tOnly runs the preprocessor (doesn't load the whole document as DOM).");
87+
System.out.println("\t--jsoupOnly\tOnly runs the JSoup DOM processor.");
4088
}
4189
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
@SuppressWarnings({ "requires-automatic", "requires-transitive-automatic" })
12
module StrutsToSpringToolkit.jspConverter.main {
23
requires org.apache.logging.log4j;
34
requires StrutsToSpringToolkit.jspServices.main;
5+
requires org.apache.commons.io;
46
}

jspServices/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ plugins {
99
tasks.named('jar') {
1010
manifest {
1111
attributes(
12-
// 'Automatic-Module-Name': 'jspConverterModule',
1312
'Class-Path': configurations.runtimeClasspath.collect { it.getName() }.join(' '))
1413
}
1514
}
@@ -19,6 +18,8 @@ dependencies {
1918
implementation 'org.apache.logging.log4j:log4j-api'
2019
implementation 'commons-io:commons-io'
2120
implementation 'org.apache.commons:commons-lang3'
21+
implementation group: 'jakarta.annotation', name: 'jakarta.annotation-api', version: '2.1.1'
2222

23-
api group: 'org.jsoup', name: 'jsoup', version: '1.16.1'
24-
}
23+
api group: 'org.jsoup', name: 'jsoup', version: '1.16.2-SNAPSHOT'
24+
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
25+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.rombalabs.strutstospringtoolkit.jspservices;
22

33
public interface FileProcessor {
4-
String processFile(String filename, boolean rewrite);
4+
String processFile(String filename, String outputFilePath);
55
}

jspServices/src/main/java/com/rombalabs/strutstospringtoolkit/jspservices/PreProcessor.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.rombalabs.strutstospringtoolkit.jspservices.transformers.preprocessing.HtmlTagTransformer;
66
import com.rombalabs.strutstospringtoolkit.jspservices.transformers.preprocessing.InlineBeanWriteTagTransformer;
77
import com.rombalabs.strutstospringtoolkit.jspservices.transformers.preprocessing.InlineScriptletTransformer;
8-
import org.apache.commons.io.FileUtils;
98
import org.apache.logging.log4j.LogManager;
109
import org.apache.logging.log4j.Logger;
1110

@@ -30,13 +29,11 @@ public PreProcessor() {
3029
}
3130

3231
@Override
33-
public String processFile(String filename, boolean rewrite) {
34-
var outputPath = filename.replace(".jsp", "_converted.jsp");
32+
public String processFile(String filename, String outputFilePath) {
3533
logger.info("Loading file " + filename);
36-
logger.info("Storing output in " + (rewrite ? "temporary " : "") + "location: " + outputPath);
3734

3835
try (var reader = new BufferedReader(new FileReader(filename))) {
39-
try (var writer = new BufferedWriter(new FileWriter(outputPath))) {
36+
try (var writer = new BufferedWriter(new FileWriter(outputFilePath))) {
4037
String line;
4138
while ((line = reader.readLine()) != null) {
4239
var processedLine = processContent(line);
@@ -45,19 +42,13 @@ public String processFile(String filename, boolean rewrite) {
4542
}
4643
}
4744

48-
if (rewrite) {
49-
File outputFile = new File(outputPath);
50-
FileUtils.copyFile(outputFile, new File(filename));
51-
FileUtils.delete(outputFile);
52-
outputPath = filename;
53-
}
5445

5546
} catch (IOException e) {
5647
logger.error("Failed to load file.", e);
5748
throw new RuntimeException(e);
5849
}
5950

60-
return outputPath;
51+
return outputFilePath;
6152
}
6253

6354
public String processContent(String content) {

jspServices/src/main/java/com/rombalabs/strutstospringtoolkit/jspservices/StrutsProcessor.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public StrutsProcessor() {
5252
}
5353

5454
@Override
55-
public String processFile(String filename, boolean rewrite) {
55+
public String processFile(String filename, String outputFilePath) {
5656
String content;
5757
try {
5858
logger.info("Loading file " + filename);
@@ -64,7 +64,7 @@ public String processFile(String filename, boolean rewrite) {
6464

6565
var result = processContent(content);
6666

67-
return save(result, filename, rewrite);
67+
return save(result, outputFilePath);
6868
}
6969

7070
public Document processContent(String content) {
@@ -102,20 +102,16 @@ private Element processElement(Element targetElement) {
102102
return targetElement;
103103
}
104104

105-
private String save(Document doc, String originalFileName, boolean rewrite)
105+
private String save(Document doc, String outputFileName)
106106
{
107107
try {
108-
if (!rewrite)
109-
originalFileName = originalFileName.replace(".jsp", "_converted.jsp");
110-
111-
logger.info("Saving output file to: " + originalFileName);
112-
File convertedFile = new File(originalFileName);
108+
logger.info("Saving output file to: " + outputFileName);
109+
File convertedFile = new File(outputFileName);
113110
FileUtils.writeStringToFile(convertedFile, Parser.unescapeEntities(doc.html(), false), "UTF-8");
114-
//FileUtils.writeStringToFile(convertedFile, doc.html(), "UTF-8");
115111
} catch (IOException e) {
116112
logger.fatal("Failed to save converted file...", e);
117113
}
118114

119-
return originalFileName;
115+
return outputFileName;
120116
}
121117
}

0 commit comments

Comments
 (0)