Skip to content

Commit

Permalink
Remove empty namespace from process definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetter committed Dec 23, 2024
1 parent b9d52d9 commit e658100
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/repository/definition/process/processmodeldefinition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ProcessFile from "@repository/serverfile/processfile";
import XML from "@util/xml";
import ParameterDefinition from "../contract/parameterdefinition";
import ModelDefinition from "../modeldefinition";
import { CAFIENNE_NAMESPACE, IMPLEMENTATION_TAG } from "../xmlserializable";
import ProcessImplementationDefinition from "./processimplementationdefinition";

export default class ProcessModelDefinition extends ModelDefinition {
Expand Down Expand Up @@ -30,6 +32,19 @@ export default class ProcessModelDefinition extends ModelDefinition {
toXML() {
const xmlDocument = super.exportModel('process', 'input', 'output', 'implementation');
this.exportNode.setAttribute('implementationType', 'http://www.omg.org/spec/CMMN/ProcessType/Unspecified');

// Remove the empty namespace attribute from children of the implementation tag and put them in the namespace of the parent
const implementationExportElement = XML.getElement(xmlDocument, IMPLEMENTATION_TAG);
if (implementationExportElement) {
implementationExportElement.setAttribute('xmlns', CAFIENNE_NAMESPACE);
const children = XML.getChildrenByTagName(implementationExportElement, '*').filter(element => element.namespaceURI === null);
children.forEach(element => {
const clone: Element = <Element> XML.cloneWithoutNamespace(element, true, CAFIENNE_NAMESPACE);
element.parentNode?.insertBefore(clone, element);
element.parentNode?.removeChild(element);
clone.removeAttribute("xmlns");
})
}
return xmlDocument;
}
}
24 changes: 17 additions & 7 deletions src/util/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,22 @@ export default class XML {
* @returns {Element} The newly created element
*/
static createChildElement(parentNode, tagName, namespace = "http://www.omg.org/spec/CMMN/20151109/MODEL") {
if (tagName.indexOf('cafienne:') === 0) {
namespace = 'org.cafienne';
}
if (parentNode instanceof Document) {
if (tagName.indexOf('cafienne:') === 0) {
namespace = 'org.cafienne';
}
return parentNode.createElementNS(namespace, tagName);
} else {
return parentNode.appendChild(parentNode.ownerDocument.createElementNS(namespace, tagName));
if (tagName.indexOf('cafienne:') === 0) {
namespace = 'org.cafienne';
} else {
// console.log("Creating node '" + tagName+"' - asked to do within namespace " +namespace);
namespace = parentNode.lookupNamespaceURI(null);
// console.log("Creating node '" + tagName+"' - changed namespace to " +namespace);
}
const child = parentNode.appendChild(parentNode.ownerDocument.createElementNS(namespace, tagName));
// console.log("Created child " + XML.prettyPrint(child) +"\t with ns: " + child.namespaceURI)
return child;
}
}

Expand Down Expand Up @@ -182,11 +191,12 @@ export default class XML {
* that it can be attached to.
* @param {Node} node
* @param {Boolean} deep Whether to include any children of the node if it is of type element, defaults to true
* @param {String | undefined} newNamespace Optional new namespace to clone the element to
*/
static cloneWithoutNamespace(node, deep = true) {
static cloneWithoutNamespace(node, deep = true, newNamespace = undefined) {
if (node.nodeType === 1) {
const element = /** @type {Element} */ (node);
const newNode = this.createChildElement(element.ownerDocument, element.localName);
const newNode = this.createChildElement(element.ownerDocument, element.localName, newNamespace);
const attributes = element.attributes;
if (attributes !== null) {
for (let i = 0; i < attributes.length; i++) {
Expand All @@ -197,7 +207,7 @@ export default class XML {
}
}
if (deep) {
XML.children(element).forEach(child => newNode.appendChild(this.cloneWithoutNamespace(child, deep)));
XML.children(element).forEach(child => newNode.appendChild(this.cloneWithoutNamespace(child, deep, newNamespace)));
}
return newNode;
} else {
Expand Down

0 comments on commit e658100

Please sign in to comment.