transservice.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 546 行 · 第 1/2 页
CPP
546 行
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: TransService.cpp,v 1.19 2004/09/08 13:56:23 peiyongz Exp $ */// ---------------------------------------------------------------------------// Includes// ---------------------------------------------------------------------------#include <xercesc/util/Janitor.hpp>#include <xercesc/util/XML88591Transcoder.hpp>#include <xercesc/util/XMLASCIITranscoder.hpp>#include <xercesc/util/XMLChTranscoder.hpp>#include <xercesc/util/XMLEBCDICTranscoder.hpp>#include <xercesc/util/XMLIBM1047Transcoder.hpp>#include <xercesc/util/XMLIBM1140Transcoder.hpp>#include <xercesc/util/XMLUCS4Transcoder.hpp>#include <xercesc/util/XMLUTF8Transcoder.hpp>#include <xercesc/util/XMLUTF16Transcoder.hpp>#include <xercesc/util/XMLWin1252Transcoder.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLUni.hpp>#include <xercesc/util/EncodingValidator.hpp>#include <xercesc/util/XMLRegisterCleanup.hpp>#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/TransENameMap.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------// Local, static data//// gStrictIANAEncoding// A flag to control whether strict IANA encoding names checking should// be done//// ---------------------------------------------------------------------------static bool gStrictIANAEncoding = false;RefHashTableOf<ENameMap>* XMLTransService::gMappings = 0;RefVectorOf<ENameMap> * XMLTransService::gMappingsRecognizer = 0;static XMLRegisterCleanup mappingsCleanup;static XMLRegisterCleanup mappingsRecognizerCleanup;// -----------------------------------------------------------------------// Notification that lazy data has been deleted// -----------------------------------------------------------------------void XMLTransService::reinitMappings() { delete gMappings; // The contents of the gMappings hash table are owned by gMappings = 0; // the it, and so will be deleted by gMapping's destructor.}void XMLTransService::reinitMappingsRecognizer() { delete XMLTransService::gMappingsRecognizer; gMappingsRecognizer = 0;}// ---------------------------------------------------------------------------// XMLTransService: Constructors and destructor// ---------------------------------------------------------------------------XMLTransService::XMLTransService(){ if (!gMappings) { RefHashTableOf<ENameMap>* t = new RefHashTableOf<ENameMap>(103); if (XMLPlatformUtils::compareAndSwap((void **)&gMappings, t, 0) != 0) { delete t; } else { mappingsCleanup.registerCleanup(reinitMappings); } } if (!gMappingsRecognizer) { RefVectorOf<ENameMap>* t = new RefVectorOf<ENameMap>(XMLRecognizer::Encodings_Count); if (XMLPlatformUtils::compareAndSwap((void **)&gMappingsRecognizer, t, 0) != 0) { delete t; } else { mappingsRecognizerCleanup.registerCleanup(reinitMappingsRecognizer); } }}XMLTransService::~XMLTransService(){}// ---------------------------------------------------------------------------// Allow user specific encodings to be added to the mappings table.// Should be called after platform init// ---------------------------------------------------------------------------void XMLTransService::addEncoding(const XMLCh* const encoding, ENameMap* const ownMapping){ gMappings->put((void *) encoding, ownMapping);}// ---------------------------------------------------------------------------// XMLTransService: Non-virtual API// ---------------------------------------------------------------------------XMLTranscoder*XMLTransService::makeNewTranscoderFor( const char* const encodingName , XMLTransService::Codes& resValue , const unsigned int blockSize , MemoryManager* const manager){ XMLCh* tmpName = XMLString::transcode(encodingName, manager); ArrayJanitor<XMLCh> janName(tmpName, manager); return makeNewTranscoderFor(tmpName, resValue, blockSize, manager);}XMLTranscoder*XMLTransService::makeNewTranscoderFor( const XMLCh* const encodingName , XMLTransService::Codes& resValue , const unsigned int blockSize , MemoryManager* const manager){ // // If strict IANA encoding flag is set, validate encoding name // if (gStrictIANAEncoding) { if (!EncodingValidator::instance()->isValidEncoding(encodingName)) { resValue = XMLTransService::UnsupportedEncoding; return 0; } } // // First try to find it in our list of mappings to intrinsically // supported encodings. We have to upper case the passed encoding // name because we use a hash table and we stored all our mappings // in all uppercase. // const unsigned int bufSize = 2048; XMLCh upBuf[bufSize + 1]; if (!XMLString::copyNString(upBuf, encodingName, bufSize)) { resValue = XMLTransService::InternalFailure; return 0; } XMLString::upperCase(upBuf); ENameMap* ourMapping = gMappings->get(upBuf); // If we found it, then call the factory method for it if (ourMapping) { XMLTranscoder* temp = ourMapping->makeNew(blockSize, manager); resValue = temp ? XMLTransService::Ok : XMLTransService::InternalFailure; return temp; } // // It wasn't an intrinsic and it wasn't disallowed, so pass it on // to the trans service to see if he can make anything of it. // XMLTranscoder* temp = makeNewXMLTranscoder(encodingName, resValue, blockSize, manager); // if successful, set resValue to OK // if failed, the makeNewXMLTranscoder has already set the proper failing resValue if (temp) resValue = XMLTransService::Ok; return temp;}XMLTranscoder*XMLTransService::makeNewTranscoderFor( XMLRecognizer::Encodings encodingEnum , XMLTransService::Codes& resValue , const unsigned int blockSize , MemoryManager* const manager){ // // We can only make transcoder if the passed encodingEnum is under this range // if (encodingEnum < XMLRecognizer::Encodings_Min || encodingEnum > XMLRecognizer::Encodings_Max) { resValue = XMLTransService::InternalFailure; return 0; } ENameMap* ourMapping = gMappingsRecognizer->elementAt(encodingEnum); // If we found it, then call the factory method for it if (ourMapping) { XMLTranscoder* temp = ourMapping->makeNew(blockSize, manager); resValue = temp ? XMLTransService::Ok : XMLTransService::InternalFailure; return temp; } else { XMLTranscoder* temp = makeNewXMLTranscoder(XMLRecognizer::nameForEncoding(encodingEnum, manager), resValue, blockSize, manager); // if successful, set resValue to OK // if failed, the makeNewXMLTranscoder has already set the proper failing resValue if (temp) resValue = XMLTransService::Ok; return temp; }}// ---------------------------------------------------------------------------// XMLTransTransService: Hidden Init Method//// This is called by platform utils during startup.// ---------------------------------------------------------------------------void XMLTransService::initTransService(){ // // A stupid way to increment the fCurCount inside the RefVectorOf // for (unsigned int i = 0; i < XMLRecognizer::Encodings_Count; i++) gMappingsRecognizer->addElement(0); // // Add in the magical mapping for the native XMLCh transcoder. This // is used for internal entities. // gMappingsRecognizer->setElementAt(new ENameMapFor<XMLChTranscoder>(XMLUni::fgXMLChEncodingString), XMLRecognizer::XERCES_XMLCH); gMappings->put((void*)XMLUni::fgXMLChEncodingString, new ENameMapFor<XMLChTranscoder>(XMLUni::fgXMLChEncodingString)); // // Add in our mappings for ASCII. // gMappingsRecognizer->setElementAt(new ENameMapFor<XMLASCIITranscoder>(XMLUni::fgUSASCIIEncodingString), XMLRecognizer::US_ASCII); gMappings->put((void*)XMLUni::fgUSASCIIEncodingString, new ENameMapFor<XMLASCIITranscoder>(XMLUni::fgUSASCIIEncodingString)); gMappings->put((void*)XMLUni::fgUSASCIIEncodingString2, new ENameMapFor<XMLASCIITranscoder>(XMLUni::fgUSASCIIEncodingString2)); gMappings->put((void*)XMLUni::fgUSASCIIEncodingString3, new ENameMapFor<XMLASCIITranscoder>(XMLUni::fgUSASCIIEncodingString3)); gMappings->put((void*)XMLUni::fgUSASCIIEncodingString4, new ENameMapFor<XMLASCIITranscoder>(XMLUni::fgUSASCIIEncodingString4)); // // Add in our mappings for UTF-8 // gMappingsRecognizer->setElementAt(new ENameMapFor<XMLUTF8Transcoder>(XMLUni::fgUTF8EncodingString), XMLRecognizer::UTF_8); gMappings->put((void*)XMLUni::fgUTF8EncodingString, new ENameMapFor<XMLUTF8Transcoder>(XMLUni::fgUTF8EncodingString)); gMappings->put((void*)XMLUni::fgUTF8EncodingString2, new ENameMapFor<XMLUTF8Transcoder>(XMLUni::fgUTF8EncodingString2)); // // Add in our mappings for Latin1 // gMappings->put((void*)XMLUni::fgISO88591EncodingString, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString)); gMappings->put((void*)XMLUni::fgISO88591EncodingString2, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString2)); gMappings->put((void*)XMLUni::fgISO88591EncodingString3, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString3)); gMappings->put((void*)XMLUni::fgISO88591EncodingString4, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString4)); gMappings->put((void*)XMLUni::fgISO88591EncodingString5, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString5)); gMappings->put((void*)XMLUni::fgISO88591EncodingString6, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString6)); gMappings->put((void*)XMLUni::fgISO88591EncodingString7, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString7)); gMappings->put((void*)XMLUni::fgISO88591EncodingString8, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString8)); gMappings->put((void*)XMLUni::fgISO88591EncodingString9, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString9)); gMappings->put((void*)XMLUni::fgISO88591EncodingString10, new ENameMapFor<XML88591Transcoder>(XMLUni::fgISO88591EncodingString10));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?