001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package xmladder;
016:
017: import org.apache.commons.digester.*;
018: import java.util.logging.*;
019: import java.io.*;
020:
021: /*
022: <add-number-req>
023: <number-a>5</number-a>
024: <number-b>15</number-b>
025: </add-number-req>
026: */
027: public class AddNumberReq {
028: private static Logger logger = Logger.getLogger(AddNumberReq.class
029: .getName());
030: //make true for performance testing
031: private static boolean performanceTest = false;
032: private static Digester digester = null;
033:
034: static {
035: performanceTest = Boolean
036: .getBoolean("xmladder.AddNumberReq.performanceTest");
037:
038: if (performanceTest == false) {
039: digester = new Digester();
040: digester.setValidating(false);
041: digester.setUseContextClassLoader(true);
042: String mainTag = "add-number-req";
043: digester.addObjectCreate(mainTag, AddNumberReq.class);
044: digester.addBeanPropertySetter(mainTag + "/number-a",
045: "numberA");
046: digester.addBeanPropertySetter(mainTag + "/number-b",
047: "numberB");
048: }
049: }
050:
051: private int numberA;
052: private int numberB;
053:
054: /**
055: * @return
056: */
057: public int getNumberA() {
058: return numberA;
059: }
060:
061: /**
062: * @param numberA
063: */
064: public void setNumberA(int numberA) {
065: this .numberA = numberA;
066: }
067:
068: /**
069: * @return
070: */
071: public int getNumberB() {
072: return numberB;
073: }
074:
075: /**
076: * @param numberB
077: */
078: public void setNumberB(int numberB) {
079: this .numberB = numberB;
080: }
081:
082: public String toXML() {
083: StringBuffer sb = new StringBuffer();
084: sb.append("<add-number-req>\n");
085: sb.append("\t<number-a>").append(getNumberA()).append(
086: "</number-a>\n");
087: sb.append("\t<number-b>").append(getNumberB()).append(
088: "</number-b>\n");
089: sb.append("</add-number-req>");
090: return sb.toString();
091: }
092:
093: public static AddNumberReq fromXML(String data) throws IOException,
094: org.xml.sax.SAXException {
095: AddNumberReq addNumberReq = null;
096: if (performanceTest == false) {
097: logger.fine("Got xml:\n" + data);
098:
099: addNumberReq = (AddNumberReq) digester
100: .parse(new ByteArrayInputStream(data
101: .getBytes("UTF-8")));
102: } else {
103: addNumberReq = new AddNumberReq();
104: addNumberReq.setNumberA(14);
105: addNumberReq.setNumberB(9);
106: }
107: return addNumberReq;
108: }
109: }
|