Nasdanika Drawio module provides Java API for reading and manipulating Drawio diagrams. It is built on top of Graph.
To use the module add the below dependency to your pom.xml
:
<dependency>
<groupId>org.nasdanika.core</groupId>
<artifactId>drawio</artifactId>
<version>...</version>
</dependency>
See https://mvnrepository.com/artifact/org.nasdanika.core/drawio for the latest version.
The module requires Java 11 or above.
The module provides the following interfaces representing elements of a diagram file:
The below diagram shows relationships between the above interfaces including their super-interfaces:
org.nasdanika.drawio.Util provides utility methods such as layout()
and methods to navigate and query documents and their elements.
To create a new document use Document.create()
:
Document document = Document.create(false, null);
Page page = document.createPage();
page.setName("My first new page");
Model model = page.getModel();
Root root = model.getRoot();
List<Layer> layers = root.getLayers();
// Add layer
Layer newLayer = root.createLayer();
newLayer.setLabel("My new layer");
// Add nodes
Node source = newLayer.createNode();
source.setLabel("My source node");
Rectangle sourceGeometry = source.getGeometry();
sourceGeometry.setX(200);
sourceGeometry.setX(100);
sourceGeometry.setWidth(70);
sourceGeometry.setHeight(30);
source.getTags().add("aws");
Node target = newLayer.createNode();
target.setLabel("My target node");
target.getGeometry().setBounds(300, 150, 100, 30);
Set<String> targetTags = target.getTags();
targetTags.add("aws");
targetTags.add("azure");
// Add connection
Connection connection = newLayer.createConnection(source, target);
connection.setLabel("My connection");
Map<String, String> connectionStyle = connection.getStyle();
connectionStyle.put("edgeStyle", "orthogonalEdgeStyle");
connectionStyle.put("rounded", "1");
connectionStyle.put("orthogonalLoop", "1");
connectionStyle.put("jettySize", "auto");
connectionStyle.put("html", "1");
Files.writeString(new File("new-uncompressed.drawio").toPath(), document.save(null));
To load use one of load()
methods. The below snippet loads from a class loader resource.
Document document = Document.load(getClass().getResource("compressed.drawio"));
It is also possible to load a document from PNG metadata - this can be used with PNG files generated by Drawio editors with “Include copy of my diagram” checked.
List<Document> documents = Document.loadFromPngMetadata(getClass().getResource("illustration.png"));
Document content can be retrieved as a String using save()
method:
Files.writeString(new File("compressed.drawio").toPath(), document.save(null));
To embed a diagram into HTML use toHtml()
method:
Files.writeString(new File("compressed.html").toPath(), document.toHtml(null, "https://cdn.jsdelivr.net/gh/Nasdanika/drawio@dev/src/main/webapp/js/viewer-static.min.js"));
The API provide a simple layout algorithm which arranges nodes so that they don’t overlap. It can be used with generated diagrams to make them easier to layout manually after generation.
org.nasdanika.drawio.Util.layout(root, 20);
The API extends the page linking concept of Drawio to link an element in one document to a page in another document. This allows to create a tree/network of linked diagrams. For example, a complex system or process may be modeled in multiple diagrams maintained by multiple teams.
To link a page in another document (file):
data:page,<URL>
, where <URL>
is resolved relative to the URL of the current document. Example: data:page,compressed.drawio
.data:page/name,<URL>#<URL encoded page name>
. Example: data:page/name,compressed.drawio#Page+2
To traverse document elements you can use either accept(<visitor>)
methods or stream()
method.
org.nasdanika.drawio.emf.DrawioResource is a base class for mapping of diagram elements to EMF Ecore model elements. With DrawioResource Drawio files are treated as model resources which can be loaded into a resource set and as such reference model elements in other resources and be referenced from other resources. org.nasdanika.html.model.app.drawio.ResourceFactory is a concrete implementation for mapping diagram elements to application model actions.
org.nasdanika.drawio.emf.DrawioEObjectFactory is a specialization of org.nasdanika.graph.processor.emf.PropertySourceEObjectFactory for Drawio diagrams. It loads semantic information from properties of diagram elements as explained below. DrawioEObjectFactory
is abstract. It does not dictate semantic specification format - subclasses shall implement T load(String spec, URI specBase, ProcessorConfig<T> config, ProgressMonitor progressMonitor)
method.
By default semantic elements from linked pages are wired in the same way as element children. To disable wiring of linked page elements set this property to false
.
org.nasdanika.drawio.comparators package contains comparators for sorting diagram elements based on their label, properties, and geometry.
Graph documentation explains how to implement graph processors which can be used to make diagrams executable.
This section lists some possible applications of Drawio Java API and semantic mappings: