GME  13
DOMXPathNSResolverImpl.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "DOMXPathNSResolverImpl.hpp"
00019 #include <xercesc/dom/DOMNode.hpp>
00020 #include <xercesc/util/XMLString.hpp>
00021 #include <xercesc/util/Janitor.hpp>
00022 #include <xercesc/util/XMLString.hpp>
00023 
00024 XERCES_CPP_NAMESPACE_BEGIN
00025 
00026 DOMXPathNSResolverImpl::DOMXPathNSResolverImpl(const DOMNode *nodeResolver, MemoryManager* const manager) :
00027     fNamespaceBindings(0),
00028     fResolverNode(nodeResolver),
00029     fManager(manager)
00030 {
00031     fNamespaceBindings = new (fManager) RefHashTableOf<KVStringPair>(7, true, fManager);
00032 }
00033 
00034 DOMXPathNSResolverImpl::~DOMXPathNSResolverImpl()
00035 {
00036     delete fNamespaceBindings;
00037 }
00038 
00039 const XMLCh* DOMXPathNSResolverImpl::lookupNamespaceURI(const XMLCh* prefix) const
00040 {
00041     if(prefix == 0) prefix = XMLUni::fgZeroLenString;
00042 
00043     if(XMLString::equals(prefix, XMLUni::fgXMLString))
00044         return XMLUni::fgXMLURIName;
00045 
00046     const KVStringPair *pair = fNamespaceBindings->get((void*)prefix);
00047     if(pair) {
00048 
00049         // An empty namespace URI indicated that this binding was removed
00050         // by the user.
00051         //
00052         if(*pair->getValue() == 0) return NULL;
00053 
00054         return pair->getValue();
00055     }
00056 
00057     if(fResolverNode)
00058       return fResolverNode->lookupNamespaceURI(
00059         *prefix == 0 ? 0 : prefix); // Expects 0 for default namespace.
00060 
00061     return NULL;
00062 }
00063 
00064 const XMLCh* DOMXPathNSResolverImpl::lookupPrefix(const XMLCh* uri) const
00065 {
00066     if (uri == 0 || *uri == 0)
00067         return 0;
00068 
00069     if(XMLString::equals(uri, XMLUni::fgXMLURIName))
00070         return XMLUni::fgXMLString;
00071 
00072     RefHashTableOfEnumerator<KVStringPair> enumerator((RefHashTableOf<KVStringPair>*)fNamespaceBindings);
00073     while(enumerator.hasMoreElements()) {
00074         KVStringPair &pair = enumerator.nextElement();
00075         if(XMLString::equals(pair.getValue(), uri)) {
00076             return pair.getKey();
00077         }
00078     }
00079 
00080     if(fResolverNode)
00081     {
00082       const XMLCh* r = fResolverNode->lookupPrefix(uri);
00083 
00084       if (r == 0 && fResolverNode->isDefaultNamespace(uri))
00085         r = XMLUni::fgZeroLenString;
00086 
00087       return r;
00088     }
00089 
00090     return 0;
00091 }
00092 
00093 void DOMXPathNSResolverImpl::addNamespaceBinding(const XMLCh* prefix, const XMLCh* uri)
00094 {
00095     if(prefix == 0) prefix = XMLUni::fgZeroLenString;
00096     if(uri == 0) uri = XMLUni::fgZeroLenString;
00097 
00098     KVStringPair* pair = new (fManager) KVStringPair(prefix, uri, fManager);
00099 
00100     fNamespaceBindings->put((void*)pair->getKey(), pair);
00101 }
00102 
00103 void DOMXPathNSResolverImpl::release()
00104 {
00105     DOMXPathNSResolverImpl* me=(DOMXPathNSResolverImpl*)this;
00106     delete me;
00107 }
00108 
00109 XERCES_CPP_NAMESPACE_END