All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsimpledb.spring.JSimpleDBBeanDefinitionParser Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.spring;

import org.jsimpledb.JSimpleDB;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Parses <jsimpledb:jsimpledb> tags.
 *
 * @see org.jsimpledb.spring
 */
class JSimpleDBBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    private static final String KVSTORE_ATTRIBUTE = "kvstore";
    private static final String SCHEMA_VERSION_ATTRIBUTE = "schema-version";
    private static final String STORAGE_ID_GENERATOR_ATTRIBUTE = "storage-id-generator";
    private static final String AUTO_GENERATE_STORAGE_IDS_ATTRIBUTE = "auto-generate-storage-ids";

    @Override
    protected Class getBeanClass(Element element) {
        return JSimpleDB.class;
    }

    @Override
    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

        // Get KVDatabase bean name (optional)
        if (element.hasAttribute(KVSTORE_ATTRIBUTE))
            builder.addPropertyReference("KVStore", element.getAttribute(KVSTORE_ATTRIBUTE));

        // Get schema version (optional)
        if (element.hasAttribute(SCHEMA_VERSION_ATTRIBUTE))
            builder.addPropertyValue("schemaVersion", element.getAttribute(SCHEMA_VERSION_ATTRIBUTE));

        // Get storage ID generator bean name (optional)
        final boolean autogenStorageIds = !element.hasAttribute(AUTO_GENERATE_STORAGE_IDS_ATTRIBUTE)
          || Boolean.valueOf(element.getAttribute(AUTO_GENERATE_STORAGE_IDS_ATTRIBUTE));
        if (!autogenStorageIds)
            builder.addPropertyValue("storageIdGenerator", null);
        if (element.hasAttribute(STORAGE_ID_GENERATOR_ATTRIBUTE)) {
            if (!autogenStorageIds) {
                parserContext.getReaderContext().fatal("<" + element.getTagName() + "> cannot have a `"
                  + STORAGE_ID_GENERATOR_ATTRIBUTE + "' attribute and " + AUTO_GENERATE_STORAGE_IDS_ATTRIBUTE + "=\"false\"",
                  parserContext.extractSource(element));
                return;
            }
            builder.addPropertyReference("storageIdGenerator", element.getAttribute(STORAGE_ID_GENERATOR_ATTRIBUTE));
        }

        // Construct JSimpleDBFactoryBean bean definition
        builder.getRawBeanDefinition().setBeanClass(JSimpleDBFactoryBean.class);

        // Look for nested  and 
        final NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            if (!(nodeList.item(i) instanceof Element))
                continue;
            final Element child = (Element)nodeList.item(i);
            if (!JSimpleDBNamespaceHandler.JSIMPLEDB_NAMESPACE_URI.equals(child.getNamespaceURI()))
                continue;
            if (child.getLocalName().equals(JSimpleDBNamespaceHandler.SCAN_CLASSES_TAG))
                builder.addPropertyValue("modelClasses", new ScanClassesBeanDefinitionParser().parse(child, parserContext));
            else if (child.getLocalName().equals(JSimpleDBNamespaceHandler.SCAN_FIELD_TYPES_TAG)) {
                builder.addPropertyValue("fieldTypeClasses",
                  new ScanFieldTypesBeanDefinitionParser().parse(child, parserContext));
            } else {
                parserContext.getReaderContext().fatal("unsupported <" + child.getTagName() + "> element found"
                  + " inside <" + element.getTagName() + "> element", parserContext.extractSource(element));
                return;
            }
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy