Skip to content

Commit

Permalink
Merge pull request #22 from basil/cleanup
Browse files Browse the repository at this point in the history
Miscellaneous code cleanup
  • Loading branch information
basil committed Jan 3, 2023
2 parents b1ee32d + 60c6823 commit 2fe99fd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
Expand Up @@ -21,7 +21,6 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.AbstractMojoExecutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -57,7 +56,6 @@
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -111,7 +109,8 @@ public class JellydocMojo extends AbstractMojo implements MavenReport {

private File outputDirectory;

public void execute() throws MojoExecutionException, MojoFailureException {
@Override
public void execute() throws MojoExecutionException {
Project p = new Project();

DefaultLogger logger = new DefaultLogger();
Expand All @@ -130,7 +129,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
fs.setDir(new File(dir.toString()));
javadoc.addFileset(fs);
}
javadoc.setClasspath(makePath(p,(Collection<Artifact>)project.getArtifacts()));
javadoc.setClasspath(makePath(p,project.getArtifacts()));

Javadoc.DocletInfo d = javadoc.createDoclet();
d.setProject(p);
Expand Down Expand Up @@ -177,11 +176,7 @@ public void generateSchema() throws MojoExecutionException {

helper.attachArtifact(project,"xsd","taglib-"+prefix,schema);
}
} catch (TransformerException e) {
throw new MojoExecutionException("Failed to generate schema",e);
} catch (DocumentException e) {
throw new MojoExecutionException("Failed to generate schema",e);
} catch (FileNotFoundException e) {
} catch (TransformerException | FileNotFoundException | DocumentException e) {
throw new MojoExecutionException("Failed to generate schema",e);
}
}
Expand Down Expand Up @@ -210,50 +205,53 @@ private void setParam(Javadoc.DocletInfo d, String name, String value) {
// return src;
// }

@Override
public void generate(Sink sink, Locale locale) throws MavenReportException {
try {
execute();
new ReferenceRenderer(sink,new File(targetDir(),"taglib.xml").toURI().toURL()).render();
FileUtils.copyDirectory(targetDir(),new File(targetDir(),"site"),"taglib-*.xsd",null);
} catch (AbstractMojoExecutionException e) {
throw new MavenReportException("Failed to generate report",e);
} catch (MalformedURLException e) {
throw new MavenReportException("Failed to generate report",e);
} catch (DocumentException e) {
throw new MavenReportException("Failed to generate report",e);
} catch (IOException e) {
} catch (AbstractMojoExecutionException | DocumentException | IOException e) {
throw new MavenReportException("Failed to generate report",e);
}
}

@Override
public String getOutputName() {
return "jelly-taglib-ref";
}

@Override
public String getName(Locale locale) {
return "Jelly taglib reference";
}

@Override
public String getCategoryName() {
return CATEGORY_PROJECT_REPORTS;
}

@Override
public String getDescription(Locale locale) {
return "Jelly taglib reference";
}

@Override
public void setReportOutputDirectory(File outputDirectory) {
this.outputDirectory = outputDirectory;
}

@Override
public File getReportOutputDirectory() {
return this.outputDirectory;
}

@Override
public boolean isExternalReport() {
return false;
}

@Override
public boolean canGenerateReport() {
// TODO: check if the current project has any source files
return true;
Expand Down
Expand Up @@ -27,7 +27,6 @@
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
import java.io.StringWriter;
Expand All @@ -43,23 +42,21 @@
*/
public class ReferenceRenderer extends AbstractMavenReportRenderer {
private final Document taglibXml;
private static final Comparator<Element> SORT_BY_NAME = new Comparator<Element>() {
public int compare(Element o1, Element o2) {
return o1.attributeValue("name").compareTo(o2.attributeValue("name"));
}
};
private static final Comparator<Element> SORT_BY_NAME = Comparator.comparing(o -> o.attributeValue("name"));

public ReferenceRenderer(Sink sink, URL taglibXml) throws DocumentException {
super(sink);
this.taglibXml = new SAXReader().read(taglibXml);
}

@Override
public String getTitle() {
return "Jelly Taglib references";
}

@Override
protected void renderBody() {
List<Element> libraries = sortByName((List<Element>) taglibXml.getRootElement().elements("library"));
List<Element> libraries = sortByName(taglibXml.getRootElement().elements("library"));

paragraph("The following Jelly tag libraries are defined in this project.");

Expand Down Expand Up @@ -89,7 +86,7 @@ protected void renderBody() {
paragraphHtml("This tag library is <a href='taglib-"+prefix+".xsd'>also available as an XML Schema</a>");
renderSummaryTable(library,prefix);

for( Element tag : sortByName((List<Element>)library.elements("tag")))
for( Element tag : sortByName(library.elements("tag")))
renderTagReference(prefix,tag);

endSection();
Expand All @@ -106,7 +103,7 @@ private void renderSummaryTable(Element library, String prefix) {
startTable();
tableHeader(new String[]{"Tag Name","Description"});

List<Element> tags = sortByName((List<Element>) library.elements("tag"));
List<Element> tags = sortByName(library.elements("tag"));

for( Element tag : tags) {
sink.tableRow();
Expand All @@ -121,8 +118,8 @@ private void renderSummaryTable(Element library, String prefix) {
}

private List<Element> sortByName(List<Element> list) {
List<Element> tags = new ArrayList<Element>(list);
Collections.sort(tags,SORT_BY_NAME);
List<Element> tags = new ArrayList<>(list);
tags.sort(SORT_BY_NAME);
return tags;
}

Expand All @@ -138,7 +135,7 @@ private void renderTagReference(String taglibPrefix,Element tag) {
if(hasVisibleAttributes(tag)) {
startTable();
tableHeader(new String[]{"Attribute Name","Type","Description"});
for( Element att : sortByName((List<Element>)tag.elements("attribute")))
for( Element att : sortByName(tag.elements("attribute")))
renderAttribute(att);
endTable();
}
Expand All @@ -157,7 +154,7 @@ private void renderTagReference(String taglibPrefix,Element tag) {
}

private boolean hasVisibleAttributes(Element tag) {
for( Element att : (List<Element>)tag.elements("attribute")) {
for( Element att : tag.elements("attribute")) {
String name = att.attributeValue("name");
if(!HIDDEN_ATTRIBUTES.contains(name))
return true;
Expand Down Expand Up @@ -227,5 +224,5 @@ public void lineBreak() {
return w.toString();
}

private static final Set<String> HIDDEN_ATTRIBUTES = new HashSet<String>(Arrays.asList("escapeText","trim"));
private static final Set<String> HIDDEN_ATTRIBUTES = new HashSet<>(Arrays.asList("escapeText","trim"));
}
Expand Up @@ -82,7 +82,7 @@ public TagXMLDoclet (RootDoc root) throws Exception
* Generates the xml for the tag libraries
*/
private void javadocXML(RootDoc root, Tags tw) throws SAXException {
Set<PackageDoc> pkgs = new HashSet<PackageDoc>();
Set<PackageDoc> pkgs = new HashSet<>();
for (ClassDoc c : root.specifiedClasses())
pkgs.add(c.containingPackage());
pkgs.addAll(Arrays.asList(root.specifiedPackages()));
Expand Down Expand Up @@ -263,7 +263,7 @@ private void docXML(Doc doc, Item w) throws SAXException {
String label = ((SeeTag) tag).label();
// if the label is null or empty, use the class#member part of the link
if (null == label || "".equals(label)) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
String className = ((SeeTag) tag).referencedClassName();
if ("".equals(className)) {
className = null;
Expand Down Expand Up @@ -306,18 +306,21 @@ protected void parseHTML(String text, final TypedXmlWriter d) throws SAXExceptio
);
parser.setContentHandler(
new DefaultHandler() {
private Stack<TypedXmlWriter> w = new Stack<TypedXmlWriter>();
private Stack<TypedXmlWriter> w = new Stack<>();
{ w.push(d); }
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if ( validDocElementName( localName ) ) {
w.push(w.peek()._element(localName,TypedXmlWriter.class));
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if ( validDocElementName( localName ) ) {
w.pop();
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
w.peek()._pcdata(new String(ch,start,length));
}
Expand Down Expand Up @@ -383,7 +386,7 @@ public static int optionLength(String option)
return 0;
}

public static boolean validOptions(String options[][],
public static boolean validOptions(String[][] options,
DocErrorReporter reporter)
{
boolean foundEncodingOption = false;
Expand Down

0 comments on commit 2fe99fd

Please sign in to comment.