Commit 3e4bdca3 by Owen Ryan Ang

XML parsing logic update

parent a7b591e6
......@@ -18,7 +18,12 @@ export async function WriteForm(e,metrics,doctype,section) {
for (var i=0;i<Nodes.length;i++) {
if (Nodes[i].style.display === 'none' && !Nodes[i].classList.contains('hidden')) continue
let fid = Nodes[i].id;
if (fid == 'DocType' || fid == 'Section' || fid == '' || fid == "submitButton") continue
// Add doctype as field1
if (fid === 'DocType') {
fields['1'] = Nodes[i].value;
continue;
}
if (fid == 'Section' || fid == '' || fid == "submitButton") continue
// Skip the textbox in checklist and radiolist
if (Nodes[i].classList.contains('radioOther') || Nodes[i].classList.contains('checkboxOther')) continue;
// Skip elements of type "button", "submit", and files
......
......@@ -2,5 +2,5 @@ export const PROJECT_CODE = "PROJCODE01";
export const ENCODING_PASS = "PASS1";
export const GFS_URL = "http://107.20.193.188/gfs-explorer-ws/svc/gfs-rest/";
export const TEMPORARY_FOLDER = "E:/Coding/Mobile GDE Elements";
export const TEMPORARY_FOLDER = "C:/Users/oang/Desktop/Mobile GDE/Elements";
export const GFS_ROOT_FOLDER = "/Users";
\ No newline at end of file
......@@ -40,8 +40,17 @@ public class SubRecord {
* @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)))
List<Field> sortedFields = fields.stream()
.sorted((f1, f2) -> {
if ("1".equals(f1.getNo())) {
return -1; // f1 comes before f2
} else if ("1".equals(f2.getNo())) {
return 1; // f2 comes before f1
} else {
return Integer.compare(getOrder(f1.getNo(), orderPipeDelimited),
getOrder(f2.getNo(), orderPipeDelimited));
}
})
.collect(Collectors.toList());
// Update the fields list with the sorted order
......
......@@ -76,29 +76,30 @@ 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();
//SAXParser sp = spf.newSAXParser();
//Source source = new SAXSource(sp.getXMLReader(),
//new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " "))));
// SAXParserFactory spf = SAXParserFactory.newInstance();
// SAXParser sp = spf.newSAXParser();
// Source source = new SAXSource(sp.getXMLReader(),
// new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[
// ](?![^\\W_])", " "))));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
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);
Node node = nl.item(i);
node.getParentNode().removeChild(node);
}
DOMSource source = new DOMSource(document);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
......@@ -348,31 +349,41 @@ public class XMLUtil {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xml.getOutputDir());
Node imagename = document.getElementsByTagName("imagename").item(0);
Node xmlSubRecord = imagename.getParentNode().getLastChild().getPreviousSibling();
Map<String, String> fields = new HashMap<>();
records.put("DocType", imagename.getParentNode().getFirstChild().getNextSibling().getNextSibling()
.getNextSibling().getTextContent());
records.put("Section", imagename.getParentNode().getFirstChild().getNextSibling().getNextSibling()
.getNextSibling().getNextSibling().getNextSibling().getTextContent());
records.put("fields", fields);
for (int i = 0; i < xmlSubRecord.getChildNodes().getLength(); i++) {
if (xmlSubRecord.getChildNodes().item(i).getNodeType() == Node.ELEMENT_NODE) {
for (int j = 0; j < xmlSubRecord.getChildNodes().item(i).getChildNodes().getLength(); j++) {
if (xmlSubRecord.getChildNodes().item(i).getChildNodes().item(j)
.getNodeType() == Node.ELEMENT_NODE) {
fields.put(
xmlSubRecord.getChildNodes().item(i).getAttributes().getNamedItem("no").getNodeValue(),
xmlSubRecord.getChildNodes().item(i).getChildNodes().item(j).getTextContent());
records.put("fields", fields);
}
}
}
}
Element record = (Element) document.getElementsByTagName("record").item(0);
if (record != null) {
records.put("DocType", getElementTextByTagName(record, "doctype"));
records.put("Section", getElementTextByTagName(record, "section"));
Map<String, String> fields = new HashMap<>();
records.put("fields", fields);
Element subRecord = (Element) record.getElementsByTagName("sub-record").item(0);
if (subRecord != null) {
NodeList fieldList = subRecord.getElementsByTagName("field");
for (int j = 0; j < fieldList.getLength(); j++) {
Element field = (Element) fieldList.item(j);
String noAttributeValue = field.getAttribute("no");
String textContentValue = field.getElementsByTagName("value").item(0).getTextContent();
if ("1".equals(noAttributeValue)) {
// Replace the value of "DocType" with the text content
records.put("DocType", textContentValue);
}
fields.put(noAttributeValue, textContentValue);
}
}
}
return records;
}
return records;
private static String getElementTextByTagName(Element parentElement, String tagName) {
NodeList elements = parentElement.getElementsByTagName(tagName);
if (elements.getLength() > 0) {
return elements.item(0).getTextContent();
}
return null; // or an appropriate default value
}
}
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