Search in sources :

Example 1 with View

use of com.structurizr.view.View in project kroki by yuzutech.

the class Structurizr method convert.

static byte[] convert(String source, FileFormat fileFormat, StructurizrPlantUMLExporter structurizrPlantUMLExporter, JsonObject options) {
    StructurizrDslParser parser = new StructurizrDslParser();
    try {
        parser.parse(source);
        Collection<View> views = parser.getWorkspace().getViews().getViews();
        if (views.isEmpty()) {
            throw new BadRequestException("Empty diagram, does not have any view.");
        }
        View selectedView;
        String viewKey = options.getString("view-key");
        if (viewKey != null && !viewKey.trim().isEmpty()) {
            Optional<View> viewFound = views.stream().filter(view -> Objects.equals(view.getKey(), viewKey)).findFirst();
            if (!viewFound.isPresent()) {
                throw new BadRequestException("Unable to find view for key: " + viewKey + ".");
            }
            selectedView = viewFound.get();
        } else {
            // take the first view if not specified
            selectedView = views.iterator().next();
        }
        final Diagram diagram;
        if (selectedView instanceof DynamicView) {
            diagram = structurizrPlantUMLExporter.export((DynamicView) selectedView);
        } else if (selectedView instanceof DeploymentView) {
            diagram = structurizrPlantUMLExporter.export((DeploymentView) selectedView);
        } else if (selectedView instanceof ComponentView) {
            diagram = structurizrPlantUMLExporter.export((ComponentView) selectedView);
        } else if (selectedView instanceof ContainerView) {
            diagram = structurizrPlantUMLExporter.export((ContainerView) selectedView);
        } else if (selectedView instanceof SystemContextView) {
            diagram = structurizrPlantUMLExporter.export((SystemContextView) selectedView);
        } else if (selectedView instanceof SystemLandscapeView) {
            diagram = structurizrPlantUMLExporter.export((SystemLandscapeView) selectedView);
        } else {
            throw new BadRequestException("View type is not supported: " + selectedView.getClass().getSimpleName() + ", must be a DynamicView, DeploymentView, ComponentView, ContainerView, SystemContextView or SystemLandscapeView.");
        }
        return Plantuml.convert(diagram.getDefinition(), fileFormat, new JsonObject());
    } catch (StructurizrDslParserException e) {
        String cause = e.getMessage();
        final String message;
        if (cause != null && !cause.trim().isEmpty()) {
            message = "Unable to parse the Structurizr DSL. " + cause + ".";
        } else {
            message = "Unable to parse the Structurizr DSL.";
        }
        throw new BadRequestException(message, e);
    }
}
Also used : Arrays(java.util.Arrays) DecodeException(io.kroki.server.error.DecodeException) DiagramSource(io.kroki.server.decode.DiagramSource) StructurizrDslParserException(com.structurizr.dsl.StructurizrDslParserException) View(com.structurizr.view.View) SystemLandscapeView(com.structurizr.view.SystemLandscapeView) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) DeploymentView(com.structurizr.view.DeploymentView) Diagram(com.structurizr.export.Diagram) BadRequestException(io.kroki.server.error.BadRequestException) ComponentView(com.structurizr.view.ComponentView) ContainerView(com.structurizr.view.ContainerView) DynamicView(com.structurizr.view.DynamicView) Collection(java.util.Collection) StructurizrPlantUMLExporter(com.structurizr.export.plantuml.StructurizrPlantUMLExporter) Vertx(io.vertx.core.Vertx) FileFormat(io.kroki.server.format.FileFormat) SourceDecoder(io.kroki.server.decode.SourceDecoder) Objects(java.util.Objects) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) StructurizrDslParser(com.structurizr.dsl.StructurizrDslParser) SystemContextView(com.structurizr.view.SystemContextView) Optional(java.util.Optional) Handler(io.vertx.core.Handler) StructurizrDslParser(com.structurizr.dsl.StructurizrDslParser) JsonObject(io.vertx.core.json.JsonObject) StructurizrDslParserException(com.structurizr.dsl.StructurizrDslParserException) View(com.structurizr.view.View) SystemLandscapeView(com.structurizr.view.SystemLandscapeView) DeploymentView(com.structurizr.view.DeploymentView) ComponentView(com.structurizr.view.ComponentView) ContainerView(com.structurizr.view.ContainerView) DynamicView(com.structurizr.view.DynamicView) SystemContextView(com.structurizr.view.SystemContextView) Diagram(com.structurizr.export.Diagram) ComponentView(com.structurizr.view.ComponentView) SystemContextView(com.structurizr.view.SystemContextView) BadRequestException(io.kroki.server.error.BadRequestException) SystemLandscapeView(com.structurizr.view.SystemLandscapeView) DynamicView(com.structurizr.view.DynamicView) ContainerView(com.structurizr.view.ContainerView) DeploymentView(com.structurizr.view.DeploymentView)

Example 2 with View

use of com.structurizr.view.View in project moduliths by moduliths.

the class Documenter method addComponentsToView.

