Skip to content

Commit 6926cc3

Browse files
committed
Initial commit
0 parents  commit 6926cc3

5 files changed

Lines changed: 300 additions & 0 deletions

File tree

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
target/
2+
pom.xml.tag
3+
pom.xml.releaseBackup
4+
pom.xml.versionsBackup
5+
pom.xml.next
6+
release.properties
7+
dependency-reduced-pom.xml
8+
buildNumber.properties
9+
.mvn/timing.properties

nbactions-release-profile.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>-classpath %classpath com.codename1.tools.javadoc.sourceembed.javadocsourceembed.Main</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>process-classes</goal>
24+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
25+
</goals>
26+
<properties>
27+
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath com.codename1.tools.javadoc.sourceembed.javadocsourceembed.Main</exec.args>
28+
<exec.executable>java</exec.executable>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath com.codename1.tools.javadoc.sourceembed.javadocsourceembed.Main</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
<action>
47+
<actionName>build</actionName>
48+
<packagings>
49+
<packaging>*</packaging>
50+
</packagings>
51+
<goals>
52+
<goal>install</goal>
53+
<goal>package</goal>
54+
</goals>
55+
</action>
56+
</actions>

nbactions.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>-classpath %classpath ${packageClassName} /Users/shai/dev/cn1/CodenameOne/src /Users/shai/dev/cn1/CodenameOne/build/tempJavaSources</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>process-classes</goal>
24+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
25+
</goals>
26+
<properties>
27+
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName} /Users/shai/dev/cn1/CodenameOne/src /Users/shai/dev/cn1/CodenameOne/build/tempJavaSources</exec.args>
28+
<exec.executable>java</exec.executable>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath ${packageClassName} /Users/shai/dev/cn1/CodenameOne/src /Users/shai/dev/cn1/CodenameOne/build/tempJavaSources</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>

pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.codename1.tools.javadoc.sourceembed</groupId>
5+
<artifactId>JavaDocSourceEmbed</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<dependencies>
9+
<dependency>
10+
<groupId>org.json</groupId>
11+
<artifactId>json</artifactId>
12+
<version>20151123</version>
13+
</dependency>
14+
</dependencies>
15+
<properties>
16+
<mainClass>com.codename1.tools.javadoc.sourceembed.javadocsourceembed.Main</mainClass>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
</properties>
21+
<build>
22+
<plugins>
23+
<plugin>
24+
<artifactId>maven-dependency-plugin</artifactId>
25+
<executions>
26+
<execution>
27+
<phase>install</phase>
28+
<goals>
29+
<goal>copy-dependencies</goal>
30+
</goals>
31+
<configuration>
32+
<outputDirectory>${project.build.directory}/lib</outputDirectory>
33+
</configuration>
34+
</execution>
35+
</executions>
36+
</plugin>
37+
<plugin>
38+
<artifactId>maven-jar-plugin</artifactId>
39+
<configuration>
40+
<archive>
41+
<manifest>
42+
<addClasspath>true</addClasspath>
43+
<classpathPrefix>lib/</classpathPrefix>
44+
<mainClass>${mainClass}</mainClass>
45+
</manifest>
46+
</archive>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
* This code is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 only, as
6+
* published by the Free Software Foundation. Codename One designates this
7+
* particular file as subject to the "Classpath" exception as provided
8+
* by Oracle in the LICENSE file that accompanied this code.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Codename One through http://www.codenameone.com/ if you
21+
* need additional information or have any questions.
22+
*/
23+
24+
package com.codename1.tools.javadoc.sourceembed.javadocsourceembed;
25+
26+
import java.io.BufferedReader;
27+
import java.io.BufferedWriter;
28+
import java.io.File;
29+
import java.io.FileFilter;
30+
import java.io.FileWriter;
31+
import java.io.InputStream;
32+
import java.io.InputStreamReader;
33+
import java.net.URL;
34+
import java.nio.charset.Charset;
35+
import java.nio.file.Files;
36+
import java.nio.file.Paths;
37+
import java.util.HashMap;
38+
import java.util.List;
39+
import java.util.function.Consumer;
40+
import java.util.stream.Collectors;
41+
import org.json.JSONObject;
42+
43+
/**
44+
* <p>A trivial tool that allows embedding Java source code into JavaDoc using github gists in a way that
45+
* works nicely in the browser and in the ide...</p>
46+
* <p>To use this just use the standard gist embed code within your javadoc comments then run this tool by
47+
* supplying the source directory and a destination directory to which the modified sources should be written
48+
* then run javadoc on the modified code. Notice that you shouldn't modify the standrad code as the generated
49+
* code is a bit too "loud".
50+
* </p>
51+
*
52+
* @author Shai Almog
53+
*/
54+
public class Main {
55+
private static Charset CHARSET;
56+
private static final HashMap<String, String> gistCache = new HashMap<>();
57+
public static void main(String[] args) throws Exception {
58+
// this accepts two arguments source directory and destination directory where the modfied files will
59+
// be written
60+
File sourceDir = new File(args[0]);
61+
File destDir = new File(args[1]);
62+
System.out.println("JavaDoc conversion " + sourceDir.getAbsolutePath() + " to " + destDir.getAbsolutePath());
63+
if(!sourceDir.exists() || !sourceDir.isDirectory()) {
64+
System.out.println("Source directory doesn't exist");
65+
System.exit(1);
66+
return;
67+
}
68+
CHARSET = Charset.forName("UTF-8");
69+
directoryWalker(sourceDir, destDir);
70+
}
71+
72+
public static void directoryWalker(File f, File destDir) throws Exception {
73+
File[] files = f.listFiles((path) -> path.isDirectory() || path.getName().endsWith(".java"));
74+
75+
for(File ff : files) {
76+
if(ff.isDirectory()) {
77+
File destDirT = new File(destDir, ff.getName());
78+
destDirT.mkdirs();
79+
directoryWalker(ff, destDirT);
80+
} else {
81+
processFile(ff, new File(destDir, ff.getName()));
82+
}
83+
}
84+
}
85+
86+
public static void processFile(File javaSourceFile, File javaDestFile) throws Exception {
87+
System.out.println("JavaSource Processing: " + javaSourceFile.getName());
88+
List<String> lines = Files.readAllLines(Paths.get(javaSourceFile.toURI()), CHARSET);
89+
for(int iter = 0 ; iter < lines.size() ; iter++) {
90+
String l = lines.get(iter);
91+
int position = l.indexOf("<script src=\"https://gist.github.com/");
92+
if(position > -1) {
93+
String id = l.substring(position + 39 );
94+
id = id.split("/")[1];
95+
id = id.substring(0, id.indexOf('.'));
96+
String fileContent = gistCache.get(id);
97+
if(fileContent != null) {
98+
lines.add(iter + 1, fileContent);
99+
iter++;
100+
continue;
101+
}
102+
103+
URL u = new URL("https://api.github.com/gists/" + id);
104+
try(BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream(), CHARSET))) {
105+
String jsonText = br.lines().collect(Collectors.joining("\n"));
106+
JSONObject json = new JSONObject(jsonText);
107+
JSONObject filesObj = json.getJSONObject("files");
108+
String str ="";
109+
for(String k : filesObj.keySet()) {
110+
JSONObject jsonFileEntry = filesObj.getJSONObject(k);
111+
// breaking line to fix the problem with a blank space on the first line
112+
String current = "\n" + jsonFileEntry.getString("content");
113+
str += current;
114+
}
115+
int commentStartPos = str.indexOf("/*");
116+
while(commentStartPos > -1) {
117+
// we just remove the comment as its pretty hard to escape it properly
118+
int commentEndPos = str.indexOf("*/");
119+
str = str.substring(commentStartPos, commentEndPos + 1);
120+
commentStartPos = str.indexOf("/*");
121+
}
122+
123+
// allows things like the @Override annotation
124+
str = "<noscript><pre>{@code " + str.replace("@", "{@literal @}") + "}</pre></noscript>";
125+
gistCache.put(id, str);
126+
lines.add(iter + 1, str);
127+
iter++;
128+
}
129+
}
130+
}
131+
try(BufferedWriter fw = new BufferedWriter(new FileWriter(javaDestFile))) {
132+
for(String l : lines) {
133+
fw.write(l);
134+
fw.write('\n');
135+
}
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)