<commit_before>/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed 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 com.aspectran.core.context.builder.xml;

import java.util.Map;

import org.w3c.dom.Node;

import com.aspectran.core.context.builder.ContextBuilderAssistant;
import com.aspectran.core.context.builder.apon.params.FilterParameters;
import com.aspectran.core.context.rule.BeanRule;
import com.aspectran.core.context.rule.ItemRuleMap;
import com.aspectran.core.context.rule.type.ScopeType;
import com.aspectran.core.util.BooleanUtils;
import com.aspectran.core.util.StringUtils;
import com.aspectran.core.util.apon.Parameters;
import com.aspectran.core.util.xml.Nodelet;
import com.aspectran.core.util.xml.NodeletAdder;
import com.aspectran.core.util.xml.NodeletParser;

/**
 * The Class BeanInnerNodeletAdder.
 * 
 * <p>Created: 2008. 06. 14 오전 6:56:29</p>
 */
public class BeanNodeletAdder implements NodeletAdder {
	
	protected ContextBuilderAssistant assistant;
	
	/**
	 * Instantiates a new BeanNodeletAdder.
	 *
	 * @param assistant the assistant for Context Builder
	 */
	public BeanNodeletAdder(ContextBuilderAssistant assistant) {
		this.assistant = assistant;
	}

