A Mini DOM Viewer

CSE 788A07

October 7, 2005

Abstract

The program provides a DOM oriented view of XML files. It offers both textual and graphical user interfaces for navigation. The graphical user interface is XML-based and implemeted through SwiXML ( http://www.swixml.org). XML-based interfaces are in/will be used in the major browsers.

Installation instructions:

  • Download the zip file of the swixml disribution.
  • Extract ‘swixml.jar’ and ‘jdom.jar’
  • Check the installation with the HelloWorld example of SwiXML (note a corresponding plain java code)
  • Compile with an instruction similar to ‘javac -classpath swixml.jar:jdom.jar:. DOMViewer.java’
  • Run with an instruction similar to
           java -cp swixml.jar:jdom.jar:. DOMViewer  
           java -cp swixml.jar:jdom.jar:. DOMViewer gdom.gui

1 Program Outline

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..DOMViewer.java..>
 <.imports.>
 public class DOMViewer {
   public static void main(String args[])
                        throws Exception  {
     DocumentBuilderFactory factory =
       DocumentBuilderFactory.newInstance();
     DocumentBuilder builder =
       factory.newDocumentBuilder();
     if( args.length > 0 ){
       <.graphical user interface.>
     } else {
       <.textual user interface.>
   } }
   <.shared utilities.>
 }
 <.class TDom.>
-_-_-

<..imports..>
 import java.io.*;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import org.w3c.dom.Document;
-_-_-

<..db.xml..>
 <?xml version="1.0" encoding="UTF-8"?>
 <!--a comment-->
 <?pi a processing instruction?>
 <root attr="ignored">
   <section>
     Some text
   </section>
   <![CDATA[ cdata material ]]>
 </root>
-_-_-

2 Textual User Interface

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..textual user interface..>
 new TDom(builder);
-_-_-

<..class TDom..>
 class TDom{
    <.iterface fields.>
    TDom(DocumentBuilder builder){
       boolean quit = false;
       while( !quit ){
         <.request = user’s input.>
         switch( request ){
            case ’q’: quit = true;  break;
            case ’h’: <.help request.> break;
            case ’n’: <.read xml filename.>
                      <.set dom.>
                      break;
            case ’p’: String s = "";
                      <.s := dom into xml.>
                      System.out.println( s );
                      break;
            case ’d’: <.move cursor down.>  break;
            case ’u’: <.move cursor up.>  break;
            case ’l’: <.move cursor left.>  break;
            case ’r’: <.move cursor right.>  break;
            default:;
 }  } } }
-_-_-

<..iterface fields..>
 Node dom = null, curNode;
-_-_-

<..request = user’s input..>
 System.out.print( "Please type a request (h for help): " );
 int request=’h’;
 try{
    request = System.in.read();
 } catch (java.io.IOException ioe){
    System.out.print(
       "\n--- Error --- Couldn’t read request"  );
 }
 System.out.println();
-_-_-

<..help request..>
 System.out.print(
    "Available options\n" +
    "     q: quit\n" +
    "     h: help\n" +
    "     n: load a new xml file\n" +
    "     p: print xml file\n" +
    "     d: move cursor down\n" +
    "     u: move cursor up\n" +
    "     l: move cursor left\n" +
    "     r: move cursor right\n"
 );
-_-_-

<..imports..>+
 import org.w3c.dom.Node;
-_-_-

3 Load a New XML file

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..set dom..>
 curNode = dom =
    DOMViewer.load( filename, builder );
-_-_-

<..shared utilities..>
 static Node load( String filename,
              DocumentBuilder builder ){
   Node dom = null;
   try{
      new FileReader( filename );
   } catch(java.io.IOException ioe){
      System.out.println(
      "--- Error --- Couldn’t find/open file: "
      + filename );
   }
   try{
      dom = builder.parse(
           new File(filename).toURL().toString() );
   } catch(Exception pe){
      System.out.println(
         "--- Error --- Failed parsing file: "
         + filename );
   }
   return dom;
 }
-_-_-

<..read xml filename..>
 System.out.print(
             "Please type a filename: " );
 String filename = "";
 char ch=’ ’;
 try{
    while( ch <= ’ ’ ){
       ch = (char) System.in.read(); }
    while( ch > ’ ’ ){
       filename += ch;
       ch = (char) System.in.read();
    }
 } catch (java.io.IOException ioe){
    System.out.println(
       "--- Error --- Couldn’t read filename"  );
 }
-_-_-

4 Serialization: DOM Into XML

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..s := dom into xml..>
 s = (dom==null)? "" : DOMViewer.toString ( dom );
-_-_-

<..shared utilities..>+
 static String toString( Node dom ){
   return toString (
           dom.getFirstChild(), "" );
 }
 static String toString ( Node node,
                        String indent ){
       String s = "";
    if( node!=null ){
       switch( node.getNodeType() ){
         case Node.TEXT_NODE:
           <.text to string.>
         case Node.COMMENT_NODE:
           <.comment to string.>
         case Node.CDATA_SECTION_NODE:
           <.cdata to string.>
         case Node.PROCESSING_INSTRUCTION_NODE:
           <.processing instruction to string.>
         case Node.ELEMENT_NODE:
           <.element to string.>
         default: <.non suported node type.>
       }
       s += toString(node.getNextSibling(), indent);
    }
    return s;
 }
-_-_-

4.1 Type Ids: Numbers into Names

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..non suported node type..>
 String v = node.getNodeValue();
 if( v!=null ){ v = v.trim(); }
 System.out.println( "Type not suppoerted: "
            + typeName( node.getNodeType() )
            + "       name=\""
            + node.getNodeName()
            + "\"  value=\"" + v + "\"" );
 s += toString( node.getFirstChild(),
                           indent + "  ");
-_-_-

<..shared utilities..>+
 static String typeName( int typeNum ){
    switch( typeNum ){
       case Node.CDATA_SECTION_NODE:
           return "CDATA_SECTION_NODE";
       case Node.COMMENT_NODE:
           return "COMMENT_NODE";
       case Node.ELEMENT_NODE:
           return "ELEMENT_NODE";
       case Node.PROCESSING_INSTRUCTION_NODE:
           return "PROCESSING_INSTRUCTION_NODE";
       case Node.TEXT_NODE:
           return "TEXT_NODE";
       default: return "not considered";
 }  }
-_-_-

4.2 Text, Comments, CData, Processing Instructions

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..text to string..>
 String txt = node.getNodeValue().trim();
 if( !txt.equals("") ){
    s += indent + txt + "\n";
 }
 break;
-_-_-

<..comment to string..>
 s += indent  + "<!--"
              + node.getNodeValue().trim()
              + "-->\n";
 break;
-_-_-

<..cdata to string..>
 s += indent + "<![CDATA["
             + node.getNodeValue().trim()
             + "]]>\n";
 break;
-_-_-

<..processing instruction to string..>
 s += indent +
    "<?" + node.getNodeName()
         + " "
         + node.getNodeValue()
  + "?>\n";
 break;
-_-_-

4.3 Elements

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..element to string..>
 s += indent + "<" + node.getNodeName();
 if( node.hasAttributes() ){
   NamedNodeMap attributes =
                 node.getAttributes();
   for( int i=0;
      i < attributes.getLength(); i++ ){
     Node attr = attributes.item(i);
     s += " "
       + attr.getNodeName()
       + "=\""  + attr.getNodeValue()
       + "\"";
 } }
 s += ">\n";
   s += toString( node.getFirstChild(),
                         indent + "  " );
 s += indent + "</" +
              node.getNodeName() + ">\n";
 break;
-_-_-

<..imports..>+
 import org.w3c.dom.NamedNodeMap;
-_-_-

5 Cursor Moving

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..move cursor down..>
 curNode = DOMViewer.move( curNode, curNode.getFirstChild() );
-_-_-

<..move cursor up..>
 curNode = DOMViewer.move( curNode, curNode.getParentNode() );
-_-_-

<..move cursor left..>
 curNode = DOMViewer.move( curNode, curNode.getPreviousSibling() );
-_-_-

<..move cursor right..>
 curNode = DOMViewer.move( curNode, curNode.getNextSibling() );
-_-_-

<..shared utilities..>+
 static Node move( Node node, Node nd ){
   if( nd != null ){
     <.remove cursor loc attribute.>
     node = nd;
     <.add cursor loc attribute.>
   }
   return node;
 }
-_-_-

<..remove cursor loc attribute..>
 switch( node.getNodeType() ){
   case Node.ELEMENT_NODE:
      Element e = (Element) node;
      if( e.hasAttribute( "cursor" ) ){
          e.removeAttribute( "cursor" );
      }
      break;
   case Node.TEXT_NODE:
   case Node.COMMENT_NODE:
   case Node.PROCESSING_INSTRUCTION_NODE:
   case Node.CDATA_SECTION_NODE:
      node.setNodeValue( node.getNodeValue()
                                   .replaceAll( <.mark.>, "" ) );
      break;
   default:
 }
-_-_-

<..add cursor loc attribute..>
 switch( node.getNodeType() ){
   case Node.ELEMENT_NODE:
     ((Element) node) .
       setAttribute( "cursor", <.mark.> );
     break;
   case Node.TEXT_NODE:
   case Node.COMMENT_NODE:
   case Node.PROCESSING_INSTRUCTION_NODE:
   case Node.CDATA_SECTION_NODE:
   node.setNodeValue( node.getNodeValue() +  <.mark.> );
      break;
   default:
 }
-_-_-

<..mark..>
 " ========================="
-_-_-

<..imports..>+
 import org.w3c.dom.Element;
-_-_-

6 Graphical User Interface

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

6.1 Activation

<..graphical user interface..>
 new GDom(builder, args[0]);
-_-_-

<..GDom.java..>
 <.imports.>
 import org.swixml.SwingEngine;
 import javax.swing.*;
 import java.awt.event.ActionEvent;
 public class GDom {
    DocumentBuilder builder;
    <.iterface fields.>
   <.event actions.>
    public GDom(DocumentBuilder builder, String gui )
                   throws Exception {
      this.builder = builder;
      new SwingEngine( this )
          . render( gui )
          . setVisible( true );
 } }
-_-_-

6.2 XML Specifications

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..gdom.gui..>
 <?xml version="1.0" encoding="UTF-8"?>
 <frame                size = "600,600"
                      title = <.gdom title.>
      defaultCloseOperation =
                     "JFrame.EXIT_ON_CLOSE">
   <vbox>
     <.view panel.>
     <.commands’ panel.>
   </vbox>
 </frame>
-_-_-

<..gdom title..>
 "GUI for DOM Viewer"
-_-_-

<..commands’ panel..>
 <panel>
 <vbox>
    <hbox>
       <button text="load" action="nRequest"/>
       <textfield id="filenameId" columns="20" Text="db.xml"/>
    </hbox>
    <panel Layout="GridLayout(3,2)">
        <button text="up" action="uRequest"/>
          <button text="quit" action="qRequest" foreground="blue"/>
        <button text="down" action="dRequest"/>
          <button text="show" action="pRequest" foreground="blue"/>
        <button text="left" action="lRequest"/>
        <button text="right" action="rRequest"/>
    </panel>
 </vbox>
 </panel>
-_-_-

<..view panel..>
 <textarea id="domId" size="600,500" Layout="BorderLayout"/>
-_-_-

6.3 Events Handling

1 Program Outline
2 Textual User Interface
3 Load a New XML file
4 Serialization: DOM Into XML
5 Cursor Moving
6 Graphical User Interface
DOMViewer.java, org.w3c.dom.Node
<?xml version="1.0" encoding="UTF-8"?>  
<!--a comment-->  
<?pi a processing instruction?>  
<root attr="ignored">  
  <section>  
    Some text  
  </section>  
  <![CDATA[ cdata material ]]>  
</root>  

<..event actions..>
 public JTextField filenameId;
 public JTextArea domId;
 public Action qRequest = new AbstractAction() {
   public void actionPerformed( ActionEvent e ) {
     System.exit(0);
   }
 };
 public Action nRequest = new AbstractAction() {
   public void actionPerformed( ActionEvent e ) {
     String filename = filenameId.getText();
     <.set dom.>
   }
 };
 public Action pRequest = new AbstractAction() {
   public void actionPerformed( ActionEvent e ) {
     String s = "";
     <.s := dom into xml.>
     domId.setText(s);
   }
 };
 public Action dRequest = new AbstractAction() {
   public void actionPerformed( ActionEvent e ) {
     <.move cursor down.>
   }
 };
 public Action uRequest = new AbstractAction() {
   public void actionPerformed( ActionEvent e ) {
     <.move cursor up.>
   }
 };
 public Action lRequest = new AbstractAction() {
   public void actionPerformed( ActionEvent e ) {
     <.move cursor left.>
   }
 };
 public Action rRequest = new AbstractAction() {
   public void actionPerformed( ActionEvent e ) {
     <.move cursor right.>
   }
 };
-_-_-