Skip to content

Commit

Permalink
✨ Add possibility to create Graphviz instance using service factory (p…
Browse files Browse the repository at this point in the history
…lantuml#2013)

* Add possibility to use custom graphviz implementation (plantuml#2009)

* Add possibility to create Graphviz instance using service factory (plantuml#2012)

---------
Authored-by: PavelTurk <[email protected]>
  • Loading branch information
arnaudroques authored Dec 17, 2024
1 parent d7b8735 commit 0a22623
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
54 changes: 54 additions & 0 deletions src/net/sourceforge/plantuml/dot/GraphvizFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.dot;

import net.sourceforge.plantuml.style.ISkinParam;

/**
* @author Pavel Castornii
*/
public interface GraphvizFactory {

/**
* Creates a {@link Graphviz} instance if possible with the specified parameters.
*
* @param skinParam
* @param dotString
* @param type
* @return a {@link Graphviz} instance or {@code null}.
*/
Graphviz create(ISkinParam skinParam, String dotString, String... type);
}
33 changes: 27 additions & 6 deletions src/net/sourceforge/plantuml/dot/GraphvizUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
*
* If you like this project or if you find it useful, you can support us at:
*
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
*
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
Expand All @@ -30,15 +30,17 @@
*
*
* Original Author: Arnaud Roques
*
*
*
*/
package net.sourceforge.plantuml.dot;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -73,11 +75,14 @@ public static final void setDotExecutable(String value) {
}

public static Graphviz createForSystemDot(ISkinParam skinParam, String dotString, String... type) {
Graphviz result = createWithFactory(skinParam, dotString, type);
if (result != null) {
return result;
}
if (useVizJs(skinParam)) {
Log.info("Using " + VIZJS);
return new GraphvizJs(dotString);
}
final AbstractGraphviz result;
if (isWindows())
result = new GraphvizWindowsOld(skinParam, dotString, type);
else
Expand All @@ -92,11 +97,14 @@ public static Graphviz createForSystemDot(ISkinParam skinParam, String dotString
}

public static Graphviz create(ISkinParam skinParam, String dotString, String... type) {
Graphviz result = createWithFactory(skinParam, dotString, type);
if (result != null) {
return result;
}
if (useVizJs(skinParam)) {
Log.info("Using " + VIZJS);
return new GraphvizJs(dotString);
}
final AbstractGraphviz result;
if (isWindows())
result = new GraphvizWindowsLite(skinParam, dotString, type);
else
Expand All @@ -110,6 +118,19 @@ public static Graphviz create(ISkinParam skinParam, String dotString, String...
return result;
}

private static Graphviz createWithFactory(ISkinParam skinParam, String dotString, String... type) {
Iterator<GraphvizFactory> iterator = ServiceLoader.load(GraphvizFactory.class).iterator();
while (iterator.hasNext()) {
GraphvizFactory factory = iterator.next();
Graphviz graphviz = factory.create(skinParam, dotString, type);
if (graphviz != null) {
Log.info("Using " + graphviz.getClass().getName() + " created by " + factory.getClass().getName());
return graphviz;
}
}
return null;
}

private static boolean useVizJs(ISkinParam skinParam) {
if (skinParam != null && skinParam.isUseVizJs() && VizJsEngine.isOk())
return true;
Expand Down

0 comments on commit 0a22623

Please sign in to comment.