Skip to content

Commit baef875

Browse files
committed
optimized namespace resolving (Issue #402)
1 parent 72961cc commit baef875

1 file changed

Lines changed: 61 additions & 39 deletions

File tree

open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/BPMNModel.java

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,15 @@ private void parseAllNamespaces() {
150150
// Test for default namespace without prefix
151151
String defaultNameSpace = definitions.getAttribute("xmlns");
152152
if (defaultNameSpace != null && !defaultNameSpace.isEmpty()) {
153-
if (getUri(BPMNNS.BPMN2).equals(defaultNameSpace)) {
154-
setPrefix(BPMNNS.BPMN2, "");
153+
// Try to map the default namespace to any BPMN namespace
154+
BPMNNS matchedNamespace = findMatchingNamespace(defaultNameSpace);
155+
if (matchedNamespace != null) {
156+
if (!getUri(matchedNamespace).equals(defaultNameSpace)) {
157+
logger.warning("Non-standard default namespace detected: " + defaultNameSpace + " - mapped to "
158+
+ matchedNamespace);
159+
}
160+
setPrefix(matchedNamespace, "");
161+
setUri(matchedNamespace, defaultNameSpace);
155162
}
156163
}
157164

@@ -176,36 +183,60 @@ private void parseAllNamespaces() {
176183

177184
/**
178185
* Helper method that maps a URI to the correct BPMN namespace based on the URI
179-
* value
186+
* value. Uses tolerant matching for non-standard but BPMN-like URIs.
180187
*/
181188
private void mapUriToNamespace(String prefix, String uri) {
182-
// BPMN2 Model namespace variants
183-
if ("http://www.omg.org/spec/BPMN/20100524/MODEL".equals(uri)) {
184-
setPrefix(BPMNNS.BPMN2, prefix);
185-
setUri(BPMNNS.BPMN2, uri);
186-
return;
189+
// Try to find a matching namespace for this URI
190+
BPMNNS matchedNamespace = findMatchingNamespace(uri);
191+
if (matchedNamespace != null) {
192+
// Check if it's a non-standard URI
193+
if (!getUri(matchedNamespace).equals(uri)) {
194+
logger.warning("Non-standard namespace detected: " + uri + " with prefix: " + prefix + " - mapped to "
195+
+ matchedNamespace);
196+
}
197+
setPrefix(matchedNamespace, prefix);
198+
setUri(matchedNamespace, uri);
187199
}
200+
}
188201

189-
// BPMN DI namespace - can be mapped to different prefixes!
190-
if ("http://www.omg.org/spec/BPMN/20100524/DI".equals(uri)) {
191-
setPrefix(BPMNNS.BPMNDI, prefix);
192-
setUri(BPMNNS.BPMNDI, uri);
193-
return;
202+
/**
203+
* Finds the matching BPMNNS enum for a given URI.
204+
* First tries exact matches, then tolerant matching.
205+
*
206+
* @param uri The namespace URI to match
207+
* @return The matching BPMNNS enum or null if no match found
208+
*/
209+
private BPMNNS findMatchingNamespace(String uri) {
210+
if (uri == null || uri.isEmpty()) {
211+
return null;
194212
}
195213

196-
// DC namespace
197-
if ("http://www.omg.org/spec/DD/20100524/DC".equals(uri)) {
198-
setPrefix(BPMNNS.DC, prefix);
199-
setUri(BPMNNS.DC, uri);
200-
return;
214+
// Try exact matches first for all namespaces
215+
for (BPMNNS namespace : BPMNNS.values()) {
216+
if (getUri(namespace).equals(uri)) {
217+
return namespace;
218+
}
201219
}
202220

203-
// DI namespace
204-
if ("http://www.omg.org/spec/DD/20100524/DI".equals(uri)) {
205-
setPrefix(BPMNNS.DI, prefix);
206-
setUri(BPMNNS.DI, uri);
207-
return;
221+
// Tolerant matching for BPMN-like URIs
222+
if (uri.startsWith("http://www.omg.org/spec/BPMN/")) {
223+
if (uri.endsWith("/MODEL")) {
224+
return BPMNNS.BPMN2;
225+
} else if (uri.endsWith("/DI")) {
226+
return BPMNNS.BPMNDI;
227+
}
208228
}
229+
230+
// Tolerant matching for DD-like URIs
231+
if (uri.startsWith("http://www.omg.org/spec/DD/")) {
232+
if (uri.endsWith("/DC")) {
233+
return BPMNNS.DC;
234+
} else if (uri.endsWith("/DI")) {
235+
return BPMNNS.DI;
236+
}
237+
}
238+
239+
return null; // No match found
209240
}
210241

211242
/*
@@ -229,21 +260,18 @@ public NodeList findElementsByName(Element parent, BPMNNS namespace, String elem
229260
*/
230261
private void loadBpmnDiagram() {
231262
// find bpmndi:BPMNDiagram
232-
// NodeList diagramList = doc.getElementsByTagName(getPrefix(BPMNNS.BPMNDI) +
233-
// "BPMNDiagram");
234263
NodeList diagramList = findElementsByName(doc.getDocumentElement(), BPMNNS.BPMNDI, "BPMNDiagram");
235264
if (diagramList != null && diagramList.getLength() > 0) {
236265
bpmnDiagram = diagramList.item(0);
237266
} else {
238-
logger.warning("Fatal Diagram Error - no BPMNDiagram element found!");
239267
// no diagram included - so we create an empty one
240-
// getLogger().warning("No bpmndi:BPMNDiagram found - created default diagram");
241-
// // <bpmndi:BPMNDiagram id="BPMNDiagram_1" name="Default Process Diagram">
242-
// Element bpmnDiagram = createElement(BPMNNS.BPMNDI, "BPMNDiagram");
243-
// bpmnDiagram.setAttribute("id", "BPMNDiagram_1");
244-
// bpmnDiagram.setAttribute("name", "OpenBPMN Diagram");
245-
// definitions.appendChild(bpmnDiagram);
246-
// setBpmnDiagram(bpmnDiagram);
268+
getLogger().warning("No bpmndi:BPMNDiagram found - created default diagram");
269+
// <bpmndi:BPMNDiagram id="BPMNDiagram_1" name="Default Process Diagram">
270+
Element bpmnDiagram = createElement(BPMNNS.BPMNDI, "BPMNDiagram");
271+
bpmnDiagram.setAttribute("id", "BPMNDiagram_1");
272+
bpmnDiagram.setAttribute("name", "OpenBPMN Diagram");
273+
definitions.appendChild(bpmnDiagram);
274+
setBpmnDiagram(bpmnDiagram);
247275
}
248276
}
249277

@@ -254,8 +282,6 @@ private void loadBpmnDiagram() {
254282
*/
255283
private void loadBpmnPlane() {
256284
// find the corresponding BPMNPlane
257-
// NodeList planeList = doc.getElementsByTagName(getPrefix(BPMNNS.BPMNDI) +
258-
// "BPMNPlane");
259285
NodeList planeList = findElementsByName(doc.getDocumentElement(), BPMNNS.BPMNDI, "BPMNPlane");
260286
if (planeList != null && planeList.getLength() > 0) {
261287
bpmnPlane = (Element) planeList.item(0);
@@ -266,13 +292,9 @@ private void loadBpmnPlane() {
266292
getLogger().warning("No bpmndi:BPMNPlane found - created default plane");
267293
bpmnPlane = createElement(BPMNNS.BPMNDI, "BPMNPlane");
268294
bpmnPlane.setAttribute("id", "BPMNPlane_1");
269-
// NodeList nodeList = definitions.getElementsByTagName(getPrefix(BPMNNS.BPMN2)
270-
// + "collaboration");
271295
NodeList nodeList = findElementsByName(definitions, BPMNNS.BPMN2, "collaboration");
272296
if (nodeList == null || nodeList.getLength() == 0) {
273297
// Take the default process as plane ref...
274-
// nodeList = definitions.getElementsByTagName(getPrefix(BPMNNS.BPMN2) +
275-
// "process");
276298
nodeList = findElementsByName(definitions, BPMNNS.BPMN2, "process");
277299
}
278300
if (nodeList != null && nodeList.getLength() > 0) {

0 commit comments

Comments
 (0)