package com.equalize.xpi.af.modules.json;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import com.equalize.xpi.af.modules.util.AbstractModuleConverter;
import com.equalize.xpi.af.modules.util.AuditLogHelper;
import com.equalize.xpi.af.modules.util.DynamicConfigurationHelper;
import com.equalize.xpi.af.modules.util.ParameterHelper;
import com.equalize.xpi.util.converter.ConversionDOMOutput;
import com.equalize.xpi.util.converter.ConversionJSONInput;
import com.equalize.xpi.util.converter.Field;
import com.sap.aii.af.lib.mp.module.ModuleException;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;

public class JSON2XMLConverter extends AbstractModuleConverter {
	private ConversionJSONInput jsonIn;
	private ConversionDOMOutput domOut;
	private String documentName;
	private String documentNamespace;
	private int indentFactor;
	private ArrayList<Field> inputContents;

	public JSON2XMLConverter(Message msg, ParameterHelper param, AuditLogHelper audit, DynamicConfigurationHelper dyncfg) {
		super(msg, param, audit, dyncfg);
	}

	@Override
	public void retrieveModuleParameters() throws ModuleException {
		this.documentName = this.param.getMandatoryParameter("documentName");
		this.documentNamespace = this.param.getMandatoryParameter("documentNamespace");
		this.indentFactor = this.param.getIntParameter("indentFactor");
	}

	@Override
	public void parseInput() throws ModuleException {
		// Parse input JSON contents
		String content = this.payload.getText();
		this.jsonIn = new ConversionJSONInput(content);
		this.audit.addLog(AuditLogStatus.SUCCESS, "Parsing input JSON");
		//this.inputContents = this.jsonIn.extractJSONContent();
		this.inputContents = this.jsonIn.extractJSONContentArray();
	}

	@Override
	public byte[] generateOutput() throws ModuleException {
		try {
			// Create output converter and generate output DOM
			this.domOut = new ConversionDOMOutput(this.documentName, this.documentNamespace);
			this.audit.addLog(AuditLogStatus.SUCCESS, "Constructing output XML");	
			
			// Generate OutputStream from DOM
			if(this.indentFactor > 0) {
				this.domOut.setIndentFactor(this.indentFactor);
				this.audit.addLog(AuditLogStatus.SUCCESS, "Output XML will be indented");
			}

			ByteArrayOutputStream baos = this.domOut.generateDOMOutput(this.inputContents);
			this.audit.addLog(AuditLogStatus.SUCCESS, "Conversion complete");
			return baos.toByteArray();
		} catch (Exception e) {
			this.audit.addLog(AuditLogStatus.ERROR, e.getMessage());
			throw new ModuleException(e.getMessage(), e);
		}
	}
}

@@ -927,16 +927,80 @@
 Factor;%0A
+%09private boolean allowArrayAtTop;%0A%09private String topArrayName;%0A
 %09private
@@ -1490,16 +1490,244 @@
 ctor%22);%0A
+%09%09this.allowArrayAtTop = this.param.getBoolParameter(%22allowArrayAtTop%22, %22N%22, false);%0A%09%09if(this.allowArrayAtTop) %7B%0A%09%09%09this.topArrayName = this.param.getConditionallyMandatoryParameter(%22topArrayName%22, %22allowArrayAtTop%22, %22Y%22);%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -1854,24 +1854,54 @@
 .getText();%0A
+%09%09if(this.allowArrayAtTop) %7B%0A%09
 %09%09this.jsonI
@@ -1935,18 +1935,103 @@
 (content
-);
+, this.topArrayName);%0A%09%09%7D else %7B%0A%09%09%09this.jsonIn = new ConversionJSONInput(content);%0A%09%09%7D
 %0A%09%09this.
@@ -2092,18 +2092,16 @@
 ON%22);%0A%09%09
-//
 this.inp
@@ -2151,70 +2151,8 @@
 ();%0A
-%09%09this.inputContents = this.jsonIn.extractJSONContentArray();%0A
 %09%7D%0A%0A
@@ -2440,19 +2440,16 @@
 XML%22);%09%0A
-%09%09%09
 %0A%09%09%09// G

Update JSON2XMLConverter.java