Skip to content

Commit

Permalink
Save element name in extension element if it has any new lines (#3946)
Browse files Browse the repository at this point in the history
  • Loading branch information
BertMatthys authored Aug 19, 2024
1 parent 518ec3d commit 234f9d2
Show file tree
Hide file tree
Showing 18 changed files with 958 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface BpmnXMLConstants {

public static final String ATTRIBUTE_ID = "id";
public static final String ATTRIBUTE_NAME = "name";
public static final String ATTRIBUTE_ELEMENT_NAME = "element-name";
public static final String ATTRIBUTE_TYPE = "type";
public static final String ATTRIBUTE_EXPORTER = "exporter";
public static final String ATTRIBUTE_EXPORTER_VERSION = "exporterVersion";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public void convertToBpmnModel(XMLStreamReader xtr, BpmnModel model, Process act

FlowElement currentFlowElement = (FlowElement) parsedElement;
currentFlowElement.setId(elementId);
currentFlowElement.setName(elementName);
if (currentFlowElement.getName() == null) {
currentFlowElement.setName(elementName);
}

if (currentFlowElement instanceof FlowNode) {
FlowNode flowNode = (FlowNode) currentFlowElement;
Expand Down Expand Up @@ -167,7 +169,10 @@ public void convertToXML(XMLStreamWriter xtw, BaseElement baseElement, BpmnModel
boolean didWriteExtensionStartElement = false;
writeDefaultAttribute(ATTRIBUTE_ID, baseElement.getId(), xtw);
if (baseElement instanceof FlowElement) {
writeDefaultAttribute(ATTRIBUTE_NAME, ((FlowElement) baseElement).getName(), xtw);
String name = ((FlowElement) baseElement).getName();
if (!BpmnXMLUtil.containsNewLine(name)) {
writeDefaultAttribute(ATTRIBUTE_NAME, name, xtw);
}
}

if (baseElement instanceof FlowNode) {
Expand Down Expand Up @@ -220,6 +225,8 @@ public void convertToXML(XMLStreamWriter xtw, BaseElement baseElement, BpmnModel
xtw.writeCharacters(flowElement.getDocumentation());
xtw.writeEndElement();
}

didWriteExtensionStartElement = BpmnXMLUtil.writeElementNameExtensionElement(flowElement, didWriteExtensionStartElement, xtw);
}

didWriteExtensionStartElement = writeExtensionChildElements(baseElement, didWriteExtensionStartElement, xtw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ protected void createXML(FlowElement flowElement, BpmnModel model, XMLStreamWrit

boolean didWriteExtensionStartElement = FlowableListenerExport.writeListeners(subProcess, false, xtw);

didWriteExtensionStartElement = BpmnXMLUtil.writeElementNameExtensionElement(subProcess, didWriteExtensionStartElement, xtw);

didWriteExtensionStartElement = BpmnXMLUtil.writeExtensionElements(subProcess, didWriteExtensionStartElement, model.getNamespaces(), xtw);
if (didWriteExtensionStartElement) {
// closing extensions element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ protected BaseElement convertXMLToElement(XMLStreamReader xtr, BpmnModel model)
BpmnXMLUtil.addXMLLocation(sequenceFlow, xtr);
sequenceFlow.setSourceRef(xtr.getAttributeValue(null, ATTRIBUTE_FLOW_SOURCE_REF));
sequenceFlow.setTargetRef(xtr.getAttributeValue(null, ATTRIBUTE_FLOW_TARGET_REF));
sequenceFlow.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
sequenceFlow.setSkipExpression(xtr.getAttributeValue(null, ATTRIBUTE_FLOW_SKIP_EXPRESSION));

parseChildElements(getXMLElementName(), sequenceFlow, model, xtr);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flowable.bpmn.converter.child;

import javax.xml.stream.XMLStreamReader;

import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;

public class ElementNameParser extends BaseChildElementParser {

@Override
public String getElementName() {
return ATTRIBUTE_ELEMENT_NAME;
}

@Override
public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model) throws Exception {
String elementName = xtr.getElementText();
if (StringUtils.isNotEmpty(elementName)) {
if (parentElement instanceof FlowElement) {
((FlowElement) parentElement).setName(elementName.trim());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import javax.xml.stream.XMLStreamReader;

import org.flowable.bpmn.constants.BpmnXMLConstants;
import org.flowable.bpmn.converter.child.ElementNameParser;
import org.flowable.bpmn.converter.child.ExecutionListenerParser;
import org.flowable.bpmn.converter.child.FlowableEventListenerParser;
import org.flowable.bpmn.converter.util.BpmnXMLUtil;
Expand Down Expand Up @@ -50,6 +51,8 @@ public void parse(XMLStreamReader xtr, List<SubProcess> activeSubProcessList, Pr
new FlowableEventListenerParser().parseChildElement(xtr, parentElement, model);
} else if (ELEMENT_POTENTIAL_STARTER.equals(xtr.getLocalName())) {
new PotentialStarterParser().parse(xtr, activeProcess);
} else if (ATTRIBUTE_ELEMENT_NAME.equals(xtr.getLocalName())) {
new ElementNameParser().parseChildElement(xtr, parentElement, model);
} else {
ExtensionElement extensionElement = BpmnXMLUtil.parseExtensionElement(xtr);
parentElement.addExtensionElement(extensionElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.flowable.bpmn.converter.child.DataOutputAssociationParser;
import org.flowable.bpmn.converter.child.DataStateParser;
import org.flowable.bpmn.converter.child.DocumentationParser;
import org.flowable.bpmn.converter.child.ElementNameParser;
import org.flowable.bpmn.converter.child.ErrorEventDefinitionParser;
import org.flowable.bpmn.converter.child.EscalationEventDefinitionParser;
import org.flowable.bpmn.converter.child.ExecutionListenerParser;
Expand Down Expand Up @@ -65,6 +66,7 @@
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.bpmn.model.ExtensionElement;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.GraphicInfo;
import org.flowable.bpmn.model.IOParameter;

Expand Down Expand Up @@ -104,6 +106,7 @@ public class BpmnXMLUtil implements BpmnXMLConstants {
addGenericParser(new FlowNodeRefParser());
addGenericParser(new FlowableFailedjobRetryParser());
addGenericParser(new FlowableMapExceptionParser());
addGenericParser(new ElementNameParser());
}

private static void addGenericParser(BaseChildElementParser parser) {
Expand Down Expand Up @@ -408,6 +411,22 @@ protected static void writeExtensionElement(ExtensionElement extensionElement, M
xtw.writeEndElement();
}
}

public static boolean writeElementNameExtensionElement(FlowElement element, boolean didWriteExtensionStartElement, XMLStreamWriter xtw) throws Exception {
String name = element.getName();
if (BpmnXMLUtil.containsNewLine(name)) {
if (!didWriteExtensionStartElement) {
xtw.writeStartElement(ELEMENT_EXTENSIONS);
didWriteExtensionStartElement = true;
}

xtw.writeStartElement(FLOWABLE_EXTENSIONS_PREFIX, ATTRIBUTE_ELEMENT_NAME, FLOWABLE_EXTENSIONS_NAMESPACE);
xtw.writeCharacters(element.getName());
xtw.writeEndElement();
}

return didWriteExtensionStartElement;
}

public static boolean writeIOParameters(String elementName, List<IOParameter> parameterList, boolean didWriteExtensionStartElement, XMLStreamWriter xtw) throws Exception {

Expand Down Expand Up @@ -614,4 +633,8 @@ public static void parseLabelElement(XMLStreamReader xtr, BpmnModel model, Strin
}
}
}

public static boolean containsNewLine(String str) {
return str != null && str.contains("\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flowable.editor.language.xml;

import static org.assertj.core.api.Assertions.assertThat;
import static org.flowable.bpmn.constants.BpmnXMLConstants.ATTRIBUTE_ELEMENT_NAME;

import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.ExtensionElement;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.SubProcess;
import org.flowable.editor.language.xml.util.BpmnXmlConverterTest;

class NameWithNewLineTest {

@BpmnXmlConverterTest("nameWithNewLineTestProcess.bpmn")
void validateModel(BpmnModel model) {
FlowElement flowElement = model.getMainProcess().getFlowElement("startnoneevent1");
assertThat(flowElement.getName()).isEqualTo("start\nevent");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = model.getMainProcess().getFlowElement("bpmnCatchEvent_12");
assertThat(flowElement.getName()).isEqualTo("intermediate\nevent");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = model.getMainProcess().getFlowElement("bpmnGateway_14");
assertThat(flowElement.getName()).isEqualTo("gate\nway");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = model.getMainProcess().getFlowElement("bpmnEndEvent_3");
assertThat(flowElement.getName()).isEqualTo("end\nevent");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

SubProcess subProcess = (SubProcess) model.getMainProcess().getFlowElement("bpmnStructure_1");
assertThat(subProcess.getName()).isEqualTo("sub\nprocess");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = subProcess.getFlowElement("bpmnTask_5");
assertThat(flowElement.getName()).isEqualTo("user\ntask");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = subProcess.getFlowElement("bpmnBoundaryEvent_10");
assertThat(flowElement.getName()).isEqualTo("boundary\nevent");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();
}

@BpmnXmlConverterTest("nameWithoutNewLineTestProcess.bpmn")
void validateModelWithoutNewLines(BpmnModel model) {
FlowElement flowElement = model.getMainProcess().getFlowElement("startnoneevent1");
assertThat(flowElement.getName()).isEqualTo("startevent");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = model.getMainProcess().getFlowElement("bpmnCatchEvent_12");
assertThat(flowElement.getName()).isEqualTo("intermediateevent");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = model.getMainProcess().getFlowElement("bpmnGateway_14");
assertThat(flowElement.getName()).isEqualTo("gateway");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = model.getMainProcess().getFlowElement("bpmnEndEvent_3");
assertThat(flowElement.getName()).isEqualTo("endevent");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

SubProcess subProcess = (SubProcess) model.getMainProcess().getFlowElement("bpmnStructure_1");
assertThat(subProcess.getName()).isEqualTo("subprocess");
assertThat(subProcess.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = subProcess.getFlowElement("bpmnTask_5");
assertThat(flowElement.getName()).isEqualTo("usertask");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();

flowElement = subProcess.getFlowElement("bpmnBoundaryEvent_10");
assertThat(flowElement.getName()).isEqualTo("boundaryevent");
assertThat(flowElement.getExtensionElements().get(ATTRIBUTE_ELEMENT_NAME)).isNull();
}
}
Loading

0 comments on commit 234f9d2

Please sign in to comment.