вторник, 25 июня 2013 г.

GWT: генерация XML

Генерировать xml по шаблону на стороне браузера (например, для создания xml-запроса для поиска)  удобно при помощи XTemplates из библиотеки GXT. Список возможностей, которые поддерживает XTemplates:
  • String, primitive and object value variable support.
  • Formatting object values into strings.
  • Iterative object and nested iterative object support.
  • Conditional support.
  • Ability to call other Java methods that return boolean.
  • Basic math and expression support.
  • HTML external source support.
Пример использования:

Файл шаблона get-entity-by-uri-query.xml:
<query>
<select>
<field>*</field>
</select>
<from>
<schema>{entityUri.entityType.schemaCode}</schema>
</from>
<where>
<in>
<field>INSTANCE_ID</field>
<value>{entityUri.schemaLocalId}</value>
</in>
</where>
</query>

Класс рендерера GetEntityByUriQueryTemplate.java:
public interface GetEntityByUriQueryTemplate extends XTemplates{
    GetEntityByUriQueryTemplate INSTANCE =  GWT.create(GetEntityByUriQueryTemplate.class);

    @XTemplate(source="get-entity-by-uri-query.xml")
    SafeHtml getQueryXml(EntityUri entityUri);
}

Вызов рендерера:
String queryXml=GetEntityByUriQueryTemplate.INSTANCE.getQueryXml(entityUri).asString())