private void addComponentsToView(Supplier<Stream<Module>> modules, ComponentView view, Options options, Consumer<ComponentView> afterCleanup) {
    Styles styles = view.getViewSet().getConfiguration().getStyles();
    Map<Module, Component> components = getComponents(options);
    // 
    modules.get().distinct().filter(// 
    options.getExclusions().negate()).map(// 
    it -> applyBackgroundColor(it, components, options, styles)).filter(// 
    options.getComponentFilter()).forEach(view::add);
    // view.getViewSet().getConfiguration().getStyles().findElementStyle(element).getBackground()
    // Remove filtered dependency types
    // 
    DependencyType.allBut(options.getDependencyTypes()).map(// 
    Object::toString).forEach(it -> view.removeRelationshipsWithTag(it));
    afterCleanup.accept(view);
    // Filter outgoing relationships of target-only modules
    // 
    modules.get().filter(options.getTargetOnly()).forEach(module -> {
        Component component = components.get(module);
        // 
        view.getRelationships().stream().map(// 
        RelationshipView::getRelationship).filter(// 
        it -> it.getSource().equals(component)).forEach(it -> view.remove(it));
    });
    // … as well as all elements left without a relationship
    if (options.hideElementsWithoutRelationships()) {
        view.removeElementsWithNoRelationships();
    }
    afterCleanup.accept(view);
    // Remove default relationships if more qualified ones exist
    // 
    view.getRelationships().stream().map(// 
    RelationshipView::getRelationship).collect(// 
    Collectors.groupingBy(Connection::of)).values().stream().forEach(it -> potentiallyRemoveDefaultRelationship(view, it));
}
Also used : RequiredArgsConstructor(lombok.RequiredArgsConstructor) Diagram(com.structurizr.io.Diagram) C4PlantUMLExporter(com.structurizr.io.plantuml.C4PlantUMLExporter) BasicPlantUMLWriter(com.structurizr.io.plantuml.BasicPlantUMLWriter) SpringBean(org.moduliths.model.SpringBean) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Path(java.nio.file.Path) With(lombok.With) Predicate(java.util.function.Predicate) SoftwareSystem(com.structurizr.model.SoftwareSystem) RelationshipView(com.structurizr.view.RelationshipView) Asciidoctor(org.moduliths.docs.Asciidoctor) Collectors(java.util.stream.Collectors) DependencyDepth(org.moduliths.model.Module.DependencyDepth) Shape(com.structurizr.view.Shape) Tags(com.structurizr.model.Tags) Stream(java.util.stream.Stream) Relationship(com.structurizr.model.Relationship) Writer(java.io.Writer) Entry(java.util.Map.Entry) Styles(com.structurizr.view.Styles) java.util(java.util) Getter(lombok.Getter) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Component(com.structurizr.model.Component) Value(lombok.Value) Modules(org.moduliths.model.Modules) AccessLevel(lombok.AccessLevel) BiConsumer(java.util.function.BiConsumer) View(com.structurizr.view.View) Nullable(org.springframework.lang.Nullable) Model(com.structurizr.model.Model) Container(com.structurizr.model.Container) ComponentView(com.structurizr.view.ComponentView) Files(java.nio.file.Files) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) MultiValueMap(org.springframework.util.MultiValueMap) IOException(java.io.IOException) File(java.io.File) Workspace(com.structurizr.Workspace) Consumer(java.util.function.Consumer) DependencyType(org.moduliths.model.Module.DependencyType) Module(org.moduliths.model.Module) Paths(java.nio.file.Paths) PlantUMLWriter(com.structurizr.io.plantuml.PlantUMLWriter) AllArgsConstructor(lombok.AllArgsConstructor) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Element(com.structurizr.model.Element) Assert(org.springframework.util.Assert) RelationshipView(com.structurizr.view.RelationshipView) Module(org.moduliths.model.Module) Component(com.structurizr.model.Component) Styles(com.structurizr.view.Styles)

Aggregations

ComponentView (com.structurizr.view.ComponentView)2 View (com.structurizr.view.View)2 Workspace (com.structurizr.Workspace)1 StructurizrDslParser (com.structurizr.dsl.StructurizrDslParser)1 StructurizrDslParserException (com.structurizr.dsl.StructurizrDslParserException)1 Diagram (com.structurizr.export.Diagram)1 StructurizrPlantUMLExporter (com.structurizr.export.plantuml.StructurizrPlantUMLExporter)1 Diagram (com.structurizr.io.Diagram)1 BasicPlantUMLWriter (com.structurizr.io.plantuml.BasicPlantUMLWriter)1 C4PlantUMLExporter (com.structurizr.io.plantuml.C4PlantUMLExporter)1 PlantUMLWriter (com.structurizr.io.plantuml.PlantUMLWriter)1 Component (com.structurizr.model.Component)1 Container (com.structurizr.model.Container)1 Element (com.structurizr.model.Element)1 Model (com.structurizr.model.Model)1 Relationship (com.structurizr.model.Relationship)1 SoftwareSystem (com.structurizr.model.SoftwareSystem)1 Tags (com.structurizr.model.Tags)1 ContainerView (com.structurizr.view.ContainerView)1 DeploymentView (com.structurizr.view.DeploymentView)1