Skip to content

Commit

Permalink
Added generic method to create XML representation
Browse files Browse the repository at this point in the history
  • Loading branch information
sfieten committed Sep 3, 2020
1 parent 93eb78a commit 602402f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.o
*.so
target/
bin/

# Packages #
############
Expand All @@ -28,13 +29,14 @@ target/
*.sqlite
db/

# IDE generated files #
# IDE Config files #
######################
/.project
/.settings/
*.iml
/.idea
nb-configuration.xml
.project
.settings/
nbproject/
*.iml
.idea
.classpath

# OS generated files #
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 1.1.0
##### 2020-09-03
### Added
* Generic method to `org.holodeckb2b.delivery.signals.utils.SMDFactory` to create XML representation of a Signal

### Changed
* Updated dependencies to Holodeck B2B 5.0.0
* When the the _"Include Receipt content"_ parameter is set to _false_ only the first child element of the original
Receipt is included without its children.

## 1.0.0
##### 2016-04-08
### Added
* Initial release.

6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.holodeckb2b</groupId>
<artifactId>signaldelivery</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -71,7 +71,7 @@
<dependency>
<groupId>org.holodeckb2b</groupId>
<artifactId>holodeckb2b-interfaces</artifactId>
<version>2.0.0</version>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<!-- Axiom is needed because the content of the Receipt is represented in Axiom elements and needs
Expand All @@ -81,7 +81,7 @@
<artifactId>axiom-api</artifactId>
<!-- Make sure that the Axiom version used here is in sync with the one used by the Holodeck B2B
version this lib is used with -->
<version>1.2.14</version>
<version>1.2.20</version>
</dependency>
<!-- Log4j is used for logging -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
Expand All @@ -36,6 +38,7 @@
import org.holodeckb2b.interfaces.messagemodel.IEbmsError;
import org.holodeckb2b.interfaces.messagemodel.IErrorMessage;
import org.holodeckb2b.interfaces.messagemodel.IReceipt;
import org.holodeckb2b.interfaces.messagemodel.ISignalMessage;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

Expand All @@ -46,6 +49,21 @@
*/
public class SMDFactory {

/**
* Creates a new {@link SignalMessage} object based on the given Holodeck B2B Signal message. When the signal is a
* Receipt only its content's first child element is included in the XML representation.
*
* @param error The meta-data about the signal message unit
* @return The new {@link SignalMessage} containing the signal meta-data for the given signal
* @since 1.1.0
*/
public static SignalMessage createSMD(final ISignalMessage signal) {
if (signal instanceof IErrorMessage)
return createSMD((IErrorMessage) signal);
else
return createSMD((IReceipt) signal);
}

/**
* Creates a new {@link SignalMessage} object based on the given Holodeck B2B Error signal message.
*
Expand Down Expand Up @@ -147,10 +165,10 @@ public static SignalMessage createSMD(final IReceipt receipt, final boolean incl
if (includeFullContent) {
// Copy all child elements of the Receipt
for (OMElement e : rcptContent)
content.add(convertElement(e, doc));
content.add(convertElement(e, doc, true));
} else {
// Only the first child element of the Receipt needs to be included
content.add(convertElement(rcptContent.get(0), doc));
content.add(convertElement(rcptContent.get(0), doc, false));
}

// Add Receipt info to SignalMessage
Expand Down Expand Up @@ -179,11 +197,12 @@ private static XMLGregorianCalendar convertDate(final Date date) {
/**
* Helper method to convert an Axiom {@link OMElement} to a W3 DOM representation.
*
* @param e The Axiom representation of the element
* @param doc The W3 Document in which the DOM representation is to be inserted
* @param e The Axiom representation of the element
* @param doc The W3 Document in which the DOM representation is to be inserted
* @param deepCopy Indicates whether the child elements should be included in the converted element
* @return The converted element
*/
private static Element convertElement(OMElement e, Document doc) {
private static Element convertElement(OMElement e, Document doc, boolean deepCopy) {
// Convert the element itself
Element d = doc.createElementNS(e.getNamespaceURI(), e.getLocalName());
String prefix = e.getPrefix();
Expand All @@ -199,14 +218,16 @@ private static Element convertElement(OMElement e, Document doc) {
else
d.setAttribute(a.getLocalName(), a.getAttributeValue());
}
// And convert all content child nodes, calling this method recursively for child elements
Iterator<OMNode> content = e.getChildren();
while (content.hasNext()) {
OMNode n = content.next();
if (n instanceof OMElement)
d.appendChild(convertElement((OMElement) n, doc));
else if (n instanceof OMText)
d.appendChild(doc.createTextNode(((OMText) n).getText()));
if (deepCopy) {
// And convert all content child nodes, calling this method recursively for child elements
Iterator<OMNode> content = e.getChildren();
while (content.hasNext()) {
OMNode n = content.next();
if (n instanceof OMElement)
d.appendChild(convertElement((OMElement) n, doc, true));
else if (n instanceof OMText)
d.appendChild(doc.createTextNode(((OMText) n).getText()));
}
}

return d;
Expand Down

0 comments on commit 602402f

Please sign in to comment.