December 19, 2014
Hot Topics:

Simkin: A Scripting Language for XML

  • October 6, 2000
  • By Benoît Marchal
  • Send Email »
  • More Articles »

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.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel