SAX_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > SAX

SAX

 2014/5/4 12:50:44  349946334  程序员俱乐部  我要评论(0)
  • 摘要:<?xmlversion="1.0"encoding="UTF-8"?><captchas><captchaname="default"><wordMinLength></wordMinLength><wordMaxLength></wordMaxLength><imageWidth></imageWidth><imageHeight></imageHeight>
  • 标签:
class="java" name="code">
<?xml version="1.0" encoding="UTF-8"?>
<captchas>
	<captcha name="default">
		<wordMinLength></wordMinLength>
		<wordMaxLength></wordMaxLength>
		<imageWidth></imageWidth>
		<imageHeight></imageHeight>
		<fontMinSize></fontMinSize>
		<fontMaxSize></fontMaxSize>
		<wordGenerator>
			<value>digitalWordGenerator</value>
			<value>letterWordGenerator</value>
			<value>digitalAndLetterWordGenerator</value>
			<value>chineseWordGenerator</value>
			<value>arithmeticWordGenerator</value>
		</wordGenerator>
	</captcha>
</captchas>

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.lianzhong.captcha.model.ImageCaptchaGroup;

public class XmlParseUtil {
	private static Logger logger = LoggerFactory.getLogger(XmlParseUtil.class);
	private static Map<String, ImageCaptchaGroup> map = new HashMap<String, ImageCaptchaGroup>();
	private List<String> wordList;
	private ImageCaptchaGroup group;
	private String value;
	private String key;
	private String tag;
	
	public static Map<String,ImageCaptchaGroup> parseImageCaptchaXML() {
		try {
			logger.info("parse imgaeCaptcha.xml-----main!");
			XmlParseUtil util = new XmlParseUtil();
			XmlParse handler = util.new XmlParse();
			String xmlfile = "./src/imageCaptcha.xml";
			SAXParserFactory spf = SAXParserFactory.newInstance();
			spf.setValidating(false);
			SAXParser parser = spf.newSAXParser();
			InputSource source = new InputSource(xmlfile);
			parser.parse(source,handler);
			
			Set<String> keys = map.keySet();
			for (Iterator<String> it = keys.iterator(); it.hasNext();) {
				String key = it.next();
				ImageCaptchaGroup g = map.get(key);
				logger.info("groupKey:"+key);
				logger.info("groupBody:"+g.toString());
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("parse imgaeCaptcha.xml-----failed!",e);
		}
		return map;
	}
	
	class XmlParse extends DefaultHandler {
		@Override
		public void startDocument() throws SAXException {
			// TODO Auto-generated method stub
			logger.info("parse imgaeCaptcha.xml-----startDocument!");
		}
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			// TODO Auto-generated method stub
			if ("captcha".equals(qName)) {
				group = new ImageCaptchaGroup();
				String nameAttributes = attributes.getValue("name");
				key = nameAttributes;
			}
			if ("wordGenerator".equals(qName)) {
				wordList = new ArrayList<String>();
			}
			tag = qName;
		}
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			// TODO Auto-generated method stub
			if ("wordGenerator".equals(qName)) {
				group.setWordGeneratorNameList(wordList);
				wordList = null;
			}
			if ("captcha".equals(qName)) {
				map.put(key, group);
			}
			tag = null;
		}
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			// TODO Auto-generated method stub
			if (tag!=null) {
				value = new String(ch,start,length);
				if ("wordMinLength".equals(tag))
					group.setWordMinLength(Integer.valueOf(value));
				if ("wordMaxLength".equals(tag))
					group.setWordMaxLength(Integer.valueOf(value));
				if ("imageWidth".equals(tag))
					group.setImageWidth(Integer.valueOf(value));
				if ("imageHeight".equals(tag))
					group.setImageHeight(Integer.valueOf(value));
				if ("fontMinSize".equals(tag))
					group.setFontMinSize(Integer.valueOf(value));
				if ("fontMaxSize".equals(tag))
					group.setFontMaxSize(Integer.valueOf(value));
				if ("value".equals(tag))
					wordList.add(value);
			}
		}
	}
	
	
}

上一篇: C#入门经典札记_Ch05变量的更多内容 下一篇: 没有下一篇了!
  • 相关文章
发表评论
用户名: 匿名