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