/*
 * 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.
 */

package org.apache.shardingsphere.shardingjdbc.spring.namespace.parser;

import com.google.common.base.Strings;
import org.apache.shardingsphere.api.config.encrypt.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptorRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.spring.namespace.constants.EncryptDataSourceBeanDefinitionParserTag;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Encrypt rule parser for spring namespace.
 * 
 * @author panjuan
 */
public final class EncryptRuleBeanDefinitionParser extends AbstractBeanDefinitionParser {
    
    @Override
    protected AbstractBeanDefinition parseInternal(final Element element, final ParserContext parserContext) {
        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(EncryptRuleConfiguration.class);
        factory.addConstructorArgValue(parseEncryptorRuleConfigurations(element));
        factory.addConstructorArgValue(parseEncryptTableRuleConfigurations(element));
        return factory.getBeanDefinition();
    }
    
    private Map<String, BeanDefinition> parseEncryptorRuleConfigurations(final Element element) {
        List<Element> encryptorRuleElements = DomUtils.getChildElementsByTagName(element, EncryptDataSourceBeanDefinitionParserTag.ENCRYPTOR_CONFIG_TAG);
        Map<String, BeanDefinition> result = new ManagedMap<>(encryptorRuleElements.size());
        for (Element each : encryptorRuleElements) {
            result.put(each.getAttribute(ID_ATTRIBUTE), parseEncryptorRuleConfiguration(each));
        }
        return result;
    }
    
    private AbstractBeanDefinition parseEncryptorRuleConfiguration(final Element element) {
        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(EncryptorRuleConfiguration.class);
        factory.addConstructorArgValue(element.getAttribute(EncryptDataSourceBeanDefinitionParserTag.ENCRYPTOR_TYPE_ATTRIBUTE));
        parseProperties(element, factory);
        return factory.getBeanDefinition();
    }
    
    private void parseProperties(final Element element, final BeanDefinitionBuilder factory) {
        String properties = element.getAttribute(EncryptDataSourceBeanDefinitionParserTag.ENCRYPTOR_PROPERTY_REF_ATTRIBUTE);
        if (!Strings.isNullOrEmpty(properties)) {
            factory.addConstructorArgReference(properties);
        } else {
            factory.addConstructorArgValue(new Properties());
        }
    }
    
    private Map<String, BeanDefinition> parseEncryptTableRuleConfigurations(final Element element) {
        List<Element> encryptTableElements = DomUtils.getChildElementsByTagName(element, EncryptDataSourceBeanDefinitionParserTag.TABLE_CONFIG_TAG);
        Map<String, BeanDefinition> result = new ManagedMap<>(encryptTableElements.size());
        for (Element each : encryptTableElements) {
            result.put(each.getAttribute(ID_ATTRIBUTE), parseEncryptTableConfiguration(each));
        }
        return result;
    }
    
    private AbstractBeanDefinition parseEncryptTableConfiguration(final Element element) {
        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(EncryptTableRuleConfiguration.class);
        factory.addConstructorArgValue(parseEncryptColumnConfigurations(element));
        return factory.getBeanDefinition();
    }
    
    private Map<String, BeanDefinition> parseEncryptColumnConfigurations(final Element element) {
        List<Element> encryptTableElements = DomUtils.getChildElementsByTagName(element, EncryptDataSourceBeanDefinitionParserTag.COLUMN_CONFIG_TAG);
        Map<String, BeanDefinition> result = new ManagedMap<>(encryptTableElements.size());
        for (Element each : encryptTableElements) {
            result.put(each.getAttribute(EncryptDataSourceBeanDefinitionParserTag.COLUMN_LOGIC_COLUMN_ATTRIBUTE), parseEncryptColumnConfiguration(each));
        }
        return result;
    }
    
    private AbstractBeanDefinition parseEncryptColumnConfiguration(final Element element) {
        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(EncryptColumnRuleConfiguration.class);
        factory.addConstructorArgValue(element.getAttribute(EncryptDataSourceBeanDefinitionParserTag.COLUMN_PLAIN_COLUMN_ATTRIBUTE));
        factory.addConstructorArgValue(element.getAttribute(EncryptDataSourceBeanDefinitionParserTag.COLUMN_CIPHER_COLUMN_ATTRIBUTE));
        factory.addConstructorArgValue(element.getAttribute(EncryptDataSourceBeanDefinitionParserTag.COLUMN_ASSISTED_QUERY_COLUMN_ATTRIBUTE));
        factory.addConstructorArgValue(element.getAttribute(EncryptDataSourceBeanDefinitionParserTag.COLUMN_ENCRYPTOR_REF_ATTRIBUTE));
        return factory.getBeanDefinition();
    }
}

@@ -908,16 +908,76 @@
 trings;%0A
+import lombok.AccessLevel;%0Aimport lombok.NoArgsConstructor;%0A
 import o
@@ -1682,143 +1682,8 @@
 ap;%0A
-import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;%0Aimport org.springframework.beans.factory.xml.ParserContext;%0A
 impo
@@ -1828,16 +1828,112 @@
 rties;%0A%0A
+import static org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.ID_ATTRIBUTE;%0A%0A
 /**%0A * E
@@ -2000,16 +2000,65 @@
 uan%0A */%0A
+@NoArgsConstructor(access = AccessLevel.PRIVATE)%0A
 public f
@@ -2104,29 +2104,117 @@
 ser 
-extends Abstract
+%7B%0A    %0A    /**%0A     * Parse encrypt rule element.%0A     * %0A     * @param element element%0A     * @return 
 Bean
-D
+ d
 efin
@@ -2222,49 +2222,50 @@
 tion
-Parser %7B%0A    %0A    @Override%0A    protected
+ of encrypt rule%0A     */%0A    public static
  Abs
@@ -2293,16 +2293,26 @@
 arse
-Internal
+EncryptRuleElement
 (fin
@@ -2333,43 +2333,8 @@
 ment
-, final ParserContext parserContext
 ) %7B%0A
@@ -2675,32 +2675,39 @@
    %0A    private 
+static 
 Map%3CString, Bean
@@ -3220,32 +3220,39 @@
    %0A    private 
+static 
 AbstractBeanDefi
@@ -3670,16 +3670,23 @@
 private 
+static 
 void par
@@ -4099,32 +4099,39 @@
    %0A    private 
+static 
 Map%3CString, Bean
@@ -4639,32 +4639,39 @@
    %0A    private 
+static 
 AbstractBeanDefi
@@ -5002,16 +5002,23 @@
 private 
+static 
 Map%3CStri
@@ -5599,16 +5599,23 @@
 private 
+static 
 Abstract

make it to static class