1.2 XML Processing

<svg xmlns="http://www.w3.org/2000/svg" >
  <circle cx="50" cy="50" r="40" fill="yellow"/>
  <rect   x="50"  y="50"  width="40" height="40" fill="green"/>
  <rect   x="10"  y="10"  width="40" height="40" fill="green"/>
</svg>

<..svg into java graphics..>
 if( QName.equals( "rect" ) ){
     g.setColor( Color.getColor( atts.getValue("fill") ));
     g.fillRect(
       Integer.getInteger( atts.getValue( "x"    )).intValue(),
       Integer.getInteger( atts.getValue( "y"    )).intValue(),
       Integer.getInteger( atts.getValue("width" )).intValue(),
       Integer.getInteger( atts.getValue("height")).intValue()
     );
 } else if( QName.equals( "circle" ) ){
     g.setColor( Color.getColor( atts.getValue("fill") ));
     int r = Integer.getInteger( atts.getValue("r")).intValue();
     g.fillRect(
       Integer.getInteger( atts.getValue( "cx" )).intValue() - r,
       Integer.getInteger( atts.getValue( "cy" )).intValue() - r,
       2*r, 2*r
     );
 }
-_-_-

<..MySVGBrowser.java..>
 import javax.xml.parsers.*;
 import org.xml.sax.XMLReader;
 import java.io.File;
 import org.xml.sax.Attributes;
 import org.xml.sax.helpers.DefaultHandler;
 
 import java.awt.*;
 import javax.swing.*;
 
 
 class MySVGBrowser {
   static public void main(String[] args) {
     new WebPage( args[0]);
 } }
 
 class MyContentHandler extends DefaultHandler {
      Graphics g;
 
   MyContentHandler(Graphics g){ this.g = g; }
 
   public void startElement(String namespace, String localName,
                             String QName, Attributes atts) {
     <.svg into java graphics.>
   }
 }
 
 class WebPage  extends JFrame {
      String fileName;
 
    WebPage ( String fileName) {
      this.fileName = fileName;
      setSize(200,200); setVisible(true);
    }
    public void paint(Graphics g) {
      try{
         SAXParserFactory factory = SAXParserFactory.newInstance();
         factory.setNamespaceAware( true );
         SAXParser saxParser = factory.newSAXParser();
         XMLReader xmlReader = saxParser.getXMLReader();
         xmlReader.setContentHandler( new MyContentHandler(g) );
         xmlReader.parse( new File(fileName).toURL().toString() );
      } catch( Exception  e ){}
    }
 }
-_-_-

The painting componnet is similar to that in the following program.

<..JavaPaint.java..>
 import java.awt.*;
 import javax.swing.*;
 
 class JavaPaint {
    public static void main(String args[]) {
      new Pic();
    }
 }
 class Pic extends JFrame {
    Pic() { setSize(200,200); setVisible(true); }
    public void paint(Graphics g) {
       g.fillRect(0, 0, 50, 60);
    }
 }
-_-_-