/**
   * Encode DFieldTemplate object into XML.
   *
   * @param dFieldTemplate Object to encode.
   * @param parent Parent of object.
   * @return Encoded element.
   * @throws DFieldTemplatesException Unable to encode object.
   */
  public static org.w3c.dom.Element encode(
      final DFieldTemplate dFieldTemplate, final org.w3c.dom.Element parent)
      throws DFieldTemplatesException {

    if (parent == null) {
      throw new DFieldTemplatesException("parameter must not be null [parent]");
    }

    if (dFieldTemplate == null) {
      throw new DFieldTemplatesException("parameter must not be null [dFieldTemplate]");
    }
    final org.w3c.dom.Document doc = parent.getOwnerDocument();
    final org.w3c.dom.Element el = doc.createElementNS(DFieldTemplatesCodec.NS, "FieldTemplate");

    // set custom attributes
    el.setAttribute("Name", dFieldTemplate.getName());

    // create elements
    Element elTemp = null;
    elTemp = doc.createElementNS(DFieldTemplatesCodec.NS, "Description");
    elTemp.appendChild(doc.createCDATASection(dFieldTemplate.getDescription()));
    el.appendChild(elTemp);

    DSelectorCodec.encode(dFieldTemplate.getSelector(), el);

    try {
      final Plugin plugin = PluginFactory.getPlugin();
      plugin.getAdvSearchAccess().encodeTerm(dFieldTemplate.getTerm(), el);
    } catch (AdvSearchException e) {
      throw new DFieldTemplatesException(e.getMessage());
    }

    // insert into dom tree
    parent.appendChild(el);
    return el;
  }