Newer
Older
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
* 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.
James David Berry
committed
* $Id$
*
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
James David Berry
committed
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if HAVE_LIMITS_H
# include <limits.h>
#endif
#if HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#if HAVE_SYS_TIMEB_H
# include <sys/timeb.h>
#endif
#include <xercesc/util/Mutexes.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/RefVectorOf.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/internal/XMLReader.hpp>
#include <xercesc/util/RuntimeException.hpp>
James David Berry
committed
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/util/XMLInitializer.hpp>
#include <xercesc/internal/MemoryManagerImpl.hpp>
James David Berry
committed
#include <xercesc/internal/MemoryManagerArrayImpl.hpp>
James David Berry
committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <xercesc/util/XMLFileMgr.hpp>
#if XERCES_USE_FILEMGR_POSIX
# include <xercesc/util/FileManagers/PosixFileMgr.hpp>
#endif
#if XERCES_USE_FILEMGR_WINDOWS
# include <xercesc/util/FileManagers/WindowsFileMgr.hpp>
#endif
#include <xercesc/util/XMLMutexMgr.hpp>
#if XERCES_USE_MUTEXMGR_NOTHREAD
# include <xercesc/util/MutexManagers/NoThreadMutexMgr.hpp>
#endif
#if XERCES_USE_MUTEXMGR_POSIX
# include <xercesc/util/MutexManagers/PosixMutexMgr.hpp>
#endif
#if XERCES_USE_MUTEXMGR_WINDOWS
# include <xercesc/util/MutexManagers/WindowsMutexMgr.hpp>
#endif
#include <xercesc/util/XMLNetAccessor.hpp>
#if XERCES_USE_NETACCESSOR_CURL
# include <xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp>
#endif
#if XERCES_USE_NETACCESSOR_SOCKET
# include <xercesc/util/NetAccessors/Socket/SocketNetAccessor.hpp>
#endif
#if XERCES_USE_NETACCESSOR_LIBWWW
# include <xercesc/util/NetAccessors/libWWW/LibWWWNetAccessor.hpp>
#endif
#if XERCES_USE_NETACCESSOR_CFURL
# include <xercesc/util/NetAccessors/MacOSURLAccessCF/MacOSURLAccessCF.hpp>
#endif
#if XERCES_USE_NETACCESSOR_WINSOCK
# include <xercesc/util/NetAccessors/WinSock/WinSockNetAccessor.hpp>
#endif
#include <xercesc/util/XMLMsgLoader.hpp>
#if XERCES_USE_MSGLOADER_ICU
# include <xercesc/util/MsgLoaders/ICU/ICUMsgLoader.hpp>
#endif
#if XERCES_USE_MSGLOADER_ICONV
# include <xercesc/util/MsgLoaders/MsgCatalog/MsgCatalogLoader.hpp>
#endif
#if XERCES_USE_MSGLOADER_INMEMORY
# include <xercesc/util/MsgLoaders/InMemory/InMemMsgLoader.hpp>
#endif
#if XERCES_USE_WIN32_MSGLOADER
# include <xercesc/util/MsgLoaders/Win32/Win32MsgLoader.hpp>
#endif
#include <xercesc/util/TransService.hpp>
#if XERCES_USE_TRANSCODER_ICU
# include <xercesc/util/Transcoders/ICU/ICUTransService.hpp>
#endif
#if XERCES_USE_TRANSCODER_GNUICONV
# include <xercesc/util/Transcoders/IconvGNU/IconvGNUTransService.hpp>
#endif
James David Berry
committed
#if XERCES_USE_TRANSCODER_ICONV
# include <xercesc/util/Transcoders/Iconv/IconvTransService.hpp>
#endif
#if XERCES_USE_TRANSCODER_MACOSUNICODECONVERTER
# include <xercesc/util/Transcoders/MacOSUnicodeConverter/MacOSUnicodeConverter.hpp>
#endif
#if XERCES_USE_TRANSCODER_WINDOWS
# include <xercesc/util/Transcoders/Win32/Win32TransService.hpp>
#endif
// ---------------------------------------------------------------------------
// Local data members
//
// gSyncMutex
// This is a mutex that will be used to synchronize access to some of
// the static data of the platform utilities class and here locally.
// ---------------------------------------------------------------------------
static XMLMutex* gSyncMutex = 0;
static long gInitFlag = 0;
// ---------------------------------------------------------------------------
// Global data
//
// gXMLCleanupList
// This is a list of cleanup functions to be called on
// XMLPlatformUtils::Terminate. Their function is to reset static
// data in classes that use it.
//
// gXMLCleanupListMutex
// This is a mutex that will be used to synchronise access to the global
// static data cleanup list
// ---------------------------------------------------------------------------
XMLRegisterCleanup* gXMLCleanupList = 0;
XMLMutex* gXMLCleanupListMutex = 0;
// ---------------------------------------------------------------------------
// XMLPlatformUtils: Static Data Members
// ---------------------------------------------------------------------------
James David Berry
committed
XMLNetAccessor* XMLPlatformUtils::fgNetAccessor = 0;
XMLTransService* XMLPlatformUtils::fgTransService = 0;
#ifdef OS390
XMLTransService* XMLPlatformUtils::fgTransService2 = 0;
#endif
James David Berry
committed
PanicHandler* XMLPlatformUtils::fgUserPanicHandler = 0;
PanicHandler* XMLPlatformUtils::fgDefaultPanicHandler = 0;
MemoryManager* XMLPlatformUtils::fgMemoryManager = 0;
bool XMLPlatformUtils::fgMemMgrAdopted = true;
James David Berry
committed
XMLFileMgr* XMLPlatformUtils::fgFileMgr = 0;
XMLMutexMgr* XMLPlatformUtils::fgMutexMgr = 0;
XMLMutex* XMLPlatformUtils::fgAtomicMutex = 0;
James David Berry
committed
bool XMLPlatformUtils::fgXMLChBigEndian = true;
// ---------------------------------------------------------------------------
// XMLPlatformUtils: Init/term methods
// ---------------------------------------------------------------------------
void XMLPlatformUtils::Initialize(const char* const locale
, const char* const nlsHome
, PanicHandler* const panicHandler
, MemoryManager* const memoryManager
, bool toInitStatics)
{
//
// Effects of overflow:
// . resouce re-allocations
// . consequently resource leaks
// . potentially terminate() may never get executed
//
// We got to prevent overflow from happening.
// no error or exception
//
// Make sure we haven't already been initialized. Note that this is not
// thread safe and is not intended for that. Its more for those COM
// like processes that cannot keep up with whether they have initialized
// us yet or not.
//
gInitFlag++;
if (gInitFlag > 1)
return;
// Set pluggable memory manager
if (!fgMemoryManager)
{
if (memoryManager)
{
fgMemoryManager = memoryManager;
fgMemMgrAdopted = false;
}
else
{
fgMemoryManager = new MemoryManagerImpl();
}
}
/***
* Panic Handler:
*
***/
if (!panicHandler)
{
fgDefaultPanicHandler = new DefaultPanicHandler();
}
else
{
fgUserPanicHandler = panicHandler;
}
James David Berry
committed
// Determine our endianness (with regard to a XMLCh 16-bit word)
union {
XMLCh ch;
unsigned char ar[sizeof(XMLCh)];
} endianTest;
endianTest.ch = 1;
fgXMLChBigEndian = (endianTest.ar[sizeof(XMLCh)-1] == 1);
// Initialize the platform-specific mutex and file mgrs
James David Berry
committed
fgMutexMgr = makeMutexMgr(fgMemoryManager);
fgFileMgr = makeFileMgr(fgMemoryManager);
gSyncMutex = new XMLMutex(fgMemoryManager);
// Create the mutex for the static data cleanup list
gXMLCleanupListMutex = new XMLMutex(fgMemoryManager);
fgAtomicMutex = new XMLMutex(fgMemoryManager);
//
// Ask the per-platform code to make the desired transcoding service for
// us to use. This call cannot throw any exceptions or do anything that
// cause any transcoding to happen. It should create the service and
// return it or zero if it cannot.
//
// This one also cannot use any utility services. It can only create a
// transcoding service object and return it.
//
// If we cannot make one, then we call panic to end the process.
//
fgTransService = makeTransService();
if (!fgTransService)
// Initialize the transcoder service
fgTransService->initTransService();
//
// Try to create a default local code page transcoder. This is the one
// that will be used internally by the XMLString class. If we cannot
// create one, then call the panic method.
//
XMLLCPTranscoder* defXCode = XMLPlatformUtils::fgTransService->makeNewLCPTranscoder();
if (!defXCode)
XMLString::initString(defXCode, fgMemoryManager);
//
// Now lets ask the per-platform code to give us an instance of the type
// of network access implementation he wants to use. This can return
// a zero pointer if this platform doesn't want to support this.
//
fgNetAccessor = makeNetAccessor();
PeiYong Zhang
committed
* Message Loader:
* Locale setting
* nlsHome setting
***/
XMLMsgLoader::setLocale(locale);
PeiYong Zhang
committed
XMLMsgLoader::setNLSHome(nlsHome);
if (toInitStatics) {
XMLInitializer::InitializeAllStaticData();
}
}
void XMLPlatformUtils::Terminate()
{
//
// To prevent it from running underflow.
// otherwise we come to delete non-existing resources.
//
// no error or exception
// Delete any net accessor that got installed
delete fgNetAccessor;
fgNetAccessor = 0;
//
// Call some other internal modules to give them a chance to clean up.
// Do the string class last in case something tries to use it during
// cleanup.
//
XMLString::termString();
// Clean up the the transcoding service
delete fgTransService;
fgTransService = 0;
James David Berry
committed
// Clean up mutexes
delete gSyncMutex; gSyncMutex = 0;
delete fgAtomicMutex; fgAtomicMutex = 0;
Khaled Noaman
committed
// Clean up statically allocated, lazily cleaned data in each class
// that has registered for it.
// Note that calling doCleanup() also unregisters the cleanup
// function, so that we are chewing the list down to nothing here
while (gXMLCleanupList)
gXMLCleanupList->doCleanup();
// Clean up the mutex for accessing gXMLCleanupList
delete gXMLCleanupListMutex;
gXMLCleanupListMutex = 0;
// Clean up our mgrs
delete fgFileMgr; fgFileMgr = 0;
delete fgMutexMgr; fgMutexMgr = 0;
/***
* de-allocate resource
*
* refer to discussion in the Initialize()
***/
XMLMsgLoader::setLocale(0);
PeiYong Zhang
committed
XMLMsgLoader::setNLSHome(0);
James David Berry
committed
delete fgDefaultPanicHandler;
Neil Graham
committed
fgDefaultPanicHandler = 0;
fgUserPanicHandler = 0;
// de-allocate default memory manager
if (fgMemMgrAdopted)
delete fgMemoryManager;
else
fgMemMgrAdopted = true;
// And say we are no longer initialized
gInitFlag = 0;
}
James David Berry
committed
// ---------------------------------------------------------------------------
// XMLPlatformUtils: The panic method
// ---------------------------------------------------------------------------
void XMLPlatformUtils::panic(const PanicHandler::PanicReasons reason)
{
fgUserPanicHandler? fgUserPanicHandler->panic(reason) : fgDefaultPanicHandler->panic(reason);
James David Berry
committed
}
// ---------------------------------------------------------------------------
// XMLPlatformUtils: Private Static Methods
// ---------------------------------------------------------------------------
XMLNetAccessor* XMLPlatformUtils::makeNetAccessor()
{
XMLNetAccessor* na = 0;
#if defined (XERCES_USE_NETACCESSOR_CURL)
James David Berry
committed
na = new CurlNetAccessor();
#elif defined (XERCES_USE_NETACCESSOR_SOCKET)
James David Berry
committed
na = new SocketNetAccessor();
#elif defined (XERCES_USE_NETACCESSOR_LIBWWW)
James David Berry
committed
na = new LibWWWNetAccessor();
#elif defined (XERCES_USE_NETACCESSOR_CFURL)
James David Berry
committed
na = new MacOSURLAccessCF();
#elif defined (XERCES_USE_NETACCESSOR_WINSOCK)
James David Berry
committed
na = new WinSockNetAccessor();
#endif
James David Berry
committed
return na;
}
//
// This method is called by the platform independent part of this class
// when client code asks to have one of the supported message sets loaded.
//
XMLMsgLoader* XMLPlatformUtils::loadAMsgSet(const XMLCh* const msgDomain)
{
James David Berry
committed
try
{
#if defined (XERCES_USE_MSGLOADER_ICU)
ms = new ICUMsgLoader(msgDomain);
#elif defined (XERCES_USE_MSGLOADER_ICONV)
ms = new MsgCatalogLoader(msgDomain);
#elif defined (XERCES_USE_WIN32_MSGLOADER)
ms = new Win32MsgLoader(msgDomain);
#elif defined (XERCES_USE_MSGLOADER_INMEMORY)
James David Berry
committed
ms = new InMemMsgLoader(msgDomain);
#else
#error No MsgLoader configured for platform! You must configure it.
James David Berry
committed
#endif
}
catch(const OutOfMemoryException&)
{
throw;
}
catch(...)
{
panic(PanicHandler::Panic_CantLoadMsgDomain);
}
James David Berry
committed
return ms;
}
//
// This method is called very early in the bootstrapping process. This guy
// must create a transcoding service and return it. It cannot use any string
// methods, any transcoding services, throw any exceptions, etc... It just
// makes a transcoding service and returns it, or returns zero on failure.
//
XMLTransService* XMLPlatformUtils::makeTransService()
{
XMLTransService* tc = 0;
James David Berry
committed
#if defined (XERCES_USE_TRANSCODER_ICU)
tc = new ICUTransService;
#elif defined (XERCES_USE_TRANSCODER_GNUICONV)
tc = new IconvGNUTransService;
James David Berry
committed
#elif defined (XERCES_USE_TRANSCODER_ICONV)
tc = new IconvTransService;
#elif defined (XERCES_USE_TRANSCODER_MACOSUNICODECONVERTER)
tc = new MacOSUnicodeConverter;
#elif defined (XERCES_USE_TRANSCODER_WINDOWS)
tc = new Win32TransService;
#else
#error No Transcoder configured for platform! You must configure it.
#endif
James David Berry
committed
return tc;
}
// ---------------------------------------------------------------------------
// XMLPlatformUtils: File Methods
// ---------------------------------------------------------------------------
XMLFileMgr*
XMLPlatformUtils::makeFileMgr(MemoryManager* const memmgr)
{
XMLFileMgr* mgr = NULL;
James David Berry
committed
#if XERCES_USE_FILEMGR_POSIX
mgr = new (memmgr) PosixFileMgr;
#elif XERCES_USE_FILEMGR_WINDOWS
mgr = new (memmgr) WindowsFileMgr;
#else
#error No File Manager configured for platform! You must configure it.
#endif
James David Berry
committed
return mgr;
}
FileHandle
XMLPlatformUtils::openFile(const char* const fileName
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
return fgFileMgr->fileOpen(fileName, false, memmgr);
James David Berry
committed
}
FileHandle
XMLPlatformUtils::openFile(const XMLCh* const fileName, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
return fgFileMgr->fileOpen(fileName, false, memmgr);
James David Berry
committed
}
FileHandle
XMLPlatformUtils::openFileToWrite(const char* const fileName
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
return fgFileMgr->fileOpen(fileName, true, memmgr);
James David Berry
committed
}
FileHandle
XMLPlatformUtils::openFileToWrite(const XMLCh* const fileName
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
return fgFileMgr->fileOpen(fileName, true, memmgr);
James David Berry
committed
}
FileHandle
XMLPlatformUtils::openStdInHandle(MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
return fgFileMgr->openStdIn(memmgr);
}
void
Boris Kolpackov
committed
XMLPlatformUtils::closeFile(FileHandle theFile
James David Berry
committed
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
fgFileMgr->fileClose(theFile, memmgr);
James David Berry
committed
}
void
XMLPlatformUtils::resetFile(FileHandle theFile
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
fgFileMgr->fileReset(theFile, memmgr);
James David Berry
committed
}
XMLFilePos
Boris Kolpackov
committed
XMLPlatformUtils::curFilePos(FileHandle theFile
James David Berry
committed
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
return fgFileMgr->curPos(theFile, memmgr);
}
XMLFilePos
Boris Kolpackov
committed
XMLPlatformUtils::fileSize(FileHandle theFile
James David Berry
committed
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
return fgFileMgr->fileSize(theFile, memmgr);
James David Berry
committed
}
XMLSize_t
Boris Kolpackov
committed
XMLPlatformUtils::readFileBuffer( FileHandle theFile
James David Berry
committed
, const XMLSize_t toRead
, XMLByte* const toFill
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
return fgFileMgr->fileRead(theFile, toRead, toFill, memmgr);
James David Berry
committed
}
void
XMLPlatformUtils::writeBufferToFile( const FileHandle theFile
Boris Kolpackov
committed
, XMLSize_t toWrite
James David Berry
committed
, const XMLByte* const toFlush
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
Alberto Massari
committed
fgFileMgr->fileWrite(theFile, toWrite, toFlush, memmgr);
James David Berry
committed
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
}
// ---------------------------------------------------------------------------
// XMLPlatformUtils: File system methods
// ---------------------------------------------------------------------------
XMLCh* XMLPlatformUtils::getFullPath(const XMLCh* const srcPath,
MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
return fgFileMgr->getFullPath(srcPath, memmgr);
}
XMLCh* XMLPlatformUtils::getCurrentDirectory(MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
return fgFileMgr->getCurrentDirectory(memmgr);
}
bool XMLPlatformUtils::isRelative(const XMLCh* const toCheck
, MemoryManager* const memmgr)
{
if (!fgFileMgr)
ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, memmgr);
return fgFileMgr->isRelative(toCheck, memmgr);
}
inline bool
XMLPlatformUtils::isAnySlash(XMLCh c)
James David Berry
committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
{
// As far as we know, all supported Xerces
// platforms use at least a forward slash
// as a path delimiter. So we always check for
// that.
//
// If XERCES_PATH_DELIMITER_BACKSLASH evaluates to true,
// we also consider that as a slash.
//
// XERCES_PATH_DELIMITER_BACKSLASH may be set in config.h
// by configure, or elsewhere by platform-specific
// code.
return (
false
|| chForwardSlash == c
#if XERCES_PATH_DELIMITER_BACKSLASH
|| chBackSlash == c
#endif
);
}
// ---------------------------------------------------------------------------
// XMLPlatformUtils: Timing Methods
// ---------------------------------------------------------------------------
unsigned long XMLPlatformUtils::getCurrentMillis()
{
unsigned long ms = 0;
James David Berry
committed
// *** TODO: additional platform support?
#if HAVE_GETTIMEOFDAY
struct timeval aTime;
gettimeofday(&aTime, NULL);
ms = (unsigned long) (aTime.tv_sec * 1000 + aTime.tv_usec / 1000);
#elif HAVE_FTIME
timeb aTime;
ftime(&aTime);
ms = (unsigned long)(aTime.time*1000 + aTime.millitm);
#else
// Make this a warning instead?
#error No timing support is configured for this platform. You must configure it.
#endif
return ms;
}
// -----------------------------------------------------------------------
// Mutex methods
// -----------------------------------------------------------------------
XMLMutexMgr* XMLPlatformUtils::makeMutexMgr(MemoryManager* const memmgr)
{
XMLMutexMgr* mgr = NULL;
James David Berry
committed
#if XERCES_USE_MUTEXMGR_NOTHREAD
mgr = new (memmgr) NoThreadMutexMgr;
#elif XERCES_USE_MUTEXMGR_POSIX
mgr = new (memmgr) PosixMutexMgr;
#elif XERCES_USE_MUTEXMGR_WINDOWS
mgr = new (memmgr) WindowsMutexMgr;
#else
#error No Mutex Manager configured for platform! You must configure it.
#endif
James David Berry
committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
return mgr;
}
XMLMutexHandle XMLPlatformUtils::makeMutex(MemoryManager* const memmgr)
{
if (!fgMutexMgr)
XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
return fgMutexMgr->create(memmgr);
}
void XMLPlatformUtils::closeMutex(XMLMutexHandle const mtx, MemoryManager* const memmgr)
{
if (!fgMutexMgr)
XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
fgMutexMgr->destroy(mtx, memmgr);
}
void XMLPlatformUtils::lockMutex(XMLMutexHandle const mtx)
{
if (!fgMutexMgr)
XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
fgMutexMgr->lock(mtx);
}
void XMLPlatformUtils::unlockMutex(XMLMutexHandle const mtx)
{
if (!fgMutexMgr)
XMLPlatformUtils::panic(PanicHandler::Panic_MutexErr);
fgMutexMgr->unlock(mtx);
}
// ---------------------------------------------------------------------------
// XMLPlatformUtils: Msg support methods
// ---------------------------------------------------------------------------
XMLMsgLoader* XMLPlatformUtils::loadMsgSet(const XMLCh* const msgDomain)
{
//
// Ask the platform support to load up the correct type of message
// loader for the indicated message set. We don't check here whether it
// works or not. That's their decision.
//
return loadAMsgSet(msgDomain);
}
// ---------------------------------------------------------------------------
// XMLPlatformUtils: NEL Character Handling
// ---------------------------------------------------------------------------
David Abram Cargill
committed
void XMLPlatformUtils::recognizeNEL(bool state, MemoryManager* const manager) {
//Make sure initialize has been called
if (gInitFlag == 0) {
return;
}
if (state) {
David Abram Cargill
committed
ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::NEL_RepeatedCalls, manager);
}
}
}
bool XMLPlatformUtils::isNELRecognized() {
// ---------------------------------------------------------------------------
// XMLPlatformUtils: IANA Encoding checking setting
// ---------------------------------------------------------------------------
void XMLPlatformUtils::strictIANAEncoding(const bool state) {
//Make sure initialize has been called
if (gInitFlag == 0) {
return;
}
fgTransService->strictIANAEncoding(state);
}
bool XMLPlatformUtils::isStrictIANAEncoding() {
if (gInitFlag)
return fgTransService->isStrictIANAEncoding();
return false;
}
James David Berry
committed
/***
*
* Previously, each <OS>PlatformUtils.cpp has its onw copy of the
* method weavePaths(), and almost of them implemented the same logic,
* with few platform specific difference, and unfortunately that
James David Berry
committed
* implementation was wrong.
*
James David Berry
committed
* The only platform specific issue is slash character.
* On all platforms other than Windows, chForwardSlash and chBackSlash
* are considered slash, while on Windows, two additional characters,
James David Berry
committed
* chYenSign and chWonSign are slash as well.
*
* The idea is to maintain a SINGLE copy of this method rather than
* each <OS>PlatformUtils.cpp has its own copy, we introduce a new
* method, XMLPlatformUtils::isAnySlash(), to replace the direct checking
* code ( if ( c == chForwardSlash || c == chBackSlash).
*
* With this approach, we might have a performance hit since isAnySlash()
* is so frequently used in this implementation, so we intend to make it
James David Berry
committed
* inline. Then we face a complier issue.
*
* There are two compilation units involved, one is PlatformUtils.cpp and
James David Berry
committed
* the other <OS>PlatformUtils.cpp. When PlatformUtils.cp get compiled,
* the weavePath(), remove**Slash() have dependency upon isAnySlash() which
* is in <OS>PlatformUtils.cpp (and what is worse, it is inlined), so we have
* undefined/unresolved symbol: isAnySlash() on AIX/xlc_r, Solaris/cc and
James David Berry
committed
* Linux/gcc, while MSVC and HP/aCC are fine with this.
*
James David Berry
committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
* That means we can not place these new methods in PlatformUtils.cpp with
* inlined XMLPlatformUtils::isAnySlash() in <OS>PlatformUtils.cpp.
*
* The solution to this is <os>PlatformUtils.cpp will include this file so that
* we have only one copy of these methods while get compiled in <os>PlatformUtils
* inlined isAnySlash().
*
***/
XMLCh* XMLPlatformUtils::weavePaths(const XMLCh* const basePath
, const XMLCh* const relativePath
, MemoryManager* const manager)
{
// Create a buffer as large as both parts and empty it
XMLCh* tmpBuf = (XMLCh*) manager->allocate
(
(XMLString::stringLen(basePath)
+ XMLString::stringLen(relativePath) + 2) * sizeof(XMLCh)
);//new XMLCh[XMLString::stringLen(basePath) + XMLString::stringLen(relativePath) + 2];
*tmpBuf = 0;
//
// If we have no base path, then just take the relative path as is.
//
if ((!basePath) || (!*basePath))
{
XMLString::copyString(tmpBuf, relativePath);
return tmpBuf;
}
//
// Remove anything after the last slash
//
const XMLCh* basePtr = basePath + (XMLString::stringLen(basePath) - 1);
while ((basePtr >= basePath) && ((isAnySlash(*basePtr) == false)))
{
basePtr--;
}
// There is no relevant base path, so just take the relative part
if (basePtr < basePath)
{
XMLString::copyString(tmpBuf, relativePath);
return tmpBuf;
}
//
// 1. concatenate the base and relative
// 2. remove all occurences of "/./"
// 3. remove all occurences of segment/../ where segment is not ../
//
James David Berry
committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
XMLString::subString(tmpBuf, basePath, 0, (basePtr - basePath + 1), manager);
tmpBuf[basePtr - basePath + 1] = 0;
XMLString::catString(tmpBuf, relativePath);
removeDotSlash(tmpBuf, manager);
removeDotDotSlash(tmpBuf, manager);
return tmpBuf;
}
//
// Remove all occurences of './' when it is part of '/./'
//
// Since it could be '.\' or other combination on windows ( eg, '.'+chYanSign)
// we can't make use of patterMatch().
//
//
void XMLPlatformUtils::removeDotSlash(XMLCh* const path
, MemoryManager* const manager)
{
if ((!path) || (!*path))
return;
XMLCh* srcPtr = XMLString::replicate(path, manager);
XMLSize_t srcLen = XMLString::stringLen(srcPtr);
ArrayJanitor<XMLCh> janName(srcPtr, manager);
James David Berry
committed
XMLCh* tarPtr = path;
while (*srcPtr)
{
if ( 3 <= srcLen )
{
if ( (isAnySlash(*srcPtr)) &&
(chPeriod == *(srcPtr+1)) &&
(isAnySlash(*(srcPtr+2))) )
{
// "\.\x" seen
// skip the first two, and start from the 3rd,
// since "\x" could be another "\."
srcPtr+=2;
James David Berry
committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
srcLen-=2;
}
else
{
*tarPtr++ = *srcPtr++; // eat the current char
srcLen--;
}
}
else if ( 1 == srcLen )
{
*tarPtr++ = *srcPtr++;
}
else if ( 2 == srcLen)
{
*tarPtr++ = *srcPtr++;
*tarPtr++ = *srcPtr++;
}
}
*tarPtr = 0;
return;
}
//
// Remove all occurences of '/segment/../' when segment is not '..'
//
// Cases with extra /../ is left to the underlying file system.
//
void XMLPlatformUtils::removeDotDotSlash(XMLCh* const path
, MemoryManager* const manager)
{
XMLSize_t pathLen = XMLString::stringLen(path);
James David Berry
committed
XMLCh* tmp1 = (XMLCh*) manager->allocate
(
(pathLen+1) * sizeof(XMLCh)
);//new XMLCh [pathLen+1];
ArrayJanitor<XMLCh> tmp1Name(tmp1, manager);
XMLCh* tmp2 = (XMLCh*) manager->allocate
(
(pathLen+1) * sizeof(XMLCh)
);//new XMLCh [pathLen+1];
ArrayJanitor<XMLCh> tmp2Name(tmp2, manager);
// remove all "<segment>/../" where "<segment>" is a complete
// path segment not equal to ".."
int index = -1;
int segIndex = -1;
int offset = 1;
while ((index = searchSlashDotDotSlash(&(path[offset]))) != -1)
{
// Undo offset
index += offset;