SAX解析web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
 <web-app>
 <servlet>
  <servlet-name>login</servlet-name>
  <servlet-class>com.sxt.server.basic.servlet.LoginServlet</servlet-class>
 </servlet>
   <servlet>
  <servlet-name>reg</servlet-name>
  <servlet-class>com.sxt.server.basic.servlet.RegisterServlet</servlet-class>
 </servlet>   
 <servlet-mapping>
  <servlet-name>login</servlet-name>
  <url-pattern>/login</url-pattern> 
  <url-pattern>/g</url-pattern> 
 </servlet-mapping>  
 <servlet-mapping>
  <servlet-name>reg</servlet-name>
  <url-pattern>/reg</url-pattern> 
 </servlet-mapping>
 </web-app>

Entity类存储 标签:

public class Entity {
	private String name;
	private String clz;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getClz() {
		return clz;
	}

	public void setClz(String clz) {
		this.clz = clz;
	}

	public Entity() {

	}

}

Mapping类存储标签:

public class Mapping {
	private String name; 
	private Set<String> patterns;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<String> getPatterns() {
		return patterns;
	}

	public void setPatterns(Set<String> patterns) {
		this.patterns = patterns;
	}

	public Mapping() {
		patterns=new HashSet<String>();
	}
	
	public void addPattern(String pattern) {
		this.patterns.add(pattern);
	}

}

SAX解析存储:

public class XMLTest02 {
	public static void main(String[] args) throws Exception {
		// SAX解析
		// 1、获取SAX解析工厂
		SAXParserFactory factory = SAXParserFactory.newInstance();
		// 2、从解析工厂中获取解析器
		SAXParser parse = factory.newSAXParser();
		// 3、加载文档Document注册处理器
		// 4、编写处理器
		WebHandler handler = new WebHandler();
		// 5、解析
		parse.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/sxt/server/basic/servlet/web.xml"),
				handler);
		//6、获取数据
		WebContext context=new WebContext(handler.getEntitys(), handler.getMappings());
		//假设输入了:**/login
		String className=context.getClz("/g");
		Class clz=Class.forName(className);
		//反射
		Servlet servlet=(Servlet)clz.getConstructor().newInstance();
		System.out.println(servlet);
		servlet.service();
	}
}

class WebHandler extends DefaultHandler {
	private List<Entity> entitys;
	private List<Mapping> mappings;
	private Entity entity;
	private Mapping mapping;
	private String tag;//存储操作的标签
	private boolean isMapping=false;
	@Override
	public void startDocument() throws SAXException {
		entitys=new ArrayList<Entity>();
		mappings=new ArrayList<Mapping>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (null != qName) {
			tag = qName;// 存储标签名
			if (tag.equals("servlet")) {
				entity = new Entity();
				isMapping=false;
			}else if(tag.equals("servlet-mapping")){
				mapping=new Mapping();
				isMapping=true;
			}
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String contents = new String(ch, start, length).trim();
		if(null!=tag) {//处理空的问题
			if(isMapping) {//操作servlet-mapping
				if(tag.equals("servlet-name")) {
					mapping.setName(contents);
				}else if(tag.equals("url-pattern")){
					mapping.addPattern(contents);
				}
			}else {//操作servlet
				if(tag.equals("servlet-name")) {
					entity.setName(contents);
				}else if(tag.equals("servlet-class")){
					entity.setClz(contents);
				}
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if(qName!=null) {//处理空的问题
			if(qName.equals("servlet")) {
				entitys.add(entity);
			}else if(qName.equals("servlet-mapping")) {
				mappings.add(mapping);
			}
		}
		tag=null;//tag丢弃,不然会导致标签值被空值覆盖
	}

	@Override
	public void endDocument() throws SAXException {
	}

	
	public List<Entity> getEntitys() {
		return entitys;
	}


	public List<Mapping> getMappings() {
		return mappings;
	}


	
}

你可能感兴趣的:(#,手写webserver)