April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Simkin: A Scripting Language for XML, Page 2

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

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.





Page 2 of 2



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel