01: /**
02: *
03: * © Copyright International Business Machines Corporation 2004, 2006.
04: * All rights reserved.
05: *
06: * The 'ReaderPool' class provides a pooling mechanism for XML readers.
07: *
08: * File : ReaderPool.java
09: * Created : 2004/09/08
10: *
11: * @author Rene Pawlitzek (rpa@zurich.ibm.com)
12: * @version 2.00, 2004/09/08
13: * @since JDK 1.3
14: *
15: * History : 2004/09/08, rpa, new file
16: * 2006/03/14, rpa, code review
17: *
18: */package com.ibm.hamlet;
19:
20: import java.util.*;
21: import org.xml.sax.*;
22: import org.xml.sax.helpers.*;
23:
24: public class ReaderPool {
25:
26: private Hashtable readers = new Hashtable();
27:
28: public ReaderPool() {
29: String className = System.getProperty("org.xml.sax.parser");
30: if (className == null)
31: System.setProperty("org.xml.sax.parser",
32: "org.apache.xerces.parsers.SAXParser");
33: } // ReaderPool
34:
35: public synchronized XMLReader getReader () throws Exception {
36: Enumeration enum = readers.keys ();
37: while (enum.hasMoreElements ()) {
38: XMLReader reader = (XMLReader) enum.nextElement ();
39: Boolean b = (Boolean) readers.get (reader);
40: if (b.equals (Boolean.FALSE)) {
41: readers.put (reader, Boolean.TRUE);
42: return reader;
43: } // if
44: } // while
45: XMLReader reader = XMLReaderFactory.createXMLReader ();
46: readers.put (reader, Boolean.TRUE);
47: return reader;
48: } // getReader
49:
50: public synchronized void returnReader(XMLReader aReader) {
51: if (readers.containsKey(aReader))
52: readers.put(aReader, Boolean.FALSE);
53: } // returnReader
54:
55: } // ReaderPool
56:
57: /* ----- End of File ----- */
|