Skip to content

Commit 2f78246

Browse files
committed
add Taglet module for jdk<9
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent 44d31af commit 2f78246

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

graphviz-java/src/main/java/guru/nidi/graphviz/engine/Renderer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public String toString() {
4646
return graphviz.execute();
4747
}
4848

49-
public void toFile(File file) throws IOException {
49+
public File toFile(File file) throws IOException {
5050
Files.createDirectories(file.getAbsoluteFile().getParentFile().toPath());
5151
final File target = file.getName().contains(".")
5252
? file
@@ -58,6 +58,7 @@ public void toFile(File file) throws IOException {
5858
out.write(toString());
5959
}
6060
}
61+
return target;
6162
}
6263

6364
public void toOutputStream(OutputStream outputStream) throws IOException {

graphviz-taglet/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>graphviz-java-parent</artifactId>
7+
<groupId>guru.nidi</groupId>
8+
<version>0.8.6-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>graphviz-taglet</artifactId>
12+
<name>${project.artifactId}</name>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<artifactId>maven-javadoc-plugin</artifactId>
18+
<configuration>
19+
<taglet>guru.nidi.graphviz.GraphvizTaglet</taglet>
20+
<tagletArtifact>
21+
<groupId>${project.groupId}</groupId>
22+
<artifactId>${project.artifactId}</artifactId>
23+
<version>${project.version}</version>
24+
</tagletArtifact>
25+
</configuration>
26+
<dependencies>
27+
<dependency>
28+
<groupId>${project.groupId}</groupId>
29+
<artifactId>graphviz-java</artifactId>
30+
<version>${project.version}</version>
31+
</dependency>
32+
</dependencies>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>${project.groupId}</groupId>
40+
<artifactId>graphviz-java</artifactId>
41+
<version>${project.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>ch.qos.logback</groupId>
45+
<artifactId>logback-classic</artifactId>
46+
<version>1.2.3</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.sun</groupId>
50+
<artifactId>tools</artifactId>
51+
<version>1.8</version>
52+
<scope>system</scope>
53+
<systemPath>${java.home}/../lib/tools.jar</systemPath>
54+
</dependency>
55+
</dependencies>
56+
57+
</project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright © 2015 Stefan Niederhauser ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package guru.nidi.graphviz;
17+
18+
import com.sun.javadoc.Tag;
19+
import com.sun.tools.doclets.Taglet;
20+
import guru.nidi.graphviz.engine.Format;
21+
import guru.nidi.graphviz.engine.Graphviz;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.util.Map;
26+
import java.util.regex.Matcher;
27+
import java.util.regex.Pattern;
28+
29+
/**
30+
* Support graphviz inside javadoc.
31+
* <p>
32+
* {@graphviz
33+
* graph test {a -- b}
34+
* }
35+
* end
36+
*/
37+
public class GraphvizTaglet implements Taglet {
38+
public static void register(Map<String, Taglet> taglets) {
39+
final GraphvizTaglet taglet = new GraphvizTaglet();
40+
taglets.put(taglet.getName(), taglet);
41+
}
42+
43+
@Override
44+
public boolean inField() {
45+
return false;
46+
}
47+
48+
@Override
49+
public boolean inConstructor() {
50+
return false;
51+
}
52+
53+
@Override
54+
public boolean inMethod() {
55+
return false;
56+
}
57+
58+
@Override
59+
public boolean inOverview() {
60+
return false;
61+
}
62+
63+
@Override
64+
public boolean inPackage() {
65+
return false;
66+
}
67+
68+
@Override
69+
public boolean inType() {
70+
return false;
71+
}
72+
73+
@Override
74+
public boolean isInlineTag() {
75+
return true;
76+
}
77+
78+
@Override
79+
public String getName() {
80+
return "graphviz";
81+
}
82+
83+
@Override
84+
public String toString(Tag tag) {
85+
try {
86+
final File file = new File(packageOf(tag), imageNameOf(tag));
87+
final File output = Graphviz.fromString(tag.text()).render(Format.PNG).toFile(file);
88+
return "<img title='" + imageTitleOf(tag) + "' src='" + output.getName() + "'></img>";
89+
} catch (IOException e) {
90+
throw new RuntimeException("Problem writing graphviz file", e);
91+
}
92+
}
93+
94+
private String imageTitleOf(Tag tag) {
95+
final Pattern startPattern = Pattern.compile("^\\s*(di)?graph\\s*(.*?)\\s\\{");
96+
final Matcher matcher = startPattern.matcher(tag.text());
97+
return matcher.find() ? matcher.group(2) : "";
98+
}
99+
100+
private String imageNameOf(Tag tag) {
101+
final String name = tag.position().file().getName();
102+
final String simple = name.substring(0, name.lastIndexOf('.'));
103+
return simple + "@" + tag.position().line();
104+
}
105+
106+
private String packageOf(Tag tag) {
107+
final String className = tag.holder().toString();
108+
final String pack = className.substring(0, className.lastIndexOf('.'));
109+
return pack.replace('.', '/');
110+
}
111+
112+
@Override
113+
public String toString(Tag[] tags) {
114+
return null;
115+
}
116+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<modules>
2828
<module>graphviz-java</module>
2929
<module>graphviz-kotlin</module>
30+
<module>graphviz-taglet</module>
3031
</modules>
3132

3233
<scm>

0 commit comments

Comments
 (0)