01: package uk.org.ponder.saxalizer;
02:
03: import uk.org.ponder.util.Logger;
04:
05: import uk.org.ponder.intutil.intVector;
06:
07: import uk.org.ponder.arrayutil.ArrayUtil;
08:
09: /** The SAXHPool implements a global pool of SAXalizerHelper (and
10: * hence SAXalizer) objects that may be dipped into by all clients in
11: * a threadsafe manner. SAXalizerHelpers are supplied from the
12: * getSAXalizerHelper method, and are automatically returned to the
13: * pool when their parse is complete. The purpose of this class is to
14: * avoid frequent construction of these highly expensive SAX parser
15: * objects, and also to avoid the overhead (and danger) of the silly
16: * situation we once had when each major class kept its own pet
17: * SAXalizer for its personal use.
18: * <p> Typical usage of this class is as
19: * <code>SAXHPool.getSAXalizerHelper().produceSubtree(rootobj, stream)</code>;
20: */
21:
22: public class SAXHPool {
23: private static SAXalizerHelper[] saxers = new SAXalizerHelper[10];
24: private static int saxersfilled = 0;
25: private static intVector freeindices = new intVector(10);
26:
27: private static ParseCompleteCallback callback = new ParseCompleteCallback() {
28: public void parseComplete(int returningindex) {
29: synchronized (saxers) {
30: Logger.println("Returning SAXH to element "
31: + returningindex);
32: freeindices.addElement(returningindex);
33: }
34: }
35: };
36:
37: /** Returns a SAXalizerHelper object.
38: * @return The required SAXalizerHelper.
39: */
40: public static SAXalizerHelper getSAXalizerHelper() {
41: return getSAXalizerHelper(null);
42: }
43:
44: /* Returns a SAXalizerHelper object bound to a particular entity resolver.
45: * @param ers The entity resolver to be used by the supplied SAXalizerHelper.
46: * @return The required SAXalizerHelper.
47: */
48: public static SAXalizerHelper getSAXalizerHelper(
49: EntityResolverStash ers) {
50: synchronized (saxers) {
51: int indextogo;
52: if (freeindices.size() > 0) {
53: indextogo = freeindices.popElement();
54: } else {
55: if (saxersfilled == saxers.length) {
56: saxers = (SAXalizerHelper[]) ArrayUtil.expand(
57: saxers, 2.0);
58: }
59: saxers[saxersfilled] = new SAXalizerHelper();
60: ++saxersfilled;
61: indextogo = saxersfilled - 1;
62: }
63: Logger.println("Allocating SAXH at element " + indextogo);
64: SAXalizerHelper togo = saxers[indextogo];
65: togo.setEntityResolverStash(ers);
66: togo.setParseCompleteCallback(callback, indextogo);
67: return togo;
68: }
69: }
70:
71: }
|