GME  13
DOMImplementationImpl.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 /*
00019  * $Id: DOMImplementationImpl.cpp 671894 2008-06-26 13:29:21Z borisk $
00020  */
00021 
00022 #include "DOMImplementationImpl.hpp"
00023 #include "DOMDocumentImpl.hpp"
00024 #include "DOMDocumentTypeImpl.hpp"
00025 #include "DOMLSSerializerImpl.hpp"
00026 #include "DOMLSInputImpl.hpp"
00027 #include "DOMLSOutputImpl.hpp"
00028 #include "DOMImplementationListImpl.hpp"
00029 
00030 #include <xercesc/dom/DOMDocument.hpp>
00031 #include <xercesc/dom/DOMDocumentType.hpp>
00032 #include <xercesc/dom/DOMException.hpp>
00033 #include <xercesc/util/PlatformUtils.hpp>
00034 #include <xercesc/util/XMLInitializer.hpp>
00035 #include <xercesc/util/XMLUniDefs.hpp>
00036 #include <xercesc/util/XMLChar.hpp>
00037 #include <xercesc/util/XMLStringTokenizer.hpp>
00038 #include <xercesc/util/XMLDOMMsg.hpp>
00039 #include <xercesc/util/XMLMsgLoader.hpp>
00040 #include <xercesc/parsers/DOMLSParserImpl.hpp>
00041 
00042 XERCES_CPP_NAMESPACE_BEGIN
00043 
00044 
00045 // ------------------------------------------------------------
00046 //  Static constants
00047 // ------------------------------------------------------------
00048 static const XMLCh  g1_0[] =     // Points to "1.0"
00049         {chDigit_1, chPeriod, chDigit_0, chNull};
00050 static const XMLCh  g2_0[] =      // Points to "2.0"
00051         {chDigit_2, chPeriod, chDigit_0, chNull};
00052 static const XMLCh  g3_0[] =      // Points to "3.0"
00053         {chDigit_3, chPeriod, chDigit_0, chNull};
00054 static const XMLCh  gTrav[] =     // Points to "Traversal"
00055         {chLatin_T, chLatin_r, chLatin_a, chLatin_v, chLatin_e, chLatin_r,
00056             chLatin_s, chLatin_a, chLatin_l, chNull};
00057 static const XMLCh  gCore[] =     // Points to "Core"
00058         {chLatin_C, chLatin_o, chLatin_r, chLatin_e, chNull};
00059 static const XMLCh  gRange[] =     // Points to "Range"
00060         {chLatin_R, chLatin_a, chLatin_n, chLatin_g, chLatin_e, chNull};
00061 static const XMLCh  gLS[] =     // Points to "LS"
00062         {chLatin_L, chLatin_S, chNull};
00063 static const XMLCh  gXPath[] =     // Points to "XPath"
00064         {chLatin_X, chLatin_P, chLatin_a, chLatin_t, chLatin_h, chNull};
00065 
00066 
00067 static XMLMsgLoader *sMsgLoader = 0;
00068 static DOMImplementationImpl *gDomimp = 0;
00069 
00070 void XMLInitializer::initializeDOMImplementationImpl()
00071 {
00072     sMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgXMLDOMMsgDomain);
00073 
00074     if (!sMsgLoader)
00075       XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain);
00076 
00077     gDomimp = new DOMImplementationImpl;
00078 }
00079 
00080 void XMLInitializer::terminateDOMImplementationImpl()
00081 {
00082     delete gDomimp;
00083     gDomimp = 0;
00084 
00085     delete sMsgLoader;
00086     sMsgLoader = 0;
00087 }
00088 
00089 //
00090 //
00091 XMLMsgLoader* DOMImplementationImpl::getMsgLoader4DOM()
00092 {
00093     return sMsgLoader;
00094 }
00095 
00096 DOMImplementationImpl *DOMImplementationImpl::getDOMImplementationImpl()
00097 {
00098     return gDomimp;
00099 }
00100 
00101 // ------------------------------------------------------------
00102 // DOMImplementation Virtual interface
00103 // ------------------------------------------------------------
00104 bool  DOMImplementationImpl::hasFeature(const  XMLCh * feature,  const  XMLCh * version) const
00105 {
00106     if (!feature)
00107         return false;
00108 
00109     // ignore the + modifier
00110     if(*feature==chPlus)
00111         feature++;
00112 
00113     bool anyVersion = (version == 0 || !*version);
00114     bool version1_0 = XMLString::equals(version, g1_0);
00115     bool version2_0 = XMLString::equals(version, g2_0);
00116     bool version3_0 = XMLString::equals(version, g3_0);
00117 
00118     // Currently, we support only XML Level 1 version 1.0
00119     if (XMLString::compareIStringASCII(feature, XMLUni::fgXMLString) == 0
00120         && (anyVersion || version1_0 || version2_0))
00121         return true;
00122 
00123     if (XMLString::compareIStringASCII(feature, gCore) == 0
00124         && (anyVersion || version1_0 || version2_0 || version3_0))
00125         return true;
00126 
00127     if (XMLString::compareIStringASCII(feature, gTrav) == 0
00128         && (anyVersion || version2_0))
00129         return true;
00130 
00131     if (XMLString::compareIStringASCII(feature, gRange) == 0
00132         && (anyVersion || version2_0))
00133         return true;
00134 
00135     if (XMLString::compareIStringASCII(feature, gLS) == 0
00136         && (anyVersion || version3_0))
00137         return true;
00138 
00139     if (XMLString::compareIStringASCII(feature, gXPath) == 0
00140         && (anyVersion || version3_0))
00141         return true;
00142 
00143     return false;
00144 }
00145 
00146 
00147 //Introduced in DOM Level 2
00148 DOMDocumentType *DOMImplementationImpl::createDocumentType(const XMLCh *qualifiedName,
00149         const XMLCh * publicId, const XMLCh *systemId)
00150 {
00151     // assume XML 1.0 since we do not know its version yet.
00152     if(!XMLChar1_0::isValidName(qualifiedName))
00153         throw DOMException(DOMException::INVALID_CHARACTER_ERR, 0);
00154 
00155     //to do: do we need to create with user's memorymanager???
00156     DOMDocumentTypeImpl* docType = new DOMDocumentTypeImpl(0, qualifiedName, publicId, systemId, true);
00157     return docType;
00158 }
00159 
00160 DOMDocument *DOMImplementationImpl::createDocument(const XMLCh *namespaceURI,
00161         const XMLCh *qualifiedName, DOMDocumentType *doctype,
00162     MemoryManager* const manager)
00163 {
00164     return new (manager) DOMDocumentImpl(namespaceURI, qualifiedName, doctype, this, manager);
00165 }
00166 
00167 
00168 //Introduced in DOM Level 3
00169 void* DOMImplementationImpl::getFeature(const XMLCh*, const XMLCh*) const {
00170     return 0;
00171 }
00172 
00173 // Non-standard extension
00174 DOMDocument *DOMImplementationImpl::createDocument(MemoryManager* const manager)
00175 {
00176         return new (manager) DOMDocumentImpl(this, manager);
00177 }
00178 
00179 //
00180 //  DOMImplementation::getImplementation.  DOMImplementation is supposed to
00181 //                                              be a pure interface class.  This one static
00182 //                                              function is the hook that lets things get started.
00183 DOMImplementation *DOMImplementation::getImplementation()
00184 {
00185     return (DOMImplementation*) DOMImplementationImpl::getDOMImplementationImpl();
00186 }
00187 
00188 bool DOMImplementation::loadDOMExceptionMsg
00189 (
00190       const short                        msgToLoad
00191     ,       XMLCh* const                 toFill
00192     , const XMLSize_t                    maxChars
00193 )
00194 {
00195   // Figure out which exception range this code is and load the corresponding
00196   // message.
00197   //
00198   if (msgToLoad <= 50)
00199   {
00200     // DOMException
00201     return sMsgLoader->loadMsg(XMLDOMMsg::DOMEXCEPTION_ERRX+msgToLoad, toFill, maxChars);
00202   }
00203   else if (msgToLoad <= 80)
00204   {
00205     // DOMXPathException
00206     return sMsgLoader->loadMsg(XMLDOMMsg::DOMXPATHEXCEPTION_ERRX+msgToLoad-DOMXPathException::INVALID_EXPRESSION_ERR+1, toFill, maxChars);
00207   }
00208   else if (msgToLoad <= 110)
00209   {
00210     // DOMXLSException
00211     return sMsgLoader->loadMsg(XMLDOMMsg::DOMLSEXCEPTION_ERRX+msgToLoad-DOMLSException::PARSE_ERR+1, toFill, maxChars);
00212   }
00213   else
00214   {
00215     // DOMRangeException
00216     return sMsgLoader->loadMsg(XMLDOMMsg::DOMRANGEEXCEPTION_ERRX+msgToLoad-DOMRangeException::BAD_BOUNDARYPOINTS_ERR+1, toFill, maxChars);
00217   }
00218 }
00219 
00220 // ------------------------------------------------------------
00221 // DOMImplementationLS Virtual interface
00222 // ------------------------------------------------------------
00223 //Introduced in DOM Level 3
00224 DOMLSParser* DOMImplementationImpl::createLSParser( const DOMImplementationLSMode   mode,
00225                                                     const XMLCh* const     /*schemaType*/,
00226                                                     MemoryManager* const  manager,
00227                                                     XMLGrammarPool* const gramPool)
00228 {
00229     if (mode == DOMImplementationLS::MODE_ASYNCHRONOUS)
00230         throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0, manager);
00231 
00232     // TODO: schemaType
00233     return new (manager) DOMLSParserImpl(0, manager, gramPool);
00234 }
00235 
00236 
00237 DOMLSSerializer* DOMImplementationImpl::createLSSerializer(MemoryManager* const manager)
00238 {
00239     return new (manager) DOMLSSerializerImpl(manager);
00240 }
00241 
00242 DOMLSInput* DOMImplementationImpl::createLSInput(MemoryManager* const manager)
00243 {
00244     return new (manager) DOMLSInputImpl(manager);
00245 }
00246 
00247 DOMLSOutput* DOMImplementationImpl::createLSOutput(MemoryManager* const manager)
00248 {
00249     return new (manager) DOMLSOutputImpl(manager);
00250 }
00251 
00252 // ------------------------------------------------------------
00253 // DOMImplementationSource Virtual interface
00254 // ------------------------------------------------------------
00255 DOMImplementation* DOMImplementationImpl::getDOMImplementation(const XMLCh* features) const
00256 {
00257     DOMImplementation* impl = DOMImplementation::getImplementation();
00258 
00259     XMLStringTokenizer tokenizer(features, XMLPlatformUtils::fgMemoryManager);
00260     const XMLCh* feature = 0;
00261 
00262     while (feature || tokenizer.hasMoreTokens()) {
00263 
00264         if (!feature)
00265             feature = tokenizer.nextToken();
00266 
00267         const XMLCh* version = 0;
00268         const XMLCh* token = tokenizer.nextToken();
00269 
00270         if (token && XMLString::isDigit(token[0]))
00271             version = token;
00272 
00273         if (!impl->hasFeature(feature, version))
00274             return 0;
00275 
00276         if (!version)
00277             feature = token;
00278     }
00279     return impl;
00280 }
00281 
00282 DOMImplementationList* DOMImplementationImpl::getDOMImplementationList(const XMLCh* features) const
00283 {
00284     DOMImplementationListImpl* list = new DOMImplementationListImpl;
00285     DOMImplementation* myImpl=getDOMImplementation(features);
00286     if(myImpl)
00287         list->add(myImpl);
00288     return list;
00289 }
00290 
00291 XERCES_CPP_NAMESPACE_END