public static Document createItemListDocument(Collection<Item> list) {

    try {

      DocumentBuilder documentBuilder = XmlUtil.getDocumentBuilder();
      Document document = documentBuilder.newDocument();

      Element localeElement = document.createElement(items);

      Element itemElem = document.createElement(item_node);
      Element itemLabelElem = document.createElement(item_node_label);
      Element itemValueElem = document.createElement(item_value_label);

      Node itemNode;

      for (Item item : list) {

        itemLabelElem.setTextContent(item.getItemLabel());
        itemValueElem.setTextContent(item.getItemValue());

        itemElem.appendChild(itemLabelElem);
        itemElem.appendChild(itemValueElem);

        itemNode = localeElement.getOwnerDocument().importNode(itemElem, true);
        localeElement.appendChild(itemNode);
      }
      document.appendChild(localeElement);

      return document;

    } catch (ParserConfigurationException e) {
      // TODO: handle exception
    }
    return null;
  }
  public void setFormSourceCode(String newSourceCode) throws Exception {

    if (builder == null) builder = XmlUtil.getDocumentBuilder();

    clear();

    ByteArrayInputStream bais = new ByteArrayInputStream(newSourceCode.getBytes("UTF-8"));
    Document newXForm = builder.parse(new InputSource(bais));
    setXformsDocument(newXForm);
    loadDocument();
  }
  public static Document createDocumentFromBeanProperties(Object bean) {

    try {

      final DocumentBuilder documentBuilder = XmlUtil.getDocumentBuilder();
      final Document document = documentBuilder.newDocument();

      final Element rootElement = (Element) document.appendChild(document.createElement("result"));

      final Field[] fields = bean.getClass().getDeclaredFields();

      for (Field field : fields) {

        try {

          final Object val = BeanUtils.getProperty(bean, field.getName());

          final Element el = document.createElement(field.getName());
          rootElement.appendChild(el);

          if (val != null) {

            el.setTextContent(String.valueOf(val));
          }

        } catch (Exception e) {
          // skipping field
          Logger.getLogger(ExtensionFunctionUtil.class.getName())
              .log(
                  Level.SEVERE,
                  "Error while parsing bean property, and setting it to resulting document. Skipping.",
                  e);
        }
      }

      return document;

    } catch (ParserConfigurationException e) {
      Logger.getLogger(ExtensionFunctionUtil.class.getName()).log(Level.SEVERE, "Err", e);
    }
    return null;
  }
  /**
   * Creates {@link org.w3.dom.Document} with structure
   * <\tableRow><\ColumName>Value<\/ColumName><\/tableRow>. ColumnName's are parsed from {@link
   * com.idega.util.text.TableRecord#getItems()}.
   *
   * @param list {@link Collection} of {@link com.idega.util.text.TableRecord}.
   * @return {@link org.w3.dom.Document} or null if error happen.
   */
  public static Document createTableDocument(Collection<TableRecord> list) {
    if (ListUtil.isEmpty(list)) {
      return null;
    }

    DocumentBuilder documentBuilder = null;
    try {
      documentBuilder = XmlUtil.getDocumentBuilder();
    } catch (ParserConfigurationException e) {
      LOGGER.log(Level.WARNING, "Unable to create table document.", e);
      return null;
    }

    if (documentBuilder == null) {
      return null;
    }

    Document document = documentBuilder.newDocument();
    if (document == null) {
      return null;
    }

    Element localeElement = document.createElement(items);
    if (localeElement == null) {
      return null;
    }

    for (TableRecord tableRecord : list) {
      Element itemElem = document.createElement(tableRow);
      if (itemElem == null) {
        continue;
      }

      Map<String, String> data = tableRecord.getItems();
      if (MapUtil.isEmpty(data)) {
        continue;
      }

      for (Iterator<Map.Entry<String, String>> iter = data.entrySet().iterator();
          iter.hasNext(); ) {
        Map.Entry<String, String> entry = iter.next();
        if (entry == null) {
          continue;
        }

        String key = entry.getKey();
        String value = entry.getValue();
        if (StringUtil.isEmpty(key) || StringUtil.isEmpty(value)) {
          continue;
        }

        Element itemValueElem = document.createElement(key);
        if (itemValueElem == null) {
          continue;
        }

        itemValueElem.setTextContent(value);
        itemElem.appendChild(itemValueElem);
      }

      Document documentOfItemNode = localeElement.getOwnerDocument();
      if (documentOfItemNode == null) {
        continue;
      }

      Node itemNode = documentOfItemNode.importNode(itemElem, true);
      if (itemNode == null) {
        continue;
      }

      localeElement.appendChild(itemNode);
    }

    document.appendChild(localeElement);
    return document;
  }