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

org.tinygroup.weblayer.util.TinyConfigUtil Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 1997-2013, www.tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
 *
 *  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.tinygroup.weblayer.util;

import org.tinygroup.parser.filter.NameFilter;
import org.tinygroup.xmlparser.node.XmlNode;

import java.util.*;

/**
 * 配置解析工具类
 * @author renhui
 *
 */
public final class TinyConfigUtil {
	
	private static final String URL_PATTERN = "url-pattern";

	private static final String INIT_PARAM = "init-param";

	private static final String SERVLET_MAPPING = "servlet-mapping";
	
	private static final String FILTER_MAPPING = "filter-mapping";

	public static Map getInitParam(XmlNode xmlNode){
		Map initParamMap = new HashMap();
		NameFilter nameFilter = new NameFilter(xmlNode);
		List initParamNodes = nameFilter.findNodeList(INIT_PARAM);
		for (XmlNode initParamNode : initParamNodes) {
			String name = initParamNode.getAttribute("name");
			String value = initParamNode.getAttribute("value");
			initParamMap.put(name, value);
		}
		return initParamMap;
	}
	
	public static Set getServletMapping(XmlNode xmlNode){
		return getUrlMapping(xmlNode,SERVLET_MAPPING);
	}
	
	public static Set getFiltterMapping(XmlNode xmlNode){
		return getUrlMapping(xmlNode,FILTER_MAPPING);
	}

	public static XmlNode createServletXmlNode(String nodeName,Map initParams,Set patterns){
		return createXmlNode(nodeName, SERVLET_MAPPING, initParams, patterns);
	}
	
	public static XmlNode createFilterXmlNode(String nodeName,Map initParams,Set patterns){
		return createXmlNode(nodeName, FILTER_MAPPING, initParams, patterns);
	}
	
	
	private static Set getUrlMapping(XmlNode xmlNode,String nodeName) {
		Set patternStrs = new HashSet();
		NameFilter nameFilter = new NameFilter(xmlNode);
		List servletMappings = nameFilter
				.findNodeList(nodeName);
		for (XmlNode servletMapping : servletMappings) {
			String urlPattern = servletMapping.getAttribute(URL_PATTERN);
			patternStrs.add(urlPattern);
		}
		return patternStrs;
	}
	
	private static XmlNode createXmlNode(String nodeName,String urlPatternNodeName,Map initParams,Set patterns){
		XmlNode xmlNode=new XmlNode(nodeName);
		if(initParams!=null){
			for (String name : initParams.keySet()) {
				String value=initParams.get(name);
				XmlNode initNode=new XmlNode(INIT_PARAM);
				initNode.setAttribute("name", name);
				initNode.setAttribute("value", value);
				xmlNode.addNode(initNode);
			}
		}
		if(patterns!=null){
			for (String urlPattern : patterns) {
				xmlNode.addNode(createPatternNode(urlPatternNodeName,urlPattern));
			}
		}
		
		return xmlNode;
		
	}

	private static XmlNode createPatternNode(String nodeName,String urlPattern) {
		XmlNode urlPatternNode=new XmlNode(nodeName);
		urlPatternNode.setAttribute(URL_PATTERN, urlPattern);
		return urlPatternNode;
	}
	
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy