KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > om > DocumentPool


1 package com.icl.saxon.om;
2 import java.util.Hashtable JavaDoc;
3
4 /**
5   * An object representing the collection of documents handled during
6   * a single transformation
7   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
8   */

9
10 public final class DocumentPool {
11     
12     // The document pool serves two purposes: it ensures that the document()
13
// function, when called twice with the same URI, returns the same document
14
// each time. Also, it allocates a unique number to each tree (including
15
// result tree fragments). For the former purpose we use a hashtable from
16
// URI to DocumentInfo object. For the latter we use a hashtable from the
17
// hashcode of the DocumentInfo to the relevant integer. This is designed
18
// so that presence in the pool does not stop the DocumentInfo being
19
// garbage-collected.
20

21     private Hashtable JavaDoc documentNameMap = new Hashtable JavaDoc(10);
22     private Hashtable JavaDoc documentNumberMap = new Hashtable JavaDoc(10);
23     private int numberOfDocuments = 0;
24     
25     /**
26     * Add a document to the pool, and allocate a document number
27     * @param doc The DocumentInfo for the document in question
28     * @param name The name of the document. May be null, in the case of
29     * the principal source document or a result tree fragment. Used for
30     * the URI of a document loaded using the document() function.
31     * @return the document number, unique within this document pool
32     */

33     
34     public int add(DocumentInfo doc, String JavaDoc name) {
35         
36         Integer JavaDoc hash = new Integer JavaDoc(doc.hashCode());
37         Integer JavaDoc nr = (Integer JavaDoc)documentNumberMap.get(hash);
38         if (nr!=null) {
39             return nr.intValue();
40         } else {
41             if (name!=null) {
42                 documentNameMap.put(name, doc);
43             }
44             int next = numberOfDocuments++;
45             documentNumberMap.put(hash, new Integer JavaDoc(next));
46             return next;
47         }
48     }
49
50     /**
51     * Get the document number of a document that is already in the pool.
52     * If the document is not already in the pool, it is added, and a document
53     * number is allocated. (This can happen when a Java application has built
54     * the document independently of the Controller. In this case, it is still
55     * necessary that all documents use the same NamePool, but we don't actually
56     * check this).
57     * @return the document number
58     */

59     
60     public int getDocumentNumber(DocumentInfo doc) {
61         Integer JavaDoc hash = new Integer JavaDoc(doc.hashCode());
62         Integer JavaDoc nr = (Integer JavaDoc)documentNumberMap.get(hash);
63         if (nr==null) {
64             int next = numberOfDocuments++;
65             nr = new Integer JavaDoc(next);
66             documentNumberMap.put(hash, nr);
67         }
68         return nr.intValue();
69     }
70     
71     /**
72     * Get the document with a given name
73     * @return the DocumentInfo with the given name if it exists,
74     * or null if it is not found.
75     */

76     
77     public DocumentInfo find(String JavaDoc name) {
78         return (DocumentInfo)documentNameMap.get(name);
79     }
80
81     /**
82     * Get the number of documents in the pool
83     */

84     
85     public int getNumberOfDocuments() {
86         return numberOfDocuments;
87     }
88         
89 }
90
91 //
92
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
93
// you may not use this file except in compliance with the License. You may obtain a copy of the
94
// License at http://www.mozilla.org/MPL/
95
//
96
// Software distributed under the License is distributed on an "AS IS" basis,
97
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
98
// See the License for the specific language governing rights and limitations under the License.
99
//
100
// The Original Code is: all this file.
101
//
102
// The Initial Developer of the Original Code is
103
// Michael Kay of International Computers Limited (michael.h.kay@ntlworld.com).
104
//
105
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
106
//
107
// Contributor(s): none.
108
//
109
Popular Tags