Commit 92006a51 by Owen

fieldno ordering acc to schema, removed extra spacing on xml.

parent 6827826d
......@@ -74,7 +74,11 @@ export async function WriteForm(e,metrics,doctype,section) {
}
fields[Object.keys(lookup[fid]).includes('aka') ? lookup[fid].aka.replace("field", "") : fid] = Nodes[i].value;
}
await createOutputXml(fields, myArray, doctype, section);
let fieldOrder = extractAkaValues(schema, doctype, section);
console.log(fieldOrder);
await createOutputXml(fields, myArray, doctype, section, fieldOrder);
}
}
catch(Err) {
......@@ -83,7 +87,25 @@ export async function WriteForm(e,metrics,doctype,section) {
return false;
}
async function createOutputXml(fields, metrics, doctype, section) {
function extractAkaValues(json, doctype, section) {
const akaValues = [];
function recursiveExtract(obj) {
for (const key in obj) {
if (typeof obj[key] === 'object') {
recursiveExtract(obj[key]);
} else if (key === 'aka') {
akaValues.push(obj[key].replace('field', ''));
}
}
}
recursiveExtract(json[doctype][section]);
return akaValues.join('|');
}
async function createOutputXml(fields, metrics, doctype, section, fieldOrder) {
let elementId = sessionStorage.getItem("element_id");
let filePaths = JSON.parse(sessionStorage.getItem("dir_files"));
......@@ -111,7 +133,8 @@ async function createOutputXml(fields, metrics, doctype, section) {
"fields": fields,
"outputDir": sessionStorage.getItem("element_file_loc") + "/" + (ENCODING_PASS == "PASS1" ? elementId + ".DTA" : elementId + ".DTB"),
"doctype": doctype,
"section": section
"section": section,
"fieldOrder" : fieldOrder
}
......
......@@ -24,4 +24,8 @@ public class Field {
this.no = no;
this.value = value;
}
public String getNo() {
return this.no;
}
}
......@@ -36,4 +36,8 @@ public class Record {
this.section = xml.getSection();
this.subRecord = new SubRecord(xml);
}
public SubRecord getSubRecord() {
return this.subRecord;
}
}
package com.svi.webgde.restservice.object;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import com.svi.webgde.restservice.utils.XMLUtil;
import com.thoughtworks.xstream.annotations.XStreamAlias;
......@@ -31,4 +33,37 @@ public class SubRecord {
this.eor = xml.getEor();
this.fields = XMLUtil.generateFields(xml);
}
/**
* Arrange the contents of the fields list based on the order specified in the pipe-delimited string.
*
* @param orderPipeDelimited Pipe-delimited string specifying the order.
*/
public void arrangeFields(String orderPipeDelimited) {
List<Field> sortedFields = fields.stream()
.sorted(Comparator.comparingInt(f -> getOrder(f.getNo(), orderPipeDelimited)))
.collect(Collectors.toList());
// Update the fields list with the sorted order
fields.clear();
fields.addAll(sortedFields);
}
/**
* Helper method to get the order of a field based on the pipe-delimited string.
*
* @param fieldNo The field number to get the order for.
* @param orderPipeDelimited The pipe-delimited string specifying the order.
* @return The order of the field.
*/
private int getOrder(String fieldNo, String orderPipeDelimited) {
String[] orderArray = orderPipeDelimited.split("\\|");
for (int i = 0; i < orderArray.length; i++) {
if (orderArray[i].equals(fieldNo)) {
return i;
}
}
// Default to a high value if not found (end of the list)
return orderArray.length;
}
}
......@@ -25,6 +25,15 @@ public class XMLContents {
private String outputDir;
private String doctype;
private String section;
private String fieldOrder;
public String getFieldOrder() {
return fieldOrder;
}
public void setFieldOrder(String fieldOrder) {
this.fieldOrder = fieldOrder;
}
public String getProjCode() {
return projCode;
......
......@@ -20,7 +20,10 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
......@@ -74,6 +77,8 @@ public class XMLUtil {
Header header = new Header(xml);
Record record = new Record(xml);
record.getSubRecord().arrangeFields(xml.getFieldOrder());
OutputXML output = new OutputXML(header, record);
// SAXParserFactory spf = SAXParserFactory.newInstance();
......@@ -85,6 +90,15 @@ public class XMLUtil {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(
new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " "))));
// Generate list of all empty Nodes, them remove them
XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xp.evaluate("//text()[normalize-space(.)='']", document, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); ++i) { // note the position of the '++'
Node node = nl.item(i);
node.getParentNode().removeChild(node);
}
DOMSource source = new DOMSource(document);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment