Skip to content

Commit beaa6b8

Browse files
committed
Added src and pom.xml.
1 parent 8cb8d2c commit beaa6b8

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed

β€Žpom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>generatejavadoc</groupId>
4+
<artifactId>generatejavadoc</artifactId>
5+
<version>1.0</version>
6+
<packaging>jar</packaging>
7+
<name>generatejavadoc</name>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
</properties>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<version>3.8.1</version>
16+
<configuration>
17+
<source>1.8</source>
18+
<target>1.8</target>
19+
</configuration>
20+
</plugin>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-jar-plugin</artifactId>
24+
<version>3.2.0</version>
25+
<configuration>
26+
<archive>
27+
<manifestEntries>
28+
<Main-Class>generatejavadoc.Main</Main-Class>
29+
<Built-By></Built-By>
30+
<Created-By></Created-By>
31+
<Build-Jdk></Build-Jdk>
32+
</manifestEntries>
33+
</archive>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-shade-plugin</artifactId>
39+
<version>2.4.3</version>
40+
<executions>
41+
<execution>
42+
<phase>package</phase>
43+
<goals>
44+
<goal>shade</goal>
45+
</goals>
46+
<configuration>
47+
<filters>
48+
<filter>
49+
<artifact>*:*</artifact>
50+
<excludes>
51+
<exclude>META-INF/maven/**</exclude>
52+
<exclude>META-INF/services/**</exclude>
53+
<exclude>META-INF/LICENSE*</exclude>
54+
<exclude>META-INF/NOTICE*</exclude>
55+
<exclude>META-INF/CHANGES*</exclude>
56+
<exclude>META-INF/README.md*</exclude>
57+
</excludes>
58+
</filter>
59+
</filters>
60+
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
61+
</configuration>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package generatejavadoc;
2+
3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.DirectoryStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.regex.Pattern;
14+
import java.util.stream.Collectors;
15+
16+
public class Main {
17+
private static final Pattern PATTERN = Pattern.compile("\\r?\\n");
18+
19+
private static void fillFilesRecursively(
20+
Path directory, final List<File> resultFiles)
21+
throws IOException {
22+
Files.walkFileTree(
23+
directory,
24+
new java.nio.file.SimpleFileVisitor<Path>() {
25+
@Override
26+
public java.nio.file.FileVisitResult visitFile(
27+
Path file, java.nio.file.attribute.BasicFileAttributes attrs) {
28+
final String filePath = file.toString().toLowerCase();
29+
if (filePath.endsWith("readme.md")) {
30+
resultFiles.add(file.toFile());
31+
}
32+
return java.nio.file.FileVisitResult.CONTINUE;
33+
}
34+
});
35+
}
36+
37+
public static void main(String[] args) throws IOException {
38+
List<File> resultFiles = new ArrayList<>();
39+
fillFilesRecursively(Paths.get("."), resultFiles);
40+
int ind = 0;
41+
for (File file : resultFiles) {
42+
if (ind % 20 == 0) {
43+
System.out.println(file.getAbsolutePath());
44+
}
45+
ind++;
46+
final byte[] readmeMd = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
47+
String readmeMdText = new String(readmeMd, UTF_8);
48+
try (DirectoryStream<Path> dirStream =
49+
Files.newDirectoryStream(
50+
Paths.get(file.getAbsolutePath().replace("readme.md", "")), "*.java")) {
51+
for (Path entry : dirStream) {
52+
File javaFile = entry.toFile();
53+
Path path = Paths.get(javaFile.getAbsolutePath());
54+
final byte[] solutionJava = Files.readAllBytes(path);
55+
String solutionJavaText = new String(solutionJava, UTF_8);
56+
int[] index = {0};
57+
String[] fromStr = {
58+
"(\"{{",
59+
"= \"{{",
60+
"\n- [",
61+
" board = [[",
62+
" grid = [[",
63+
" = [[",
64+
",**",
65+
"**,",
66+
"**]",
67+
"(**",
68+
"**)"
69+
};
70+
String[] toStr = {
71+
"(\"{ {",
72+
"= \"{ {",
73+
"\n- \\[",
74+
" board = [ [",
75+
" grid = [ [",
76+
" = \\[\\[",
77+
", **",
78+
"** ,",
79+
"** ]",
80+
"( **",
81+
"** )"
82+
};
83+
String readmeMdJavadoc =
84+
"/**\n"
85+
+ StringUtils.replaceEach(
86+
PATTERN.splitAsStream(readmeMdText)
87+
.map(
88+
line -> {
89+
String firstLine =
90+
line.replace(
91+
"\\.",
92+
" -")
93+
+ "\\.";
94+
String str =
95+
index[0]++ == 0
96+
? firstLine
97+
: line;
98+
return line.isEmpty()
99+
? " *"
100+
: " * " + str;
101+
})
102+
.collect(Collectors.joining("\n")),
103+
fromStr,
104+
toStr)
105+
.replace("`**", "` **")
106+
.replace("/*", "{@literal /}*")
107+
.replace("*/", "*{@literal /}")
108+
+ "\n**/";
109+
String publicClass =
110+
solutionJavaText.contains("@SuppressWarnings")
111+
? "@SuppressWarnings"
112+
: "public class ";
113+
Files.write(path, getBytes(solutionJavaText, readmeMdJavadoc, publicClass));
114+
}
115+
}
116+
}
117+
}
118+
119+
private static byte[] getBytes(
120+
String solutionJavaText, String readmeMdJavadoc, String publicClass) {
121+
return solutionJavaText
122+
.replace("\n" + publicClass, "\n" + readmeMdJavadoc + "\n" + publicClass)
123+
.getBytes(UTF_8);
124+
}
125+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package generatejavadoc;
2+
3+
import java.lang.reflect.Array;
4+
5+
@SuppressWarnings("java:S3776")
6+
public class StringUtils {
7+
private StringUtils() {}
8+
9+
public static String replaceEach(
10+
final String text, final String[] searchList, final String[] replacementList) {
11+
if (isEmpty(text) || isEmpty(searchList) || isEmpty(replacementList)) {
12+
return text;
13+
}
14+
15+
final int searchLength = searchList.length;
16+
final int replacementLength = replacementList.length;
17+
18+
// make sure lengths are ok, these need to be equal
19+
if (searchLength != replacementLength) {
20+
throw new IllegalArgumentException(
21+
"Search and Replace array lengths don't match: "
22+
+ searchLength
23+
+ " vs "
24+
+ replacementLength);
25+
}
26+
27+
// keep track of which still have matches
28+
final boolean[] noMoreMatchesForReplIndex = new boolean[searchLength];
29+
30+
// index on index that the match was found
31+
int textIndex = -1;
32+
int replaceIndex = -1;
33+
int tempIndex = -1;
34+
35+
// index of replace array that will replace the search string found
36+
for (int i = 0; i < searchLength; i++) {
37+
if (noMoreMatchesForReplIndex[i]
38+
|| isEmpty(searchList[i])
39+
|| replacementList[i] == null) {
40+
continue;
41+
}
42+
tempIndex = text.indexOf(searchList[i]);
43+
44+
// see if we need to keep searching for this
45+
if (tempIndex == -1) {
46+
noMoreMatchesForReplIndex[i] = true;
47+
} else if (textIndex == -1 || tempIndex < textIndex) {
48+
textIndex = tempIndex;
49+
replaceIndex = i;
50+
}
51+
}
52+
// no search strings found, we are done
53+
if (textIndex == -1) {
54+
return text;
55+
}
56+
57+
int start = 0;
58+
59+
// get a good guess on the size of the result buffer so it doesn't have to double if it goes
60+
// over a bit
61+
int increase = 0;
62+
63+
// count the replacement text elements that are larger than their corresponding text being
64+
// replaced
65+
for (int i = 0; i < searchList.length; i++) {
66+
if (searchList[i] == null || replacementList[i] == null) {
67+
continue;
68+
}
69+
final int greater = replacementList[i].length() - searchList[i].length();
70+
if (greater > 0) {
71+
increase += 3 * greater; // assume 3 matches
72+
}
73+
}
74+
// have upper-bound at 20% increase, then let Java take over
75+
increase = Math.min(increase, text.length() / 5);
76+
77+
final StringBuilder buf = new StringBuilder(text.length() + increase);
78+
79+
while (textIndex != -1) {
80+
81+
for (int i = start; i < textIndex; i++) {
82+
buf.append(text.charAt(i));
83+
}
84+
buf.append(replacementList[replaceIndex]);
85+
86+
start = textIndex + searchList[replaceIndex].length();
87+
88+
textIndex = -1;
89+
replaceIndex = -1;
90+
// find the next earliest match
91+
// NOTE: logic mostly duplicated above START
92+
for (int i = 0; i < searchLength; i++) {
93+
if (noMoreMatchesForReplIndex[i]
94+
|| searchList[i] == null
95+
|| searchList[i].isEmpty()
96+
|| replacementList[i] == null) {
97+
continue;
98+
}
99+
tempIndex = text.indexOf(searchList[i], start);
100+
101+
// see if we need to keep searching for this
102+
if (tempIndex == -1) {
103+
noMoreMatchesForReplIndex[i] = true;
104+
} else if (textIndex == -1 || tempIndex < textIndex) {
105+
textIndex = tempIndex;
106+
replaceIndex = i;
107+
}
108+
}
109+
// NOTE: logic duplicated above END
110+
111+
}
112+
final int textLength = text.length();
113+
for (int i = start; i < textLength; i++) {
114+
buf.append(text.charAt(i));
115+
}
116+
return buf.toString();
117+
}
118+
119+
public static boolean isEmpty(CharSequence cs) {
120+
return cs == null || cs.length() == 0;
121+
}
122+
123+
public static boolean isEmpty(final Object[] array) {
124+
return getLength(array) == 0;
125+
}
126+
127+
public static int getLength(final Object array) {
128+
if (array == null) {
129+
return 0;
130+
}
131+
return Array.getLength(array);
132+
}
133+
}

0 commit comments

Comments
 (0)