KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > DocumentPool


1 package net.sf.saxon.om;
2 import java.util.HashMap JavaDoc;
3 import java.util.Iterator JavaDoc;
4
5 /**
6   * An object representing the collection of documents handled during
7   * a single transformation.
8   *
9   * <p>From Saxon 7.2, the function of allocating document numbers is performed
10   * by the NamePool, not by the DocumentPool. This has a
11   * number of effects: in particular it allows operations involving multiple
12   * documents (such as generateId() and document()) to occur in a free-standing
13   * XPath environment.</p>
14   */

15
16 public final class DocumentPool {
17
18     // The document pool ensures that the document()
19
// function, when called twice with the same URI, returns the same document
20
// each time. For this purpose we use a hashtable from
21
// URI to DocumentInfo object.
22

23     private HashMap JavaDoc documentNameMap = new HashMap JavaDoc(10);
24
25     /**
26     * Add a document to the pool
27     * @param doc The DocumentInfo for the document in question
28     * @param name The name of the document.
29     */

30
31     public void add(DocumentInfo doc, String JavaDoc name) {
32         if (name!=null) {
33             documentNameMap.put(name, doc);
34         }
35     }
36
37     /**
38     * Get the document with a given name
39     * @return the DocumentInfo with the given name if it exists,
40     * or null if it is not found.
41     */

42
43     public DocumentInfo find(String JavaDoc name) {
44         return (DocumentInfo)documentNameMap.get(name);
45     }
46
47     /**
48      * Release a document from the document pool. This means that if the same document is
49      * loaded again later, the source will need to be re-parsed, and nodes will get new identities.
50      */

51
52     public DocumentInfo discard(DocumentInfo doc) {
53         Iterator JavaDoc iter = documentNameMap.keySet().iterator();
54         while (iter.hasNext()) {
55             Object JavaDoc name = iter.next();
56             DocumentInfo entry = (DocumentInfo) documentNameMap.get(name);
57             if (entry == doc) {
58                 documentNameMap.remove(name);
59                 return doc;
60             }
61         }
62         return doc;
63     }
64
65 }
66
67 //
68
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
69
// you may not use this file except in compliance with the License. You may obtain a copy of the
70
// License at http://www.mozilla.org/MPL/
71
//
72
// Software distributed under the License is distributed on an "AS IS" basis,
73
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
74
// See the License for the specific language governing rights and limitations under the License.
75
//
76
// The Original Code is: all this file.
77
//
78
// The Initial Developer of the Original Code is Michael H. Kay.
79
//
80
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
81
//
82
// Contributor(s): none.
83
//
84
Popular Tags