Creating a Graphical Browser

In the previous chapter, I adapted the DOM parser browser we wrote to display circles. It will be instructive to do the same here for the SAX parser browser because it will show how to retrieve specific attribute values. Here's what the document this browser, circlesSAX.java, might read—this document is called circles.xml, and as in the previous chapter, I'm specifying the (x, y) origin of each circle and the radius of the circle as attributes of the <CIRCLE> element:

<?xml version = "1.0" ?>
<!DOCTYPE DOCUMENT [
<!ELEMENT DOCUMENT (CIRCLE|ELLIPSE)*>
<!ELEMENT CIRCLE EMPTY>
<!ELEMENT ELLIPSE EMPTY>
<!ATTLIST CIRCLE
    X CDATA #IMPLIED
    Y CDATA #IMPLIED
    RADIUS CDATA #IMPLIED>
<!ATTLIST ELLIPSE
    X CDATA #IMPLIED
    Y CDATA #IMPLIED
    WIDTH CDATA #IMPLIED
    HEIGHT CDATA #IMPLIED>
]>
<DOCUMENT>
    <CIRCLE X='200' Y='160' RADIUS='50' />
    <CIRCLE X='170' Y='100' RADIUS='15' />
    <CIRCLE X='80' Y='200' RADIUS='45' />
    <CIRCLE X='200' Y='140' RADIUS='35' />
    <CIRCLE X='130' Y='240' RADIUS='25' />
    <CIRCLE X='270' Y='300' RADIUS='45' />
    <CIRCLE X='210' Y='240' RADIUS='25' />
    <CIRCLE X='60' Y='160' RADIUS='35' />
    <CIRCLE X='160' Y='260' RADIUS='55' />
</DOCUMENT>

Here the trick will be to recover the values of the attributes X, Y, and RADIUS. I'll store those values in arrays named x, y, and radius. Getting an element's attribute values is easier using the XML for Java's SAX parser than it is with the DOM parser. The startElement method is passed an object of Attributes interface, and all you have to do is use that object's getValue method, passing it the name of the attribute you're interested in:

import java.awt.*;
import java.awt.event.*;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;

public class circlesSAX extends DefaultHandler
{
    static int numberFigures = 0;
    static int x[] = new int[100];
    static int y[] = new int[100];
    static int radius[] = new int[100];
    .
    .
    .
    public void startElement(String uri, String localName,
        String rawName, Attributes attrs)
    {
        if (rawName.equals("CIRCLE")) {
            x[numberFigures] = Integer.parseInt(attrs.getValue("X"));
            y[numberFigures] = Integer.parseInt(attrs.getValue("Y"));
            radius[numberFigures] =
                Integer.parseInt(attrs.getValue("RADIUS"));
            numberFigures++;
        }
    }
    .
    .
    .
    public static void main(String args[])
    {
        displayDocument(args[0]);

        AppFrame f = new AppFrame(numberFigures, x, y, radius);

        f.setSize(400, 400);

        f.addWindowListener(new WindowAdapter() {public void
            windowClosing(WindowEvent e) {System.exit(0);}});

        f.show();
    }
}

Having stored all the circles' data, I display them in the AppFrame class as we did in the previous chapter:

class AppFrame extends Frame
{
    int numberFigures;
    int[] xValues;
    int[] yValues;
    int[] radiusValues;

    public AppFrame(int number, int[] x, int[] y, int[] radius)
    {
        numberFigures = number;
        xValues = x;
        yValues = y;
        radiusValues = radius;
    }

    public void paint(Graphics g)
    {
        for(int loopIndex = 0; loopIndex < numberFigures; loopIndex++){
            g.drawOval(xValues[loopIndex], yValues[loopIndex],
            radiusValues[loopIndex], radiusValues[loopIndex]);
        }
    }
}

And that's all it takes; you can see the results in Figure 12.4, where the browser is displaying circles.xml. The complete listing appears in Listing 12.4.

Figure 12.4. Creating a graphical XML browser using a SAX parser.


Code Listing 12.4. circlesSAX.java
import java.awt.*;
import java.awt.event.*;

import org.xml.sax.*;
import org.w3c.dom.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;

public class circlesSAX extends DefaultHandler
{
    static int numberFigures = 0;
    static int x[] = new int[100];
    static int y[] = new int[100];
    static int radius[] = new int[100];

    public static void displayDocument(String uri)
    {
        try {
            circlesSAX SAXHandler = new circlesSAX();

            SAXParser parser = new SAXParser();
            parser.setContentHandler(SAXHandler);
            parser.setErrorHandler(SAXHandler);
            parser.parse(uri);
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    public void startElement(String uri, String localName,
        String rawName, Attributes attrs)
    {
        if (rawName.equals("CIRCLE")) {
            x[numberFigures] = Integer.parseInt(attrs.getValue("X"));
            y[numberFigures] = Integer.parseInt(attrs.getValue("Y"));
            radius[numberFigures] = Integer.parseInt(attrs.getValue("RADIUS"));
            numberFigures++;
        }
    }

    public void warning(SAXParseException exception)
    {
        System.err.println("WARNING! " +
            exception.getMessage());
    }

    public void error(SAXParseException exception)
    {
        System.err.println("ERROR! " +
            exception.getMessage());
    }

    public void fatalError(SAXParseException exception)
    {
        System.err.println("FATAL ERROR! " +
            exception.getMessage());
    }

    public static void main(String args[])
    {
        displayDocument(args[0]);

        AppFrame f = new AppFrame(numberFigures, x, y, radius);

        f.setSize(400, 400);

        f.addWindowListener(new WindowAdapter() {public void
            windowClosing(WindowEvent e) {System.exit(0);}});

        f.show();
    }
}

class AppFrame extends Frame
{
    int numberFigures;
    int[] xValues;
    int[] yValues;
    int[] radiusValues;

    public AppFrame(int number, int[] x, int[] y, int[] radius)
    {
        numberFigures = number;
        xValues = x;
        yValues = y;
        radiusValues = radius;
    }

    public void paint(Graphics g)
    {
        for(int loopIndex = 0; loopIndex < numberFigures; loopIndex++){
            g.drawOval(xValues[loopIndex], yValues[loopIndex],
            radiusValues[loopIndex], radiusValues[loopIndex]);
        }
    }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset