001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.blog;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.Serializable;
022:
023: import org.apache.commons.digester.Digester;
024: import org.xml.sax.SAXException;
025:
026: /**
027: * TrackBack执行后的回应
028: * @author Winter Lau
029: */
030: public class TrackBackResp implements Serializable {
031:
032: int error;
033: String message;
034: String encode = "UTF-8";
035:
036: public static TrackBackResp parse(InputStream in)
037: throws IOException, SAXException {
038: Digester digester = new Digester();
039: digester.setValidating(false);
040: digester.addObjectCreate("response", TrackBackResp.class);
041: digester.addBeanPropertySetter("response/error", "error");
042: digester.addBeanPropertySetter("response/message", "message");
043: return (TrackBackResp) digester.parse(in);
044: }
045:
046: public static TrackBackResp parse(String xml) throws IOException,
047: SAXException {
048: ByteArrayInputStream bais = new ByteArrayInputStream(xml
049: .getBytes());
050: Digester digester = new Digester();
051: digester.setValidating(false);
052: digester.addObjectCreate("response", TrackBackResp.class);
053: digester.addBeanPropertySetter("response/error", "error");
054: digester.addBeanPropertySetter("response/message", "message");
055: return (TrackBackResp) digester.parse(bais);
056: }
057:
058: public String toString() {
059: StringBuffer xml = new StringBuffer();
060: xml.append("<?xml version=\"1.0\" encoding=\"");
061: xml.append(encode);
062: xml.append("\"?>\n");
063: xml.append("<response>\n");
064: xml.append("\t<error>");
065: xml.append(error);
066: xml.append("</error>");
067: if (message != null) {
068: xml.append("\t<message>");
069: xml.append(message);
070: xml.append("</message>");
071: }
072: xml.append("\n</response>");
073: return xml.toString();
074: }
075:
076: public int getError() {
077: return error;
078: }
079:
080: public void setError(int error) {
081: this .error = error;
082: }
083:
084: public String getMessage() {
085: return message;
086: }
087:
088: public void setMessage(String message) {
089: this .message = message;
090: }
091:
092: public String getEncode() {
093: return encode;
094: }
095:
096: public void setEncode(String encode) {
097: this .encode = encode;
098: }
099:
100: public static void main(String[] args) throws IOException,
101: SAXException {
102: TrackBackResp resp = new TrackBackResp();
103: resp.setError(0);
104: System.out.println(TrackBackResp.parse(resp.toString()));
105: }
106: }
|