buildPostingBody static method

Future<String?> buildPostingBody(
  1. IForm form,
  2. List<IFormField>? fields, {
  3. String rootname = "FORM",
})

Implementation

static Future<String?> buildPostingBody(IForm form, List<IFormField>? fields,
    {String rootname = "FORM"}) async {
  try {
    // build xml document
    XmlDocument document = XmlDocument();
    XmlElement root =
        XmlElement(XmlName(isNullOrEmpty(rootname) ? "FORM" : rootname));
    document.children.add(root);

    if (fields != null) {
      for (var field in fields) {
        // postable?
        if (isPostable(form, field) == true) {
          if (field.values != null) {
            field.values?.forEach((value) {
              XmlElement node;
              String name = field.field ?? field.id ?? "";
              try {
                // valid element name?
                if (!isNumeric(name.substring(0, 1))) {
                  node = XmlElement(XmlName(name));
                } else {
                  node = XmlElement(XmlName("FIELD"));
                  node.attributes.add(XmlAttribute(XmlName('id'), name));
                }
              } catch (e) {
                node = XmlElement(XmlName("FIELD"));
                node.attributes.add(XmlAttribute(XmlName('id'), name));
              }

              // add field type
              if (!isNullOrEmpty(field.elementName)) {
                node.attributes
                    .add(XmlAttribute(XmlName('type'), field.elementName));
              }

              /// GeoCode for each [iFormField] which is set on answer
              if (field.geocode != null) field.geocode!.serialize(node);

              // add meta data
              if (!isNullOrEmpty(field.metaData)) {
                node.attributes
                    .add(XmlAttribute(XmlName('meta'), field.metaData));
              }

              // value
              try {
                // Xml Data
                if (field is InputModel && field.formatType == "xml") {
                  var document = XmlDocument.parse(value);
                  var e = document.rootElement;
                  document.children.remove(e);
                  node.children.add(e);
                }

                // Non-XML? Wrap in CDATA
                else if (Xml.hasIllegalCharacters(value)) {
                  node.children.add(XmlCDATA(value));
                } else {
                  node.children.add(XmlText(value));
                }
              } on XmlException catch (e) {
                node.children.add(XmlCDATA(e.message));
              }

              // Add Node
              root.children.add(node);
            });
          } else {
            // Build Element
            XmlElement node;
            String name = field.field ?? field.id ?? "";
            try {
              // Valid Element Name
              if (!isNumeric(name.substring(0, 1))) {
                node = XmlElement(XmlName(name));
              }

              // In-Valid Element Name
              else {
                node = XmlElement(XmlName("FIELD"));
                node.attributes.add(XmlAttribute(XmlName('id'), name));
              }
            } catch (e) {
              // In-Valid Element Name
              node = XmlElement(XmlName("FIELD"));
              node.attributes.add(XmlAttribute(XmlName('id'), name));
            }

            // Add Field Type
            if (!isNullOrEmpty(field.elementName)) {
              node.attributes
                  .add(XmlAttribute(XmlName('type'), field.elementName));
            }

            // Add Node
            root.children.add(node);
          }
        }
      }
    }

    // Set Body
    return document.toXmlString(pretty: true);
  } catch (e) {
    Log().error(
        "Error serializing posting document. Error is ${e.toString()}");
    return null;
  }
}