package org.mitre.midiki.workshop;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
import java.io.IOException;
import java.util.*;
/**
* Factory class for parsing XML files conforming to the diseasedb.dtd
* into WorkshopDatabase objects.
*
* @author Carl Burke
* @version 1.0
* @since 1.0
*/
public class WorkshopDatabaseFactory
{
/**
* Generates a master list of all disease names in the database.
* The details of each disease are filled in later.
*
* @param l an org.w3c.dom.NodeList
value
* @param wdb a WorkshopDatabase
value
*/
static protected void extractDiseases(org.w3c.dom.NodeList l,
WorkshopDatabase wdb)
{
wdb.diseaseList = new ArrayList();
if (l==null) {
System.out.println("No disease nodes found.");
return;
}
int count = l.getLength();
for (int i=0; iorg.w3c.dom.NodeList value
* @param wdb a WorkshopDatabase
value
*/
static protected void extractSymptoms(org.w3c.dom.NodeList l,
WorkshopDatabase wdb)
{
wdb.uniqueSymptoms = new HashMap();
if (l==null) {
System.out.println("No symptom nodes found.");
return;
}
int count = l.getLength();
for (int i=0; iorg.w3c.dom.NodeList value
* @param wdb a WorkshopDatabase
value
*/
static protected void extractTests(org.w3c.dom.NodeList l,
WorkshopDatabase wdb)
{
wdb.uniqueTests = new HashMap();
if (l==null) {
System.out.println("No test nodes found.");
return;
}
int count = l.getLength();
for (int i=0; iorg.w3c.dom.NodeList value
* @param wdb a WorkshopDatabase
value
*/
static protected void extractHistory(org.w3c.dom.NodeList l,
WorkshopDatabase wdb)
{
wdb.uniqueHistory = new HashMap();
if (l==null) {
System.out.println("No history nodes found.");
return;
}
int count = l.getLength();
for (int i=0; i tags in the database.
*
* @param l an org.w3c.dom.NodeList
value
* @param wdb a WorkshopDatabase
value
*/
static protected void extractSynonyms(org.w3c.dom.NodeList l,
WorkshopDatabase wdb)
{
wdb.synonymList = new ArrayList();
if (l==null) {
System.out.println("No synonym nodes found.");
return;
}
int count = l.getLength();
for (int i=0; iorg.w3c.dom.Node value
* @return a String
value
*/
static protected String textChild(org.w3c.dom.Node node)
{
org.w3c.dom.NodeList l = node.getChildNodes();
for (int i=0; iorg.w3c.dom.NodeList value
* @param wdb a WorkshopDatabase
value
*/
static protected void mapSymptomsToDiseases(org.w3c.dom.NodeList diseaseNodes,
WorkshopDatabase wdb)
{
wdb.diseases = new ArrayList();
int dcnt = diseaseNodes.getLength();
for (int i=0; iorg.w3c.dom.Document value
* @param wdb a WorkshopDatabase
value
*/
static protected void processDocument(org.w3c.dom.Document doc,
WorkshopDatabase wdb)
{
org.w3c.dom.NodeList diseaseNodes =
doc.getElementsByTagName("disease");
org.w3c.dom.NodeList symptomNodes =
doc.getElementsByTagName("symptom");
org.w3c.dom.NodeList testNodes =
doc.getElementsByTagName("test");
org.w3c.dom.NodeList historyNodes =
doc.getElementsByTagName("history-item");
org.w3c.dom.NodeList synonymNodes =
doc.getElementsByTagName("synonyms");
extractSynonyms(synonymNodes, wdb);
extractDiseases(diseaseNodes, wdb);
extractSymptoms(symptomNodes, wdb);
extractTests(testNodes, wdb);
extractHistory(historyNodes, wdb);
mapSymptomsToDiseases(diseaseNodes, wdb);
}
/**
* Opens the specified database file and interprets it as
* an XML file conforming to diseasedb.dtd. If successful,
* it returns a WorkshopDatabase object which can be queried.
* The resulting WorkshopDatabase does not require an XML parser
* to be present in the system.
*
* @param databaseFileName a String
value
* @return a WorkshopDatabase
value
*/
static public WorkshopDatabase newDatabase(String databaseFileName) {
WorkshopDatabase wdb = new WorkshopDatabase();
try {
// in order to do validation, must enable validation
// and set a SAXErrorHandler.
DocumentBuilderFactory dbFac =
DocumentBuilderFactory.newInstance();
dbFac.setValidating(true);
dbFac.setNamespaceAware(true);
// May need next line for compatibility with future
// versions of Xerces.
// Currently turning off validation, since only DTD created
dbFac.setAttribute("http://xml.org/sax/features/validation", new Boolean(false));
dbFac.setAttribute("http://apache.org/xml/features/validation/schema", new Boolean(false));
DocumentBuilder db = dbFac.newDocumentBuilder();
db.setErrorHandler(new WorkshopErrorHandler());
// make an input source from the document
InputSource ss = new InputSource(new FileReader(databaseFileName));
// parse the source
org.w3c.dom.Document doc = db.parse(ss);
processDocument(doc, wdb);
} catch (Exception e) {
e.printStackTrace();
}
return wdb;
}
/**
* Reads the file specified by the first program argument and parses it
* into a WorkshopDatabase by a call to
* newDatabase(). When called with one argument, this routine executes
* a few simple tests against the database and dumps the results to
* System.out. When called with at least two arguments, the second
* argument is interpreted as a filename to which the generated
* WorkshopDatabase should be serialized.
*
* @param args a String[]
value
*/
public static void main(String[] args) {
if (args.length==0) {
System.out.println("Usage: java WorkshopDatabaseFactory ");
return;
}
WorkshopDatabase wd = WorkshopDatabaseFactory.newDatabase(args[0]);
if (wd==null) {
System.out.println("Failed to load database from "+args[0]);
return;
}
if (args.length == 1) {
System.out.println("*** GET DISEASE NAMES ***");
System.out.println(wd.getAllDiseases());
System.out.println("*** GET SYMPTOM NAMES ***");
System.out.println(wd.getAllSymptoms());
System.out.println("*** GET SYNONYMS ***");
System.out.println(wd.getAllSymptomSynonyms());
System.out.println("*** GET DISEASE DESCRIPTIONS ***");
Iterator it = wd.getAllDiseases().iterator();
while (it.hasNext()) {
String diseaseName = (String)it.next();
System.out.println(wd.getDiseaseDescription(diseaseName));
}
System.out.println("*** GET DISEASES FOR SOME SELECTED SYMPTOMS ***");
LinkedList symp = new LinkedList();
LinkedList test = new LinkedList();
LinkedList hist = new LinkedList();
test.add(new DiagnosticTest("fever","","",true,true));
System.out.println(test);
System.out.println(wd.getApplicableDiseases(symp,test,hist));
test.clear();
symp.add(new Symptom("jaundice",true,true));
System.out.println(symp);
System.out.println(wd.getApplicableDiseases(symp,test,hist));
symp.clear();
symp.add(new Symptom("flu-like illness",true,true));
System.out.println(symp);
System.out.println(wd.getApplicableDiseases(symp,test,hist));
symp.add(new Symptom("headache",true,true));
System.out.println(symp);
System.out.println(wd.getApplicableDiseases(symp,test,hist));
symp.add(new Symptom("diarrhea",true,true));
System.out.println(symp);
System.out.println(wd.getApplicableDiseases(symp,test,hist));
} else {
try {
FileOutputStream fos = new FileOutputStream(args[1]);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(wd);
oos.flush();
fos.close();
System.out.println("Wrote serialized database to "+args[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}