import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ArrayList;
import com.sun.xml.parser.Resolver;
import com.sun.xml.tree.XmlDocument;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/***
* Simple class to read an XML file and create corresponding
* java classes from the elements. Each element MUST have a
* twin java class.
* This uses the sun ProjectX parser and the org.xml exceptions.
* @author mitch fincher, February 2000, mitch[at]fincher[dot]org
* You will still need a factory class with something like the following:
* try {
* input = Resolver.createInputSource(new File(filename));
* doc = XmlDocument.createXmlDocument(input, true);
* }
* catch(Exception e) {
* e.printStackTrace(System.out);
* System.out.println("Problem opening file \"" + filename +"\".\n
"+ e);
* throw(e);
* }
*
* MyTopObject myTopObject = new MyTopObject();
* myTopObject.load(doc.getDocumentElement());
* //myTopObject.test(); a good thing to add until dtd's are better
*
*
**/
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
public class XMLObject
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
{
protected Element thisElement;
protected String value = "";
protected ArrayList childrenArrayList = new ArrayList();
protected HashMap attributeHashMap = new HashMap();
protected XMLObject parentObject = null;
protected static int level = 0; // used for prettyprinting the structure
public static String packageString = null;
////////////////////////////////////////////////////////////////
public XMLObject()
////////////////////////////////////////////////////////////////
{
}
////////////////////////////////////////////////////////////////
public void load(Element thisElement) throws Exception
////////////////////////////////////////////////////////////////
{
this.thisElement = thisElement;
loadAttributes();
loadChildren();
}
////////////////////////////////////////////////////////////////
protected void loadAttributes()
////////////////////////////////////////////////////////////////
{
NamedNodeMap namedNodeMap = thisElement.getAttributes();
for(int i=0;i");
//desc.append("<"+this.getClass().getName()+">\n");
desc.append(spaces+value);
//desc.append(""+spaces+ "attributeHashMap="+attributeHashMap);
++level;
for(int i=0;i");
} else {
desc.append(""+getAttribute("xmltype") + ">");
}
return new String(desc);
}
////////////////////////////////////////////////////////////////
public ArrayList getChildren(String xmltype)
////////////////////////////////////////////////////////////////
{
ArrayList arrayList = new ArrayList();
XMLObject xmlobject = null;
for (java.util.Iterator e = getChildren(); e.hasNext() ;) {
xmlobject = (XMLObject)e.next();
if(xmlobject.getAttribute("xmltype").equals(xmltype)) {
arrayList.add(xmlobject);
}
}
return arrayList;
}
////////////////////////////////////////////////////////////////
public java.util.Iterator getChildren()
////////////////////////////////////////////////////////////////
{
return(childrenArrayList.iterator());
}
/***
* This returns whether an attribute has a particular value.
* This function exists because sometimes an attribute might not exist
* and each function would then have to test to see if the attribute exists,
* and then test it. So to minimize code we put it here.
* @param attribute attribute to test, eg, "name"
* @param text string to test
**/
////////////////////////////////////////////////////////////////
public boolean attributeEquals(String attribute, String text)
////////////////////////////////////////////////////////////////
{
boolean test = false;
if(getAttribute(attribute) != null) {
test = getAttribute(attribute).equals(text);
}
return(test);
}
/***
* returns an integer value from an attribute. If a problem occurs
* we return a zero.
* @param attribute the attribute that should be an integer
**/
////////////////////////////////////////////////////////////////
public int getAttributeInt(String attribute)
////////////////////////////////////////////////////////////////
{
int value = 0;
try {
value = Integer.parseInt(getAttribute(attribute));
} catch (Exception e){
value = 0;
}
return(value);
}
////////////////////////////////////////////////////////////////
public boolean isValid(int userID) throws Exception
////////////////////////////////////////////////////////////////
{
XMLObject xmlobject = null;
for (java.util.Iterator e = getChildren(); e.hasNext() ;) {
xmlobject = (XMLObject)e.next();
if(xmlobject.isValid(userID) == false)
{return(false);}
}
return true;
}
////////////////////////////////////////////////////////////////
public String toHTML(String language)
////////////////////////////////////////////////////////////////
{
StringBuffer desc = new StringBuffer("");
desc.append("\n "+getAttribute("xmltype")+"name=\""+getAttribute("name")+"\"");
for(int i=0;i