CSCI 5733.2
XML Application Development
Spring 2006
Suggested Solution to Mid-Term Examination

(1) For example,

use strict;
use CGI;
$|++;

#
#   t1bq1.pl
#
#   By Kwok-Bun Yue   February 15, 2006
#

print <<_XML_HEAD;
Content-Type: text/xml; charset=ISO-8859-1

<?xml version='1.0' encoding='ISO-8859-1' ?>
<quotations>
_XML_HEAD

my $q = new CGI;
my $self_url = $q->path_translated();
$self_url =~ s/\\[^\\]*$//;
my $datafile = "$self_url/quotations.txt";

unless (open IN, "<$datafile" )
{   print "</quotations>\n";
   die "";
}

while (my $line = <IN>) {
   chomp $line;
   my ($author, $quote) = $line =~ /^\[(.*?)\](.*)$/;
   print "  <quote author=\"" , xmlEncode($author) , "\">", xmlEncode($quote), "</quote>\n";
}
print "</quotations>\n";

exit 0;   #   main

sub xmlEncode {
   my $s = shift;
   $s =~ s/&/&amp;/g;
   $s =~ s/</&lt;/g;
   $s =~ s/>/&gt;/g;
   $s =~ s/'/&apos;/g;
   $s =~ s/"/&quot;/g;
   $s;
}

(2) For example,

import java.util.*;
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 NumberInteriorElements extends DefaultHandler
{
    public static void main(String argv[])
    {
        if (argv.length != 1) {
            System.err.println("Usage: java umberInteriorElements filename");
            System.exit(1);
        }
  
        DefaultHandler handler = new NumberInteriorElements();
        SAXParserFactory factory = SAXParserFactory.newInstance();

        try
   {   SAXParser saxParser = factory.newSAXParser();  
            saxParser.parse(new File(argv [0]), handler);

        } catch (Throwable t) {
            t.printStackTrace();
        }

   System.out.println("\n\nNumber of interior elements: " + (numElements - numLeafElements) + ".\n");
    }
  
    //   Data members
    static private boolean isLeaf = false;
    static private int numLeafElements = 0;
    static private int numElements = 0;

    //   Handler methods
    public void startElement(String namespaceURI,
                             String lName,
                             String qName,
                             Attributes attrs)
    throws SAXException
    {   isLeaf = true;
   numElements++;
    }   //   startElement

    public void endElement(String namespaceURI,
                             String lName,
                             String qName)
    throws SAXException
    {   if (isLeaf) { numLeafElements++; }
        isLeaf = false;
    }   //   endElement
}   //   NumberInteriorElements

(3) For example,

   private static org.w3c.dom.Element rightmostDescendant(org.w3c.dom.Element element) {
      if (element == null) return null;

      org.w3c.dom.Element lastChild = lastChild(element);
      return (lastChild == null) ? element : rightmostDescendant(lastChild);
   }


   //  
   private static org.w3c.dom.Element lastChild(org.w3c.dom.Element element) {
      if (element == null) return null;

      NodeList childNodes = element.getChildNodes();
      for (int i=childNodes.getLength()-1; i >=0; i--) {
         if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
            return (org.w3c.dom.Element) childNodes.item(i);
         }   //   if
      }   //   for
      return null;
   }   //   lastChild


(4) (a) T (b) T (c) F

(5)

(a) No:

<!ELEMENT a (#PCDATA | b | c)*>: limitations: there may be no <b> or <c> child elements.

(b) For example:

<simpleType name="AccountAmount">
  <restriction base="xsd:integer">
    <pattern value="((0|[1-9]\d*)(\.\d+)?)|\((0|[1-9]\d*)(\.\d+)?)\)" />
  </restriction>
</simpleType>