@Test public void testToSMSMessage() throws Exception{ System.out.println("toSMSMessage"); String xml = "<?xml version='1.0' encoding='UTF-8' ?><patientreg description-template=\"${/patientreg/lastname}$ in ${/patientreg/continent}$\" id=\"1\" name=\"Patient Registration\" xmlns:xf=\"http: String expResult = "Type=patientreg\npatientid=123\ntitle=mrs\nfirstname=Test\nlastname=Test\nsex=female\nbirthdate=03/06/1990\nweight=40\nheight=20\npregnant=false\ncontinent=africa\ncountry=uganda\ndistrict=mbale"; String result = ((IncomingMessageXMLParserImpl)xmlParser).toSMSMessage(xml); assertEquals(result, expResult); }
public String toSMSMessage(String xml) throws JDOMException, IOException, MotechParseException { String result = ""; InputStream in = new ByteArrayInputStream(xml.getBytes()); SAXBuilder saxb = new SAXBuilder(); Document doc = null; doc = saxb.build(in); Element root = doc.getRootElement(); Element formTypeElement = getXmlUtil().getElement(doc, formTypeTagName); String formType = formTypeElement == null ? null : formTypeElement.getText(); if (formType == null || "".equals(formType.trim())) { String error = "Empty or No form type defined in xml with root element: " + root.getName() + " and id: " + root.getAttributeValue("id"); log.error(error); throw new MotechParseException(error); } String formTypeFieldName = formTypeLookup.get(formType); if (formTypeFieldName == null || formTypeFieldName.trim().equals("")) { String error = "Could not find a valid (non-null-or-white-space) form type field name associated with form type: " + formType; log.error(error); throw new MotechParseException(error); } Element formNameElement = xmlUtil.getElement(doc, formNameTagName); if (formNameElement == null) { throw new MotechParseException("No element (representing the Form Name) found by name " + formNameTagName); } result += formTypeFieldName + getSeparator() + formNameElement.getText(); List children = root.getChildren(); for (Object o : children) { Element child = (Element) o; if (!(child.getName().equalsIgnoreCase(formTypeTagName) || child.getName().equalsIgnoreCase(formNameTagName))) { result += getDelimiter() + child.getName() + getSeparator(); String text = child.getText(); Pattern p = Pattern.compile(oxdDateRegex); Matcher m = p.matcher(child.getText()); if (m.matches()) { try { SimpleDateFormat sdf = new SimpleDateFormat(oxdDateFormat); Date date = sdf.parse(child.getText()); sdf = new SimpleDateFormat(impDateFormat); text = sdf.format(date); } catch (ParseException ex) { log.error("Error changing date format", ex); } } result += text; } } return result; }
IncomingMessageXMLParserImpl implements IncomingMessageXMLParser { public String toSMSMessage(String xml) throws JDOMException, IOException, MotechParseException { String result = ""; InputStream in = new ByteArrayInputStream(xml.getBytes()); SAXBuilder saxb = new SAXBuilder(); Document doc = null; doc = saxb.build(in); Element root = doc.getRootElement(); Element formTypeElement = getXmlUtil().getElement(doc, formTypeTagName); String formType = formTypeElement == null ? null : formTypeElement.getText(); if (formType == null || "".equals(formType.trim())) { String error = "Empty or No form type defined in xml with root element: " + root.getName() + " and id: " + root.getAttributeValue("id"); log.error(error); throw new MotechParseException(error); } String formTypeFieldName = formTypeLookup.get(formType); if (formTypeFieldName == null || formTypeFieldName.trim().equals("")) { String error = "Could not find a valid (non-null-or-white-space) form type field name associated with form type: " + formType; log.error(error); throw new MotechParseException(error); } Element formNameElement = xmlUtil.getElement(doc, formNameTagName); if (formNameElement == null) { throw new MotechParseException("No element (representing the Form Name) found by name " + formNameTagName); } result += formTypeFieldName + getSeparator() + formNameElement.getText(); List children = root.getChildren(); for (Object o : children) { Element child = (Element) o; if (!(child.getName().equalsIgnoreCase(formTypeTagName) || child.getName().equalsIgnoreCase(formNameTagName))) { result += getDelimiter() + child.getName() + getSeparator(); String text = child.getText(); Pattern p = Pattern.compile(oxdDateRegex); Matcher m = p.matcher(child.getText()); if (m.matches()) { try { SimpleDateFormat sdf = new SimpleDateFormat(oxdDateFormat); Date date = sdf.parse(child.getText()); sdf = new SimpleDateFormat(impDateFormat); text = sdf.format(date); } catch (ParseException ex) { log.error("Error changing date format", ex); } } result += text; } } return result; } }
IncomingMessageXMLParserImpl implements IncomingMessageXMLParser { public String toSMSMessage(String xml) throws JDOMException, IOException, MotechParseException { String result = ""; InputStream in = new ByteArrayInputStream(xml.getBytes()); SAXBuilder saxb = new SAXBuilder(); Document doc = null; doc = saxb.build(in); Element root = doc.getRootElement(); Element formTypeElement = getXmlUtil().getElement(doc, formTypeTagName); String formType = formTypeElement == null ? null : formTypeElement.getText(); if (formType == null || "".equals(formType.trim())) { String error = "Empty or No form type defined in xml with root element: " + root.getName() + " and id: " + root.getAttributeValue("id"); log.error(error); throw new MotechParseException(error); } String formTypeFieldName = formTypeLookup.get(formType); if (formTypeFieldName == null || formTypeFieldName.trim().equals("")) { String error = "Could not find a valid (non-null-or-white-space) form type field name associated with form type: " + formType; log.error(error); throw new MotechParseException(error); } Element formNameElement = xmlUtil.getElement(doc, formNameTagName); if (formNameElement == null) { throw new MotechParseException("No element (representing the Form Name) found by name " + formNameTagName); } result += formTypeFieldName + getSeparator() + formNameElement.getText(); List children = root.getChildren(); for (Object o : children) { Element child = (Element) o; if (!(child.getName().equalsIgnoreCase(formTypeTagName) || child.getName().equalsIgnoreCase(formNameTagName))) { result += getDelimiter() + child.getName() + getSeparator(); String text = child.getText(); Pattern p = Pattern.compile(oxdDateRegex); Matcher m = p.matcher(child.getText()); if (m.matches()) { try { SimpleDateFormat sdf = new SimpleDateFormat(oxdDateFormat); Date date = sdf.parse(child.getText()); sdf = new SimpleDateFormat(impDateFormat); text = sdf.format(date); } catch (ParseException ex) { log.error("Error changing date format", ex); } } result += text; } } return result; }  }
IncomingMessageXMLParserImpl implements IncomingMessageXMLParser { public String toSMSMessage(String xml) throws JDOMException, IOException, MotechParseException { String result = ""; InputStream in = new ByteArrayInputStream(xml.getBytes()); SAXBuilder saxb = new SAXBuilder(); Document doc = null; doc = saxb.build(in); Element root = doc.getRootElement(); Element formTypeElement = getXmlUtil().getElement(doc, formTypeTagName); String formType = formTypeElement == null ? null : formTypeElement.getText(); if (formType == null || "".equals(formType.trim())) { String error = "Empty or No form type defined in xml with root element: " + root.getName() + " and id: " + root.getAttributeValue("id"); log.error(error); throw new MotechParseException(error); } String formTypeFieldName = formTypeLookup.get(formType); if (formTypeFieldName == null || formTypeFieldName.trim().equals("")) { String error = "Could not find a valid (non-null-or-white-space) form type field name associated with form type: " + formType; log.error(error); throw new MotechParseException(error); } Element formNameElement = xmlUtil.getElement(doc, formNameTagName); if (formNameElement == null) { throw new MotechParseException("No element (representing the Form Name) found by name " + formNameTagName); } result += formTypeFieldName + getSeparator() + formNameElement.getText(); List children = root.getChildren(); for (Object o : children) { Element child = (Element) o; if (!(child.getName().equalsIgnoreCase(formTypeTagName) || child.getName().equalsIgnoreCase(formNameTagName))) { result += getDelimiter() + child.getName() + getSeparator(); String text = child.getText(); Pattern p = Pattern.compile(oxdDateRegex); Matcher m = p.matcher(child.getText()); if (m.matches()) { try { SimpleDateFormat sdf = new SimpleDateFormat(oxdDateFormat); Date date = sdf.parse(child.getText()); sdf = new SimpleDateFormat(impDateFormat); text = sdf.format(date); } catch (ParseException ex) { log.error("Error changing date format", ex); } } result += text; } } return result; }  ArrayList<String> parseXML(ArrayList<String> xmls); String toSMSMessage(String xml); CoreManager getCoreManager(); void setCoreManager(CoreManager coreManager); IncomingMessageParser getMessageParser(); void setMessageParser(IncomingMessageParser messageParser); String getSeparator(); void setSeparator(String separator); String getDelimiter(); void setDelimiter(String delimiter); String getFormTypeTagName(); void setFormTypeTagName(String formTypeTagName); Map<String, String> getFormTypeLookup(); void setFormTypeLookup(Map<String, String> formTypeLookup); XMLUtil getXmlUtil(); void setXmlUtil(XMLUtil xmlUtil); String getFormNameTagName(); void setFormNameTagName(String formNameTagName); void setOxdDateFormat(String oxdDateFormat); void setOxdDateRegex(String oxdDateRegex); void setImpDateFormat(String impDateFormat); }
IncomingMessageXMLParserImpl implements IncomingMessageXMLParser { public String toSMSMessage(String xml) throws JDOMException, IOException, MotechParseException { String result = ""; InputStream in = new ByteArrayInputStream(xml.getBytes()); SAXBuilder saxb = new SAXBuilder(); Document doc = null; doc = saxb.build(in); Element root = doc.getRootElement(); Element formTypeElement = getXmlUtil().getElement(doc, formTypeTagName); String formType = formTypeElement == null ? null : formTypeElement.getText(); if (formType == null || "".equals(formType.trim())) { String error = "Empty or No form type defined in xml with root element: " + root.getName() + " and id: " + root.getAttributeValue("id"); log.error(error); throw new MotechParseException(error); } String formTypeFieldName = formTypeLookup.get(formType); if (formTypeFieldName == null || formTypeFieldName.trim().equals("")) { String error = "Could not find a valid (non-null-or-white-space) form type field name associated with form type: " + formType; log.error(error); throw new MotechParseException(error); } Element formNameElement = xmlUtil.getElement(doc, formNameTagName); if (formNameElement == null) { throw new MotechParseException("No element (representing the Form Name) found by name " + formNameTagName); } result += formTypeFieldName + getSeparator() + formNameElement.getText(); List children = root.getChildren(); for (Object o : children) { Element child = (Element) o; if (!(child.getName().equalsIgnoreCase(formTypeTagName) || child.getName().equalsIgnoreCase(formNameTagName))) { result += getDelimiter() + child.getName() + getSeparator(); String text = child.getText(); Pattern p = Pattern.compile(oxdDateRegex); Matcher m = p.matcher(child.getText()); if (m.matches()) { try { SimpleDateFormat sdf = new SimpleDateFormat(oxdDateFormat); Date date = sdf.parse(child.getText()); sdf = new SimpleDateFormat(impDateFormat); text = sdf.format(date); } catch (ParseException ex) { log.error("Error changing date format", ex); } } result += text; } } return result; }  ArrayList<String> parseXML(ArrayList<String> xmls); String toSMSMessage(String xml); CoreManager getCoreManager(); void setCoreManager(CoreManager coreManager); IncomingMessageParser getMessageParser(); void setMessageParser(IncomingMessageParser messageParser); String getSeparator(); void setSeparator(String separator); String getDelimiter(); void setDelimiter(String delimiter); String getFormTypeTagName(); void setFormTypeTagName(String formTypeTagName); Map<String, String> getFormTypeLookup(); void setFormTypeLookup(Map<String, String> formTypeLookup); XMLUtil getXmlUtil(); void setXmlUtil(XMLUtil xmlUtil); String getFormNameTagName(); void setFormNameTagName(String formNameTagName); void setOxdDateFormat(String oxdDateFormat); void setOxdDateRegex(String oxdDateRegex); void setImpDateFormat(String impDateFormat);  }