Skip to content

Commit

Permalink
Make element name extension element in case of new line optional (#3951)
Browse files Browse the repository at this point in the history
  • Loading branch information
BertMatthys committed Aug 26, 2024
1 parent 754287e commit d10e822
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ public void convertToBpmnModel(XMLStreamReader xtr, BpmnModel model, Process act
}
}

public void convertToXML(XMLStreamWriter xtw, BaseElement baseElement, BpmnModel model) throws Exception {
public void convertToXML(XMLStreamWriter xtw, BaseElement baseElement, BpmnModel model, BpmnXMLConverterOptions options) throws Exception {
xtw.writeStartElement(getXMLElementName());
boolean didWriteExtensionStartElement = false;
writeDefaultAttribute(ATTRIBUTE_ID, baseElement.getId(), xtw);
if (baseElement instanceof FlowElement) {
String name = ((FlowElement) baseElement).getName();
if (!BpmnXMLUtil.containsNewLine(name)) {
if (!(options.isSaveElementNameWithNewLineInExtensionElement() && BpmnXMLUtil.containsNewLine(name))) {
writeDefaultAttribute(ATTRIBUTE_NAME, name, xtw);
}
}
Expand Down Expand Up @@ -226,7 +226,9 @@ public void convertToXML(XMLStreamWriter xtw, BaseElement baseElement, BpmnModel
xtw.writeEndElement();
}

didWriteExtensionStartElement = BpmnXMLUtil.writeElementNameExtensionElement(flowElement, didWriteExtensionStartElement, xtw);
if (options.isSaveElementNameWithNewLineInExtensionElement()) {
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 @@ -116,6 +116,8 @@ public class BpmnXMLConverter implements BpmnXMLConstants {
protected List<String> userTaskFormTypes;
protected List<String> startEventFormTypes;

protected BpmnXMLConverterOptions options = new BpmnXMLConverterOptions();

protected BpmnEdgeParser bpmnEdgeParser = new BpmnEdgeParser();
protected BpmnShapeParser bpmnShapeParser = new BpmnShapeParser();
protected DefinitionsParser definitionsParser = new DefinitionsParser();
Expand Down Expand Up @@ -204,6 +206,14 @@ public static void addConverter(BaseBpmnXMLConverter converter, Class<? extends
convertersToXMLMap.put(elementType, converter);
}

public BpmnXMLConverterOptions getBpmnXmlConverterOptions() {
return options;
}

public void setBpmnXmlConverterOptions(BpmnXMLConverterOptions options) {
this.options = options;
}

public void setClassloader(ClassLoader classloader) {
this.classloader = classloader;
}
Expand Down Expand Up @@ -581,7 +591,9 @@ protected void createXML(FlowElement flowElement, BpmnModel model, XMLStreamWrit

xtw.writeAttribute(ATTRIBUTE_ID, subProcess.getId());
if (StringUtils.isNotEmpty(subProcess.getName())) {
xtw.writeAttribute(ATTRIBUTE_NAME, subProcess.getName());
if (!(options.isSaveElementNameWithNewLineInExtensionElement() && BpmnXMLUtil.containsNewLine(subProcess.getName()))) {
xtw.writeAttribute(ATTRIBUTE_NAME, subProcess.getName());
}
} else {
xtw.writeAttribute(ATTRIBUTE_NAME, "subProcess");
}
Expand Down Expand Up @@ -619,7 +631,9 @@ protected void createXML(FlowElement flowElement, BpmnModel model, XMLStreamWrit

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

didWriteExtensionStartElement = BpmnXMLUtil.writeElementNameExtensionElement(subProcess, didWriteExtensionStartElement, xtw);
if (options.isSaveElementNameWithNewLineInExtensionElement()) {
didWriteExtensionStartElement = BpmnXMLUtil.writeElementNameExtensionElement(subProcess, didWriteExtensionStartElement, xtw);
}

didWriteExtensionStartElement = BpmnXMLUtil.writeExtensionElements(subProcess, didWriteExtensionStartElement, model.getNamespaces(), xtw);
if (didWriteExtensionStartElement) {
Expand Down Expand Up @@ -656,7 +670,7 @@ protected void createXML(FlowElement flowElement, BpmnModel model, XMLStreamWrit
throw new XMLException("No converter for " + flowElement.getClass() + " found");
}

converter.convertToXML(xtw, flowElement, model);
converter.convertToXML(xtw, flowElement, model, options);
}
}

Expand All @@ -668,6 +682,6 @@ protected void createXML(Artifact artifact, BpmnModel model, XMLStreamWriter xtw
throw new XMLException("No converter for " + artifact.getClass() + " found");
}

converter.convertToXML(xtw, artifact, model);
converter.convertToXML(xtw, artifact, model, options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* 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;

public class BpmnXMLConverterOptions {

protected boolean saveElementNameWithNewLineInExtensionElement = false;

public boolean isSaveElementNameWithNewLineInExtensionElement() {
return saveElementNameWithNewLineInExtensionElement;
}

public void setSaveElementNameWithNewLineInExtensionElement(boolean saveElementNameWithNewLineInExtensionElement) {
this.saveElementNameWithNewLineInExtensionElement = saveElementNameWithNewLineInExtensionElement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ public static BpmnModel readXmlExportAndReadAgain(String resource) {
}

public static BpmnModel exportAndReadXMLFile(BpmnModel model) {
byte[] xml = new BpmnXMLConverter().convertToXML(model);
return new BpmnXMLConverter().convertToBpmnModel(new InputStreamSource(new ByteArrayInputStream(xml)), true, false, "UTF-8");
byte[] xml = getBpmnXMLConverter().convertToXML(model);
return getBpmnXMLConverter().convertToBpmnModel(new InputStreamSource(new ByteArrayInputStream(xml)), true, false, "UTF-8");
}

public static BpmnModel readXMLFile(String resource) {
return new BpmnXMLConverter().convertToBpmnModel(new ClasspathStreamResource(resource), true, false);
return getBpmnXMLConverter().convertToBpmnModel(new ClasspathStreamResource(resource), true, false);
}

private static BpmnXMLConverter getBpmnXMLConverter() {
BpmnXMLConverter converter = new BpmnXMLConverter();
converter.getBpmnXmlConverterOptions().setSaveElementNameWithNewLineInExtensionElement(true);
return converter;
}

protected static class ClasspathStreamResource implements InputStreamProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public class CmmnXmlConverter implements CmmnXmlConstants {

protected static Map<String, BaseCmmnXmlConverter> elementConverters = new HashMap<>();

protected CmmnXmlConverterOptions options = new CmmnXmlConverterOptions();

protected ClassLoader classloader;

static {
Expand Down Expand Up @@ -295,7 +297,7 @@ public byte[] convertToXML(CmmnModel model, String encoding) {

Stage planModel = caseModel.getPlanModel();

PlanItemDefinitionExport.writePlanItemDefinition(model, planModel, xtw);
PlanItemDefinitionExport.writePlanItemDefinition(model, planModel, xtw, options);

// end case element
xtw.writeEndElement();
Expand Down Expand Up @@ -719,6 +721,14 @@ protected void processAssociations(CmmnModel cmmnModel) {
}
}

public CmmnXmlConverterOptions getCmmnXmlConverterOptions() {
return options;
}

public void setCmmnXmlConverterOptions(CmmnXmlConverterOptions options) {
this.options = options;
}

public void setClassloader(ClassLoader classloader) {
this.classloader = classloader;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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.cmmn.converter;

public class CmmnXmlConverterOptions {

protected boolean saveElementNameWithNewLineInExtensionElement = false;

public boolean isSaveElementNameWithNewLineInExtensionElement() {
return saveElementNameWithNewLineInExtensionElement;
}

public void setSaveElementNameWithNewLineInExtensionElement(boolean saveElementNameWithNewLineInExtensionElement) {
this.saveElementNameWithNewLineInExtensionElement = saveElementNameWithNewLineInExtensionElement;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.apache.commons.lang3.StringUtils;
import org.flowable.cmmn.converter.CmmnXmlConstants;
import org.flowable.cmmn.converter.CmmnXmlConverterOptions;
import org.flowable.cmmn.converter.util.CmmnXmlUtil;
import org.flowable.cmmn.model.CmmnModel;
import org.flowable.cmmn.model.PlanItemDefinition;
Expand All @@ -35,20 +36,20 @@ public abstract class AbstractPlanItemDefinitionExport<T extends PlanItemDefinit
* @param xtw the XML to write the definition to
* @throws Exception in case of write exception
*/
public void writePlanItemDefinition(CmmnModel model, T planItemDefinition, XMLStreamWriter xtw) throws Exception {
public void writePlanItemDefinition(CmmnModel model, T planItemDefinition, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {

writePlanItemDefinitionStartElement(planItemDefinition, xtw);
writePlanItemDefinitionCommonAttributes(planItemDefinition, xtw);
writePlanItemDefinitionCommonAttributes(planItemDefinition, xtw, options);
writePlanItemDefinitionSpecificAttributes(planItemDefinition, xtw);

boolean didWriteExtensionElement = writePlanItemDefinitionCommonElements(model, planItemDefinition, xtw);
boolean didWriteExtensionElement = writePlanItemDefinitionCommonElements(model, planItemDefinition, xtw, options);
didWriteExtensionElement = writePlanItemDefinitionExtensionElements(model, planItemDefinition, didWriteExtensionElement, xtw);
if (didWriteExtensionElement) {
xtw.writeEndElement();
}

writePlanItemDefinitionDefaultItemControl(model, planItemDefinition, xtw);
writePlanItemDefinitionBody(model, planItemDefinition, xtw);
writePlanItemDefinitionBody(model, planItemDefinition, xtw, options);
writePlanItemDefinitionEndElement(xtw);
}

Expand All @@ -64,10 +65,11 @@ protected void writePlanItemDefinitionStartElement(T planItemDefinition, XMLStre
*/
protected abstract String getPlanItemDefinitionXmlElementValue(T planItemDefinition);

protected void writePlanItemDefinitionCommonAttributes(T planItemDefinition, XMLStreamWriter xtw) throws Exception {
protected void writePlanItemDefinitionCommonAttributes(T planItemDefinition, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {
xtw.writeAttribute(ATTRIBUTE_ID, planItemDefinition.getId());

if (StringUtils.isNotEmpty(planItemDefinition.getName()) && !CmmnXmlUtil.containsNewLine(planItemDefinition.getName())) {
if (StringUtils.isNotEmpty(planItemDefinition.getName()) &&
!(options.isSaveElementNameWithNewLineInExtensionElement() && CmmnXmlUtil.containsNewLine(planItemDefinition.getName()))) {
xtw.writeAttribute(ATTRIBUTE_NAME, planItemDefinition.getName());
}
}
Expand All @@ -92,14 +94,17 @@ protected void writePlanItemDefinitionSpecificAttributes(T planItemDefinition, X
* @param xtw the XML to write the definition to
* @throws Exception in case of write exception
*/
protected boolean writePlanItemDefinitionCommonElements(CmmnModel model, T planItemDefinition, XMLStreamWriter xtw) throws Exception {
protected boolean writePlanItemDefinitionCommonElements(CmmnModel model, T planItemDefinition, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {
if (StringUtils.isNotEmpty(planItemDefinition.getDocumentation())) {
xtw.writeStartElement(ELEMENT_DOCUMENTATION);
xtw.writeCharacters(planItemDefinition.getDocumentation());
xtw.writeEndElement();
}

boolean didWriteExtensionStartElement = CmmnXmlUtil.writeElementNameExtensionElement(planItemDefinition, false, xtw);
boolean didWriteExtensionStartElement = false;
if (options.isSaveElementNameWithNewLineInExtensionElement()) {
didWriteExtensionStartElement = CmmnXmlUtil.writeElementNameExtensionElement(planItemDefinition, didWriteExtensionStartElement, xtw);
}

return CmmnXmlUtil.writeExtensionElements(planItemDefinition, didWriteExtensionStartElement, model.getNamespaces(), xtw);
}
Expand All @@ -122,7 +127,7 @@ protected void writePlanItemDefinitionDefaultItemControl(CmmnModel model, T plan
* @param xtw the XML to write the definition to
* @throws Exception in case of write exception
*/
protected void writePlanItemDefinitionBody(CmmnModel model, T planItemDefinition, XMLStreamWriter xtw) throws Exception {
protected void writePlanItemDefinitionBody(CmmnModel model, T planItemDefinition, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.xml.stream.XMLStreamWriter;

import org.apache.commons.lang3.StringUtils;
import org.flowable.cmmn.converter.CmmnXmlConverterOptions;
import org.flowable.cmmn.model.CmmnModel;
import org.flowable.cmmn.model.HttpServiceTask;
import org.flowable.cmmn.model.ImplementationType;
Expand Down Expand Up @@ -93,8 +94,8 @@ protected boolean writePlanItemDefinitionExtensionElements(CmmnModel model, Serv
}

@Override
protected void writePlanItemDefinitionBody(CmmnModel model, ServiceTask serviceTask, XMLStreamWriter xtw) throws Exception {
super.writePlanItemDefinitionBody(model, serviceTask, xtw);
protected void writePlanItemDefinitionBody(CmmnModel model, ServiceTask serviceTask, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {
super.writePlanItemDefinitionBody(model, serviceTask, xtw, options);
}

public static class ServiceTaskExport extends AbstractServiceTaskExport<ServiceTask> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.xml.stream.XMLStreamWriter;

import org.apache.commons.lang3.StringUtils;
import org.flowable.cmmn.converter.CmmnXmlConverterOptions;
import org.flowable.cmmn.model.CaseTask;
import org.flowable.cmmn.model.CmmnModel;

Expand Down Expand Up @@ -46,8 +47,8 @@ protected void writePlanItemDefinitionSpecificAttributes(CaseTask caseTask, XMLS
}

@Override
protected void writePlanItemDefinitionBody(CmmnModel model, CaseTask caseTask, XMLStreamWriter xtw) throws Exception {
super.writePlanItemDefinitionBody(model, caseTask, xtw);
protected void writePlanItemDefinitionBody(CmmnModel model, CaseTask caseTask, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {
super.writePlanItemDefinitionBody(model, caseTask, xtw, options);

// Always export the case reference as an expression, even if the caseRef is set
if (StringUtils.isNotEmpty(caseTask.getCaseRef()) || StringUtils.isNotEmpty(caseTask.getCaseRefExpression())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.xml.stream.XMLStreamWriter;

import org.apache.commons.lang3.StringUtils;
import org.flowable.cmmn.converter.CmmnXmlConverterOptions;
import org.flowable.cmmn.model.CmmnModel;
import org.flowable.cmmn.model.DecisionTask;

Expand Down Expand Up @@ -43,8 +44,8 @@ protected boolean writePlanItemDefinitionExtensionElements(CmmnModel model, Deci
}

@Override
protected void writePlanItemDefinitionBody(CmmnModel model, DecisionTask decisionTask, XMLStreamWriter xtw) throws Exception {
super.writePlanItemDefinitionBody(model, decisionTask, xtw);
protected void writePlanItemDefinitionBody(CmmnModel model, DecisionTask decisionTask, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {
super.writePlanItemDefinitionBody(model, decisionTask, xtw, options);
if (StringUtils.isNotEmpty(decisionTask.getDecisionRef()) || StringUtils.isNotEmpty(decisionTask.getDecisionRefExpression())) {
xtw.writeStartElement(ELEMENT_DECISION_REF_EXPRESSION);
xtw.writeCData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import javax.xml.stream.XMLStreamWriter;

import org.flowable.cmmn.converter.CmmnXmlConverterOptions;
import org.flowable.cmmn.model.CmmnModel;
import org.flowable.cmmn.model.PlanFragment;
import org.flowable.cmmn.model.PlanItem;
Expand Down Expand Up @@ -49,8 +50,8 @@ protected void writePlanItemDefinitionSpecificAttributes(PlanFragment planFragme
}

@Override
protected void writePlanItemDefinitionBody(CmmnModel model, PlanFragment planFragment, XMLStreamWriter xtw) throws Exception {
super.writePlanItemDefinitionBody(model, planFragment, xtw);
protected void writePlanItemDefinitionBody(CmmnModel model, PlanFragment planFragment, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {
super.writePlanItemDefinitionBody(model, planFragment, xtw, options);
for (PlanItem planItem : planFragment.getPlanItems()) {
PlanItemExport.writePlanItem(model, planItem, xtw);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.xml.stream.XMLStreamWriter;

import org.flowable.cmmn.converter.CmmnXmlConstants;
import org.flowable.cmmn.converter.CmmnXmlConverterOptions;
import org.flowable.cmmn.model.CmmnModel;
import org.flowable.cmmn.model.PlanItemDefinition;
import org.flowable.common.engine.api.FlowableException;
Expand Down Expand Up @@ -51,12 +52,12 @@ public static void addPlanItemDefinitionExport(AbstractPlanItemDefinitionExport
planItemDefinitionExporters.put(exporter.getExportablePlanItemDefinitionClass().getCanonicalName(), exporter);
}

public static void writePlanItemDefinition(CmmnModel model, PlanItemDefinition planItemDefinition, XMLStreamWriter xtw) throws Exception {
public static void writePlanItemDefinition(CmmnModel model, PlanItemDefinition planItemDefinition, XMLStreamWriter xtw, CmmnXmlConverterOptions options) throws Exception {
AbstractPlanItemDefinitionExport exporter = determineExporter(planItemDefinition);
if (exporter == null) {
throw new FlowableException("Cannot find a PlanItemDefinitionExporter for '" + planItemDefinition.getClass().getCanonicalName() + "'");
}
exporter.writePlanItemDefinition(model, planItemDefinition, xtw);
exporter.writePlanItemDefinition(model, planItemDefinition, xtw, options);
}

protected static AbstractPlanItemDefinitionExport determineExporter(PlanItemDefinition planItemDefinition) {
Expand Down
Loading

0 comments on commit d10e822

Please sign in to comment.