jdk/src/share/classes/com/sun/org/apache/xml/internal/security/utils/CachedXPathAPIHolder.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1337 e8d6cef36199
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * Copyright  1999-2004 The Apache Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *  Licensed under the Apache License, Version 2.0 (the "License");
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *  you may not use this file except in compliance with the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *  You may obtain a copy of the License at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *  Unless required by applicable law or agreed to in writing, software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *  distributed under the License is distributed on an "AS IS" BASIS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *  See the License for the specific language governing permissions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *  limitations under the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xml.internal.security.utils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
import com.sun.org.apache.xpath.internal.CachedXPathAPI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Raul Benito
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
public class CachedXPathAPIHolder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
         static java.util.logging.Logger log =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
                java.util.logging.Logger.getLogger(CachedXPathAPIHolder.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static ThreadLocal  local=new ThreadLocal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static ThreadLocal localDoc=new ThreadLocal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
         * Sets the doc for the xpath transformation. Resets the cache if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
         * @param doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        public static void setDoc(Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
       if (localDoc.get()!=doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            CachedXPathAPI cx=(CachedXPathAPI)local.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            if (cx==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
               cx=new CachedXPathAPI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
               local.set(cx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
               localDoc.set(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            //Different docs reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            cx.getXPathContext().reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            localDoc.set(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @return the cachexpathapi for this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static CachedXPathAPI getCachedXPathAPI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        CachedXPathAPI cx=(CachedXPathAPI)local.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (cx==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            cx=new CachedXPathAPI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            local.set(cx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            localDoc.set(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return cx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}