/**
     *
     * Returns a section as a complete htmlform, including the three required encounter tags.
     *
     * @param htmlForm
     * @param sectionIndex
     * @return
     * @throws Exception
     */
    public static String getSectionAsFormXml(HtmlForm htmlForm, Integer sectionIndex) throws Exception{
        Document doc = HtmlFormEntryUtil.stringToDocument(htmlForm.getXmlData());
        NodeList nl = doc.getElementsByTagName("section");
        Node sectionNode = null;
        try {
            sectionNode = nl.item(sectionIndex);
        } catch (Exception ex){
            throw new RuntimeException("The section index that you've passed in is out of range.  There are only " + nl.getLength() + " section tags in the document and you requested section tag " + sectionIndex);
        }
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc2 = db.newDocument();
        Node formRoot = doc2.createElement("htmlform");
        doc2.appendChild(formRoot);
        formRoot.appendChild(doc2.importNode(sectionNode, true));
        if (doc2.getElementsByTagName("encounterLocation").getLength() == 0){
            Node encLoc = doc2.createElement("encounterLocation");
            formRoot.appendChild(encLoc);
        }
        if (doc2.getElementsByTagName("encounterDate").getLength() == 0){
            Node encDate = doc2.createElement("encounterDate");
            formRoot.appendChild(encDate);
        }
        if (doc2.getElementsByTagName("encounterProvider").getLength() == 0){
            Node encDate = doc2.createElement("encounterProvider");
            Element encDateElement = (Element) encDate;
            encDateElement.setAttribute("role","Provider");
            formRoot.appendChild(encDate);

        }
        doc2.normalize();

        try
        {
           DOMSource domSource = new DOMSource(doc2);
           StringWriter writer = new StringWriter();
           StreamResult result = new StreamResult(writer);
           TransformerFactory tf = TransformerFactory.newInstance();
           Transformer transformer = tf.newTransformer();
           transformer.transform(domSource, result);
           return writer.toString();
        }
        catch(TransformerException ex)
        {
           ex.printStackTrace();
           return null;
        }
    }
 /**
  * returns a Map<Integer, String> of all form sections, in order,
  * where the String value is either the specified name, or an arbitrary one if none was given.
  * and the integer is the numeric index of the sections, starting with 0.
  *
  * @param HtmlForm htmlForm
  * @return
  */
 public static Map<Integer, String> getSectionIndex(HtmlForm htmlForm) throws Exception{
     Document doc = HtmlFormEntryUtil.stringToDocument(htmlForm.getXmlData());
     FormEntrySession session = new FormEntrySession(HtmlFormEntryUtil.getFakePerson(), htmlForm, null); // session gets a null HttpSession
     NodeList nl = doc.getElementsByTagName("section");
     Map<Integer, String> ret = new LinkedHashMap<Integer, String>();
     for (int i = 0; i < nl.getLength(); i++){
         Node sectionNode = nl.item(i);
         NamedNodeMap map = sectionNode.getAttributes();
         String headerLabel = "no name specified";
         for (int j = 0; j < map.getLength(); ++j) {
             Node attribute = map.item(j);
             if (attribute.getNodeName().equals("headerLabel")) {
                 headerLabel = attribute.getNodeValue();
             }
             if (attribute.getNodeName().equals("headerCode")) {
                 Translator trans = session.getContext().getTranslator();
                 headerLabel = trans.translate(Context.getLocale().toString(), attribute.getNodeValue());
             }
         }
         ret.put(i, headerLabel);
     }
     return ret;
 }