A Mini DOM Viewer
CSE 788A07
October 7, 2005
|
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
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
-_-_-
<?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
Node dom = null, curNode;
-_-_-
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();
-_-_-
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"
);
-_-_-
import org.w3c.dom.Node;
-_-_-
3 Load a New XML file
curNode = dom =
DOMViewer.load( filename, builder );
-_-_-
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;
}
-_-_-
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
s = (dom==null)? "" : DOMViewer.toString ( dom );
-_-_-
4.1 Type Ids: Numbers into Names
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 + " ");
-_-_-
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
String txt = node.getNodeValue().trim();
if( !txt.equals("") ){
s += indent + txt + "\n";
}
break;
-_-_-
s += indent + "<!--"
+ node.getNodeValue().trim()
+ "-->\n";
break;
-_-_-
s += indent + "<![CDATA["
+ node.getNodeValue().trim()
+ "]]>\n";
break;
-_-_-
s += indent +
"<?" + node.getNodeName()
+ " "
+ node.getNodeValue()
+ "?>\n";
break;
-_-_-
4.3 Elements
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;
-_-_-
import org.w3c.dom.NamedNodeMap;
-_-_-
5 Cursor Moving
curNode = DOMViewer.move( curNode, curNode.getFirstChild() );
-_-_-
curNode = DOMViewer.move( curNode, curNode.getParentNode() );
-_-_-
curNode = DOMViewer.move( curNode, curNode.getPreviousSibling() );
-_-_-
curNode = DOMViewer.move( curNode, curNode.getNextSibling() );
-_-_-
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:
} -_-_-
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:
} -_-_-
" ========================="
-_-_-
import org.w3c.dom.Element;
-_-_-
6 Graphical User Interface
6.1 Activation
new GDom(builder, args[0]);
-_-_-
<.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
"GUI for DOM Viewer"
-_-_-
<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>
-_-_-
<textarea id="domId" size="600,500" Layout="BorderLayout"/>
-_-_-
6.3 Events Handling
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.>
}
}; -_-_-