适配器模式举例【结构模式第八篇】

XMLProperties与适配器模式举例:

//-----
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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值