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. ;)

-- 
Christopher Elkins


---
ClassMapLoader.java
===================================================================
package org.apache.turbine.opl.mapping;
/*
 * Copyright (c) 1997-1999 The Java Apache Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the Java Apache
 *    Project for use in the Apache JServ servlet engine project
 *    <http://java.apache.org/>."
 *
 * 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine",
 *    "Apache Turbine", "Turbine Project", "Apache Turbine Project" and
 *    "Java Apache Project" must not be used to endorse or promote products
 *    derived from this software without prior written permission.
 *
 * 5. Products derived from this software may not be called "Apache JServ"
 *    nor may "Apache" nor "Apache JServ" appear in their names without
 *    prior written permission of the Java Apache Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the Java Apache
 *    Project for use in the Apache JServ servlet engine project
 *    <http://java.apache.org/>."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Java Apache Group. For more information
 * on the Java Apache Project and the Apache JServ Servlet Engine project,
 * please see <http://java.apache.org/>.
 *
 */

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";

    private ClassMap currentClassMap;
    private AttributeMap currentAttributeMap;
    private ColumnMap currentColumnMap;
    private TableMap currentTableMap;
    private Vector classMaps;

    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);
        parser.parse(new InputSource(source));
        return classMaps;
    }

    public void startElement(String name, AttributeList atts) 
        throws SAXException
    {
        if (name.equals(ELEMENT_CLASSMAP)) {
            ClassMap classMap = new ClassMap(atts.getValue(ATTRIBUTE_NAME));
            currentClassMap = classMap;
        } 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;
        }
    }
}

---
Index: PersistenceBroker.java
===================================================================
RCS file: 
/products/cvs/turbine/turbine/src/java/org/apache/turbine/opl/database/PersistenceBroker.java,v
retrieving revision 1.24
diff -c -r1.24 PersistenceBroker.java
*** PersistenceBroker.java      2000/01/15 00:00:54     1.24
--- PersistenceBroker.java      2000/01/21 20:13:04
***************
*** 56,61 ****
--- 56,62 ----
   */
      
  // JDK Classe
+ import java.io.*;
  import java.sql.*;
  import java.util.*;
  
***************
*** 332,338 ****
      
      public int retrieveClassMaps()
      {
!         return 0;
      }
  
      // FIXME! Maybe this should be private.  It's public to facilitate
--- 333,353 ----
      
      public int retrieveClassMaps()
      {
!       try {
!           ClassMapLoader classMapLoader = new ClassMapLoader();
! 
!           // FIXME: Need to read the XML mapping document here
!           InputStream is = null;
!           Vector classMaps = classMapLoader.load(is);
! 
!           Enumeration e = classMaps.elements();
!           while (e.hasMoreElements()) {
!               addClassMap((ClassMap)e.nextElement());
!           }
!           return 1;
!       } catch (Exception e) {
!           return 0;
!       }
      }
  
      // FIXME! Maybe this should be private.  It's public to facilitate


------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Problems?:           [EMAIL PROTECTED]

Reply via email to