Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for @value tags #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@
.Trashes
Icon?
ehthumbs.db
Thumbs.db
Thumbs.db

# IntelliJ Artifacts
/target
*.iml
/.idea/
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.stfm</groupId>
<artifactId>texdoclet</artifactId>
<version>0.9-SNAPSHOT</version>
<version>0.9.1-SNAPSHOT</version>
<name>TexDoclet</name>
<packaging>jar</packaging>

Expand Down Expand Up @@ -82,6 +82,11 @@
<artifactId>png-encoder</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

<!-- some mark down processors -->

Expand Down
90 changes: 89 additions & 1 deletion src/main/java/org/stfm/texdoclet/TeXDoclet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -32,11 +33,13 @@
import com.sun.javadoc.PackageDoc;
import com.sun.javadoc.ParamTag;
import com.sun.javadoc.Parameter;
import com.sun.javadoc.ProgramElementDoc;
import com.sun.javadoc.RootDoc;
import com.sun.javadoc.SeeTag;
import com.sun.javadoc.Tag;
import com.sun.javadoc.ThrowsTag;
import com.sun.javadoc.Type;
import org.apache.commons.lang.StringUtils;

/**
* This class provides a Java <code>javadoc</code> Doclet which generates a <TEX
Expand Down Expand Up @@ -538,7 +541,6 @@ static public boolean validOptions(String[][] args, DocErrorReporter err) {
* the root of the starting document
*/
public static boolean start(RootDoc root) {

theroot = root;

init();
Expand Down Expand Up @@ -2079,6 +2081,11 @@ static void printTags(PackageDoc this_package, Tag[] tags) {
}
} else if ("@code".equals(tags[i].name())) {
htmlstr += "<code>" + tags[i].text() + "</code>";
} else if ("@value".equals(tags[i].name())) {
final String value = getValue(tags[i]);
if (value != null) {
htmlstr += "<code>" + value + "</code>";
}
} else {
htmlstr += tags[i].text();
}
Expand All @@ -2087,6 +2094,87 @@ static void printTags(PackageDoc this_package, Tag[] tags) {
os.print(HTMLtoLaTeXBackEnd.fixText(htmlstr));
}

static String getValue(final Tag tag) {
final String text = tag.text().trim();
final String classpath;
final String member;

if (text.isEmpty()) {
if (tag.holder() instanceof FieldDoc) {
final FieldDoc fieldDoc = ((FieldDoc) tag.holder());
return fieldDoc.constantValueExpression();
} else {
throw new RuntimeException(
"@value cannot be empty when not documenting a field. " + tag + ", position: " + tag.position());
}
} else if (!text.contains("#")) {
return null;
} else {
final String[] parts = text.split("#");

if (parts.length != 2 || parts[1].isEmpty()) {
throw new RuntimeException(
"Invalid tag format: " + tag + ", position: " + tag.position());
}

member = parts[1];

if (parts[0].isEmpty()) {
if (tag.holder() instanceof ClassDoc) {
final ClassDoc classDoc = ((ClassDoc) tag.holder());
classpath = classDoc.qualifiedName();
} else if (tag.holder() instanceof ProgramElementDoc) {
final ProgramElementDoc elDoc = ((ProgramElementDoc) tag.holder());
classpath = elDoc.qualifiedName().replaceAll("\\." + elDoc.name() + "$", "");
} else {
throw new RuntimeException("Unable to find parent class.");
}
} else {
classpath = parts[0];
}
}

final Class clazz;
try {
clazz = getClassForNameBackoff(classpath);
} catch (final ClassNotFoundException e) {
throw new RuntimeException(
"Unable to find class in {@value " + text + "}. " + tag.position(), e);
}

final Field field;
try {
field = clazz.getDeclaredField(member);
} catch (final NoSuchFieldException e) {
throw new RuntimeException(
"Field \"" + member + "\n on class \"" + clazz.getCanonicalName() + "\". "
+ tag.position(), e);
}

field.setAccessible(true);
try {
return field.get(null).toString();
} catch (final IllegalAccessException e) {
throw new RuntimeException(
"Unable to access field: " + field + ". " + tag.position(), e);
}
}

static Class getClassForNameBackoff(final String classpath) throws ClassNotFoundException {
final int dots = StringUtils.countMatches(classpath, ".");
for (int i = 0; i <= dots; i++) {
final String newClasspath = StringUtils.reverse(
StringUtils.replace(StringUtils.reverse(classpath), ".", "$", i));
try {
return Class.forName(newClasspath);
} catch (final ClassNotFoundException e) {
System.out.println("Couldn't find: " + newClasspath);
}
}

throw new ClassNotFoundException(classpath);
}

/**
* Prints a reference to a package, class or member.
*/
Expand Down