CSCI 5931A.1
XML Application Development
Summer 2002
Mid-Term Examination

Name:  _________________________________

Time allowed: one hour and 20 minutes.  Total score: 30 points.

Open: text book, lecture notes, every file I wrote and posted in my Web page and your project assignments.  No other materials are allowed.

Answer all questions.  Turn in both question and answer sheets.  Plan your time well.

Academic honesty policy will be followed strictly.  Cheating will result in a failing grade of D or below and a permanent academic record. 

(1) [9 points] Write a CGI-Perl (or JSP) program, t1q1.pl (or t1q1.jsp) to read in a text file of quotations and generate an output XML file. The input file has the format of:

author

quotation-line
quotation-line
...
quotation-line

author
quotation-line
quotation-line
...
quotation-line

...

That is, each quotation is composed of an author in a line by itself and a sequence of quotation lines (that are not empty lines). Each quotation is separated by an empty line.

For example, the input file "t1q1dat.txt" contains three quotations:

Kwok-Bun Yue
If a < b then b > a.

Sadegh Davari
CS & CIS are the two largest programs in NAS.

Joseph Giarratano
I like to teach
expert systems and
artificial intelligence.

Your CGI program, t1q1.pl, should display the result in IE as below:

http://dcm.uhcl.edu/[youraccount]/t1q1.pl:


Make sure your program outputs XML using the format above.

Hint: if you are using CGI-Perl, the following code will get the directory of your CGI-Perl script in Windows server:

use CGI::Pretty qw(-nosticky);
my $q = new CGI;
my $path = $q->path_translated();
$path =~ s/\\[^\\]*$/\\/;


(2)

(a) [4 points] Consider the following XML document, graph1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<graph>
<node name="n1" weight="20" To="n2 n3 n4"/>
<node name="n2" To="n1"/>
<node name="n3" weight="10" To="n2 n4"/>
<node name="n4"/>
</graph>

The XML document represents a graph with nodes n1, n2, n3, n4, and arcs from n1 to n2, n3 and n4, from n2 to n1 and from n3 to n2 and n4. Give a reasonable and minimal DTD for it.

(b) [4 points] Provide the DTD definition of the followings.

(i) The element A, which has no attribute and may contain any number of elements B, C or D in any order.

(ii) The element A, which has a required attribute X, an integer, and contains one to three elements of B, C or D in any order.

(iii) The element A, which has no attribute and contains one to three elements of B, C or D, but all B's must come before C's or D's, and all C's must come before D's.

(c) [3 points] Is the following XML document validated with the DTD below? If not, modify the DTD so that the XML document complies to the new DTD.

XML:

<?xml version="1.0"?>
<!DOCTYPE a SYSTEM "t2q2c.dtd">
<a>
   <b />
   <c id="c1" />
   <f>
      <c   id="c2" x="c1" />
   </f>
</a>

t2q2c.dtd:

<!ELEMENT a ((a | b), c, (d | e)*, f)>
<!ELEMENT b EMPTY>
<!ELEMENT c (#PCDATA)>
<!ELEMENT d EMPTY>
<!ELEMENT e EMPTY>
<!ELEMENT f ANY>

<!ATTLIST c id ID #REQUIRED
         x IDREF #IMPLIED >

(3) [10 points] Write a simple Java program t2q3.java using SAX (other methods not acceptable) to print the depth of the input XML tree (the depth of an empty tree is 0) and the maximum number of attributes of an element. For example, if the content of t2q3.xml is:

<?xml version="1.0"?>
<!DOCTYPE a SYSTEM "t2q2c.dtd">
<a>
   <b />
   <c id="c1" />
   <f>
      <c id="c2" x="c1" y="12" z = "14"/>
      <g><h /></g>
   </f>
</a>

Running:

java t2q3 t2q3.xml

output:

Depth of tree = 3
Maximum number of attributes in an element = 4

The skeleton of the program, including the main method, is given below. You will only need to give the definitions of data members, SAX callback handlers and any utility methods that you may use.

import java.io.*;

Import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

public class t2q3 extends DefaultHandler
{
   public static void main(String argv[])
   {
      if (argv.length != 1) {
         System.err.println("Usage: java t2q3 xml_filename");
         System.exit(1);
      }
      DefaultHandler handler = new t2q3();
      //   Use the default (non-validating) parser
      SAXParserFactory factory =
         SAXParserFactory.newInstance();

      try {
         SAXParser saxParser = factory.newSAXParser();
         saxParser.parse(new File(argv [0]), handler);
      } catch (Throwable t) {
         t.printStackTrace();
      }
   }   //   main

   /*  Data members    */
   /*  --------------- */
  

   /*   Handler methods */
   /*  --------------- */


}   //   t2q3