Skip to content

Commit

Permalink
Merge 89f470c into 29001ab
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoerdtalsma committed Jul 25, 2018
2 parents 29001ab + 89f470c commit 7189964
Show file tree
Hide file tree
Showing 35 changed files with 424 additions and 695 deletions.
56 changes: 40 additions & 16 deletions src/main/java/nl/talsmasoftware/umldoclet/UMLDoclet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import jdk.javadoc.doclet.Reporter;
import jdk.javadoc.doclet.StandardDoclet;
import net.sourceforge.plantuml.version.Version;
import nl.talsmasoftware.umldoclet.uml.Diagram;
import nl.talsmasoftware.umldoclet.html.HtmlPostprocessor;
import nl.talsmasoftware.umldoclet.javadoc.DocletConfig;
import nl.talsmasoftware.umldoclet.javadoc.UMLFactory;
import nl.talsmasoftware.umldoclet.uml.UMLDiagram;
import nl.talsmasoftware.umldoclet.uml.UMLRoot;

import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
Expand All @@ -37,8 +38,10 @@
import java.util.Set;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static nl.talsmasoftware.umldoclet.logging.Message.DOCLET_COPYRIGHT;
import static nl.talsmasoftware.umldoclet.logging.Message.DOCLET_VERSION;
import static nl.talsmasoftware.umldoclet.logging.Message.ERROR_UNANTICIPATED_ERROR_GENERATING_DIAGRAMS;
import static nl.talsmasoftware.umldoclet.logging.Message.ERROR_UNANTICIPATED_ERROR_GENERATING_UML;
import static nl.talsmasoftware.umldoclet.logging.Message.ERROR_UNANTICIPATED_ERROR_POSTPROCESSING_HTML;
import static nl.talsmasoftware.umldoclet.logging.Message.PLANTUML_COPYRIGHT;
Expand Down Expand Up @@ -82,34 +85,55 @@ public SourceVersion getSupportedSourceVersion() {

@Override
public boolean run(DocletEnvironment docEnv) {
return generateUMLDiagrams(docEnv)
&& super.run(docEnv)
&& postProcessHtml();
config.logger().info(DOCLET_COPYRIGHT, DOCLET_VERSION);
config.logger().info(PLANTUML_COPYRIGHT, Version.versionString());

// First generate Standard HTML documentation
if (!super.run(docEnv)) return false;

try {
Collection<Diagram> umlDiagrams = generatePlantUMLContent(docEnv)
.flatMap(this::generateDiagrams)
.collect(toList());

return postProcessHtml(umlDiagrams);
} catch (UMLDocletException docletException) {
docletException.logTo(config.logger());
return false;
}
}

private boolean generateUMLDiagrams(DocletEnvironment docEnv) {
private Stream<UMLRoot> generatePlantUMLContent(DocletEnvironment docEnv) {
try {
config.logger().info(DOCLET_COPYRIGHT, DOCLET_VERSION);
config.logger().info(PLANTUML_COPYRIGHT, Version.versionString());

UMLFactory factory = new UMLFactory(config, docEnv);
return streamIncludedElements(docEnv.getIncludedElements())
// return streamIncludedElements(docEnv.getIncludedElements())
return docEnv.getIncludedElements().stream()
.map(element -> mapToDiagram(factory, element))
.filter(Optional::isPresent).map(Optional::get)
.map(UMLDiagram::render)
.reduce(Boolean.TRUE, (a, b) -> a & b);
.peek(UMLRoot::render);

} catch (RuntimeException rte) {
config.logger().error(ERROR_UNANTICIPATED_ERROR_GENERATING_UML, rte);
rte.printStackTrace(System.err);
return false;
throw new UMLDocletException(ERROR_UNANTICIPATED_ERROR_GENERATING_UML, rte);
}
}

private Stream<Diagram> generateDiagrams(UMLRoot plantUMLRoot) {
try {

return config.images().formats().stream()
.map(format -> new Diagram(plantUMLRoot, format))
.peek(Diagram::render);

} catch (RuntimeException rte) {
throw new UMLDocletException(ERROR_UNANTICIPATED_ERROR_GENERATING_DIAGRAMS, rte);
}
}

private boolean postProcessHtml() {
private boolean postProcessHtml(Collection<Diagram> diagrams) {
try {

return new HtmlPostprocessor(config).postProcessHtml();
return new HtmlPostprocessor(config, diagrams).postProcessHtml();

} catch (IOException | RuntimeException ex) {
config.logger().error(ERROR_UNANTICIPATED_ERROR_POSTPROCESSING_HTML, ex);
Expand All @@ -118,7 +142,7 @@ private boolean postProcessHtml() {
}
}

private Optional<UMLDiagram> mapToDiagram(UMLFactory factory, Element element) {
private Optional<UMLRoot> mapToDiagram(UMLFactory factory, Element element) {
if (element instanceof PackageElement) {
return Optional.of(factory.createPackageDiagram((PackageElement) element));
} else if (element instanceof TypeElement && (element.getKind().isClass() || element.getKind().isInterface())) {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/nl/talsmasoftware/umldoclet/UMLDocletException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016-2018 Talsma ICT
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.talsmasoftware.umldoclet;

import nl.talsmasoftware.umldoclet.logging.Logger;
import nl.talsmasoftware.umldoclet.logging.Message;

import static java.util.Objects.requireNonNull;

/**
* Exception containing a predefined log message.
*
* @author Sjoerd Talsma
*/
public class UMLDocletException extends RuntimeException {

private final Message message;

public UMLDocletException(Message message, Throwable cause) {
super(requireNonNull(message, "Message is <null>").toString());
if (cause != null) super.initCause(cause);
this.message = message;
}

void logTo(Logger logger) {
logger.error(message, getCause());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public interface Configuration {
*/
String destinationDirectory();

/**
* @return Whether or not to render PlantUML {@code .puml} files.
*/
boolean renderPumlFile();

/**
* @return The configuration for the images that are generated.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package nl.talsmasoftware.umldoclet.configuration;

import net.sourceforge.plantuml.FileFormat;

import java.util.Collection;
import java.util.Optional;

Expand All @@ -38,6 +40,6 @@ public interface ImageConfig {
*
* @return The image formats that are generated (by default only {@code SVG}).
*/
Collection<String> formats();
Collection<FileFormat> formats();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package nl.talsmasoftware.umldoclet.html;

import net.sourceforge.plantuml.FileFormat;
import nl.talsmasoftware.umldoclet.configuration.Configuration;
import nl.talsmasoftware.umldoclet.util.FileUtils;

Expand Down Expand Up @@ -52,6 +53,7 @@ final class DiagramCollector extends SimpleFileVisitor<Path> {
DiagramCollector(Configuration config) {
this.basedir = new File(config.destinationDirectory());
this.diagramExtensions = unmodifiableList(config.images().formats().stream()
.map(FileFormat::getFileSuffix)
.map(String::toLowerCase)
.map(format -> format.startsWith(".") ? format : "." + format)
.collect(toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package nl.talsmasoftware.umldoclet.html;

import nl.talsmasoftware.umldoclet.configuration.Configuration;
import nl.talsmasoftware.umldoclet.uml.Diagram;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;

import static java.util.Objects.requireNonNull;
Expand All @@ -32,9 +32,11 @@
*/
public class HtmlPostprocessor {
private final Configuration config;
private final Collection<Diagram> diagrams;

public HtmlPostprocessor(Configuration config) {
public HtmlPostprocessor(Configuration config, Collection<Diagram> diagrams) {
this.config = requireNonNull(config, "Configuration is <null>.");
this.diagrams = requireNonNull(diagrams, "Generated diagram collection is <null>.");
}

public boolean postProcessHtml() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.Reporter;
import net.sourceforge.plantuml.FileFormat;
import nl.talsmasoftware.umldoclet.UMLDoclet;
import nl.talsmasoftware.umldoclet.logging.Logger;
import nl.talsmasoftware.umldoclet.rendering.indent.Indentation;
import nl.talsmasoftware.umldoclet.uml.Visibility;
import nl.talsmasoftware.umldoclet.configuration.Configuration;
import nl.talsmasoftware.umldoclet.configuration.FieldConfig;
import nl.talsmasoftware.umldoclet.configuration.ImageConfig;
import nl.talsmasoftware.umldoclet.configuration.MethodConfig;
import nl.talsmasoftware.umldoclet.configuration.TypeDisplay;
import nl.talsmasoftware.umldoclet.logging.Logger;
import nl.talsmasoftware.umldoclet.logging.Message;
import nl.talsmasoftware.umldoclet.rendering.indent.Indentation;
import nl.talsmasoftware.umldoclet.uml.Visibility;

import java.nio.charset.Charset;
import java.util.ArrayList;
Expand All @@ -40,7 +42,9 @@

import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
import static net.sourceforge.plantuml.FileFormat.SVG;

public class DocletConfig implements Configuration {

Expand All @@ -55,6 +59,13 @@ public class DocletConfig implements Configuration {
*/
String destDirName = "";

/**
* Whether or not to render PlantUML {@code .puml} files.
* <p>
* Set by option {@code -umlPlantumlFiles}, default is {@code false}.
*/
boolean renderPumlFile = false;

/**
* Whether the doclet should run more quite (errors must still be displayed).
* <p>
Expand Down Expand Up @@ -128,6 +139,11 @@ public String destinationDirectory() {
return destDirName;
}

@Override
public boolean renderPumlFile() {
return renderPumlFile;
}

@Override
public ImageConfig images() {
return images;
Expand Down Expand Up @@ -161,9 +177,9 @@ public Charset htmlCharset() {
: Charset.defaultCharset();
}

static final class ImageCfg implements ImageConfig {
final class ImageCfg implements ImageConfig {
String directory = null;
Collection<String> imageFormats = null;
Collection<FileFormat> imageFormats = null;

/**
* Directory where UML images are generated.
Expand All @@ -183,14 +199,28 @@ void addImageFormat(String imageFormat) {
.map(String::trim)
.map(s -> s.replaceFirst("^\\.", ""))
.map(String::toUpperCase)
.filter(s -> !s.isEmpty())
.filter(s -> !s.isEmpty() && !"NONE".equals(s))
.map(this::toFileFormat)
.filter(Optional::isPresent).map(Optional::get)
.forEach(imageFormats::add);
}
}

private Optional<FileFormat> toFileFormat(String format) {
String formatAsSuffix = "." + format.toLowerCase(ENGLISH);
Optional<FileFormat> fileFormat = Stream.concat(
Stream.of(FileFormat.values()).filter(ff -> format.equals(ff.name())),
Stream.of(FileFormat.values()).filter(ff -> formatAsSuffix.equals(ff.getFileSuffix())))
.findFirst();
if (!fileFormat.isPresent()) {
logger().warn(Message.WARNING_UNRECOGNIZED_IMAGE_FORMAT, format);
}
return fileFormat;
}

@Override
public Collection<String> formats() {
return Optional.ofNullable(imageFormats).orElseGet(() -> singleton("SVG"));
public Collection<FileFormat> formats() {
return Optional.ofNullable(imageFormats).orElseGet(() -> singleton(SVG));
}
}

Expand Down
Loading

0 comments on commit 7189964

Please sign in to comment.