gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Car Donations
Shop
Hurricane Shutters
Cell Phones
Find Software
Promote Your Website
Auto Insurance Quote
Logo Design
Promotional Golf
Prepaid Phone Card
Career Education
KVM over IP
Online Education
Memory Upgrades

 


Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Simkin: A Scripting Language for XML
By Benoît Marchal

Go to page: 1  2  Next  

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.

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Other Java Archives

Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.
Whitepaper: XML Processing in Applications--Take the Next Step
Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Whitepaper: Embeddable Content Platform for OEM's



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES