Generator

A generator is a server-side component written in Java which produces an XML response from a content request. Berlioz libraries provide a number of built-in generators for common use cases.

 It must follow the ContentGenerator Java interface.

public interface ContentGenerator {

  void process(ContentRequest req, XMLWriter xml)
    throws BerliozException, IOException;

}

Generators can have other features, e. g. cacheable.

Usage

In order for a generator to be invoked, it must be part of a service. The same generator can be used in multiple services.

In the context of a service, a generator can be given a name and a target to make it easier for XSLT templates to match.

For example:

<service id="get-user" method="get">
  <url pattern="/user/{username}"/>
  <generator name="user" target="main" class="org.example.GetUser"/>
</service>

Examples

Generators can be used to return any content.

Note

The examples below are only used to demonstrate how to write simple generators, they are not meant to be used verbatim in a production environment where other aspects such as caching and error handling should be considered.

Current user

A generator could return the user currently in the session. This kind of generator would be included in most services in the application.

public void process(ContentRequest req, XMLWriter xml) throws IOException {
  HttpSession session = req.getSession();
  User user = (user)session.getAttribute("org.example.User");
  if (user != null) {
    user.toXML(xml);
  }
}

PSML document

A generator could also be used to return an XML or PSML document on the server.

public void process(ContentRequest req, XMLWriter xml) throws IOException {
  String path = req.getBerliozPath();
  File file = new File(PSML_ROOT, path+".psml");
  if (file.exists()) {
    XMLCopy.copyTo(file, xml);
  }
}

Created on , last edited on