Powered by SmartDoc

Data-oriented application

In this section, we show how application programs can be developed using Relaxer.

First, as an example of data-oriented application, we consider a program containing an interactive menu .

We assume that this program reads an XML document list[menu.xml] and constructs a menu from it (figure figure[Execution of MyMenuApp]).

menu.xml
<?xml version="1.0"?>

<!DOCTYPE menubar SYSTEM "menu.dtd">

<menubar>
  <menu>
    <title>Edit</title>
    <item>New</item>
    <item>Open</item>
    <item>Close</item>
    <item>Exit</item>
  </menu>
  <menu>
    <title>Setup</title>
    <menu>
      <title>Display</title>
      <item>Color</item>
      <item>Font</item>
    </menu>
    <item>WWW</item>
  </menu>
  <menu>
    <title>Help</title>
    <item>MyMenuApp</item>
  </menu>
</menubar>
Execution of MyMenuApp

First, we need a RELAX module for menu documents such as list[menu.xml]. Here we can use the list[menu.rlx] module we have seen before.

In this example, we specify the factory option to generate Java classes with Relaxer. Generated classes are later combined with codes written by programmers.

C:\tmp> relaxer -factory menu.rlx

Next, on top of classes genereated with this operation, we create classes dedicated to the behaviour of this application. In Figure list[IMyMenuContent.java], such classes are MyMenuFactory,MyMenubar, MyMenu,MyItem and IMyMenuCounter (shown as "User Program") as well as MyMenuApp, which is the main routine of these classes.

IMyMenuContent.java
import javax.swing.*;

public interface IMyMenuContent {
    JMenuItem getJMenuItem();
}
MyMenuFactory.java
import org.w3c.dom.*;

public class MyMenuFactory extends AbstractMenuFactory {
    public Menubar createMenubar() {
        return (new MyMenubar());
    }

    public Menu createMenu() {
        return (new MyMenu());
    }

    public Item createItem() {
        return (new MyItem());
    }
}
MyMenubar.java
import javax.swing.*;

public class MyMenubar extends Menubar {
    public JMenuBar getJMenuBar() {
	JMenuBar jMenuBar = new JMenuBar();
	Menu[] menus = getMenu();
	for (int i = 0;i < menus.length;i++) {
	    MyMenu menu = (MyMenu)menus[i];
	    jMenuBar.add(menu.getJMenuItem());
	}
	return (jMenuBar);
    }
}
MyMenu.java
import javax.swing.*;

public class MyMenu extends Menu implements IMyMenuContent {
    public JMenuItem getJMenuItem() {
	JMenu jMenu = new JMenu(getTitle());
	IMenuContent[] contents = getContent();
	for (int i = 0;i < contents.length;i++) {
	    IMyMenuContent content = (IMyMenuContent)contents[i];
	    jMenu.add(content.getJMenuItem());
	}
	return (jMenu);
    }
}
MyItem.java
import javax.swing.*;

public class MyItem extends Item implements IMyMenuContent {
    public JMenuItem getJMenuItem() {
	return (new JMenuItem(getContent()));
    }
}
MyMenuApp.java
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class MyMenuApp {
    public static void main(String[] args) throws Exception {
	URL url = resolveURL(args[0]);
	MenuFactory.setFactory(new MyMenuFactory());
	IMenuFactory menuFactory = MenuFactory.getFactory();
	MyMenubar menubar = (MyMenubar)menuFactory.createMenubar(url);
	JMenuBar jMenuBar = menubar.getJMenuBar();
	JFrame frame = new JFrame("MyMenuApp");
	frame.addWindowListener(
	    new WindowAdapter() {
		public void windowClosing(WindowEvent evt) {
		    System.exit(0);
	        }
	    }
	);
	frame.setJMenuBar(jMenuBar);
	frame.pack();
	frame.show();
    }

    public static URL resolveURL(String name) throws MalformedURLException {
	try {
	    return (new URL(name));
	} catch (MalformedURLException e) {
	}
	return (new File(name).toURL());
    }
}

This application is also included in the sample/factory directory of the Relaxer distribution. Interested readers are encouraged to study it.