1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-04 09:30:48 +00:00
plantuml/src/net/sourceforge/plantuml/xmlsc/ScxmlStateDiagramStandard.java

165 lines
5.3 KiB
Java
Raw Normal View History

2016-04-22 20:36:08 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2016-04-22 20:36:08 +00:00
*
* Project Info: http://plantuml.com
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2016-04-22 20:36:08 +00:00
* 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.xmlsc;
import static java.nio.charset.StandardCharsets.UTF_8;
2016-04-22 20:36:08 +00:00
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
2020-05-30 15:20:23 +00:00
import org.w3c.dom.Document;
import org.w3c.dom.Element;
2022-09-01 17:40:58 +00:00
import net.sourceforge.plantuml.Guillemet;
2016-04-22 20:36:08 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
2022-09-01 17:40:58 +00:00
import net.sourceforge.plantuml.cucadiagram.EntityUtils;
2016-04-22 20:36:08 +00:00
import net.sourceforge.plantuml.cucadiagram.IEntity;
2022-09-01 17:40:58 +00:00
import net.sourceforge.plantuml.cucadiagram.IGroup;
import net.sourceforge.plantuml.cucadiagram.ILeaf;
2016-04-22 20:36:08 +00:00
import net.sourceforge.plantuml.cucadiagram.LeafType;
import net.sourceforge.plantuml.cucadiagram.Link;
2022-09-01 17:40:58 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
2016-04-22 20:36:08 +00:00
import net.sourceforge.plantuml.statediagram.StateDiagram;
import net.sourceforge.plantuml.xml.XmlFactories;
2016-04-22 20:36:08 +00:00
public class ScxmlStateDiagramStandard {
private final StateDiagram diagram;
private final Document document;
public ScxmlStateDiagramStandard(StateDiagram diagram) throws ParserConfigurationException {
this.diagram = diagram;
final DocumentBuilder builder = XmlFactories.newDocumentBuilder();
2016-04-22 20:36:08 +00:00
this.document = builder.newDocument();
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
final Element scxml = document.createElement("scxml");
scxml.setAttribute("xmlns", "http://www.w3.org/2005/07/scxml");
scxml.setAttribute("version", "1.0");
final String initial = getInitial();
2022-09-01 17:40:58 +00:00
if (initial != null)
2016-04-22 20:36:08 +00:00
scxml.setAttribute("initial", initial);
2022-09-01 17:40:58 +00:00
2016-04-22 20:36:08 +00:00
document.appendChild(scxml);
2022-09-01 17:40:58 +00:00
for (final IEntity ent : diagram.getLeafsvalues())
if (EntityUtils.groupRoot(ent.getParentContainer()))
scxml.appendChild(createState(ent));
for (IGroup ent : diagram.getGroups(false))
if (EntityUtils.groupRoot(ent.getParentContainer()))
exportGroup(scxml, ent);
2016-04-22 20:36:08 +00:00
}
2022-09-01 17:40:58 +00:00
private Element exportGroup(Element dest, IGroup ent) {
final Element gr = createGroup(ent);
dest.appendChild(gr);
for (ILeaf leaf : ent.getLeafsDirect())
gr.appendChild(createState(leaf));
for (IGroup child : ent.getChildren())
exportGroup(gr, child);
return gr;
}
2016-04-22 20:36:08 +00:00
private String getInitial() {
2022-09-01 17:40:58 +00:00
for (final IEntity ent : diagram.getLeafsvalues())
if (ent.getLeafType() == LeafType.CIRCLE_START)
2016-04-22 20:36:08 +00:00
return getId(ent);
2022-09-01 17:40:58 +00:00
2016-04-22 20:36:08 +00:00
return null;
}
2022-09-01 17:40:58 +00:00
private Element createGroup(IEntity entity) {
return createState(entity);
}
2016-04-22 20:36:08 +00:00
private Element createState(IEntity entity) {
final Element state = document.createElement("state");
state.setAttribute("id", getId(entity));
2022-09-01 17:40:58 +00:00
final Stereotype stereotype = entity.getStereotype();
if (stereotype != null) {
state.setAttribute("stereotype", stereotype.getLabels(Guillemet.NONE).get(0));
2016-04-22 20:36:08 +00:00
}
2022-09-01 17:40:58 +00:00
for (final Link link : diagram.getLinks())
if (link.getEntity1() == entity)
addLink(state, link);
2016-04-22 20:36:08 +00:00
return state;
}
private void addLink(Element state, Link link) {
final Element transition = document.createElement("transition");
final Display label = link.getLabel();
if (Display.isNull(label) == false) {
final String event = label.get(0).toString();
transition.setAttribute("event", event);
}
transition.setAttribute("target", getId(link.getEntity2()));
state.appendChild(transition);
}
private String getId(IEntity entity) {
String result = entity.getDisplay().get(0).toString();
result = result.replaceAll("\\*", "");
return result;
}
public void transformerXml(OutputStream os) throws TransformerException, ParserConfigurationException {
final Source source = new DOMSource(document);
final Result resultat = new StreamResult(os);
final Transformer transformer = XmlFactories.newTransformer();
2016-04-22 20:36:08 +00:00
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name());
2016-04-22 20:36:08 +00:00
transformer.transform(source, resultat);
}
}