Christopher Elkins wrote:
>
> Hi, all. Because my current project went on a temporary hiatus, I've finally
> had some time to start working with OPaL. Here's my first cut at a ClassMap
> loader based on Scott's draft XML schema. This code is rough and undocumented,
> and it's hard-coded to use Xerces. It compiles successfully using JDK 1.2, but
> I don't know if it successfully compiles under JDK 1.1 or if it even works yet!
> However, it's a start. Also, this is the first time I've done _any_ SAX
> programming, so be gentle. ;)
Comments below...
>
> --
> Christopher Elkins
>
> ---
<snip - license />
>
> import java.io.*;
> import java.util.*;
> import org.xml.sax.*;
> import org.xml.sax.helpers.*;
>
> /**
> * This class implements the SAX handler for class map documents.
> *
> * @author Christopher Elkins
> */
> public class ClassMapLoader extends HandlerBase {
> private static final String ELEMENT_CLASSMAPS = "ClassMaps";
> private static final String ELEMENT_CLASSMAP = "ClassMap";
> private static final String ELEMENT_ATTRIBUTEMAP = "AttributeMap";
> private static final String ELEMENT_COLUMNMAP = "ColumnMap";
> private static final String ELEMENT_TABLEMAP = "TableMap";
> private static final String ATTRIBUTE_NAME = "Name";
> private static final String ATTRIBUTE_PROXY = "Proxy";
> private static final String ATTRIBUTE_PKEY = "PKey";
I am fundamentally against constants like this being put in compiled
code. I would say put these in a properties file... in other words, if
we decide that we want to rename "PKey" "PrimaryKey" it is a trivial
change, not a code one.
>
> private ClassMap currentClassMap;
> private AttributeMap currentAttributeMap;
> private ColumnMap currentColumnMap;
> private TableMap currentTableMap;
> private Vector classMaps;
I know this may not be your final work, but we need to JavaDoc
_everything_ we do.
>
> public ClassMapLoader() {
> currentClassMap = null;
> currentAttributeMap = null;
> currentColumnMap = null;
> currentTableMap = null;
> classMaps = new Vector();
> }
>
> public Vector load(InputStream source) throws Exception {
> Parser parser =
> ParserFactory.makeParser("org.apache.xerces.parsers.SAXParser");
> parser.setDocumentHandler(this);
You should consider turning on validation if a property in the props
file is set:
((Configurable)parser).setFeature("http://xml.org/sax/features/validation",
useValidation /*[true or false]*/);
> parser.parse(new InputSource(source));
> return classMaps;
> }
You should consider taking in a URI as well, as the filename may be read
from a properties file.
>
> public void startElement(String name, AttributeList atts)
> throws SAXException
> {
> if (name.equals(ELEMENT_CLASSMAP)) {
> ClassMap classMap = new ClassMap(atts.getValue(ATTRIBUTE_NAME));
> currentClassMap = classMap;
I see what you are doing here, but you are assuming that all the
required elements and attributes are present. If not in production,
this is certainly not a given in development. You might want to turn on
validation, as well (See my note above).
> } else if (name.equals(ELEMENT_ATTRIBUTEMAP)) {
> AttributeMap attributeMap =
> new AttributeMap(atts.getValue(ATTRIBUTE_NAME));
> ColumnMap columnMap = new ColumnMap();
> attributeMap.setColumnMap(columnMap);
>
> Boolean isPrimaryKey =
> Boolean.valueOf(atts.getValue(ATTRIBUTE_PKEY));
> columnMap.setIsKeyColumn(isPrimaryKey.booleanValue());
> // FIXME: not currently defined in XML schema
> //columnMap.setType();
>
> currentAttributeMap = attributeMap;
> currentColumnMap = columnMap;
> } else if (name.equals(ELEMENT_COLUMNMAP)) {
> currentColumnMap.setName(atts.getValue(ATTRIBUTE_NAME));
> } else if (name.equals(ELEMENT_TABLEMAP)) {
> TableMap tableMap = new TableMap(atts.getValue(ATTRIBUTE_NAME));
> currentTableMap = tableMap;
> }
> }
>
> public void endElement(String name) throws SAXException {
> if (name.equals(ELEMENT_CLASSMAP)) {
> classMaps.addElement(currentClassMap);
> currentClassMap = null;
> } else if (name.equals(ELEMENT_ATTRIBUTEMAP)) {
> currentClassMap.addAttributeMap(currentAttributeMap);
> currentAttributeMap = null;
> } else if (name.equals(ELEMENT_COLUMNMAP)) {
> currentColumnMap = null;
> } else if (name.equals(ELEMENT_TABLEMAP)) {
> currentColumnMap.setTableMap(currentTableMap);
> currentTableMap = null;
> }
> }
All in all, though, a very good start.
-Brett
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]