Chris Pollett > Students >
Padmini

    ( Print View )

    [Bio]

    [CS297Proposal]

    [Del1]

    [Del2]

    [Del3]

    [Del4]

    [CS297 Report]

    [CS298Proposal]

    [CS298Report-PDF]

    [CS298Presentation-PDF]

    [CS298Source-ZIP]

                          

























Padmini's TicTacToe Board display using XML and Java

Description: This deliverable was supposed to describe the state of TicTacToe board using the XML Language, and to parse this XML document using SAX Parser. The result is then supposed to be displayed using Java Swing.

    <!-- tboard1.xml- an XML document was designed to describe 
            the state of TicTacToe board.This document describes
            a standard 3X3 TicTacToe Board.
    -->
    <?xml version="1.0" standalone="no"?>
    <!DOCTYPE board SYSTEM "tboard1.dtd">
    <board>
	<row1>
		<column1 value="O"/>
		<column2 value="O"/>
		<column3 value="O"/>
	</row1>
	<row2>
		<column1 value="X"/>
		<column2 value="X"/>
		<column3 value="X"/>
	</row2>
	<row3>
		<column1 value="B"/>
		<column2 value="B"/>
		<column3 value="B"/>
	</row3>
    </board>

    <!-- tboard1.dtd - an XML DTD was designed for 
            validating the tboard1.xml document
    -->
    <!ELEMENT board (row1, row2, row3)>
    <!ELEMENT row1 (column1, column2, column3)>
    <!ELEMENT row2 (column1, column2, column3)>
    <!ELEMENT row3 (column1, column2, column3)>
    <!ELEMENT column1 EMPTY>
    <!ELEMENT column2 EMPTY>
    <!ELEMENT column3 EMPTY>
    <!ATTLIST column1 value (O | X | B) #REQUIRED>
    <!ATTLIST column2 value (O | X | B) #REQUIRED>
    <!ATTLIST column3 value (O | X | B) #REQUIRED>

    /** Class Name : TicTacToeParser.java
        Author     : Padmini Paladugu
        Purpose    : This class is designed to read an XML document 
          using SAXParser.It is responsible for fetching
          Attribute values from XML document and placing Attribute 
          values into a Vector.
    */

    /* Imported Packages */
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import java.io.*;
    import java.util.*;

    /** This class was designed to override the methods of Default 
        handler class and get attribute values into the Vector 
        defined in this class It is run from the command line as: 
        java TicTacToeParser tboard1.xml
        Here tboard1.xml is the filename of an XML documet.
    */
    public class TicTacToeParser extends DefaultHandler
    {

	/** Local variable to store the Attributes found in the XML document*/
	public  String  column1   = "";
	/** Local variable to store the Attributes found in the XML document*/
	public  String  column2   = "";
	/** Local variable to store the Attributes found in the XML document*/
	public  String  column3   = "";

	/** Vector to store the values of attributes*/
	public Vector values = new Vector();

	/** Override methods of the DefaultHandler class to gain notification 
            of SAX Events. 

	    Receive notification of the beginning of the XML document.
	    @exception SAXException.
	 */
	public void startDocument( ) throws SAXException
	{
	}

	/** Receive notification of the end of the document.
	    @exception SAXException.
	 */
	public void endDocument( ) throws SAXException
	{
	}

	/** Receives notification of the start of an element in the XML 
            document and Attribute values in an XML document.
	    @param namespaceURI specifies the namespace of an XML document.
	    @param localName specifies the local name of an XML document.
	    @param qName specifies the qualified name of an XML document.
	    @param attr specifies Attributes of an XML document.
	    @exception SAXException.
	 */
	public void startElement( String namespaceURI,String localName, 
           String qName,Attributes attr ) throws SAXException
	{

		/** Adding attribute values to the Vector values*/

		if( localName.equals("column1") ||
                   localName.equals("column2") || 
                   localName.equals("column3"))
		{
        	for ( int i = 0; i < attr.getLength(); i++ )
        	{
				values.add(attr.getValue(i));
		 	}
		}

	}

        /** Receives notification of the end of an element in the XML document.
	    @param namespaceURI specifies the namespace of an XML document.
	    @param localName specifies the local name of an XML document.
	    @param qName specifies the qualified name of an XMl document.
	    @exception SAXException.
	*/
	public void endElement( String namespaceURI, 
           String localName, String qName ) throws SAXException
	{

	}

        /** Receive notification of character data inside an element in the 
            XML document.

            @param ch specifies an array of characters from the XML document.
            @param start specifies the starting position in the char array.
            @param length specifies number of characters reading from the 
               char array.
            @exception SAXException.
        */
	public void characters( char[] ch, int start, int length )
           throws SAXException
	{
		try
		{
			OutputStreamWriter outw = 
                           new OutputStreamWriter(System.out);
			outw.write( ch, start,length );
			outw.flush();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

        /** Read the XML document using FileReader and parse it using the 
            parser.
	    @param argv a variable array of type String.
	 */

	public static void main( String[] argv )
	{
		try
		{
			// Create SAX 2 parser...
			XMLReader xr = 
                    XMLReaderFactory.createXMLReader(DEFAULT_SAX_DRIVER);

			// Set the ContentHandler...
			TicTacToeParser parser1 = new TicTacToeParser();
			xr.setContentHandler( parser1 );

			// Parse the file...
			xr.parse( 
                          new InputSource(new FileReader( "tboard1.xml" )));

			TicTacToeBoard tboard =
                           new TicTacToeBoard(parser1.values);
			tboard.show();

			System.out.println("Attribute Values : " + 
                           parser1.values);
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}

	}
	private static final String DEFAULT_SAX_DRIVER 	= 
           "org.apache.xerces.parsers.SAXParser";

    }

    /** Class Name : TicTacToeBoard.java
        Author     : Padmini Paladugu
        Purpose    : This class is designed to display Tic-Tac-Toe Board.
    */

    /*imported Packages*/
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.awt.*;


    /** This class was designed to displsy TicTacToeBoard using JButtons*/
    public class TicTacToeBoard extends JFrame
    {
	/**Constructor initializes the TicTacToeBoard with given parameter.*/
	public TicTacToeBoard(Vector valueTypes)
	{
	    this.getContentPane().setLayout(null);
            String buttons = valueTypes.get(0).toString();
            System.out.println(buttons);
            int k=0;
            int x=200;
            int y=100;
            for(int i=0;i< 3;i++)
            {
		for(int j=0;j<3;j++)
		{
		    JButton cij = new JButton(valueTypes.get(k++).toString());
		    cij.setBounds(x+j*51,y+i*51,50,50);
		    this.getContentPane().add(cij);
		}
	    }

	    setupEventHandlers();
            setSize(500,500);


	}

	void setupEventHandlers()
	{
	    addWindowListener(new WindowHandler());

	}
	public class WindowHandler extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
		{
		   System.exit(0);
		}
	}

    }