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); } } } }