Netdesign Manual

Part 4

Java XML Programmers Reference

Chapter 11: XML Tools for Information Appliances


October 15, 2001


Brought to you by:





Check It Out!

Writing the XML Document

The final step is to ask handle the yes/no button click. If yes is pressed, we must open the database and write the contact (received XML) as a new record. If no is pressed, we repaint the screen with the "Waiting for contact information…" message. As we've already seen, button clicks are handled by overriding the com.sun.kjava.Spotlet.penDown() event handler. If the exit button has been pressed, we register our "owner" — the ContactBook object — as the current Spotlet event listener (remember, there is only one Spotlet listening for events at a time), and tell ContactBook to paint itself. This effectively hands control back to the main menu. The owner is set in the ReceiveContact constructor:


import com.sun.kjava.*;
import nanoxml.kXMLElement;
import nanoxml.XMLParseException;

public class ReceiveContact extends Spotlet {

  private Button exitBtn = new Button("Quit", 135, 140),
    yesBtn, noBtn;
  private Graphics g = Graphics.getGraphics();
  private ContactBook owner;
  private String contact;

  public ReceiveContact(ContactBook owner) {
    register(NO_EVENT_OPTIONS);
    this.owner = owner;
    paint(); //paint the "Waiting for..." message
}

  public void penDown(int x, int y) {
    //did the user press the exit btn?
    if (exitBtn.pressed(x,y)) {
      owner.register(NO_EVENT_OPTIONS);
      owner.paint();
    }
    //did the user press the yes btn, telling us to
    //write the received contact into the database?
    else if (yesBtn.pressed(x,y))     {
      Database db = new Database(ContactBook.dbType,
        ContactBook.creatorId, Database.WRITEONLY);
      if (!db.isOpen()){ 
        //the database doesn't exist yet. create it.
        Database.create(0, ContactBook.dbName,
          ContactBook.creatorId, ContactBook.dbType, false);
        db = new Database(ContactBook.dbType,
          ContactBook.creatorId, Database.WRITEONLY);
      }
      db.addRecord(contact.getBytes());
      db.close();
      paint(); //repaint the "Waiting for..." message
    }
    else if (noBtn.pressed(x,y))
      paint();
  }

  public void beamReceive(byte[] buf) {
    //parse the received xml using NanoXML

    contact = new String(buf);
    kXMLElement elem = new kXMLElement();
    try     {
      elem.parseString(contact);
    }
    catch (XMLParseException e)     {
      //can't parse received data--the sender probably
      //wasn't BeamContact!! ignore it and return.
      paint();
    
 
      return;
    }
    String fname = elem.getProperty("fname"), 
      lname = elem.getProperty("lname");
    if (fname != null && lname != null)     {
      g.clearScreen();
      g.drawString("Contact info was received for:", 0, 20);
      g.drawString(fname + " " + lname, 0, 40);
      g.drawString("Import into contact book?", 0, 60);
      yesBtn = new Button("Yes", 40, 80);
      noBtn = new Button("No", 60, 80);
      yesBtn.paint();
      noBtn.paint();
    }
    else
      paint();
  }

  public void paint() {
    g.clearScreen();
    yesBtn = noBtn = null;
    exitBtn.paint();
    g.drawString("Waiting for contact information...", 10,
      70);
  }
}

Summary

In this chapter, we examined Java and XML on lightweight clients, also known as information appliances. We talked about where XML on lightweight clients stands today, and why we need XML on lightweights.

Then we discussed the architecture of Java 2 Micro Edition: the application layer, the device profile layer, the configuration layer, and finally the operating system layer. Focusing more on the Connected Limited Device Configuration, as it is more "lightweight" than the Connected Device Configuration, we discussed the Java Kilobyte Virtual Machine, kjava, and kAWT.

Next we reviewed parsers for lightweights. Pull parsers such as kXML and XPP hold a lot of potential for information appliances, but since there is no industry-accepted standard interface, we focused on DOM-style and SAX parsers. We covered two very different versions of NanoXML in-depth, as well as NanoXML's SAX interface. We also discussed MinML with its SAX interface.

XSLT Compiler, by Sun Microsystems, was covered in detail. We wrote an XSL stylesheet, compiled a translet, and executed a small TroubleTicket application that used the translet with an input XML document. XSLTC's speedy execution and small size make it ideal for performing XSL transformations on information appliances.

SOAP on information appliances is very appealing, but library support is limited currently. It might be best to write your own library if you can't wait until kSOAP or another small library matures.

Finally, we built a peer-to-peer contact book application for the Palm OS using NanoXML, Java KVM, the Palm OS Emulator, and some GUI classes made available by Sun. This infrared beaming application prompted the user with a list of contact book entries to beam, while a receiving application waited for an XML document to insert into its contact book database.


PAGE: 1 | 2 | 3 | 4 | 5 | 6 | 7 | FIRST PAGE
 

Research and Reports

Storage Virtualization Guide
May 2012

Network Computing: May 2012

TechWeb Careers