cdc.mf.html.MfToHtmlGenerator Maven / Gradle / Ivy
package cdc.mf.html;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupDir;
import cdc.issues.Issue;
import cdc.issues.Metas;
import cdc.issues.Params;
import cdc.issues.io.SnapshotData;
import cdc.issues.locations.Location;
import cdc.issues.rules.Profile;
import cdc.issues.rules.Rule;
import cdc.mf.html.adaptors.IndexSectionAdaptor;
import cdc.mf.html.adaptors.IssueAdaptor;
import cdc.mf.html.adaptors.LocationAdaptor;
import cdc.mf.html.adaptors.MetasAdaptor;
import cdc.mf.html.adaptors.MfElementAdaptor;
import cdc.mf.html.adaptors.ParamsAdaptor;
import cdc.mf.html.adaptors.ProfileAdaptor;
import cdc.mf.html.adaptors.RuleAdaptor;
import cdc.mf.html.adaptors.SnapshotDataAdaptor;
import cdc.mf.html.index.IndexSection;
import cdc.mf.model.MfClass;
import cdc.mf.model.MfElement;
import cdc.mf.model.MfEnumeration;
import cdc.mf.model.MfInterface;
import cdc.mf.model.MfNameItem;
import cdc.mf.model.MfPackage;
import cdc.mf.model.MfQNameItem;
import cdc.mf.model.MfType;
import cdc.util.files.Resources;
/**
* Generator of HTML file(s) from an MfModel, using ST4.
*
* One can generate one file (for printing) or a set of files using frames.
*/
public final class MfToHtmlGenerator extends AbstractGenerator {
public MfToHtmlGenerator(MfHtmlGenerationArgs args) {
super(args);
}
public void generate() throws IOException, ExecutionException {
final URL templates = Resources.getResource("cdc/mf/html/templates");
final STGroupDir stg = new STGroupDir(templates);
stg.registerModelAdaptor(MfElement.class, new MfElementAdaptor(args));
stg.registerModelAdaptor(Issue.class, new IssueAdaptor(args));
stg.registerModelAdaptor(Metas.class, new MetasAdaptor(args));
stg.registerModelAdaptor(Params.class, new ParamsAdaptor(args));
stg.registerModelAdaptor(Location.class, new LocationAdaptor(args));
stg.registerModelAdaptor(Profile.class, new ProfileAdaptor(args));
stg.registerModelAdaptor(Rule.class, new RuleAdaptor(args));
stg.registerModelAdaptor(SnapshotData.class, new SnapshotDataAdaptor(args));
stg.registerModelAdaptor(IndexSection.class, new IndexSectionAdaptor(args));
addDictionaries(stg);
copyFiles();
if (args.getHints().contains(MfHtmlGenerationHint.SINGLE_PAGE)) {
generateSingleDocument(stg);
} else {
final List tasks = new ArrayList<>();
addMainFrameTask(stg, tasks);
addAllPackagesFrameTask(stg, tasks);
addAllItemsFrameTask(stg, tasks);
addPackageItemsFramesTasks(stg, tasks);
addModelOverviewFrameTask(stg, tasks);
addIssuesFrameTask(stg, tasks);
addProfileFrameTask(stg, tasks);
addPackageOverviewFramesTasks(stg, tasks);
addTypeOverviewFramesTasks(stg, tasks);
addIndexFramesTasks(stg, tasks);
invokeTasks("HTML generation", tasks);
}
}
private void copyFiles() throws IOException {
final File imagesDir = new File(args.getBaseDir(), MfParams.Dirs.IMAGES.getPath());
imagesDir.mkdir();
try {
final String[] images = Resources.getResourceListing("cdc/mf/html/images");
for (final String image : images) {
Resources.copy("cdc/mf/html/images/" + image, imagesDir);
}
} catch (final Exception e) {
throw new IOException(e);
}
Resources.copy("cdc/mf/html/cdc-mf.css", args.getBaseDir());
}
private void generateSingleDocument(STGroup stg) throws IOException {
final ST st = stg.getInstanceOf("mf-frames/generateSingleDocument");
st.add(MfParams.Names.ARGS, args);
save(st, MfParams.Files.getHtmlSinglePageFile());
}
/**
* Generates {@link MfParams.Files#HTML_FRAMES} file.
*
* @param stg The {@link STGroup}.
* @param tasks The list of tasks.
*/
private void addMainFrameTask(STGroup stg,
List tasks) {
final ST st = stg.getInstanceOf("mf-frames/generateMainFrame");
st.add(MfParams.Names.ARGS, args);
tasks.add(new STWorker(st, MfParams.Files.getHtmlFramesFile()));
}
private void addIssuesFrameTask(STGroup stg,
List tasks) {
if (!args.getIssues().isEmpty()) {
final ST st = stg.getInstanceOf("mf-frames/generateIssuesFrame");
st.add(MfParams.Names.ARGS, args);
tasks.add(new STWorker(st, MfParams.Files.getHtmlIssuesFile()));
}
}
private void addProfileFrameTask(STGroup stg,
List tasks) {
if (args.getProfile() != null) {
final ST st = stg.getInstanceOf("mf-frames/generateProfileFrame");
st.add(MfParams.Names.ARGS, args);
tasks.add(new STWorker(st, MfParams.Files.getHtmlProfileFile()));
}
}
/**
* Generates {@link MfParams.Files#HTML_ALL_PACKAGES} file.
*
* @param stg The {@link STGroup}.
* @param tasks The list of tasks.
*/
private void addAllPackagesFrameTask(STGroup stg,
List tasks) {
final ST st = stg.getInstanceOf("mf-frames/generateAllPackagesFrame");
st.add(MfParams.Names.ARGS, args);
tasks.add(new STWorker(st, MfParams.Files.getHtmlAllPackagesFile()));
}
/**
* Generates {@link MfParams.Files#HTML_ALL_ITEMS} file.
*
* @param stg The {@link STGroup}.
* @param tasks The list of tasks.
*/
private void addAllItemsFrameTask(STGroup stg,
List tasks) {
final ST st = stg.getInstanceOf("mf-frames/generateAllItemsFrame");
final List interfaces = args.getModel().collect(MfInterface.class);
interfaces.sort(MfNameItem.NAME_COMPARATOR);
final List classes = args.getModel().collect(MfClass.class);
classes.sort(MfNameItem.NAME_COMPARATOR);
final List enumerations = args.getModel().collect(MfEnumeration.class);
enumerations.sort(MfNameItem.NAME_COMPARATOR);
st.add(MfParams.Names.ARGS, args);
st.add(MfParams.Names.INTERFACES, interfaces);
st.add(MfParams.Names.CLASSES, classes);
st.add(MfParams.Names.ENUMERATIONS, enumerations);
tasks.add(new STWorker(st, MfParams.Files.getHtmlAllItemsFile()));
}
private void addPackageItemsFramesTasks(STGroup stg,
List tasks) {
final List packages = args.getModel().collect(MfPackage.class);
packages.sort(MfQNameItem.QNAME_COMPARATOR);
for (final MfPackage pack : packages) {
final ST st = stg.getInstanceOf("mf-frames/generatePackageItemsFrame");
st.add(MfParams.Names.ARGS, args);
st.add(MfParams.Names.PACKAGE, pack);
tasks.add(new STWorker(st, MfParams.Files.getHtmlPackageItemsFile(pack, args)));
}
}
private void addModelOverviewFrameTask(STGroup stg,
List tasks) {
final ST st = stg.getInstanceOf("mf-frames/generateModelOverviewFrame");
st.add(MfParams.Names.ARGS, args);
tasks.add(new STWorker(st, MfParams.Files.getHtmlOverviewFile()));
}
private void addPackageOverviewFramesTasks(STGroup stg,
List tasks) {
final List packages = args.getModel().collect(MfPackage.class);
packages.sort(MfQNameItem.QNAME_COMPARATOR);
for (final MfPackage pack : packages) {
final ST st = stg.getInstanceOf("mf-frames/generatePackageOverviewFrame");
st.add(MfParams.Names.ARGS, args);
st.add(MfParams.Names.PACKAGE, pack);
tasks.add(new STWorker(st, MfParams.Files.getHtmlPackageOverviewFile(pack, args)));
}
}
private void addTypeOverviewFramesTasks(STGroup stg,
List tasks) {
final List types = args.getModel().collect(MfType.class);
types.sort(MfNameItem.NAME_COMPARATOR);
for (final MfType type : types) {
final ST st = stg.getInstanceOf("mf-frames/generateTypeOverviewFrame");
st.add(MfParams.Names.ARGS, args);
st.add(MfParams.Names.TYPE, type);
tasks.add(new STWorker(st, MfParams.Files.getHtmlItemFile(type, args)));
}
}
private void addIndexFramesTasks(STGroup stg,
List tasks) {
for (final char key : args.getIndex().getKeys()) {
final ST st = stg.getInstanceOf("mf-frames/generateIndexFrame");
st.add(MfParams.Names.ARGS, args);
st.add(MfParams.Names.SECTION, args.getIndex().getSection(key));
tasks.add(new STWorker(st, MfParams.Files.getHtmlIndexFile(key, args)));
}
}
}