Skip to content

Commit 369ab38

Browse files
committed
Use namespace map property for marshalling if using certain JAXB vendors
1 parent cb4682d commit 369ab38

5 files changed

Lines changed: 40 additions & 12 deletions

File tree

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK128MetadataExportStrategy.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.IOException;
44
import java.io.OutputStream;
5+
import java.util.HashMap;
6+
import java.util.Map;
57

68
import com.databasepreservation.model.exception.ModuleException;
79
import com.databasepreservation.model.structure.DatabaseStructure;
@@ -28,6 +30,10 @@ public void writeMetadataXML(DatabaseStructure dbStructure, SIARDArchiveContaine
2830
// TO-DO: Refactor this into one method in class that can be used by
2931
// SIARDDKDatabaseExportModule also
3032

33+
// Making the SIARD DK XML namespace default in case needed
34+
Map<String, String> namespaceMap = new HashMap<>();
35+
namespaceMap.put("http://www.sa.dk/xmlns/diark/1.0", "");
36+
3137
// Generate tableIndex.xml
3238

3339
try {
@@ -38,7 +44,7 @@ public void writeMetadataXML(DatabaseStructure dbStructure, SIARDArchiveContaine
3844
siardMarshaller.marshal("com.databasepreservation.modules.siard.bindings.siard_dk_128",
3945
metadataPathStrategy.getXsdResourcePath(SIARDDKConstants.TABLE_INDEX),
4046
"http://www.sa.dk/xmlns/diark/1.0 ../Schemas/standard/tableIndex.xsd", writer,
41-
tableIndexFileStrategy.generateXML(dbStructure));
47+
tableIndexFileStrategy.generateXML(dbStructure), namespaceMap);
4248

4349
writer.close();
4450

@@ -93,7 +99,7 @@ public void writeMetadataXML(DatabaseStructure dbStructure, SIARDArchiveContaine
9399

94100
siardMarshaller.marshal(DocIndexType.class, metadataPathStrategy.getXsdResourcePath(SIARDDKConstants.DOC_INDEX),
95101
"http://www.sa.dk/xmlns/diark/1.0 ../Schemas/standard/docIndex.xsd", writer,
96-
SIARDDKDocIndexFileStrategy.generateXML(dbStructure));
102+
SIARDDKDocIndexFileStrategy.generateXML(dbStructure), namespaceMap);
97103

98104
writer.close();
99105

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDKMetadataExportStrategy.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.OutputStream;
1414
import java.nio.file.Path;
1515
import java.nio.file.Paths;
16+
import java.util.HashMap;
1617
import java.util.Map;
1718

1819
import org.apache.commons.io.IOUtils;
@@ -66,6 +67,10 @@ public void writeMetadataXML(DatabaseStructure dbStructure, SIARDArchiveContaine
6667

6768
// Generate tableIndex.xml
6869

70+
// Making the SIARD DK XML namespace default in case needed
71+
Map<String, String> namespaceMap = new HashMap<>();
72+
namespaceMap.put("http://www.sa.dk/xmlns/diark/1.0", "");
73+
6974
try {
7075
IndexFileStrategy tableIndexFileStrategy = new SIARDDKTableIndexFileStrategy(lobsTracker, siarddkAdapter);
7176
String path = metadataPathStrategy.getXmlFilePath(SIARDDKConstants.TABLE_INDEX);
@@ -74,7 +79,7 @@ public void writeMetadataXML(DatabaseStructure dbStructure, SIARDArchiveContaine
7479
siardMarshaller.marshal(SIARDDKConstants.JAXB_CONTEXT_TABLEINDEX,
7580
metadataPathStrategy.getXsdResourcePath(SIARDDKConstants.TABLE_INDEX),
7681
"http://www.sa.dk/xmlns/diark/1.0 ../Schemas/standard/tableIndex.xsd", writer,
77-
tableIndexFileStrategy.generateXML(dbStructure));
82+
tableIndexFileStrategy.generateXML(dbStructure), namespaceMap);
7883

7984
writer.close();
8085

@@ -130,7 +135,7 @@ public void writeMetadataXML(DatabaseStructure dbStructure, SIARDArchiveContaine
130135
siardMarshaller.marshal(SIARDDKConstants.JAXB_CONTEXT_DOCINDEX,
131136
metadataPathStrategy.getXsdResourcePath(SIARDDKConstants.DOC_INDEX),
132137
"http://www.sa.dk/xmlns/diark/1.0 ../Schemas/standard/docIndex.xsd", writer,
133-
SIARDDKDocIndexFileStrategy.generateXML(dbStructure));
138+
SIARDDKDocIndexFileStrategy.generateXML(dbStructure), namespaceMap);
134139

135140
writer.close();
136141

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDMarshaller.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
* The contents of this file are subject to the license and copyright
33
* detailed in the LICENSE file at the root of the source
44
* tree and available online at
5-
*
5+
* <p>
66
* https://github.com/keeps/db-preservation-toolkit
77
*/
88
package com.databasepreservation.modules.siard.out.metadata;
99

1010
import java.io.OutputStream;
11+
import java.util.Map;
1112

1213
import com.databasepreservation.model.exception.ModuleException;
1314

@@ -19,7 +20,7 @@ public interface SIARDMarshaller {
1920

2021
/**
2122
* Generate JAXB Marshaller for writing XML object to the archive.
22-
*
23+
*
2324
* @param context
2425
* Parameter to give to JAXBContext.newInstance()
2526
* @param localeSchemaLocation
@@ -30,9 +31,11 @@ public interface SIARDMarshaller {
3031
* The OutputStream to write to.
3132
* @param jaxbElement
3233
* The JAXB element to marshal.
34+
* @param namespaceMap
35+
* The namespace map to use for marshaling. Ignored if null.
3336
*/
3437
public void marshal(String context, String localeSchemaLocation, String JAXBSchemaLocation, OutputStream writer,
35-
Object jaxbElement) throws ModuleException;
38+
Object jaxbElement, Map<String, String> namespaceMap) throws ModuleException;
3639

3740
/**
3841
* Generate JAXB Marshaller for writing XML object to the archive.
@@ -47,7 +50,10 @@ public void marshal(String context, String localeSchemaLocation, String JAXBSche
4750
* The OutputStream to write to.
4851
* @param jaxbElement
4952
* The JAXB element to marshal.
53+
* @param namespaceMap
54+
* The namespace map to use for marshaling. Ignored if null.
55+
*
5056
*/
5157
public void marshal(Class<?> archiveClass, String localeSchemaLocation, String JAXBSchemaLocation,
52-
OutputStream writer, Object jaxbElement) throws ModuleException;
58+
OutputStream writer, Object jaxbElement, Map<String, String> namespaceMap) throws ModuleException;
5359
}

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/StandardSIARDMarshaller.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.IOException;
1111
import java.io.InputStream;
1212
import java.io.OutputStream;
13+
import java.util.Map;
1314

1415
import javax.xml.XMLConstants;
1516
import javax.xml.transform.stream.StreamSource;
@@ -27,15 +28,15 @@
2728
import jakarta.xml.bind.JAXBException;
2829
import jakarta.xml.bind.Marshaller;
2930

30-
public class StandardSIARDMarshaller implements SIARDMarshaller {
31+
public class StandardSIARDMarshaller implements SIARDMarshaller {
3132

3233
private static final String ENCODING = "UTF-8";
3334

3435
private static final Logger LOGGER = LoggerFactory.getLogger(StandardSIARDMarshaller.class);
3536

3637
@Override
3738
public void marshal(String contextStr, String localeSchemaLocation, String JAXBSchemaLocation, OutputStream writer,
38-
Object jaxbElement) throws ModuleException {
39+
Object jaxbElement, Map<String, String> namespaceMap) throws ModuleException {
3940

4041
// Set up JAXB marshaller
4142

@@ -82,7 +83,7 @@ public void marshal(String contextStr, String localeSchemaLocation, String JAXBS
8283

8384
@Override
8485
public void marshal(Class<?> archiveClass, String localeSchemaLocation, String JAXBSchemaLocation,
85-
OutputStream writer, Object jaxbElement) throws ModuleException {
86+
OutputStream writer, Object jaxbElement, Map<String, String> namespaceMap) throws ModuleException {
8687

8788
// Set up JAXB marshaller
8889

@@ -115,6 +116,11 @@ public void marshal(Class<?> archiveClass, String localeSchemaLocation, String J
115116
m.setProperty(Marshaller.JAXB_ENCODING, ENCODING);
116117
m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, JAXBSchemaLocation);
117118

119+
if (m.getClass().getName().contains("eclipse") && namespaceMap != null) {
120+
// vendor requires explicit mapping
121+
m.setProperty("eclipse.namespace-prefix-mapper", namespaceMap);
122+
}
123+
118124
m.setSchema(xsdSchema);
119125

120126
m.marshal(jaxbElement, writer);

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/output/SIARDDKDatabaseExportModule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.nio.file.Path;
1616
import java.nio.file.attribute.BasicFileAttributes;
1717
import java.util.ArrayList;
18+
import java.util.HashMap;
1819
import java.util.List;
1920
import java.util.Map;
2021
import java.util.concurrent.BlockingQueue;
@@ -216,10 +217,14 @@ public void finishDatabase() throws ModuleException {
216217
OutputStream writer = SIARDDKFileIndexFileStrategy.getWriter(siarddkExportModule.getMainContainer(), path,
217218
siarddkExportModule.getWriteStrategy());
218219

220+
// Making the SIARD DK XML namespace default in case needed
221+
Map<String, String> namespaceMap = new HashMap<>();
222+
namespaceMap.put("http://www.sa.dk/xmlns/diark/1.0", "");
223+
219224
siardMarshaller.marshal(getJAXBContextClass(),
220225
metadataPathStrategy.getXsdResourcePath(SIARDDKConstants.FILE_INDEX),
221226
"http://www.sa.dk/xmlns/diark/1.0 ../Schemas/standard/fileIndex.xsd", writer,
222-
SIARDDKFileIndexFileStrategy.generateXML(null));
227+
SIARDDKFileIndexFileStrategy.generateXML(null), namespaceMap);
223228

224229
writer.close();
225230
} catch (IOException e) {

0 commit comments

Comments
 (0)