Package org.apache.jdo.impl.model.jdo.xml

Source Code of org.apache.jdo.impl.model.jdo.xml.JDOHandlerImpl

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* File:           JDOHandlerImpl.java
* Date:           July 3, 2001  2:16 PM
*
* @author  michael
* @version generated by FFJ XML module
*/
package org.apache.jdo.impl.model.jdo.xml;

import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.jdo.impl.model.jdo.JDOClassImplDynamic;
import org.apache.jdo.model.ModelException;
import org.apache.jdo.model.jdo.JDOArray;
import org.apache.jdo.model.jdo.JDOClass;
import org.apache.jdo.model.jdo.JDOCollection;
import org.apache.jdo.model.jdo.JDOElement;
import org.apache.jdo.model.jdo.JDOExtension;
import org.apache.jdo.model.jdo.JDOField;
import org.apache.jdo.model.jdo.JDOIdentityType;
import org.apache.jdo.model.jdo.JDOMap;
import org.apache.jdo.model.jdo.JDOModel;
import org.apache.jdo.model.jdo.JDOPackage;
import org.apache.jdo.model.jdo.NullValueTreatment;
import org.apache.jdo.model.jdo.PersistenceModifier;
import org.apache.jdo.util.I18NHelper;
import org.xml.sax.*;

