Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created November 10, 2012 23:15
Show Gist options
  • Save fredgrott/4052922 to your computer and use it in GitHub Desktop.
Save fredgrott/4052922 to your computer and use it in GitHub Desktop.
DocFile change on AOSP's copy of doclava
package com.google.doclava;
import com.google.clearsilver.jsilver.data.Data;
import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class DocFile {
private static final Pattern LINE = Pattern.compile("(.*)[\r]?\n", Pattern.MULTILINE);
private static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
public static String readFile(String filename) {
try {
File f = new File(filename);
int length = (int) f.length();
FileInputStream is = new FileInputStream(f);
InputStreamReader reader = new InputStreamReader(is, "UTF-8");
char[] buf = new char[length];
int index = 0;
int amt;
while (true) {
amt = reader.read(buf, index, length - index);
if (amt < 1) {
break;
}
index += amt;
}
return new String(buf, 0, index);
} catch (IOException e) {
return null;
}
}
public static void writePage(String docfile, String relative, String outfile) {
Data hdf = Doclava.makeHDF();
/*
* System.out.println("docfile='" + docfile + "' relative='" + relative + "'" + "' outfile='" +
* outfile + "'");
*/
String filedata = readFile(docfile);
// The document is properties up until the line "@jd:body".
// Any blank lines are ignored.
int start = -1;
int lineno = 1;
Matcher lines = LINE.matcher(filedata);
String line = null;
while (lines.find()) {
line = lines.group(1);
if (line.length() > 0) {
if (line.equals("@jd:body")) {
start = lines.end();
break;
}
Matcher prop = PROP.matcher(line);
if (prop.matches()) {
String key = prop.group(1);
String value = prop.group(2);
hdf.setValue(key, value);
} else {
break;
}
}
lineno++;
}
if (start < 0) {
System.err.println(docfile + ":" + lineno + ": error parsing docfile");
if (line != null) {
System.err.println(docfile + ":" + lineno + ":" + line);
}
System.exit(1);
}
// if they asked to only be for a certain template, maybe skip it
String fromTemplate = hdf.getValue("template.which", "");
String fromPage = hdf.getValue("page.onlyfortemplate", "");
if (!"".equals(fromPage) && !fromTemplate.equals(fromPage)) {
return;
}
// and the actual text after that
String commentText = filedata.substring(start);
Comment comment = new Comment(commentText, null, new SourcePositionInfo(docfile, lineno, 1));
TagInfo[] tags = comment.tags();
TagInfo.makeHDF(hdf, "root.descr", tags);
hdf.setValue("commentText", commentText);
// write the page using the appropriate root template, based on the
// whichdoc value supplied by build
String fromWhichmodule = hdf.getValue("android.whichmodule", "");
if (fromWhichmodule.equals("online-pdk")) {
// leaving this in just for temporary compatibility with pdk doc
hdf.setValue("online-pdk", "true");
// add any conditional login for root template here (such as
// for custom left nav based on tab etc.
ClearPage.write(hdf, "docpage.cs", outfile);
} else {
String filename = outfile;
// Check whether this is a localized page and remove the intl/*/ path from filename
//if (filename.indexOf("intl/") == 0) {
// filename = filename.substring(filename.indexOf("/", 5) + 1); // After intl/ to get 2nd /
//}
//if (filename.indexOf("design/") == 0) {
// hdf.setValue("design", "true");
//} else if (filename.indexOf("develop/") == 0) {
// hdf.setValue("develop", "true");
//} else if (filename.indexOf("guide/") == 0) {
// hdf.setValue("guide", "true");
//} else if (filename.indexOf("training/") == 0) {
// hdf.setValue("training", "true");
//} else if (filename.indexOf("more/") == 0) {
// hdf.setValue("more", "true");
//} else if (filename.indexOf("google/") == 0) {
// hdf.setValue("google", "true");
//} else if (filename.indexOf("distribute/") == 0) {
// hdf.setValue("distribute", "true");
//} else if (filename.indexOf("about/") == 0) {
// hdf.setValue("about", "true");
//} else if ((filename.indexOf("tools/") == 0) || (filename.indexOf("sdk/") == 0)) {
// hdf.setValue("tools", "true");
//}
//if ((filename.indexOf("tools/sdk/preview/index.html") == 0) ||
// (filename.indexOf("sdk/index.html") == 0) ||
// (filename.indexOf("tools/sdk/ndk/index.html") == 0)) {
// ClearPage.write(hdf, "sdkpage.cs", outfile);
//} else {
// ClearPage.write(hdf, "docpage.cs", outfile);
//}
// This is specifically for GrottWorkShop both project
// subfolders and because I use it as a website/blog generator
// those subfolders too. You can re-use or fork and
// modify.
//
// Basic pattern is we set a hdf var as true if we are in
// subfolder name. This hdf var is than checked to see if
// true to trigger the right left side and other menus
if (filename.indexOf("about/") == 0) {
hdf.setValue("about", "true");
}
if (filename.indexOf("foss/") == 0) {
hdf.setValue("foss", "true");
}
if (filename.indexOf("getit/") ==0) {
hdf.setValue("getit", "true");
}
if (filename.indexOf("support/") == 0){
hdf.setValue("support", "true");
}
if (filename.indexOf("usage/") == 0) {
hdf.setValue("usage","true");
}
if (filename.indexOf("develop/") == 0){
hdf.setValue("develop","true");
}
if (filename.indexOf("codeqa/") == 0) {
hdf.setValue("codeqa", "true");
}
if (filename.indexOf("devguide/") ==0){
hdf.setValue("devguide", "true");
}
if(filename.indexOf("devtools/") ==0){
hdf.setValue("devtools", "true");
}
ClearPage.write(hdf, "docpage.cs", outfile);
}
} // writePage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment