Well here it is... what do ya think?


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/>.
 *
 @author Scott C. Tavares <a
href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
*/

// OPaL Classes
import org.apache.turbine.opl.api.*;
import org.apache.turbine.opl.mapping.*;
import org.apache.turbine.opl.database.PersistenceBroker;

// Turbine Classes
import org.apache.turbine.util.Log;
import org.apache.turbine.util.TurbineResources;

// Xerces Classes
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.traversal.NodeIteratorImpl;
import org.apache.xerces.domx.traversal.*;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.dom.NodeImpl;

/**
* This class implements the DOM XML Paser from
* the Apache Xerces Project. This class also functions
* as its name implies... ClassMap factory for OPaL ClassMaps
* objects.
*/
public class ClassMapFactory
{
 private static ClassMapFactory instance;

 // OPaL Classes
 private PersistenceBroker persistenceBroker = null;
 private ClassMap classMap =null;
 private AttributeMap attributeMap =null;
 private ColumnMap columnMap =null;
 private TableMap tableMap =null;

 /**
 * FIXME: move to a properties file as per
 *   Brett McLaughlin <[EMAIL PROTECTED]>
 */
 private static final String OPALCLASSMAPS = "OPaLClassMaps";
 private static final String CLASSMAP = "ClassMap";
 private static final String ATTRIBUTEMAP = "AttributeMap";
 private static final String COLUMNMAP = "ColumnMap";
 private static final String TABLEMAP = "TableMap";
 private static final String NAME = "Name";
 private static final String PROXY = "Proxy";
 private static final String PKEY = "PKey";

 private DOMParser parser=null;
 private DocumentImpl document=null;
 private NodeIteratorImpl iterator=null;

 private ClassMapFactory(PersistenceBroker persistenceBroker)
 {
  this.persistenceBroker = persistenceBroker;
 }

 public static ClassMapFactory getInstance(PersistenceBroker
persistenceBroker)
 {
  if (instance == null)
  {
   synchronized (ClassMapFactory.class)
   {
    if (instance == null)
    {
     instance = new ClassMapFactory(persistenceBroker);
    }
   }
  }
  return instance;
 }

 /**
 * This is the primary function of this class
 * it takes a string that names the source
 * XML File parses it and create the ClassMaps
 * accordingly.
 *
 *<p>
 * FIXME:  Add an overloaded method that
 *   takes a URI (DynamicURI) as a
 *   parameter.
 *<p>
 * @param xmlFile a string with the name and location of an XML file.
 *<p>
 */
 public void buildClassMaps(String xmlFile)


  try{
   parser = new DOMParser();
   parser.parse(xmlFile);
   document = (DocumentImpl)parser.getDocument();
   iterator = (NodeIteratorImpl)document.createNodeIterator(
       document,
       NodeFilter.SHOW_ELEMENT,
       null,
       true);

   processXMLNodes(iterator);

  }catch(Exception

   Log.error(e);
  }finally{
   //
  }
 }

 /**
 * Recursively process the node of an XML file
 * and creates the matching classes and joins them
 * together.
 *
 *@param iterator an NodeIteratorImpl from Xerces
 *
 */
 private void processXMLNodes(NodeIteratorImpl iterator)
 {
  try{
   NodeImpl node = (NodeImpl)iterator.nextNode();
   if(node!=null)


    if( node.getNodeName().equals(OPALCLASSMAPS))
    {
     System.out.println(OPALCLASSMAPS);
     processXMLNodes(iterator);
    }else if( node.getNodeName().equals(CLASSMAP))


     NamedNodeMap attributes = node.getAttributes();
     classMap = new ClassMap(attributes.getNamedItem(NAME).getNodeValue());
     classMap.setName(attributes.getNamedItem(NAME).getNodeValue());
     System.out.println("\t" + node.getNodeName() + ": " +
attributes.getNamedItem(NAME).getNodeValue());
     persistenceBroker.addClassMap(classMap);
     processXMLNodes(iterator); // Goto the next node

    }else if ( node.getNodeName().equals(ATTRIBUTEMAP) )


      NamedNodeMap attributes = node.getAttributes();
      attributeMap = new
ttributeMap( attributes.getNamedItem(NAME).getNodeValue());
      System.out.println("\t\t" + node.getNodeName() + ": " +
attributes.getNamedItem(NAME).getNodeValue());
      classMap.addAttributeMap(attributeMap);
      processXMLNodes(iterator); // Goto the next node

     }else if ( node.getNodeName().equals(COLUMNMAP))


       NamedNodeMap attributes = node.getAttributes();
       columnMap = new
olumnMap( attributes.getNamedItem(NAME).getNodeValue());
       System.out.println("\t\t\t" + node.getNodeName() + ": " +
attributes.getNamedItem(NAME).getNodeValue());
       if(attributes.getNamedItem(PKEY).getNodeValue().equals("True"))
       {
        columnMap.setIsKeyColumn(true);
       }else
       {
        columnMap.setIsKeyColumn(false);
       }
       attributeMap.setColumnMap(columnMap);
       processXMLNodes(iterator); // Goto the next node

      }else if ( node.getNodeName().equals(TABLEMAP) )


        NamedNodeMap attributes = node.getAttributes();
        tableMap = new
ableMap( attributes.getNamedItem(NAME).getNodeValue());
        System.out.println("\t\t\t\t" + node.getNodeName() + ": " +
attributes.getNamedItem(NAME).getNodeValue());
        columnMap.setTableMap(tableMap);
        processXMLNodes(iterator);  // Goto the next node
       }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}




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

Reply via email to