Skip to content

Commit

Permalink
feat(bpmn): improve namespace prefixes in bpmn generated file (#50)
Browse files Browse the repository at this point in the history
Use explicit namespace prefixes instead of default ns1, ns2, ...
Notice that there is no more default namespace, BPMN model schema is mapped to the semantic prefix.

In addition, we now generated formatted/pretty-print xml.
  • Loading branch information
tbouffard authored Jul 15, 2020
1 parent 0db6f1d commit d24f8b3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
15 changes: 7 additions & 8 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<jaxb.version>2.3.1</jaxb.version>
<junit.version>5.6.2</junit.version>
<log4j.version>2.13.2</log4j.version>
</properties>
Expand All @@ -25,16 +24,16 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>${jaxb.version}</version>
<scope>runtime</scope>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.3</version>
</dependency>

<!-- to be bundled only with the application not the library -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020 Bonitasoft S.A.
*
* 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 io.process.analytics.tools.bpmn.generator.internal;

import com.sun.xml.bind.marshaller.NamespacePrefixMapper;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class BpmnNamespacePrefixMapper extends NamespacePrefixMapper {

private static final Map<String, String> namespaces = new HashMap<>();
static {
namespaces.put("http://www.omg.org/spec/BPMN/20100524/DI", "bpmndi");
namespaces.put("http://www.omg.org/spec/BPMN/20100524/MODEL", "semantic");
namespaces.put("http://www.omg.org/spec/DD/20100524/DC", "dc");
namespaces.put("http://www.omg.org/spec/DD/20100524/DI", "di");
}

@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return namespaces.getOrDefault(namespaceUri, suggestion);
}

@Override
public String[] getPreDeclaredNamespaceUris() {
Set<String> uris = namespaces.keySet();
return uris.toArray(new String[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import java.io.File;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

import io.process.analytics.tools.bpmn.generator.internal.generated.model.ObjectFactory;
import io.process.analytics.tools.bpmn.generator.internal.generated.model.TDefinitions;

public class XmlParser {

private static final JAXBContext context = initContext();

private static JAXBContext initContext() {
Expand All @@ -40,12 +39,25 @@ private static JAXBContext initContext() {
public void marshal(TDefinitions definitions, File outputFile) {
try {
JAXBElement<TDefinitions> root = new ObjectFactory().createDefinitions(definitions);
context.createMarshaller().marshal(root, outputFile);
createMarshaller().marshal(root, outputFile);
} catch (JAXBException e) {
throw new RuntimeException("Unable to marshal", e);
}
}

private Marshaller createMarshaller() throws JAXBException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
try {
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new BpmnNamespacePrefixMapper());
} catch(PropertyException e) {
// In case another JAXB implementation is used
// do not stop processing, namespace prefixes will be generated automatically in that case
e.printStackTrace();
}
return marshaller;
}

public TDefinitions unmarshall(String xml) {
try {
StreamSource source = new StreamSource(new StringReader(xml));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void main_generates_xml_output_file() throws Exception {

File bpmnFile = new File(outputPath);
assertThat(bpmnFile).exists().isFile();
assertThat(fileContent(bpmnFile)).contains("<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\"");
assertThat(fileContent(bpmnFile)).contains("<semantic:definitions xmlns:semantic=\"http://www.omg.org/spec/BPMN/20100524/MODEL\"");
}

@Test
Expand Down

0 comments on commit d24f8b3

Please sign in to comment.