You are on page 1of 3

12/23/2020 equalize-xpi-modules/XML2JSONConverter.

java at master · engswee/equalize-xpi-modules · GitHub

engswee / equalize-xpi-modules

Code Issues Pull requests 1 Actions Projects Security Insigh

master

equalize-xpi-modules / com.equalize.xpi.af.modules.ejb / ejbModule / com / equalize / xpi / af /


modules / json / XML2JSONConverter.java / Jump to

engswee Introduce end-of-line normalization History

1 contributor

Raw Blame

91 lines (82 sloc) 3.53 KB

1 package com.equalize.xpi.af.modules.json;
2
3 import java.util.HashSet;
4
5 import com.equalize.xpi.af.modules.util.AbstractModuleConverter;
6 import com.equalize.xpi.af.modules.util.AuditLogHelper;
7 import com.equalize.xpi.af.modules.util.DynamicConfigurationHelper;
8 import com.equalize.xpi.af.modules.util.ParameterHelper;
9 import com.equalize.xpi.util.converter.ConversionDOMInput;
10 import com.equalize.xpi.util.converter.ConversionJSONOutput;
11 import com.equalize.xpi.util.converter.XMLElementContainer;
12 import com.sap.aii.af.lib.mp.module.ModuleException;
13 import com.sap.engine.interfaces.messaging.api.Message;
14 import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
15
16 public class XML2JSONConverter extends AbstractModuleConverter {
17 private ConversionDOMInput domIn;
18 private ConversionJSONOutput jsonOut;
19 private XMLElementContainer rootXML;
20 private int indentFactor;
21 private boolean skipRootNode;
22 private boolean forceArrayAll;
23 private HashSet<String> arrayFields;
24
25 public XML2JSONConverter(Message msg, ParameterHelper param, AuditLogHelper audit, Dynamic
26 super(msg, param, audit, dyncfg, debug);
https://github.com/engswee/equalize-xpi-modules/blob/master/com.equalize.xpi.af.modules.ejb/ejbModule/com/equalize/xpi/af/modules/json/XML2JS… 1/3
12/23/2020 equalize-xpi-modules/XML2JSONConverter.java at master · engswee/equalize-xpi-modules · GitHub

27 }
28 @Override
29 public void retrieveModuleParameters() throws ModuleException {
30 this.indentFactor = this.param.getIntParameter("indentFactor");
31 this.skipRootNode = this.param.getBoolParameter("skipRootNode");
32 this.forceArrayAll = this.param.getBoolParameter("forceArrayAll", "N", false);
33
34 // Undecided between using a comma separated parameter or
35 // and enumeration of parameters with "array." prefix
36 String arrayFieldList = this.param.getParameter("arrayFieldList");
37 this.arrayFields = new HashSet<String>();
38 if(arrayFieldList != null && !arrayFieldList.trim().equalsIgnoreCase("")) {
39 String[] fields = arrayFieldList.split(",");
40 for(String field: fields) {
41 if(!this.arrayFields.contains(field))
42 this.arrayFields.add(field);
43 }
44 }
45
46 /*
47 // Iterate through enumeration to get elements that should use JSON Array
48 Enumeration<String> keys = this.param.getModuleContext().getContextDataKeys();
49 this.jsonArray = new ArrayList<String>();
50 while(keys.hasMoreElements()) {
51 String key = keys.nextElement();
52 if(key.startsWith("array.")) {
53 String fieldname = this.param.getParameter(key);
54 this.jsonArray.add(fieldname);
55 }
56 }*/
57 }
58
59 @Override
60 public void parseInput() throws ModuleException {
61 try {
62 // Parse input XML contents
63 this.domIn = new ConversionDOMInput(this.payload.getInputStream());
64 this.audit.addLog(AuditLogStatus.SUCCESS, "Parsing input XML");
65 this.rootXML = this.domIn.extractDOMContent();
66 } catch (Exception e) {
67 this.audit.addLog(AuditLogStatus.ERROR, e.getMessage());
68 throw new ModuleException(e.getMessage(), e);
69 }
70 }

71
72 @Override
73 public byte[] generateOutput() throws ModuleException {
74 try {
https://github.com/engswee/equalize-xpi-modules/blob/master/com.equalize.xpi.af.modules.ejb/ejbModule/com/equalize/xpi/af/modules/json/XML2JS… 2/3
12/23/2020 equalize-xpi-modules/XML2JSONConverter.java at master · engswee/equalize-xpi-modules · GitHub

75 // Create output converter and generate output JSON


76 this.jsonOut = new ConversionJSONOutput();
77 // Pass in additional parameters for forcing arrays in output
78 this.jsonOut.setForceArray(this.forceArrayAll);
79 this.jsonOut.setArrayFields(this.arrayFields);
80 this.audit.addLog(AuditLogStatus.SUCCESS, "Constructing output JSON");
81 String output = this.jsonOut.generateJSONText(this.rootXML, this.skipRootN
82
83 this.audit.addLog(AuditLogStatus.SUCCESS, "Conversion complete");
84 this.payload.setContentType("application/json; charset=utf-8");
85 return output.getBytes("UTF-8");
86 } catch (Exception e) {
87 this.audit.addLog(AuditLogStatus.ERROR, e.getMessage());
88 throw new ModuleException(e.getMessage(), e);
89 }
90 }
91 }

https://github.com/engswee/equalize-xpi-modules/blob/master/com.equalize.xpi.af.modules.ejb/ejbModule/com/equalize/xpi/af/modules/json/XML2JS… 3/3

You might also like