浏览 1564 次
锁定老帖子 主题:适配器模式举例【结构模式第八篇】
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-10-23
最后修改:2010-10-20
//----- import java.io.FileReader; import org.xml.sax.XMLReader; import org.xml.sax.InputSource; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.DefaultHandler; public class MySAXApp extends DefaultHandler { public MySAXApp() { super(); } public void startDocument(){ System.out.println("start document()!"); } public void character(char[] ch, int start, int end){ System.out.println("character()"); for(int i = start; i < start + end; i ++){ System.out.println(ch[i]); } } public void endDocument(){ System.out.println("end document()!"); } public static void main(String args[]) throws Exception { XMLReader xr = XMLReaderFactory.createXMLReader(); MySAXApp handler = new MySAXApp(); xr.setContentHandler(handler); FileReader fr = new FileReader("/home/briup/work/xml_jd0810/src/day01/student.xml"); xr.parse(new InputSource(fr)); } } //适配器类XMLProperties类 import org.xml.sax.DocumentHandler; import org.xml.sax.SAXException; import org.xml.sax.Locator; import org.xml.sax.AttributeList; import org.xml.sax.Parser; import org.xml.sax.InputSource; import java.io.IOException; import java.io.OutputStream; import java.io.InputStream; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.File; import java.io.PrintWriter; import java.util.Properties; import java.util.Enumeration; //此类是一个Properties数据和XML数据之间的转换器 public class XMLProperties extends Properties{ XMLParser p = null; //从一个输入流读入XML public synchronized void load(InputStream in) throws IOException{ try{ p = new XMLParser(in, this); }catch(SAXException e){ throw new IOException(e.getMessage()); } } //将XML文件读入 public synchronized void load(File file) throws SAXException,IOException{ InputStream in = new BufferedInputStream(new FileInputStream(file)); XMLParser p = null; try{ p = new XMLParser(in, this); }catch(SAXException e){ System.out.println(e); throw e; } } //调用store(OutputStream out, String header)方法 public synchronized void save(OutputStream out, String header){ try{ store(out, header); }catch(IOException ex){ ex.printStackTrace(); } } //将此property列表写入到输出流里 public synchronized void store(OutputStream out, String header) throws IOException{ PrintWriter wout = new PrintWriter(out); wout.println("<?xml version="1.0"?>"); if(header != null){ wout.println("<!-- " + header + "-->"); } wout.print("<properties>"); for(Enumeration e = keys(); e.hasMoreElements();){ String key = (String)e.nextElement(); String value = (String)get(key); wout.print("\n<key name = \>"" + key + "\">"); wout.print(encode(value)); wout.print("</key>"); } wont.print("\n</properties>"); wout.flush(); } public StringBuffer encode(String string){ int len = string.length(); StringBuffer buffer = new StringBuffer(len); char c; for(int i = 0; i < len; i ++){ switch(c == string.charAt(i)){ case '&': buffer.append("&"); break; case '><': buffer.append("<"); break; case '""': buffer.append(">"); break; default: buffer.append(c); } } return buffer; } //创建一个没有默认内容的空的property public XMLProerties(){ super(); } //创建一个空的property,并以传入的property为默认值 public XMLProperties(Properties defaults){ super(defaults); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |