KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > CachedXPathAPIHolder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.utils;
18
19 import com.sun.org.apache.xpath.internal.CachedXPathAPI;
20 import org.w3c.dom.Document JavaDoc;
21
22
23 /**
24  * @author Raul Benito
25  */

26 public class CachedXPathAPIHolder {
27      static java.util.logging.Logger JavaDoc log =
28             java.util.logging.Logger.getLogger(CachedXPathAPIHolder.class.getName());
29
30     static ThreadLocal JavaDoc local=new ThreadLocal JavaDoc();
31     static ThreadLocal JavaDoc localDoc=new ThreadLocal JavaDoc();
32   
33     /**
34      * Sets the doc for the xpath transformation. Resets the cache if needed
35      * @param doc
36      */

37     public static void setDoc(Document JavaDoc doc) {
38        if (localDoc.get()!=doc) {
39             CachedXPathAPI cx=(CachedXPathAPI)local.get();
40             if (cx==null) {
41                cx=new CachedXPathAPI();
42                local.set(cx);
43                localDoc.set(doc);
44                return;
45             }
46             //Different docs reset.
47
cx.getXPathContext().reset();
48             localDoc.set(doc);
49         }
50     }
51     /**
52      * @return the cachexpathapi for this thread
53      */

54     public static CachedXPathAPI getCachedXPathAPI() {
55         CachedXPathAPI cx=(CachedXPathAPI)local.get();
56         if (cx==null) {
57             cx=new CachedXPathAPI();
58             local.set(cx);
59             localDoc.set(null);
60         }
61         return cx;
62     }
63 }
64
Popular Tags