http://www.developer.com/

Back to article

Simkin: A Scripting Language for XML


October 6, 2000

It's fun to write for EarthWeb. Occasionally, I receive e-mail from developers pointing me to a new or interesting application. Recently, the creator of a new scripting language called Simkin asked me whether I'd like to examine his creation. Simkin is an intriguing idea that mixes XML with a scripting language.

Scripts and XML

There are already scripting languages for XML, most notably XSLT — the transformation language developed by the W3C. What sets Simkin apart from other offerings is that it embeds scripts within an XML document.

What for? I found that the mixture of XML tags and scripts is ideal as an intelligent configuration file format. In fact, that was the genesis of Simkin. Simon Whiteside, its developer, explains: "The language grew out of configuration files for an adventure game. The parameters in the file were getting so complicated, I started by adding an expression syntax and moved onto a fully blown language syntax."

Simkin has also been used as an API for plug-ins. Sibelius, a music processor, is an example of the latter. As we will see, a Java developer can expose functions and variables to Simkin. Plug-in developers can call functions from Simkin.

Your First Script

Let's test the configuration capability of Simkin with a simple application to process text. Figure 1 is the application. The interface is simple: a text field and a button. A configuration file controls the size of the window, the title, and the button label, as well as (and this is the Simkin novelty) what happens when the user presses the button. All these parameters, including the script, are in an XML file.

Figure 1. A scriptable window.

Listing 1 is the Simkin script. The first four XML elements (<Title>, <Label>, <Width> and <Height>) control the appearance of the window as we described. The last one (<Action>) is a tiny script that gets executed when the user presses the button.

<xmp>
<?xml version="1.0"?>
<Frame>
   <Title>Simkin Example for Gamelan</Title>
   <Label>Reverse</Label>
   <Width>330</Width>
   <Height>60</Height>
   <Action>()
   {
      input = reverse(input);
   }
   </Action>
</Frame>
</xmp>

Listing 1: frame.xml.

Note that, like XML, Simkin is extensible, and it does not enforce the element names. As you will see in the next section, it is up to the application programmer to decide on the structure of the XML document and on the scripts associated with it. You could even use Simkin in windowless applications.

Linking the Script in Your Application

Simkin comes with a simple API to embed scripts in any Java application. The API main class is XMLExecutable, from which your application will derive. Listing 2 is the Java code for the text manipulation application.

import simkin.*;
import java.io.*;
import java.awt.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.awt.event.*;

public class TextManipulator
   extends XMLExecutable
   implements ActionListener
{
   protected Frame frame;
   protected TextComponent input;

   public TextManipulator()
      throws SAXException, IOException
   {
      super(new File("frame.xml"));
      frame = new Frame(fromXML("Title"));
      int width = Integer.parseInt(fromXML("Width")),
          height = Integer.parseInt(fromXML("Height"));
      frame.setSize(width,height);
      frame.setResizable(false);
      input = new TextField(30);
      Button button = new Button(fromXML("Label"));
      frame.setLayout(new FlowLayout());
      frame.add(input);
      frame.add(button);
      button.addActionListener(this);
      frame.addWindowListener(new WindowAdapter()
      {
         public void windowClosing(WindowEvent e)
         {
            System.exit(0);
         }
      });
      frame.show();
   }

   public void actionPerformed(ActionEvent e)
   {
      try
      {
         method("Action",new RValueArray(),new RValue());
      }
      catch(Exception x)
      {
         Tracer.trace(x);
      }
   }

   public void show()
   {
      frame.show();
   }

   String fromXML(String fieldName)
   {
      Element child =
         XMLElementObject.findChild(getCode(),fieldName);
      if(child != null)
         return XMLElementObject.getData(child);
      else
         return new String();
   }

   public boolean getValue(String name,RValue value)
   {
      if(name.equals("input"))
      {
         value.str(input.getText());
         return true;
      }
      else
         return super.getValue(name,value);
   }

   public boolean setValue(String name,RValue value)
   {
      if(name.equals("input"))
      {
         input.setText(value.str());
         return true;
      }
      else
         return super.getValue(name,value);
   }

   public boolean method(String name,
                         RValueArray arguments,
                         RValue result)
      throws ParseException, RuntimeException
   {
      if(name.equals("upper") && arguments != null)
      {
         result.str(arguments.at(0).str().toUpperCase());
         return true;
      }
      if(name.equals("lower") && arguments != null)
      {
         result.str(arguments.at(0).str().toLowerCase());
         return true;
      }
      if(name.equals("reverse") && arguments != null)
      {
         StringBuffer arg =
            new StringBuffer(arguments.at(0).str());
         result.str(arg.reverse().toString());
         return true;
      }
      else
         return super.method(name,arguments,result);
   }

   public final static void main(String[] args)
      throws SAXException, IOException
   {
      XMLExecutable.setInterpreter(new Interpreter());
      TextManipulator tm = new TextManipulator();
      tm.show();
   }
}