/**
*
* TBD:
* <ul>
* <li> Reading persistence capable superclass entry: check already existing
* superclass entry for consistency
* <li> Only populate requested class info, not the entire .jdo file
*/
public class JDOHandlerImpl
    implements JDOHandler
{
    /** */
    private final JDOModel model;
   
    /** */
    private final Stack context;
   
    /**
     * Flag indicating that the current entries should no be loaded,
     * because JDO metadata has been loaded before for the current
     * persistence capable class.
     */
    private boolean skipXMLElements;

    /** */
    private final Collection handledJDOClasses;

    /** I18N support. */
    private static final I18NHelper msg = I18NHelper.getInstance(
        "org.apache.jdo.impl.model.jdo.Bundle", //NOI18N
        JDOHandlerImpl.class.getClassLoader());

    /** Logger */
    private static Log logger = LogFactory.getFactory().getInstance(
        "org.apache.jdo.impl.model.jdo.xml"); // NOI18N
   
    /**
     *
     */
    public JDOHandlerImpl (JDOModel model)
    {
        this.model = model;
        this.context = new Stack();
        this.skipXMLElements = false;
        this.handledJDOClasses = new HashSet();
    }
   
    /**
     *
     */
    public void start_jdo(final Attributes meta)
    {
        if (logger.isTraceEnabled())
            logger.trace("  <jdo>"); //NOI18N
        // push current JDOModel on context stack
        context.push(model);
    }
   
    /**
     *
     */
    public void end_jdo()
    {
        if (logger.isTraceEnabled())
            logger.trace("  </jdo>"); //NOI18N
        // remove JDOModel fom context stack
        context.pop();
    }
   
    /**
     *
     */
    public void start_package(final Attributes meta)
        throws SAXException
    {
        boolean trace = logger.isTraceEnabled();
        if (trace)
            logger.trace("  <package>"); //NOI18N
        JDOPackage jdoPackage = null;
        try {
            // get JDOModel from context stack
            JDOModel model = (JDOModel)context.peek();
            String packageName = meta.getValue("", "name"); //NOI18N
            if ((packageName == null) || packageName.length() == 0)
                packageName = ""; //NOI18N
            if (trace)
                logger.trace("    name = " + packageName); //NOI18N
            jdoPackage = model.createJDOPackage(packageName);
        }
        catch (ModelException ex) {
            SAXException e =
                new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
            if (trace)
                logger.trace("Throwing exception in " + //NOI18N
                             "JDOHandlerImpl.start_package:", e); //NOI18N
            throw e;
        }
       
        // push current JDOPackage on context stack
        context.push(jdoPackage);
    }
   
    /**
     *
     */
    public void end_package()
    {
        if (logger.isTraceEnabled())
            logger.trace("  </package>"); //NOI18N
        // remove JDOPackage fom context stack
        context.pop();
    }
   
    /**
     *
     */
    public void start_class(final Attributes meta
        throws SAXException
    {
        boolean trace = logger.isTraceEnabled();
        if (trace)
            logger.trace("  <class>"); //NOI18N
        JDOClass jdoClass = null;
        try {
            // get JDOPackage from context stack
            //String packageName = (String)context.peek();
            JDOPackage jdoPackage = (JDOPackage)context.peek();
            String packageName = jdoPackage.getName();
            String className = meta.getValue("", "name"); //NOI18N
            if ((packageName != null) && (packageName.length() > 0))
                className = packageName + "." + className; //NOI18N
            jdoClass = model.createJDOClass(className, false);
            skipXMLElements = jdoClass.isXMLMetadataLoaded();
            if (skipXMLElements) {
                if (trace)
                    logger.trace(
                        "  JDO metadata already loaded for class " + //NOI18N
                        className + ", skipping class element"); //NOI18N
                return;
            }
            for ( int i = 0; i < meta.getLength(); i++ ) {
                String name = meta.getLocalName(i);
                String value = meta.getValue(i);
                if (trace)
                    logger.trace("    " + name + " = " + value); //NOI18N
                if ("name".equals(name)) { //NOI18N
                    // name is already set during create => do nothing
                }
                else if ("identity-type".equals(name)) { //NOI18N
                    jdoClass.setIdentityType(
                        JDOIdentityType.toJDOIdentityType(value));
                }
                else if ("objectid-class".equals(name)) { //NOI18N
                    jdoClass.setDeclaredObjectIdClassName(value);
                }
                else if ("requires-extent".equals(name)) { //NOI18N
                    jdoClass.setRequiresExtent(
                        Boolean.valueOf(value).booleanValue());
                }
                else if ("persistence-capable-superclass".equals(name)) { //NOI18N
                    // Do not overwrite existing entry
                    // TBD check old and new entry for consistency
                    if (jdoClass.getPersistenceCapableSuperclassName() == null) {
                        jdoClass.setPersistenceCapableSuperclassName(value);
                    }
                }
                else {
                    /* JDO2 metadata not yet fully supported =>
                       do not throw exception now
                    SAXException e = new SAXException(
                        msg.msg("EXC_UnknownAttribute", "<class>", //NOI18N
                                name, value));
                    if (trace)
                        logger.trace("Throwing exception in " +  //NOI18N
                                     "JDOHandlerImpl.start_class:", e); //NOI18N
                    throw e;
                    */
                    if (trace)
                        logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
                                             "<class>", name, value)); //NOI18N
                }
            }
        }
        catch (ModelException ex) {
            SAXException e =
                new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
            if (trace)
                logger.trace("Throwing exception in " //NOI18N
                             "JDOHandlerImpl.start_class:", e); //NOI18N
            throw e;
        }
        // store current jdoClass in handledJDOClasses
        handledJDOClasses.add(jdoClass);

        // push current JDOClass object on context stack
        context.push(jdoClass);
    }
   
    /**
     *
     */
    public void end_class() 
    {
        if (logger.isTraceEnabled())
            logger.trace("  </class>"); //NOI18N
        if (skipXMLElements) {
            // set flag to false to allow next class entry to be populated
            skipXMLElements = false;
        }
        else {
            // remove JDOClass fom context stack
            JDOClass jdoClass = (JDOClass)context.pop();
            // set jdoClass' xmlMetadataLoaded flag
            jdoClass.setXMLMetadataLoaded();
        }
    }
   
    /**
     *
     */
    public void start_field(final Attributes meta)
        throws SAXException
    {
        if (skipXMLElements)
            return;
        boolean trace = logger.isTraceEnabled();
        if (trace)
            logger.trace("  <field>"); //NOI18N
        JDOField jdoField = null;
        try {
            // get the current JDOClass from context stack
            JDOClass jdoClass = (JDOClass)context.peek();
            String fieldName = meta.getValue("", "name"); //NOI18N
            jdoField =  jdoClass.createJDOField(fieldName);
            for (int i = 0; i < meta.getLength(); i++ ) {
                String name = meta.getLocalName(i);
                String value = meta.getValue(i);
                if (trace)
                    logger.trace("    " + name + " = " + value); //NOI18N
                if ("name".equals(name)) { //NOI18N
                    // name is already set during create => do nothing
                }
                else if ("persistence-modifier".equals(name)) { //NOI18N
                    int modifier =
                        PersistenceModifier.toPersistenceModifier(value);
                    jdoField.setPersistenceModifier(modifier);
                }
                else if ("primary-key".equals(name)) { //NOI18N
                    jdoField.setPrimaryKey(
                        Boolean.valueOf(value).booleanValue());
                }
                else if ("null-value".equals(name)) { //NOI18N
                    jdoField.setNullValueTreatment(
                        NullValueTreatment.toNullValueTreatment(value));
                }
                else if ("default-fetch-group".equals(name)) { //NOI18N
                    jdoField.setDefaultFetchGroup(
                        Boolean.valueOf(value).booleanValue());
                }
                else if ("embedded".equals(name)) { //NOI18N
                    jdoField.setEmbedded(
                        Boolean.valueOf(value).booleanValue());
                }
                else {
                    /* JDO2 metadata not yet fully supported =>
                       do not throw exception now
                    SAXException e = new SAXException(
                        msg.msg("EXC_UnknownAttribute", "<field>", //NOI18N
                                name, value));
                    if (trace)
                        logger.trace("Throwing exception in " + //NOI18N
                                     "JDOHandlerImpl.start_field:", e); //NOI18N
                    throw e;
                    */
                    if (trace)
                        logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
                                             "<field>", name, value)); //NOI18N
                }
            }
        }
        catch (ModelException ex) {
            SAXException e =
                new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
            if (trace)
                logger.trace("Throwing exception in " + //NOI18N
                             "JDOHandlerImpl.start_field:", e); //NOI18N
            throw e;
        }
       
        // push current JDOField on context stack
        context.push(jdoField);
    }
   
    /**
     *
     */
    public void end_field() 
    {
        if (skipXMLElements)
            return;
        if (logger.isTraceEnabled())
            logger.trace("  </field>"); //NOI18N
        // remove JDOField from context stack
        context.pop();
    }
   
    /**
     *
     */
    public void start_collection(final Attributes meta
        throws SAXException
    {
        if (skipXMLElements)
            return;
       
        boolean trace = logger.isTraceEnabled();
        if (trace)
            logger.trace("  <collection>"); //NOI18N
        JDOCollection jdoCollection = null;
        try {
            // get the current JDOField from context stack
            JDOField jdoField = (JDOField)context.peek();
            jdoCollection = jdoField.createJDOCollection();
            for (int i = 0; i < meta.getLength(); i++ ) {
                String name = meta.getLocalName(i);
                String value = meta.getValue(i);
                if (trace)
                    logger.trace("    " + name + " = " + value); //NOI18N
                if ("element-type".equals(name)) { //NOI18N
                    jdoCollection.setElementTypeName(value);
                }
                else if ("embedded-element".equals(name)) { //NOI18N
                    jdoCollection.setEmbeddedElement(
                        Boolean.valueOf(value).booleanValue());
                }
                else {
                    /* JDO2 metadata not yet fully supported =>
                       do not throw exception now
                    SAXException e = new SAXException(
                        msg.msg("EXC_UnknownAttribute", "<collection>", //NOI18N
                                name, value));
                    if (trace)
                        logger.trace("Throwing exception in " + //NOI18N
                                     "JDOHandlerImpl.start_collection:", e); //NOI18N
                    throw e;
                    */
                    if (trace)
                        logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
                                             "<collection>", name, value)); //NOI18N
                }
            }
        }
        catch (ModelException ex) {
            SAXException e =
                new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
            if (trace)
                logger.trace("Throwing exception in " + //NOI18N
                             "JDOHandlerImpl.start_collection:", e); //NOI18N
            throw e;
        }
       
        // push current JDOCollection on context stack
        context.push(jdoCollection);
    }
   
    /**
     *
     */
    public void end_collection() 
    {
        if (skipXMLElements)
            return;
        if (logger.isTraceEnabled())
            logger.trace("  </collection>"); //NOI18N
        // remove JDOCollection from context stack
        context.pop();
    }
   
    /**
     *
     */
    public void start_array(final Attributes meta)
        throws SAXException
    {
        if (skipXMLElements)
            return;

        boolean trace = logger.isTraceEnabled();
        if (trace)
            logger.trace("  <array>"); //NOI18N
        JDOArray jdoArray = null;
        try {
            // get the current JDOField from context stack
            JDOField jdoField = (JDOField)context.peek();
            jdoArray = jdoField.createJDOArray();
            for (int i = 0; i < meta.getLength(); i++ ) {
                String name = meta.getLocalName(i);
                String value = meta.getValue(i);
                if (trace)
                    logger.trace("    " + name + " = " + value); //NOI18N
                if ("embedded-element".equals(name)) { //NOI18N
                    jdoArray.setEmbeddedElement(
                        Boolean.valueOf(value).booleanValue());
                }
                else {
                    /* JDO2 metadata not yet fully supported =>
                       do not throw exception now
                    SAXException e = new SAXException(
                        msg.msg("EXC_UnknownAttribute", "<array>", //NOI18N
                                name, value));
                    if (trace)
                        logger.trace("Throwing exception in " + //NOI18N
                                     "JDOHandlerImpl.start_array:", e); //NOI18N
                    throw e;
                    */
                    if (trace)
                        logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
                                             "<array>", name, value)); //NOI18N
                }
            }
        }
        catch (ModelException ex) {
            SAXException e =
                new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
            if (trace)
                logger.trace("Throwing exception in " + //NOI18N
                             "JDOHandlerImpl.start_array:", e); //NOI18N
            throw e;
        }
       
        // push current JDOArray on context stack
        context.push(jdoArray);
    }
   
    /**
     *
     */
    public void end_array()
    {
        if (skipXMLElements)
            return;
        if (logger.isTraceEnabled())
            logger.trace("  </array>"); //NOI18N
        // remove JDOArray from context stack
        context.pop();
    }
   
    /**
     *
     */
    public void start_map(final Attributes meta)
        throws SAXException
    {
        if (skipXMLElements)
            return;
        boolean trace = logger.isTraceEnabled();
        if (trace)
            logger.trace("  <map>"); //NOI18N
        JDOMap jdoMap = null;
        try {
            // get the current JDOField from context stack
            JDOField jdoField = (JDOField)context.peek();
            jdoMap = jdoField.createJDOMap();
            for (int i = 0; i < meta.getLength(); i++ ) {
                String name = meta.getLocalName(i);
                String value = meta.getValue(i);
                if (trace)
                    logger.trace("    " + name + " = " + value); //NOI18N
                if ("key-type".equals(name)) { //NOI18N
                    jdoMap.setKeyTypeName(value);
                }
                else if ("embedded-key".equals(name)) { //NOI18N
                    jdoMap.setEmbeddedKey(
                        Boolean.valueOf(value).booleanValue());
                }
                else if ("value-type".equals(name)) { //NOI18N
                    jdoMap.setValueTypeName(value);
                }
                else if ("embedded-value".equals(name)) { //NOI18N
                    jdoMap.setEmbeddedValue(
                        Boolean.valueOf(value).booleanValue());
                }
                else {
                    /* JDO2 metadata not yet fully supported =>
                       do not throw exception now
                    SAXException e = new SAXException(
                        msg.msg("EXC_UnknownAttribute", "<map>", //NOI18N
                                name, value));
                    if (trace)
                        logger.trace("Throwing exception in " + //NOI18N
                                     "JDOHandlerImpl.start_map:", e); //NOI18N
                    throw e;
                    */
                    if (trace)
                        logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
                                             "<map>", name, value)); //NOI18N
                }
            }
        }
        catch (ModelException ex) {
            SAXException e =
                new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
            if (trace)
                logger.trace("Throwing exception in " + //NOI18N
                             "JDOHandlerImpl.start_map:", e); //NOI18N
            throw e;
        }
       
        // push current JDOMap on context stack
        context.push(jdoMap);
    }
   
    /**
     *
     */
    public void end_map() 
    {
        if (skipXMLElements)
            return;
        if (logger.isTraceEnabled())
            logger.trace("  </map>"); //NOI18N
        // remove JDOMap from context stack
        context.pop();
    }
   
    /**
     *
     */
    public void start_extension(final Attributes meta)
        throws SAXException
    {
        if (skipXMLElements)
            return;
        boolean trace = logger.isTraceEnabled();

        if (trace)
            logger.trace("  <extension>"); //NOI18N
        JDOExtension jdoExtension = null;
        try {
            // get the current JDOElement from context stack
            JDOElement jdoElement = (JDOElement)context.peek();
            jdoExtension = jdoElement.createJDOExtension();
            for ( int i = 0; i < meta.getLength(); i++ ) {
                String name = meta.getLocalName(i);
                String value = meta.getValue(i);
               
                if (trace)
                    logger.trace("    " + name + " = " + value); //NOI18N
                if ("vendor-name".equals(name)) { //NOI18N
                    jdoExtension.setVendorName(value);
                }
                else if ("key".equals(name)) { //NOI18N
                    jdoExtension.setKey(value);
                }
                else if ("value".equals(name)) { //NOI18N
                    jdoExtension.setValue(value);
                }
                else {
                    /* JDO2 metadata not yet fully supported =>
                       do not throw exception now
                    SAXException e = new SAXException(
                        msg.msg("EXC_UnknownAttribute", "<extension>", //NOI18N
                                name, value));
                    if (trace)
                        logger.trace("Throwing exception in " + //NOI18N
                                     "JDOHandlerImpl.start_extension:", e); //NOI18N
                    throw e;
                    */
                    if (trace)
                        logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
                                             "<extension>", name, value)); //NOI18N
                }
            }
        }
        catch (ModelException ex) {
            SAXException e =
                new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
            if (trace)
                logger.trace("Throwing exception in " + //NOI18N
                             "JDOHandlerImpl.start_extension:", e); //NOI18N
            throw e;
        }
    }
   
    /**
     *
     */
    public void end_extension() 
    {
        if (skipXMLElements)
            return;
        if (logger.isTraceEnabled())
            logger.trace("  </extension>"); //NOI18N
        // start did not push anything => do nothing
    }
   
    /**
     *
     */
    public Collection handledJDOClasses()
    {
        return handledJDOClasses;
    }

}
TOP

Related Classes of org.apache.jdo.impl.model.jdo.xml.JDOHandlerImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.