KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xmladder > Data


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package xmladder;
16
17 import org.apache.commons.digester.*;
18 import java.util.logging.*;
19 import java.io.*;
20
21
22 import org.quickserver.net.server.ClientData;
23 import org.quickserver.util.pool.PoolableObject;
24 import org.apache.commons.pool.BasePoolableObjectFactory;
25 import org.apache.commons.pool.PoolableObjectFactory;
26
27 import java.net.*;
28 import java.io.*;
29 import java.util.logging.*;
30
31 public class Data implements ClientData, PoolableObject {
32     private static Logger logger = Logger.getLogger(Data.class.getName());
33
34     private StringBuffer JavaDoc xmlDump = new StringBuffer JavaDoc();
35
36     public void addXmlPart(String JavaDoc data) throws Exception JavaDoc {
37         xmlDump.append(data);
38     }
39
40     public synchronized String JavaDoc getNextXML() {
41         int i1 = xmlDump.indexOf("<");
42         int i2 = xmlDump.indexOf(">",i1);
43         if(i1!=-1 && i2!=-1) {
44             String JavaDoc tag = xmlDump.substring(i1+1, i2);
45             int j = -1;
46             String JavaDoc xmlEndTag = null;
47             if(tag.trim().charAt(tag.trim().length()-1)=='/') {
48                 j = i2;
49                 xmlEndTag = ">";
50             } else {
51                 xmlEndTag = "</" + tag + ">";
52                 j = xmlDump.indexOf(xmlEndTag, i2);
53             }
54             if(j!=-1) {
55                 String JavaDoc data = xmlDump.substring(i1, j+xmlEndTag.length());
56                 xmlDump.delete(i1, j+xmlEndTag.length());
57                 return data;
58             }
59         }
60         return null;
61     }
62
63     //-- PoolableObject
64
public void clean() {
65         xmlDump.setLength(0);
66     }
67
68     public boolean isPoolable() {
69         return true;
70     }
71
72     public PoolableObjectFactory getPoolableObjectFactory() {
73         return new BasePoolableObjectFactory() {
74             public Object JavaDoc makeObject() {
75                 return new Data();
76             }
77             public void passivateObject(Object JavaDoc obj) {
78                 Data ed = (Data)obj;
79                 ed.clean();
80             }
81             public void destroyObject(Object JavaDoc obj) {
82                 if(obj==null) return;
83                 passivateObject(obj);
84                 obj = null;
85             }
86             public boolean validateObject(Object JavaDoc obj) {
87                 if(obj==null)
88                     return false;
89                 else
90                     return true;
91             }
92         };
93     }
94 }
95
Popular Tags