Search code examples
javaplantuml

Parse UML class diagram with PlantUML Java API


I want to parse an existing PlantUML class diagram in order to implement my own DDL code generator. As a first step I'm trying to parse a *.puml file containing the class diagram from Java, by using the plantuml-mit library. The idea is, to be able to iterate over all the classes in the diagram.

How can I access the diagram elements from code? So far I've discovered this two classes, which both expect a UmlDatasource inctance in the constructor, but I'm unsure how to create these instances.

net.sourceforge.plantuml.classdiagram.ClassDiagram
net.sourceforge.plantuml.classdiagram.ClassDiagramFactory

Solution

  • I found the answer meanwhile, by reverse engineering the API's Java code.

    String source = "@startuml\n" +
                PUT YOUR CLASS DIAGRAM HERE
                "@enduml";
    
    SourceStringReader r = new SourceStringReader(source);
    FileOutputStream file = new FileOutputStream("testGroovy2.png");
    ClassDiagram cd = (ClassDiagram) r.getBlocks().get(0).getDiagram();
    Collection<Quark<Entity>> classes = 
    cd.getEntityFactory().root().getChildren();
    classes.stream().forEach(c-> System.out.println(c));