KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > blog > TrackBackResp


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.blog;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23 import org.apache.commons.digester.Digester;
24 import org.xml.sax.SAXException JavaDoc;
25
26 /**
27  * TrackBack执行后的回应
28  * @author Winter Lau
29  */

30 public class TrackBackResp implements Serializable JavaDoc {
31
32     int error;
33     String JavaDoc message;
34     String JavaDoc encode = "UTF-8";
35
36     public static TrackBackResp parse(InputStream JavaDoc in) throws IOException JavaDoc, SAXException JavaDoc{
37         Digester digester = new Digester();
38         digester.setValidating(false);
39         digester.addObjectCreate("response", TrackBackResp.class);
40         digester.addBeanPropertySetter("response/error","error");
41         digester.addBeanPropertySetter("response/message","message");
42         return (TrackBackResp)digester.parse(in);
43     }
44     
45     public static TrackBackResp parse(String JavaDoc xml) throws IOException JavaDoc, SAXException JavaDoc{
46         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(xml.getBytes());
47         Digester digester = new Digester();
48         digester.setValidating(false);
49         digester.addObjectCreate("response", TrackBackResp.class);
50         digester.addBeanPropertySetter("response/error","error");
51         digester.addBeanPropertySetter("response/message","message");
52         return (TrackBackResp)digester.parse(bais);
53     }
54     
55     public String JavaDoc toString(){
56         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
57         xml.append("<?xml version=\"1.0\" encoding=\"");
58         xml.append(encode);
59         xml.append("\"?>\n");
60         xml.append("<response>\n");
61         xml.append("\t<error>");
62         xml.append(error);
63         xml.append("</error>");
64         if(message!=null){
65             xml.append("\t<message>");
66             xml.append(message);
67             xml.append("</message>");
68         }
69         xml.append("\n</response>");
70         return xml.toString();
71     }
72     
73     public int getError() {
74         return error;
75     }
76     public void setError(int error) {
77         this.error = error;
78     }
79     public String JavaDoc getMessage() {
80         return message;
81     }
82     public void setMessage(String JavaDoc message) {
83         this.message = message;
84     }
85     public String JavaDoc getEncode() {
86         return encode;
87     }
88     public void setEncode(String JavaDoc encode) {
89         this.encode = encode;
90     }
91     public static void main(String JavaDoc[] args) throws IOException JavaDoc, SAXException JavaDoc{
92         TrackBackResp resp = new TrackBackResp();
93         resp.setError(0);
94         System.out.println(TrackBackResp.parse(resp.toString()));
95     }
96 }
97
Popular Tags