Commit 3e4bdca3 by Owen Ryan Ang

XML parsing logic update

parent a7b591e6
...@@ -18,7 +18,12 @@ export async function WriteForm(e,metrics,doctype,section) { ...@@ -18,7 +18,12 @@ export async function WriteForm(e,metrics,doctype,section) {
for (var i=0;i<Nodes.length;i++) { for (var i=0;i<Nodes.length;i++) {
if (Nodes[i].style.display === 'none' && !Nodes[i].classList.contains('hidden')) continue if (Nodes[i].style.display === 'none' && !Nodes[i].classList.contains('hidden')) continue
let fid = Nodes[i].id; 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 // Skip the textbox in checklist and radiolist
if (Nodes[i].classList.contains('radioOther') || Nodes[i].classList.contains('checkboxOther')) continue; if (Nodes[i].classList.contains('radioOther') || Nodes[i].classList.contains('checkboxOther')) continue;
// Skip elements of type "button", "submit", and files // Skip elements of type "button", "submit", and files
......
...@@ -2,5 +2,5 @@ export const PROJECT_CODE = "PROJCODE01"; ...@@ -2,5 +2,5 @@ export const PROJECT_CODE = "PROJCODE01";
export const ENCODING_PASS = "PASS1"; export const ENCODING_PASS = "PASS1";
export const GFS_URL = "http://107.20.193.188/gfs-explorer-ws/svc/gfs-rest/"; 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"; export const GFS_ROOT_FOLDER = "/Users";
\ No newline at end of file
...@@ -40,8 +40,17 @@ public class SubRecord { ...@@ -40,8 +40,17 @@ public class SubRecord {
* @param orderPipeDelimited Pipe-delimited string specifying the order. * @param orderPipeDelimited Pipe-delimited string specifying the order.
*/ */
public void arrangeFields(String orderPipeDelimited) { public void arrangeFields(String orderPipeDelimited) {
List<Field> sortedFields = fields.stream() List<Field> sortedFields = fields.stream()
.sorted(Comparator.comparingInt(f -> getOrder(f.getNo(), orderPipeDelimited))) .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()); .collect(Collectors.toList());
// Update the fields list with the sorted order // Update the fields list with the sorted order
......
...@@ -76,29 +76,30 @@ public class XMLUtil { ...@@ -76,29 +76,30 @@ public class XMLUtil {
Header header = new Header(xml); Header header = new Header(xml);
Record record = new Record(xml); Record record = new Record(xml);
record.getSubRecord().arrangeFields(xml.getFieldOrder()); record.getSubRecord().arrangeFields(xml.getFieldOrder());
OutputXML output = new OutputXML(header, record); OutputXML output = new OutputXML(header, record);
//SAXParserFactory spf = SAXParserFactory.newInstance(); // SAXParserFactory spf = SAXParserFactory.newInstance();
//SAXParser sp = spf.newSAXParser(); // SAXParser sp = spf.newSAXParser();
//Source source = new SAXSource(sp.getXMLReader(), // Source source = new SAXSource(sp.getXMLReader(),
//new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " ")))); // new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[
// ](?![^\\W_])", " "))));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( Document document = builder.parse(
new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " ")))); new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " "))));
// Generate list of all empty Nodes, them remove them // Generate list of all empty Nodes, them remove them
XPath xp = XPathFactory.newInstance().newXPath(); XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xp.evaluate("//text()[normalize-space(.)='']", document, XPathConstants.NODESET); NodeList nl = (NodeList) xp.evaluate("//text()[normalize-space(.)='']", document, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); ++i) { // note the position of the '++' for (int i = 0; i < nl.getLength(); ++i) { // note the position of the '++'
Node node = nl.item(i); Node node = nl.item(i);
node.getParentNode().removeChild(node); node.getParentNode().removeChild(node);
} }
DOMSource source = new DOMSource(document); DOMSource source = new DOMSource(document);
TransformerFactory transformerFactory = TransformerFactory.newInstance(); TransformerFactory transformerFactory = TransformerFactory.newInstance();
...@@ -348,31 +349,41 @@ public class XMLUtil { ...@@ -348,31 +349,41 @@ public class XMLUtil {
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xml.getOutputDir()); Document document = builder.parse(xml.getOutputDir());
Node imagename = document.getElementsByTagName("imagename").item(0); Element record = (Element) document.getElementsByTagName("record").item(0);
Node xmlSubRecord = imagename.getParentNode().getLastChild().getPreviousSibling(); if (record != null) {
Map<String, String> fields = new HashMap<>(); records.put("DocType", getElementTextByTagName(record, "doctype"));
records.put("Section", getElementTextByTagName(record, "section"));
records.put("DocType", imagename.getParentNode().getFirstChild().getNextSibling().getNextSibling()
.getNextSibling().getTextContent()); Map<String, String> fields = new HashMap<>();
records.put("Section", imagename.getParentNode().getFirstChild().getNextSibling().getNextSibling() records.put("fields", fields);
.getNextSibling().getNextSibling().getNextSibling().getTextContent());
records.put("fields", fields); Element subRecord = (Element) record.getElementsByTagName("sub-record").item(0);
if (subRecord != null) {
for (int i = 0; i < xmlSubRecord.getChildNodes().getLength(); i++) { NodeList fieldList = subRecord.getElementsByTagName("field");
if (xmlSubRecord.getChildNodes().item(i).getNodeType() == Node.ELEMENT_NODE) { for (int j = 0; j < fieldList.getLength(); j++) {
for (int j = 0; j < xmlSubRecord.getChildNodes().item(i).getChildNodes().getLength(); j++) { Element field = (Element) fieldList.item(j);
if (xmlSubRecord.getChildNodes().item(i).getChildNodes().item(j) String noAttributeValue = field.getAttribute("no");
.getNodeType() == Node.ELEMENT_NODE) { String textContentValue = field.getElementsByTagName("value").item(0).getTextContent();
fields.put(
xmlSubRecord.getChildNodes().item(i).getAttributes().getNamedItem("no").getNodeValue(), if ("1".equals(noAttributeValue)) {
xmlSubRecord.getChildNodes().item(i).getChildNodes().item(j).getTextContent()); // Replace the value of "DocType" with the text content
records.put("fields", fields); 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