Listing 2: TextManipulator.java.

Let's go through Listing 2 one step at a time. It defines one class, TextManipulator, which inherits from XMLExecutable. The constructor first calls its ancestor passing the XML file. Next, it creates an AWT frame. Notice that it reads the parameters (title, size, and button label) from the XML file through the fromXML() method.

fromXML() uses the Simkin-provided XMLElementObject. The XMLElement object is a utility class offering various services to query the XML document.

TextManipulator also implements ActionListener and the actionPerformed() method is called when the user presses the button. ActionListener calls the Action script in Listing 1 through Simkin's method(). method() lets a Java application defer processing to Simkin.

method() expects the method name, an array of parameters, and a return value. For the parameters and return values, Simkin has two classes. RValue is used to pass parameters back and forth between Simkin and Java. It defines setters and getters for the most common types: string, integer, boolean, and more. As the name implies, RValueArray is an array of RValue.

The show() method should be obvious. It is followed by getValue() and setValue(), two methods inherited from XMLExecutable. getValue() and setValue() expose Java variables to Simkin. Predictably, they take the variable name and an RValue. When overloading from getValue() or setValue(), it is important to call the ancestor's method so built-in variables are also available to the script. TextManipulator defines one variable, input, for the input field.

Next comes the method()... method. method() is also inherited from XMLExecutable. method() defines new Simkin functions. Through method(), the application can expose some of its internal functions to scripts. TextManipulator offers three functions to manipulate strings: upper(), lower(), and reverse().

The last method is main(), the program entry point. main() first sets a global Simkin Interpreter object. The interpreter is responsible for executing the Simkin scripts.

Building the Project

To build this project, you need to download and install two packages:

Make sure you add both packages to your classpath. You need to add three JAR files (one for Simkin and two for JAXP).

Copy Listings 1 and 2 in two files, called respectively frame.xml and TextManipulator.java. Compile TextManipulator.java. Make sure frame.xml is in the current directory and run TextManipulator.

Benefits

The combination of XML data and Simkin scripts makes an attractive configuration format. Furthermore, because you define Simkin functions and variables, you control what the script does.

It is therefore easy to reconfigure the application. Listing 3 illustrates this with a different configuration file that changes not only the window presentation but also what the application does! This script is also more sophisticated, illustrating the use of a variable (current) and the if statement.

<?xml version="1.0"?>
<Frame>
   <Title>Sophisticated text manipulation</Title>
   <Label>Do it</Label>
   <Width>310</Width>
   <Height>60</Height>
   <Action>()
   {
      current = input;
      if(current = lower(current))
      {
         input = upper(current);
      }
      else
      {
         input = lower(current);
      }
   }
   </Action>
</Frame>

Listing 3.

Conclusion

Simkin is an attractive idea: It combines a simple scripting language with XML. Use it to create more flexible applications that can be reconfigured, or extended, through XML files.

I do wish the developer would clean up the API. Defining new Simkin methods or variables requires a long list of if statements which are not so readable. The documentation also needs some work.

However, even as it stands, Simkin is useful. Adding a scripting language to an application opens the door to user enhancements. The combination of scripting and XML is an attractive one.

About the Author

Benoît Marchal is the author of XML by Example and Applied XML Solutions. His Web site is http://www.marchal.com.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date