	/* (non-Javadoc)
	 * @see com.aspectran.core.util.xml.NodeletAdder#process(java.lang.String, com.aspectran.core.util.xml.NodeletParser)
	 */
	public void process(String xpath, NodeletParser parser) {
		parser.addNodelet(xpath, "/bean", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				String id = attributes.get("id");
				String className = assistant.resolveAliasType(attributes.get("class"));
				String mask = attributes.get("mask");
				String scope = attributes.get("scope");
				Boolean singleton = BooleanUtils.toNullableBooleanObject(attributes.get("singleton"));
				String factoryMethod = attributes.get("factoryMethod");
				String initMethodName = attributes.get("initMethod");
				String destroyMethodName = attributes.get("destroyMethod");
				Boolean lazyInit = BooleanUtils.toNullableBooleanObject(attributes.get("lazyInit"));
				Boolean important = BooleanUtils.toNullableBooleanObject(attributes.get("important"));

				BeanRule beanRule = BeanRule.newInstance(id, mask, className, scope, singleton, factoryMethod, initMethodName, destroyMethodName, lazyInit, important);
				assistant.pushObject(beanRule);					
			}
		});
		parser.addNodelet(xpath, "/bean/description", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(text != null) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setDescription(text);
				}
			}
		});
		parser.addNodelet(xpath, "/bean/filter", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				String classScanFilterClassName = attributes.get("class");
				Parameters filterParameters = null;
				
				if(StringUtils.hasText(text)) {
					filterParameters = new FilterParameters(text);
				}
				if(StringUtils.hasText(classScanFilterClassName)) {
					if(filterParameters == null)
						filterParameters = new FilterParameters();
					filterParameters.putValue(FilterParameters.filterClass, classScanFilterClassName);
				}
				if(filterParameters != null) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setFilterParameters(filterParameters);
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/class", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setClassName(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/scope", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					ScopeType scopeType = ScopeType.valueOf(text.trim());
					
					if(scopeType == null)
						throw new IllegalArgumentException("No scope-type registered for scope '" + text + "'.");
					
					BeanRule beanRule = assistant.peekObject();
					beanRule.setScopeType(scopeType);
					
					if(scopeType == ScopeType.SINGLETON)
						beanRule.setSingleton(Boolean.TRUE);
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/factoryMethod", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setFactoryMethodName(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/initMethod", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setInitMethodName(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/destroyMethod", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setDestroyMethodName(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/lazyInit", new Nodelet() {<commit_msg>Adds features such as Spring's factory bean<commit_after>/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed 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 com.aspectran.core.context.builder.xml;

import java.util.Map;

import org.w3c.dom.Node;

import com.aspectran.core.context.builder.ContextBuilderAssistant;
import com.aspectran.core.context.builder.apon.params.FilterParameters;
import com.aspectran.core.context.rule.BeanRule;
import com.aspectran.core.context.rule.ItemRuleMap;
import com.aspectran.core.context.rule.type.ScopeType;
import com.aspectran.core.util.BooleanUtils;
import com.aspectran.core.util.StringUtils;
import com.aspectran.core.util.apon.Parameters;
import com.aspectran.core.util.xml.Nodelet;
import com.aspectran.core.util.xml.NodeletAdder;
import com.aspectran.core.util.xml.NodeletParser;

/**
 * The Class BeanInnerNodeletAdder.
 * 
 * <p>Created: 2008. 06. 14 오전 6:56:29</p>
 */
public class BeanNodeletAdder implements NodeletAdder {
	
	protected ContextBuilderAssistant assistant;
	
	/**
	 * Instantiates a new BeanNodeletAdder.
	 *
	 * @param assistant the assistant for Context Builder
	 */
	public BeanNodeletAdder(ContextBuilderAssistant assistant) {
		this.assistant = assistant;
	}

	/* (non-Javadoc)
	 * @see com.aspectran.core.util.xml.NodeletAdder#process(java.lang.String, com.aspectran.core.util.xml.NodeletParser)
	 */
	public void process(String xpath, NodeletParser parser) {
		parser.addNodelet(xpath, "/bean", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				String id = attributes.get("id");
				String className = assistant.resolveAliasType(attributes.get("class"));
				String mask = attributes.get("mask");
				String scope = attributes.get("scope");
				Boolean singleton = BooleanUtils.toNullableBooleanObject(attributes.get("singleton"));
				String factoryBean = attributes.get("factoryBean");
				String factoryMethod = attributes.get("factoryMethod");
				String initMethodName = attributes.get("initMethod");
				String destroyMethodName = attributes.get("destroyMethod");
				Boolean lazyInit = BooleanUtils.toNullableBooleanObject(attributes.get("lazyInit"));
				Boolean important = BooleanUtils.toNullableBooleanObject(attributes.get("important"));

				BeanRule beanRule = BeanRule.newInstance(id, mask, className, scope, singleton, factoryBean, factoryMethod, initMethodName, destroyMethodName, lazyInit, important);
				assistant.pushObject(beanRule);					
			}
		});
		parser.addNodelet(xpath, "/bean/description", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(text != null) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setDescription(text);
				}
			}
		});
		parser.addNodelet(xpath, "/bean/filter", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				String classScanFilterClassName = attributes.get("class");
				Parameters filterParameters = null;
				
				if(StringUtils.hasText(text)) {
					filterParameters = new FilterParameters(text);
				}
				if(StringUtils.hasText(classScanFilterClassName)) {
					if(filterParameters == null)
						filterParameters = new FilterParameters();
					filterParameters.putValue(FilterParameters.filterClass, classScanFilterClassName);
				}
				if(filterParameters != null) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setFilterParameters(filterParameters);
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/class", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setClassName(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/scope", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					ScopeType scopeType = ScopeType.valueOf(text.trim());
					
					if(scopeType == null)
						throw new IllegalArgumentException("No scope-type registered for scope '" + text + "'.");
					
					BeanRule beanRule = assistant.peekObject();
					beanRule.setScopeType(scopeType);
					
					if(scopeType == ScopeType.SINGLETON)
						beanRule.setSingleton(Boolean.TRUE);
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/factoryBean", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setFactoryBeanId(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/factoryMethod", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setFactoryMethodName(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/initMethod", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setInitMethodName(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/destroyMethod", new Nodelet() {
			public void process(Node node, Map<String, String> attributes, String text) throws Exception {
				if(StringUtils.hasText(text)) {
					BeanRule beanRule = assistant.peekObject();
					beanRule.setDestroyMethodName(text.trim());
				}
			}
		});
		parser.addNodelet(xpath, "/bean/features/lazyInit", new Nodelet() {
{'fraction_non_alphanumeric': 0.1168905168905169, 'fraction_numerical': 0.004232804232804233, 'mean_word_length': 4.219201359388276, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2563674', 'n_tokens_mistral': 4145, 'n_tokens_neox': 3522, 'n_words': 820}