Added Xerces-C++ 3.1.2

This commit is contained in:
sippeangelo
2015-12-01 10:23:50 +01:00
parent 65d3f3bc31
commit 1b478d3159
815 changed files with 262638 additions and 0 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,242 @@
/*
* 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
*
* 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: ICUTransService.hpp 932887 2010-04-11 13:04:59Z borisk $
*/
#if !defined(XERCESC_INCLUDE_GUARD_ICUTRANSSERVICE_HPP)
#define XERCESC_INCLUDE_GUARD_ICUTRANSSERVICE_HPP
#include <xercesc/util/Mutexes.hpp>
#include <xercesc/util/TransService.hpp>
struct UConverter;
XERCES_CPP_NAMESPACE_BEGIN
class XMLUTIL_EXPORT ICUTransService : public XMLTransService
{
public :
friend class Uniconv390TransService;
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
ICUTransService(MemoryManager* manager);
~ICUTransService();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoding service API
// -----------------------------------------------------------------------
virtual int compareIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
);
virtual int compareNIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
, const XMLSize_t maxChars
);
virtual const XMLCh* getId() const;
virtual XMLLCPTranscoder* makeNewLCPTranscoder(MemoryManager* manager);
virtual bool supportsSrcOfs() const;
virtual void upperCase(XMLCh* const toUpperCase);
virtual void lowerCase(XMLCh* const toLowerCase);
protected :
// -----------------------------------------------------------------------
// Protected virtual methods
// -----------------------------------------------------------------------
virtual XMLTranscoder* makeNewXMLTranscoder
(
const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const XMLSize_t blockSize
, MemoryManager* const manager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
ICUTransService(const ICUTransService&);
ICUTransService& operator=(const ICUTransService&);
};
class XMLUTIL_EXPORT ICUTranscoder : public XMLTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
ICUTranscoder
(
const XMLCh* const encodingName
, UConverter* const toAdopt
, const XMLSize_t blockSize
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
~ICUTranscoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual XMLSize_t transcodeFrom
(
const XMLByte* const srcData
, const XMLSize_t srcCount
, XMLCh* const toFill
, const XMLSize_t maxChars
, XMLSize_t& bytesEaten
, unsigned char* const charSizes
);
virtual XMLSize_t transcodeTo
(
const XMLCh* const srcData
, const XMLSize_t srcCount
, XMLByte* const toFill
, const XMLSize_t maxBytes
, XMLSize_t& charsEaten
, const UnRepOpts options
);
virtual bool canTranscodeTo
(
const unsigned int toCheck
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
ICUTranscoder();
ICUTranscoder(const ICUTranscoder&);
ICUTranscoder& operator=(const ICUTranscoder&);
// -----------------------------------------------------------------------
// Private data members
//
// fConverter
// This is a pointer to the ICU converter that this transcoder
// uses.
//
// fFixed
// This is set to true if the encoding is a fixed size one. This
// can be used to optimize some operations.
//
// fSrcOffsets
// This is an array of longs, which are allocated to the size of
// the transcoding block (if any) indicated in the ctor. It is used
// to get the character offsets from ICU, which are then translated
// into an array of char sizes for return.
// -----------------------------------------------------------------------
UConverter* fConverter;
bool fFixed;
XMLUInt32* fSrcOffsets;
};
class XMLUTIL_EXPORT ICULCPTranscoder : public XMLLCPTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
ICULCPTranscoder(UConverter* const toAdopt);
~ICULCPTranscoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual char* transcode(const XMLCh* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLCh* transcode(const char* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
// -----------------------------------------------------------------------
// DEPRECATED old transcode interface
// -----------------------------------------------------------------------
virtual XMLSize_t calcRequiredSize(const char* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLSize_t calcRequiredSize(const XMLCh* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual bool transcode
(
const char* const toTranscode
, XMLCh* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
virtual bool transcode
(
const XMLCh* const toTranscode
, char* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
ICULCPTranscoder();
ICULCPTranscoder(const ICULCPTranscoder&);
ICULCPTranscoder& operator=(const ICULCPTranscoder&);
// -----------------------------------------------------------------------
// Private data members
//
// fConverter
// This is a pointer to the ICU converter that this transcoder
// uses.
//
// fMutex
// We have to synchronize threaded calls to the converter.
// -----------------------------------------------------------------------
UConverter* fConverter;
XMLMutex fMutex;
};
XERCES_CPP_NAMESPACE_END
#endif
@@ -0,0 +1,571 @@
/*
* 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
*
* 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: IconvTransService.cpp 695885 2008-09-16 14:00:19Z borisk $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if HAVE_WCHAR_H
# include <wchar.h>
#endif
#if HAVE_WCTYPE_H
# include <wctype.h>
#endif
// Fill in for broken or missing wctype functions on some platforms
#if !HAVE_TOWUPPER
# include <towupper.h>
#endif
#if !HAVE_TOWLOWER
# include <towlower.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "IconvTransService.hpp"
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/framework/MemoryManager.hpp>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// Local, const data
// ---------------------------------------------------------------------------
static const int gTempBuffArraySize = 1024;
static const XMLCh gMyServiceId[] =
{
chLatin_I, chLatin_C, chLatin_o, chLatin_n, chLatin_v, chNull
};
// ---------------------------------------------------------------------------
// the following is defined by 'man mbrtowc':
// ---------------------------------------------------------------------------
static const size_t TRANSCODING_ERROR = (size_t)(-1);
// ---------------------------------------------------------------------------
// Local methods
// ---------------------------------------------------------------------------
static unsigned int getWideCharLength(const XMLCh* const src)
{
if (!src)
return 0;
unsigned int len = 0;
const XMLCh* pTmp = src;
while (*pTmp++)
len++;
return len;
}
// ---------------------------------------------------------------------------
// IconvTransService: Constructors and Destructor
// ---------------------------------------------------------------------------
IconvTransService::IconvTransService(MemoryManager* /* manager */)
{
}
IconvTransService::~IconvTransService()
{
}
// ---------------------------------------------------------------------------
// IconvTransService: The virtual transcoding service API
// ---------------------------------------------------------------------------
int IconvTransService::compareIString( const XMLCh* const comp1
, const XMLCh* const comp2)
{
const XMLCh* cptr1 = comp1;
const XMLCh* cptr2 = comp2;
while ( (*cptr1 != 0) && (*cptr2 != 0) )
{
wint_t wch1 = towupper(*cptr1);
wint_t wch2 = towupper(*cptr2);
if (wch1 != wch2)
break;
cptr1++;
cptr2++;
}
return (int) ( towupper(*cptr1) - towupper(*cptr2) );
}
int IconvTransService::compareNIString( const XMLCh* const comp1
, const XMLCh* const comp2
, const XMLSize_t maxChars)
{
unsigned int n = 0;
const XMLCh* cptr1 = comp1;
const XMLCh* cptr2 = comp2;
while (true && maxChars)
{
wint_t wch1 = towupper(*cptr1);
wint_t wch2 = towupper(*cptr2);
if (wch1 != wch2)
return (int) (wch1 - wch2);
// If either ended, then both ended, so equal
if (!*cptr1 || !*cptr2)
break;
cptr1++;
cptr2++;
// Bump the count of chars done. If it equals the count then we
// are equal for the requested count, so break out and return
// equal.
n++;
if (n == maxChars)
break;
}
return 0;
}
const XMLCh* IconvTransService::getId() const
{
return gMyServiceId;
}
XMLLCPTranscoder* IconvTransService::makeNewLCPTranscoder(MemoryManager* manager)
{
// Just allocate a new transcoder of our type
return new (manager) IconvLCPTranscoder;
}
bool IconvTransService::supportsSrcOfs() const
{
return true;
}
// ---------------------------------------------------------------------------
// IconvTransService: The protected virtual transcoding service API
// ---------------------------------------------------------------------------
XMLTranscoder*
IconvTransService::makeNewXMLTranscoder(const XMLCh* const
, XMLTransService::Codes& resValue
, const XMLSize_t
, MemoryManager* const)
{
//
// NOTE: We don't use the block size here
//
// This is a minimalist transcoding service, that only supports a local
// default transcoder. All named encodings return zero as a failure,
// which means that only the intrinsic encodings supported by the parser
// itself will work for XML data.
//
resValue = XMLTransService::UnsupportedEncoding;
return 0;
}
void IconvTransService::upperCase(XMLCh* const toUpperCase)
{
XMLCh* outPtr = toUpperCase;
while (*outPtr)
{
*outPtr = towupper(*outPtr);
outPtr++;
}
}
void IconvTransService::lowerCase(XMLCh* const toLowerCase)
{
XMLCh* outPtr = toLowerCase;
while (*outPtr)
{
*outPtr = towlower(*outPtr);
outPtr++;
}
}
// ---------------------------------------------------------------------------
// IconvLCPTranscoder: The virtual transcoder API
// ---------------------------------------------------------------------------
XMLSize_t IconvLCPTranscoder::calcRequiredSize(const char* const srcText
, MemoryManager* const)
{
if (!srcText)
return 0;
XMLSize_t len = 0;
const char *src = srcText;
#if HAVE_MBRLEN
mbstate_t st;
memset(&st, 0, sizeof(st));
#endif
for ( ; *src; ++len)
{
#if HAVE_MBRLEN
int l=::mbrlen( src, MB_CUR_MAX, &st );
#else
int l=::mblen( src, MB_CUR_MAX );
#endif
if( l == TRANSCODING_ERROR )
return 0;
src += l;
}
return len;
}
XMLSize_t IconvLCPTranscoder::calcRequiredSize(const XMLCh* const srcText
, MemoryManager* const manager)
{
if (!srcText)
return 0;
XMLSize_t wLent = getWideCharLength(srcText);
wchar_t tmpWideCharArr[gTempBuffArraySize];
wchar_t* allocatedArray = 0;
wchar_t* wideCharBuf = 0;
if (wLent >= gTempBuffArraySize)
wideCharBuf = allocatedArray = (wchar_t*)
manager->allocate
(
(wLent + 1) * sizeof(wchar_t)
);//new wchar_t[wLent + 1];
else
wideCharBuf = tmpWideCharArr;
for (XMLSize_t i = 0; i < wLent; i++)
{
wideCharBuf[i] = srcText[i];
}
wideCharBuf[wLent] = 0x00;
const XMLSize_t retVal = ::wcstombs(NULL, wideCharBuf, 0);
if (allocatedArray)
manager->deallocate(allocatedArray);
if (retVal == ~0)
return 0;
return retVal;
}
bool IconvLCPTranscoder::transcode( const XMLCh* const toTranscode
, char* const toFill
, const XMLSize_t maxBytes
, MemoryManager* const manager)
{
// Watch for a couple of pyscho corner cases
if (!toTranscode || !maxBytes)
{
toFill[0] = 0;
return true;
}
if (!*toTranscode)
{
toFill[0] = 0;
return true;
}
unsigned int wLent = getWideCharLength(toTranscode);
wchar_t tmpWideCharArr[gTempBuffArraySize];
wchar_t* allocatedArray = 0;
wchar_t* wideCharBuf = 0;
if (wLent > maxBytes) {
wLent = maxBytes;
}
if (maxBytes >= gTempBuffArraySize) {
wideCharBuf = allocatedArray = (wchar_t*)
manager->allocate
(
(maxBytes + 1) * sizeof(wchar_t)
);//new wchar_t[maxBytes + 1];
}
else
wideCharBuf = tmpWideCharArr;
for (unsigned int i = 0; i < wLent; i++)
{
wideCharBuf[i] = toTranscode[i];
}
wideCharBuf[wLent] = 0x00;
// Ok, go ahead and try the transcoding. If it fails, then ...
size_t mblen = ::wcstombs(toFill, wideCharBuf, maxBytes);
if (mblen == (size_t)-1)
{
if (allocatedArray)
manager->deallocate(allocatedArray);
return false;
}
// Cap it off just in case
toFill[mblen] = 0;
if (allocatedArray)
manager->deallocate(allocatedArray);
return true;
}
bool IconvLCPTranscoder::transcode( const char* const toTranscode
, XMLCh* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager)
{
// Check for a couple of psycho corner cases
if (!toTranscode || !maxChars)
{
toFill[0] = 0;
return true;
}
if (!*toTranscode)
{
toFill[0] = 0;
return true;
}
XMLSize_t len = calcRequiredSize(toTranscode);
wchar_t tmpWideCharArr[gTempBuffArraySize];
wchar_t* allocatedArray = 0;
wchar_t* wideCharBuf = 0;
if (len > maxChars) {
len = maxChars;
}
if (maxChars >= gTempBuffArraySize)
wideCharBuf = allocatedArray = (wchar_t*) manager->allocate
(
(maxChars + 1) * sizeof(wchar_t)
);//new wchar_t[maxChars + 1];
else
wideCharBuf = tmpWideCharArr;
if (::mbstowcs(wideCharBuf, toTranscode, maxChars) == (size_t)-1)
{
if (allocatedArray)
manager->deallocate(allocatedArray);
return false;
}
for (XMLSize_t i = 0; i < len; i++)
{
toFill[i] = (XMLCh) wideCharBuf[i];
}
toFill[len] = 0x00;
if (allocatedArray)
manager->deallocate(allocatedArray);
return true;
}
template <typename T>
void reallocString(T *&ref, size_t &size, MemoryManager* const manager, bool releaseOld)
{
T *tmp = (T*)manager->allocate(2 * size * sizeof(T));
memcpy(tmp, ref, size * sizeof(T));
if (releaseOld) manager->deallocate(ref);
ref = tmp;
size *= 2;
}
char* IconvLCPTranscoder::transcode(const XMLCh* const toTranscode,
MemoryManager* const manager)
{
if (!toTranscode)
return 0;
size_t srcCursor = 0, dstCursor = 0;
size_t resultSize = gTempBuffArraySize;
char localBuffer[gTempBuffArraySize];
char* resultString = localBuffer;
#if HAVE_WCSRTOMBS
mbstate_t st;
memset(&st, 0, sizeof(st));
wchar_t srcBuffer[gTempBuffArraySize];
srcBuffer[gTempBuffArraySize - 1] = 0;
const wchar_t *src = 0;
while (toTranscode[srcCursor] || src)
{
if (src == 0) // copy a piece of the source string into a local
// buffer, converted to wchar_t and NULL-terminated.
// after that, src points to the beginning of the
// local buffer and is used for the call to ::wcsrtombs
{
size_t i;
for (i=0; i<gTempBuffArraySize-1; ++i)
{
srcBuffer[i] = toTranscode[srcCursor];
if (srcBuffer[i] == '\0')
break;
++srcCursor;
}
src = srcBuffer;
}
size_t len = ::wcsrtombs(resultString + dstCursor, &src, resultSize - dstCursor, &st);
if (len == TRANSCODING_ERROR)
{
dstCursor = 0;
break;
}
dstCursor += len;
if (src != 0) // conversion not finished. This *always* means there
// was not enough room in the destination buffer.
{
reallocString<char>(resultString, resultSize, manager, resultString != localBuffer);
}
}
#else
while (toTranscode[srcCursor])
{
char mbBuf[16]; // MB_CUR_MAX is not defined as a constant on some platforms
int len = wctomb(mbBuf, toTranscode[srcCursor++]);
if (len < 0)
{
dstCursor = 0;
break;
}
if (dstCursor + len >= resultSize - 1)
reallocString<char>(resultString, resultSize, manager, resultString != localBuffer);
for (int j=0; j<len; ++j)
resultString[dstCursor++] = mbBuf[j];
}
#endif
if (resultString == localBuffer)
{
resultString = (char*)manager->allocate((dstCursor + 1) * sizeof(char));
memcpy(resultString, localBuffer, dstCursor * sizeof(char));
}
resultString[dstCursor] = '\0';
return resultString;
}
XMLCh* IconvLCPTranscoder::transcode(const char* const toTranscode,
MemoryManager* const manager)
{
if (!toTranscode)
return 0;
size_t resultSize = gTempBuffArraySize;
size_t srcCursor = 0, dstCursor = 0;
#if HAVE_MBSRTOWCS
wchar_t localBuffer[gTempBuffArraySize];
wchar_t *tmpString = localBuffer;
mbstate_t st;
memset(&st, 0, sizeof(st));
const char *src = toTranscode;
while(true)
{
size_t len = ::mbsrtowcs(tmpString + dstCursor, &src, resultSize - dstCursor, &st);
if (len == TRANSCODING_ERROR)
{
dstCursor = 0;
break;
}
dstCursor += len;
if (src == 0) // conversion finished
break;
if (dstCursor >= resultSize - 1)
reallocString<wchar_t>(tmpString, resultSize, manager, tmpString != localBuffer);
}
// make a final copy, converting from wchar_t to XMLCh:
XMLCh* resultString = (XMLCh*)manager->allocate((dstCursor + 1) * sizeof(XMLCh));
size_t i;
for (i=0; i<dstCursor; ++i)
resultString[i] = tmpString[i];
if (tmpString != localBuffer) // did we allocate something?
manager->deallocate(tmpString);
#else
XMLCh localBuffer[gTempBuffArraySize];
XMLCh* resultString = localBuffer;
size_t srcLen = strlen(toTranscode);
while(srcLen > srcCursor)
{
wchar_t wcBuf[1];
int len = mbtowc(wcBuf, toTranscode + srcCursor, srcLen - srcCursor);
if (len <= 0)
{
if (len < 0)
dstCursor = 0;
break;
}
srcCursor += len;
if (dstCursor + 1 >= resultSize - 1)
reallocString<XMLCh>(resultString, resultSize, manager, resultString != localBuffer);
resultString[dstCursor++] = wcBuf[0];
}
if (resultString == localBuffer)
{
resultString = (XMLCh*)manager->allocate((dstCursor + 1) * sizeof(XMLCh));
memcpy(resultString, localBuffer, dstCursor * sizeof(XMLCh));
}
#endif
resultString[dstCursor] = L'\0';
return resultString;
}
// ---------------------------------------------------------------------------
// IconvLCPTranscoder: Constructors and Destructor
// ---------------------------------------------------------------------------
IconvLCPTranscoder::IconvLCPTranscoder()
{
}
IconvLCPTranscoder::~IconvLCPTranscoder()
{
}
XERCES_CPP_NAMESPACE_END
@@ -0,0 +1,142 @@
/*
* 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
*
* 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: IconvTransService.hpp 676954 2008-07-15 16:29:19Z dbertoni $
*/
#if !defined(XERCESC_INCLUDE_GUARD_ICONVTRANSSERVICE_HPP)
#define XERCESC_INCLUDE_GUARD_ICONVTRANSSERVICE_HPP
#include <xercesc/util/TransService.hpp>
XERCES_CPP_NAMESPACE_BEGIN
class XMLUTIL_EXPORT IconvTransService : public XMLTransService
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
IconvTransService(MemoryManager* manager);
~IconvTransService();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoding service API
// -----------------------------------------------------------------------
virtual int compareIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
);
virtual int compareNIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
, const XMLSize_t maxChars
);
virtual const XMLCh* getId() const;
virtual XMLLCPTranscoder* makeNewLCPTranscoder(MemoryManager* manager);
virtual bool supportsSrcOfs() const;
virtual void upperCase(XMLCh* const toUpperCase);
virtual void lowerCase(XMLCh* const toLowerCase);
protected :
// -----------------------------------------------------------------------
// Protected virtual methods
// -----------------------------------------------------------------------
virtual XMLTranscoder* makeNewXMLTranscoder
(
const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const XMLSize_t blockSize
, MemoryManager* const manager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
IconvTransService(const IconvTransService&);
IconvTransService& operator=(const IconvTransService&);
};
class XMLUTIL_EXPORT IconvLCPTranscoder : public XMLLCPTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
IconvLCPTranscoder();
~IconvLCPTranscoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual char* transcode(const XMLCh* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLCh* transcode(const char* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
// -----------------------------------------------------------------------
// DEPRECATED old transcode interface
// -----------------------------------------------------------------------
virtual XMLSize_t calcRequiredSize(const char* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLSize_t calcRequiredSize(const XMLCh* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual bool transcode
(
const char* const toTranscode
, XMLCh* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
virtual bool transcode
(
const XMLCh* const toTranscode
, char* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
IconvLCPTranscoder(const IconvLCPTranscoder&);
IconvLCPTranscoder& operator=(const IconvLCPTranscoder&);
};
XERCES_CPP_NAMESPACE_END
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,359 @@
/*
* 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
*
* 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: IconvGNUTransService.hpp 932887 2010-04-11 13:04:59Z borisk $
*/
#if !defined(XERCESC_INCLUDE_GUARD_ICONVGNUTRANSSERVICE_HPP)
#define XERCESC_INCLUDE_GUARD_ICONVGNUTRANSSERVICE_HPP
#include <xercesc/util/TransService.hpp>
#include <xercesc/util/Mutexes.hpp>
#include <iconv.h>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// Libiconv wrapper (low-level conversion utilities collection)
// ---------------------------------------------------------------------------
class XMLUTIL_EXPORT IconvGNUWrapper
{
public:
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
IconvGNUWrapper
(
iconv_t cd_from,
iconv_t cd_to,
size_t uchsize,
unsigned int ubo,
MemoryManager* manager
);
virtual ~IconvGNUWrapper();
// Convert "native unicode" character into XMLCh
void mbcToXMLCh (const char *mbc, XMLCh *toRet) const;
// Convert XMLCh into "native unicode" character
void xmlChToMbc (XMLCh xch, char *mbc) const;
// Fill array of XMLCh characters with data, supplied in the array
// of "native unicode" characters.
XMLCh* mbsToXML (
const char* mbs_str,
XMLCh* xml_str,
size_t cnt
) const;
// Fill array of "native unicode" characters with data, supplied
// in the array of XMLCh characters.
char* xmlToMbs
(
const XMLCh* xml_str,
char* mbs_str,
size_t cnt
) const;
// Private data accessors
inline iconv_t cdTo () const { return fCDTo; }
inline iconv_t cdFrom () const { return fCDFrom; }
inline size_t uChSize () const { return fUChSize; }
inline unsigned int UBO () const { return fUBO; }
protected:
// The following four functions should called with the fMutex
// locked.
//
// Return uppercase equivalent for XMLCh
XMLCh toUpper (const XMLCh ch);
// Return uppercase equivalent for XMLCh
XMLCh toLower (const XMLCh ch);
// Wrapper around the iconv() for transcoding from the local charset
size_t iconvFrom
(
const char *fromPtr,
size_t *fromLen,
char **toPtr,
size_t toLen
);
// Wrapper around the iconv() for transcoding to the local charset
size_t iconvTo
(
const char *fromPtr,
size_t *fromLen,
char **toPtr,
size_t toLen
);
protected:
// Hidden constructor
IconvGNUWrapper(MemoryManager* manager);
// Private data accessors
inline void setCDTo (iconv_t cd) { fCDTo = cd; }
inline void setCDFrom (iconv_t cd) { fCDFrom = cd; }
inline void setUChSize (size_t sz) { fUChSize = sz; }
inline void setUBO (unsigned int u) { fUBO = u; }
private:
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
IconvGNUWrapper(const IconvGNUWrapper&);
IconvGNUWrapper& operator=(const IconvGNUWrapper&);
// -----------------------------------------------------------------------
// Private data members
//
// fCDTo
// Characterset conversion descriptor TO the local-host encoding
// fCDFrom
// Characterset conversion descriptor FROM the local-host encoding
// fUChSize
// Sizeof the "native unicode" character in bytes
// fUBO
// "Native unicode" characters byte order
// -----------------------------------------------------------------------
size_t fUChSize;
unsigned int fUBO;
iconv_t fCDTo;
iconv_t fCDFrom;
protected:
XMLMutex fMutex;
};
// ---------------------------------------------------------------------------
// FreeBSD-specific Transcoding Service implementation
// ---------------------------------------------------------------------------
class XMLUTIL_EXPORT IconvGNUTransService : public XMLTransService, IconvGNUWrapper
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
IconvGNUTransService(MemoryManager* manager);
~IconvGNUTransService();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoding service API
// -----------------------------------------------------------------------
virtual int compareIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
);
virtual int compareNIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
, const XMLSize_t maxChars
);
virtual const XMLCh* getId() const;
virtual XMLLCPTranscoder* makeNewLCPTranscoder(MemoryManager* manager);
virtual bool supportsSrcOfs() const;
virtual void upperCase(XMLCh* const toUpperCase);
virtual void lowerCase(XMLCh* const toUpperCase);
protected :
// -----------------------------------------------------------------------
// Protected virtual methods
// -----------------------------------------------------------------------
virtual XMLTranscoder* makeNewXMLTranscoder
(
const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const XMLSize_t blockSize
, MemoryManager* const manager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
IconvGNUTransService(const IconvGNUTransService&);
IconvGNUTransService& operator=(const IconvGNUTransService&);
// -----------------------------------------------------------------------
// Private data members
//
// fUnicodeCP
// Unicode encoding schema name
// -----------------------------------------------------------------------
const char* fUnicodeCP;
};
//----------------------------------------------------------------------------
// Implementation of the transcoders for arbitrary input characterset is
// supported ONLY through libiconv interface
//----------------------------------------------------------------------------
class XMLUTIL_EXPORT IconvGNUTranscoder : public XMLTranscoder, IconvGNUWrapper
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
IconvGNUTranscoder(const XMLCh* const encodingName
, const XMLSize_t blockSize
, iconv_t cd_from
, iconv_t cd_to
, size_t uchsize
, unsigned int ubo
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
~IconvGNUTranscoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual XMLSize_t transcodeFrom
(
const XMLByte* const srcData
, const XMLSize_t srcCount
, XMLCh* const toFill
, const XMLSize_t maxChars
, XMLSize_t& bytesEaten
, unsigned char* const charSizes
);
virtual XMLSize_t transcodeTo
(
const XMLCh* const srcData
, const XMLSize_t srcCount
, XMLByte* const toFill
, const XMLSize_t maxBytes
, XMLSize_t& charsEaten
, const UnRepOpts options
);
virtual bool canTranscodeTo
(
const unsigned int toCheck
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
IconvGNUTranscoder();
IconvGNUTranscoder(const IconvGNUTranscoder&);
IconvGNUTranscoder& operator=(const IconvGNUTranscoder&);
};
// ---------------------------------------------------------------------------
// GNU-specific XMLCh <-> local (host) characterset transcoder
// ---------------------------------------------------------------------------
class XMLUTIL_EXPORT IconvGNULCPTranscoder : public XMLLCPTranscoder, IconvGNUWrapper
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
IconvGNULCPTranscoder
(
iconv_t from,
iconv_t to,
size_t uchsize,
unsigned int ubo,
MemoryManager* manager
);
protected:
IconvGNULCPTranscoder(); // Unimplemented
public:
~IconvGNULCPTranscoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual char* transcode(const XMLCh* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLCh* transcode(const char* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
// -----------------------------------------------------------------------
// DEPRECATED old transcode interface
// -----------------------------------------------------------------------
virtual XMLSize_t calcRequiredSize(const char* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLSize_t calcRequiredSize(const XMLCh* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual bool transcode
(
const char* const toTranscode
, XMLCh* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
virtual bool transcode
(
const XMLCh* const toTranscode
, char* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
IconvGNULCPTranscoder(const IconvGNULCPTranscoder&);
IconvGNULCPTranscoder& operator=(const IconvGNULCPTranscoder&);
};
XERCES_CPP_NAMESPACE_END
#endif /* ICONVGNUTRANSSERVICE */
@@ -0,0 +1,972 @@
/*
* 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
*
* 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: MacOSUnicodeConverter.cpp 942561 2010-05-09 17:00:20Z borisk $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <xercesc/util/XercesDefs.hpp>
#include <algorithm>
#include <cstddef>
#include <cstring>
#if defined(__APPLE__)
// Framework includes
#include <CoreServices/CoreServices.h>
#else
// Classic includes otherwise
#include <MacErrors.h>
#include <Script.h>
#include <TextUtils.h>
#include <TextEncodingConverter.h>
#include <TextCommon.h>
#include <CodeFragments.h>
#include <UnicodeConverter.h>
#include <UnicodeUtilities.h>
#include <CFCharacterSet.h>
#include <CFString.h>
#endif
#include <xercesc/util/Transcoders/MacOSUnicodeConverter/MacOSUnicodeConverter.hpp>
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/TranscodingException.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/Janitor.hpp>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// Typedefs
// ---------------------------------------------------------------------------
// TempBufs are used for cases where we need a temporary buffer while processing.
const std::size_t kTempBufCount = 512;
typedef char TempCharBuf[kTempBufCount];
typedef UniChar TempUniBuf[kTempBufCount];
typedef XMLCh TempXMLBuf[kTempBufCount];
// ---------------------------------------------------------------------------
// Local, const data
// ---------------------------------------------------------------------------
const XMLCh MacOSUnicodeConverter::fgMyServiceId[] =
{
chLatin_M, chLatin_a, chLatin_c, chLatin_O, chLatin_S, chNull
};
const XMLCh MacOSUnicodeConverter::fgMacLCPEncodingName[] =
{
chLatin_M, chLatin_a, chLatin_c, chLatin_O, chLatin_S, chLatin_L
, chLatin_C, chLatin_P, chLatin_E, chLatin_n, chLatin_c, chLatin_o
, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chNull
};
// ---------------------------------------------------------------------------
// MacOSUnicodeConverter: Constructors and Destructor
// ---------------------------------------------------------------------------
MacOSUnicodeConverter::MacOSUnicodeConverter(MemoryManager* manager)
: fCollator(NULL)
{
// Test for presense of unicode collation functions
fHasUnicodeCollation = (UCCompareText != NULL);
// Create a unicode collator for doing string comparisons
if (fHasUnicodeCollation)
{
// Configure collation options
UCCollateOptions collateOptions =
kUCCollateComposeInsensitiveMask
| kUCCollateWidthInsensitiveMask
| kUCCollateCaseInsensitiveMask
| kUCCollatePunctuationSignificantMask
;
OSStatus status = UCCreateCollator(NULL, 0, collateOptions, &fCollator);
}
}
MacOSUnicodeConverter::~MacOSUnicodeConverter()
{
// Dispose our collator
if (fCollator != NULL)
UCDisposeCollator(&fCollator);
}
// ---------------------------------------------------------------------------
// MacOSUnicodeConverter: The virtual transcoding service API
// ---------------------------------------------------------------------------
int MacOSUnicodeConverter::compareIString( const XMLCh* const comp1
, const XMLCh* const comp2)
{
// If unicode collation routines are available, use them.
// This should be the case on Mac OS 8.6 and later,
// with Carbon 1.0.2 or later, and under Mac OS X.
//
// Otherwise, but only for Metrowerks, since only Metrowerks
// has a c library with a valid set of wchar routines,
// fall back to the standard library.
if (fHasUnicodeCollation && fCollator != NULL)
{
std::size_t cnt1 = XMLString::stringLen(comp1);
std::size_t cnt2 = XMLString::stringLen(comp2);
Boolean equivalent = false;
SInt32 order = 0;
OSStatus status = UCCompareText(
fCollator,
reinterpret_cast<const UniChar*>(comp1),
cnt1,
reinterpret_cast<const UniChar*>(comp2),
cnt2,
&equivalent,
&order
);
return ((status != noErr) || equivalent) ? 0 : order;
}
else
{
// For some reason there is no platform utils available
// where we expect it. Bail.
XMLPlatformUtils::panic(PanicHandler::Panic_NoTransService);
return 0;
}
}
int MacOSUnicodeConverter::compareNIString( const XMLCh* const comp1
, const XMLCh* const comp2
, const XMLSize_t maxChars)
{
// If unicode collation routines are available, use them.
// This should be the case on Mac OS 8.6 and later,
// with Carbon 1.0.2 or later, and under Mac OS X.
//
// Otherwise, but only for Metrowerks, since only Metrowerks
// has a c library with a valid set of wchar routines,
// fall back to the standard library.
if (fHasUnicodeCollation && fCollator != NULL)
{
std::size_t cnt1 = XMLString::stringLen(comp1);
std::size_t cnt2 = XMLString::stringLen(comp2);
// Restrict view of source characters to first {maxChars}
if (cnt1 > maxChars)
cnt1 = maxChars;
if (cnt2 > maxChars)
cnt2 = maxChars;
Boolean equivalent = false;
SInt32 order = 0;
OSStatus status = UCCompareText(
fCollator,
reinterpret_cast<const UniChar*>(comp1),
cnt1,
reinterpret_cast<const UniChar*>(comp2),
cnt2,
&equivalent,
&order
);
return ((status != noErr) || equivalent) ? 0 : order;
}
else
{
// For some reason there is no platform utils available
// where we expect it. Bail.
XMLPlatformUtils::panic(PanicHandler::Panic_NoTransService);
return 0;
}
}
const XMLCh* MacOSUnicodeConverter::getId() const
{
return fgMyServiceId;
}
TextEncoding
MacOSUnicodeConverter::discoverLCPEncoding()
{
TextEncoding encoding = 0;
// Ask the OS for the best text encoding for this application
// We would call GetApplicationTextEncoding(), but it's available only in
// Carbon (not CarbonCore), and we try to link with frameworks only in CoreServices.
// encoding = GetApplicationTextEncoding();
// Get TextEncoding for the current Mac System Script, falling back to Mac Roman
if (noErr != UpgradeScriptInfoToTextEncoding(
smSystemScript, kTextLanguageDontCare, kTextRegionDontCare,
NULL, &encoding))
encoding = CreateTextEncoding(kTextEncodingMacRoman,
kTextEncodingDefaultVariant,
kTextEncodingDefaultFormat);
// Traditionally, the Mac transcoder has used the current system script
// as the LCP text encoding.
//
// As of Xerces 2.6, this continues to be the case if XML_MACOS_LCP_TRADITIONAL
// is defined.
//
// Otherwise, but only for Mac OS X, utf-8 will be used instead.
// Since posix paths are utf-8 encoding on OS X, and the OS X
// terminal uses utf-8 by default, this seems to make the most sense.
#if !defined(XML_MACOS_LCP_TRADITIONAL)
if (true /*gMacOSXOrBetter*/)
{
// Manufacture a text encoding for UTF8
encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
kTextEncodingDefaultVariant,
kUnicodeUTF8Format);
}
#endif
return encoding;
}
XMLLCPTranscoder* MacOSUnicodeConverter::makeNewLCPTranscoder(MemoryManager* manager)
{
XMLLCPTranscoder* result = NULL;
OSStatus status = noErr;
// Discover the text encoding to use for the LCP
TextEncoding lcpTextEncoding = discoverLCPEncoding();
// We implement the LCP transcoder in terms of the XMLTranscoder.
// Create an XMLTranscoder for this encoding
XMLTransService::Codes resValue;
XMLTranscoder* xmlTrans = makeNewXMLTranscoder(fgMacLCPEncodingName,
resValue, kTempBufCount,
lcpTextEncoding, manager);
if (xmlTrans)
{
// Pass the XMLTranscoder over to the LPC transcoder
if (resValue == XMLTransService::Ok)
result = new (manager) MacOSLCPTranscoder(xmlTrans, manager);
else
delete xmlTrans;
}
return result;
}
bool MacOSUnicodeConverter::supportsSrcOfs() const
{
// For now, we don't support source offsets
return false;
}
void MacOSUnicodeConverter::upperCase(XMLCh* const toUpperCase)
{
#if TARGET_API_MAC_CARBON
// If we're targeting carbon, use the CFString conversion to uppercase
int len = XMLString::stringLen(toUpperCase);
CFMutableStringRef cfString = CFStringCreateMutableWithExternalCharactersNoCopy(
kCFAllocatorDefault,
(UniChar*)toUpperCase,
len, // length
len, // capacity
kCFAllocatorNull);
CFStringUppercase(cfString, NULL);
CFRelease(cfString);
#elif (__GNUC__ >= 3 && _GLIBCPP_USE_WCHAR_T)
// Use this if there's a reasonable c library available.
// Metrowerks does this reasonably
wchar_t c;
for (XMLCh* p = (XMLCh*)toUpperCase; ((c = *p) != 0); )
*p++ = std::towupper(c);
#else
#error Sorry, no support for upperCase
#endif
}
void MacOSUnicodeConverter::lowerCase(XMLCh* const toLowerCase)
{
#if TARGET_API_MAC_CARBON
// If we're targeting carbon, use the CFString conversion to uppercase
int len = XMLString::stringLen(toLowerCase);
CFMutableStringRef cfString = CFStringCreateMutableWithExternalCharactersNoCopy(
kCFAllocatorDefault,
(UniChar*)toLowerCase,
len, // length
len, // capacity
kCFAllocatorNull);
CFStringLowercase(cfString, NULL);
CFRelease(cfString);
#elif (__GNUC__ >= 3 && _GLIBCPP_USE_WCHAR_T)
// Use this if there's a reasonable c library available.
// Metrowerks does this reasonably
wchar_t c;
for (XMLCh* p = (XMLCh*)toLowerCase; ((c = *p) != 0); )
*p++ = std::towlower(c);
#else
#error Sorry, no support for lowerCase
#endif
}
void
MacOSUnicodeConverter::ConvertWideToNarrow(const XMLCh* wide, char* narrow, std::size_t maxChars)
{
while (maxChars-- > 0)
if ((*narrow++ = *wide++) == 0)
break;
}
void
MacOSUnicodeConverter::CopyCStringToPascal(const char* c, Str255 pas)
{
int len = strlen(c);
if (len > sizeof(pas)-1)
len = sizeof(pas)-1;
memmove(&pas[1], c, len);
pas[0] = len;
}
// ---------------------------------------------------------------------------
// MacOSTransService: The protected virtual transcoding service API
// ---------------------------------------------------------------------------
XMLTranscoder*
MacOSUnicodeConverter::makeNewXMLTranscoder(const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const XMLSize_t blockSize
, MemoryManager* const manager)
{
XMLTranscoder* result = NULL;
resValue = XMLTransService::Ok;
TextToUnicodeInfo textToUnicodeInfo = NULL;
UnicodeToTextInfo unicodeToTextInfo = NULL;
// Map the encoding to a Mac OS Encoding value
Str255 pasEncodingName;
char cEncodingName[256];
ConvertWideToNarrow(encodingName, cEncodingName, sizeof(cEncodingName));
CopyCStringToPascal(cEncodingName, pasEncodingName);
TextEncoding textEncoding = 0;
OSStatus status = TECGetTextEncodingFromInternetName (
&textEncoding,
pasEncodingName);
// Make a transcoder for that encoding
if (status == noErr)
result = makeNewXMLTranscoder(encodingName, resValue, blockSize, textEncoding, manager);
else
resValue = XMLTransService::UnsupportedEncoding;
return result;
}
XMLTranscoder*
MacOSUnicodeConverter::makeNewXMLTranscoder(const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const XMLSize_t blockSize
, TextEncoding textEncoding
, MemoryManager* const manager)
{
XMLTranscoder* result = NULL;
resValue = XMLTransService::Ok;
OSStatus status = noErr;
TECObjectRef textToUnicode = NULL;
TECObjectRef unicodeToText = NULL;
// We convert to and from utf16
TextEncoding utf16Encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
kTextEncodingDefaultVariant,
kUnicode16BitFormat);
// Create a TEC from our encoding to utf16
if (status == noErr)
status = TECCreateConverter(&textToUnicode, textEncoding, utf16Encoding);
// Create a TEC from utf16 to our encoding
if (status == noErr)
status = TECCreateConverter(&unicodeToText, utf16Encoding, textEncoding);
if (status != noErr)
{
// Clean up on error
if (textToUnicode != NULL)
TECDisposeConverter(textToUnicode);
if (unicodeToText != NULL)
TECDisposeConverter(unicodeToText);
resValue = XMLTransService::UnsupportedEncoding;
}
else
{
// Create our transcoder, passing in the converters
result = new (manager) MacOSTranscoder(encodingName, textToUnicode, unicodeToText, blockSize, manager);
}
return result;
}
// ---------------------------------------------------------------------------
// IsMacOSUnicodeConverterSupported
// ---------------------------------------------------------------------------
bool
MacOSUnicodeConverter::IsMacOSUnicodeConverterSupported(void)
{
return UpgradeScriptInfoToTextEncoding != 0
&& CreateTextToUnicodeInfoByEncoding != 0;
}
// ---------------------------------------------------------------------------
// MacOSTranscoder: Constructors and Destructor
// ---------------------------------------------------------------------------
MacOSTranscoder::MacOSTranscoder(const XMLCh* const encodingName
, TECObjectRef textToUnicode
, TECObjectRef unicodeToText
, const XMLSize_t blockSize
, MemoryManager* const manager) :
XMLTranscoder(encodingName, blockSize, manager),
mTextToUnicode(textToUnicode),
mUnicodeToText(unicodeToText)
{
}
MacOSTranscoder::~MacOSTranscoder()
{
// Dispose our text encoding converters
TECDisposeConverter(mTextToUnicode);
TECDisposeConverter(mUnicodeToText);
}
// ---------------------------------------------------------------------------
// MacOSTranscoder: The virtual transcoder API
// ---------------------------------------------------------------------------
XMLSize_t
MacOSTranscoder::transcodeFrom( const XMLByte* const srcData
, const XMLSize_t srcCount
, XMLCh* const toFill
, const XMLSize_t maxChars
, XMLSize_t& bytesEaten
, unsigned char* const charSizes)
{
// Reset the tec state (since we don't know that we're part of a
// larger run of text).
TECClearConverterContextInfo(mTextToUnicode);
// Do the conversion
ByteCount bytesConsumed = 0;
ByteCount bytesProduced = 0;
OSStatus status = TECConvertText(mTextToUnicode,
(ConstTextPtr) srcData,
srcCount, // inputBufferLength
&bytesConsumed, // actualInputLength
(TextPtr) toFill, // outputBuffer
maxChars * sizeof(XMLCh), // outputBufferLength
&bytesProduced); // actualOutputLength
// Ignorable error codes
if( status == kTECUsedFallbacksStatus
|| status == kTECOutputBufferFullStatus
|| status == kTECPartialCharErr
)
status = noErr;
if (status != noErr)
ThrowXML(TranscodingException, XMLExcepts::Trans_BadSrcSeq);
std::size_t charsProduced = bytesProduced / sizeof(XMLCh);
bytesEaten = bytesConsumed;
return charsProduced;
}
XMLSize_t
MacOSTranscoder::transcodeTo(const XMLCh* const srcData
, const XMLSize_t srcCount
, XMLByte* const toFill
, const XMLSize_t maxBytes
, XMLSize_t& charsEaten
, const UnRepOpts options)
{
// Reset the tec state (since we don't know that we're part of a
// larger run of text).
TECClearConverterContextInfo(mUnicodeToText);
// Do the conversion
ByteCount bytesConsumed = 0;
ByteCount bytesProduced = 0;
OSStatus status = TECConvertText(mUnicodeToText,
(ConstTextPtr) srcData,
srcCount * sizeof(XMLCh), // inputBufferLength
&bytesConsumed, // actualInputLength
(TextPtr) toFill, // outputBuffer
maxBytes, // outputBufferLength
&bytesProduced); // actualOutputLength
// Ignorable error codes
if( status == kTECUsedFallbacksStatus
|| status == kTECOutputBufferFullStatus
|| status == kTECPartialCharErr
)
status = noErr;
std::size_t charsConsumed = bytesConsumed / sizeof(XMLCh);
// Deal with errors
if (status != noErr)
{
if (status == kTECUnmappableElementErr && options == UnRep_Throw)
{
XMLCh tmpBuf[17];
XMLString::binToText(srcData[charsConsumed], tmpBuf, 16, 16);
ThrowXML2
(
TranscodingException
, XMLExcepts::Trans_Unrepresentable
, tmpBuf
, getEncodingName()
);
}
}
charsEaten = charsConsumed;
return bytesProduced;
}
bool
MacOSTranscoder::canTranscodeTo(const unsigned int toCheck)
{
//
// If the passed value is really a surrogate embedded together, then
// we need to break it out into its two chars. Else just one.
//
unsigned int srcCnt = 0;
UniChar srcBuf[2];
if (toCheck & 0xFFFF0000)
{
srcBuf[srcCnt++] = XMLCh(toCheck >> 10) + 0xD800;
srcBuf[srcCnt++] = XMLCh(toCheck & 0x3FF) + 0xDC00;
}
else
{
srcBuf[srcCnt++] = XMLCh(toCheck);
}
// Clear the converter state: we're in a new run of text
TECClearConverterContextInfo(mUnicodeToText);
//
// Use a local temp buffer that would hold any sane multi-byte char
// sequence and try to transcode this guy into it.
//
char tmpBuf[64];
ByteCount bytesConsumed = 0;
ByteCount bytesProduced = 0;
OSStatus status = TECConvertText(mUnicodeToText,
(ConstTextPtr) srcBuf,
srcCnt * sizeof(XMLCh), // inputBufferLength
&bytesConsumed, // actualInputLength
(TextPtr) tmpBuf, // outputBuffer
sizeof(tmpBuf), // outputBufferLength
&bytesProduced); // actualOutputLength
std::size_t charsConsumed = bytesConsumed / sizeof(XMLCh);
// Return true if we transcoded the character(s)
// successfully
return status == noErr && charsConsumed == srcCnt;
}
// ---------------------------------------------------------------------------
// MacOSLCPTranscoder: Constructors and Destructor
// ---------------------------------------------------------------------------
MacOSLCPTranscoder::MacOSLCPTranscoder(XMLTranscoder* const transcoder, MemoryManager* const manager)
: mTranscoder(transcoder),
mManager(manager),
mMutex (manager)
{
}
MacOSLCPTranscoder::~MacOSLCPTranscoder()
{
// Dispose the XMLTranscoder we're using
delete mTranscoder;
}
// ---------------------------------------------------------------------------
// MacOSLCPTranscoder: Implementation of the virtual transcoder interface
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// In order to implement calcRequiredSize we have to go ahead and do the
// conversion, which seems quite painful. The Mac Unicode converter has
// no way of saying "don't actually do the conversion." So we end up
// converting twice. It would be nice if the calling code could do some
// extra buffering to avoid this result.
// ---------------------------------------------------------------------------
XMLSize_t MacOSLCPTranscoder::calcRequiredSize(const char* const srcText
, MemoryManager* const manager)
{
if (!srcText)
return 0;
// Lock our mutex to gain exclusive access to the transcoder
// since the lcp transcoders are used globally.
XMLMutexLock lock(&mMutex);
std::size_t totalCharsProduced = 0;
const char* src = srcText;
XMLSize_t srcCnt = std::strlen(src);
// Iterate over the characters, converting into a temporary buffer which we'll discard.
// All this to get the size required.
while (srcCnt > 0)
{
TempXMLBuf tmpBuf;
XMLSize_t bytesConsumed = 0;
XMLSize_t charsProduced = mTranscoder->transcodeFrom((XMLByte*)src, srcCnt,
tmpBuf, kTempBufCount,
bytesConsumed,
NULL);
src += bytesConsumed;
srcCnt -= bytesConsumed;
totalCharsProduced += charsProduced;
// Bail out if nothing more was produced
if (charsProduced == 0)
break;
}
// Return number of XMLCh characters required (not counting terminating NULL!)
return totalCharsProduced;
}
// ---------------------------------------------------------------------------
// In order to implement calcRequiredSize we have to go ahead and do the
// conversion, which seems quite painful. The Mac Unicode converter has
// no way of saying "don't actually do the conversion." So we end up
// converting twice. It would be nice if the calling code could do some
// extra buffering to avoid this result.
// ---------------------------------------------------------------------------
XMLSize_t MacOSLCPTranscoder::calcRequiredSize(const XMLCh* const srcText
, MemoryManager* const manager)
{
if (!srcText)
return 0;
// Lock our mutex to gain exclusive access to the transcoder
// since the lcp transcoders are used globally.
XMLMutexLock lock(&mMutex);
std::size_t totalBytesProduced = 0;
const XMLCh* src = srcText;
XMLSize_t srcCnt = XMLString::stringLen(src);
// Iterate over the characters, converting into a temporary buffer which we'll discard.
// All this to get the size required.
while (srcCnt > 0)
{
TempCharBuf tmpBuf;
XMLSize_t charsConsumed = 0;
XMLSize_t bytesProduced = mTranscoder->transcodeTo(src, srcCnt,
(XMLByte*)tmpBuf, kTempBufCount,
charsConsumed,
XMLTranscoder::UnRep_RepChar);
src += charsConsumed;
srcCnt -= charsConsumed;
totalBytesProduced += bytesProduced;
// Bail out if nothing more was produced
if (bytesProduced == 0)
break;
}
// Return number of characters required (not counting terminating NULL!)
return totalBytesProduced;
}
char*
MacOSLCPTranscoder::transcode(const XMLCh* const srcText,
MemoryManager* const manager)
{
if (!srcText)
return NULL;
// Lock our mutex to gain exclusive access to the transcoder
// since the lcp transcoders are used globally.
XMLMutexLock lock(&mMutex);
ArrayJanitor<char> result(0);
const XMLCh* src = srcText;
XMLSize_t srcCnt = XMLString::stringLen(src);
std::size_t resultCnt = 0;
// Iterate over the characters, buffering into a local temporary
// buffer, which we dump into an allocated (and reallocated, as necessary)
// string for return.
while (srcCnt > 0)
{
// Transcode some characters
TempCharBuf tmpBuf;
XMLSize_t charsConsumed = 0;
XMLSize_t bytesProduced = mTranscoder->transcodeTo(src, srcCnt,
(XMLByte*)tmpBuf, kTempBufCount,
charsConsumed,
XMLTranscoder::UnRep_RepChar);
src += charsConsumed;
srcCnt -= charsConsumed;
// Move the data to result buffer, reallocating as needed
if (bytesProduced > 0)
{
// Allocate space for result
std::size_t newCnt = resultCnt + bytesProduced;
ArrayJanitor<char> newResult
(
(char*) manager->allocate((newCnt + 1) * sizeof(char)) //new char[newCnt + 1]
, manager
);
if (newResult.get() != NULL)
{
// Incorporate previous result
if (result.get() != NULL)
std::memcpy(newResult.get(), result.get(), resultCnt);
result.reset(newResult.release());
// Copy in new data
std::memcpy(result.get() + resultCnt, tmpBuf, bytesProduced);
resultCnt = newCnt;
// Terminate the result
result[resultCnt] = '\0';
}
}
else
break;
}
if (!result.get())
{
// No error, and no result: we probably processed a zero length
// input, in which case we want a valid zero length output.
result.reset
(
(char*) manager->allocate(sizeof(char))//new char[1]
, manager
);
result[0] = '\0';
}
return result.release();
}
XMLCh*
MacOSLCPTranscoder::transcode(const char* const srcText,
MemoryManager* const manager)
{
if (!srcText)
return NULL;
// Lock our mutex to gain exclusive access to the transcoder
// since the lcp transcoders are used globally.
XMLMutexLock lock(&mMutex);
ArrayJanitor<XMLCh> result(0);
const char* src = srcText;
std::size_t srcCnt = std::strlen(src);
std::size_t resultCnt = 0;
// Iterate over the characters, buffering into a local temporary
// buffer, which we dump into an allocated (and reallocated, as necessary)
// string for return.
while (srcCnt > 0)
{
// Transcode some characters
TempXMLBuf tmpBuf;
XMLSize_t bytesConsumed = 0;
XMLSize_t charsProduced = mTranscoder->transcodeFrom((XMLByte*)src, srcCnt,
tmpBuf, kTempBufCount,
bytesConsumed,
NULL);
src += bytesConsumed;
srcCnt -= bytesConsumed;
// Move the data to result buffer, reallocating as needed
if (charsProduced > 0)
{
// Allocate space for result
std::size_t newCnt = resultCnt + charsProduced;
ArrayJanitor<XMLCh> newResult
(
(XMLCh*) manager->allocate((newCnt + 1) * sizeof(XMLCh)) //new XMLCh[newCnt + 1]
, manager
);
if (newResult.get() != NULL)
{
// Incorporate previous result
if (result.get() != NULL)
std::memcpy(newResult.get(), result.get(), resultCnt * sizeof(XMLCh));
result.reset(newResult.release());
// Copy in new data
std::memcpy(result.get() + resultCnt, tmpBuf, charsProduced * sizeof(XMLCh));
resultCnt = newCnt;
result[resultCnt] = 0;
}
}
else
break;
}
if (!result.get())
{
// No error, and no result: we probably processed a zero length
// input, in which case we want a valid zero length output.
result.reset
(
(XMLCh*) manager->allocate(sizeof(XMLCh))//new XMLCh[1]
, manager
);
result[0] = '\0';
}
return result.release();
}
bool
MacOSLCPTranscoder::transcode( const char* const toTranscode
, XMLCh* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager)
{
// toFill must contain space for maxChars XMLCh characters + 1 (for terminating NULL).
// Check for a couple of psycho corner cases
if (!toTranscode || !maxChars || !*toTranscode)
{
toFill[0] = 0;
return true;
}
// Lock our mutex to gain exclusive access to the transcoder
// since the lcp transcoders are used globally.
XMLMutexLock lock(&mMutex);
// Call the transcoder to do the work
XMLSize_t srcLen = std::strlen(toTranscode);
XMLSize_t bytesConsumed = 0;
XMLSize_t charsProduced = mTranscoder->transcodeFrom((XMLByte*)toTranscode, srcLen,
toFill, maxChars,
bytesConsumed,
NULL);
// Zero terminate the output string
toFill[charsProduced] = L'\0';
// Return true if we consumed all of the characters
return (bytesConsumed == srcLen);
}
bool
MacOSLCPTranscoder::transcode( const XMLCh* const toTranscode
, char* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager)
{
// toFill must contain space for maxChars bytes + 1 (for terminating NULL).
// Check for a couple of psycho corner cases
if (!toTranscode || !maxChars || !*toTranscode)
{
toFill[0] = 0;
return true;
}
// Lock our mutex to gain exclusive access to the transcoder
// since the lcp transcoders are used globally.
XMLMutexLock lock(&mMutex);
// Call the transcoder to do the work
XMLSize_t srcLen = XMLString::stringLen(toTranscode);
XMLSize_t charsConsumed = 0;
XMLSize_t bytesProduced = mTranscoder->transcodeTo(toTranscode, srcLen,
(XMLByte*)toFill, maxChars,
charsConsumed,
XMLTranscoder::UnRep_RepChar);
// Zero terminate the output string
toFill[bytesProduced] = '\0';
// Return true if we consumed all of the characters
return (charsConsumed == srcLen);
}
XERCES_CPP_NAMESPACE_END
@@ -0,0 +1,279 @@
/*
* 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
*
* 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: MacOSUnicodeConverter.hpp 932887 2010-04-11 13:04:59Z borisk $
*/
#if !defined(XERCESC_INCLUDE_GUARD_MACOSUNICODECONVERTER_HPP)
#define XERCESC_INCLUDE_GUARD_MACOSUNICODECONVERTER_HPP
#include <cstddef>
#include <xercesc/util/TransService.hpp>
#include <xercesc/util/Mutexes.hpp>
#if defined(__APPLE__)
// Framework includes from ProjectBuilder
#include <CoreServices/CoreServices.h>
#else
// Classic includes otherwise
#include <UnicodeConverter.h>
#endif
XERCES_CPP_NAMESPACE_BEGIN
//
// The transcoding service has to provide a couple of required string
// and character operations, but its most important service is the creation
// of transcoder objects. There are two types of transcoders, which are
// discussed below in the XMLTranscoder class' description.
//
class XMLUTIL_EXPORT MacOSUnicodeConverter : public XMLTransService
{
public :
// -----------------------------------------------------------------------
// Public Constructors and Destructor
// -----------------------------------------------------------------------
~MacOSUnicodeConverter();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoding service API
// -----------------------------------------------------------------------
virtual int compareIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
);
virtual int compareNIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
, const XMLSize_t maxChars
);
virtual const XMLCh* getId() const;
virtual XMLLCPTranscoder* makeNewLCPTranscoder(MemoryManager* manager);
virtual bool supportsSrcOfs() const;
virtual void upperCase(XMLCh* const toUpperCase);
virtual void lowerCase(XMLCh* const toLowerCase);
protected :
// -----------------------------------------------------------------------
// Hidden constructors
// -----------------------------------------------------------------------
MacOSUnicodeConverter(MemoryManager* manager);
// -----------------------------------------------------------------------
// Protected virtual methods
// -----------------------------------------------------------------------
virtual XMLTranscoder* makeNewXMLTranscoder
(
const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const XMLSize_t blockSize
, MemoryManager* const manager
);
virtual XMLTranscoder* makeNewXMLTranscoder
(
const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const XMLSize_t blockSize
, TextEncoding textEncoding
, MemoryManager* const manager
);
// Sniff for available functionality
static bool IsMacOSUnicodeConverterSupported(void);
// Copy from a C string to a Str255
static void CopyCStringToPascal(const char* c, Str255 pas);
private :
friend class XMLPlatformUtils;
static const XMLCh fgMyServiceId[]; // Name of the our unicode converter
static const XMLCh fgMacLCPEncodingName[]; // Name of the LCP transcoder we create
bool fHasUnicodeCollation; // True if unicode collation is available
CollatorRef fCollator; // Our collator
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
MacOSUnicodeConverter(const MacOSUnicodeConverter&);
MacOSUnicodeConverter& operator=(const MacOSUnicodeConverter&);
// -----------------------------------------------------------------------
// Private methods
// -----------------------------------------------------------------------
void ConvertWideToNarrow(const XMLCh* wide, char* narrow, std::size_t maxChars);
// Figure out what text encoding to use for LCP transcoder
TextEncoding discoverLCPEncoding();
};
//
// This type of transcoder is for non-local code page encodings, i.e.
// named encodings. These are used internally by the scanner to internalize
// raw XML into the internal Unicode format, and by writer classes to
// convert that internal Unicode format (which comes out of the parser)
// back out to a format that the receiving client code wants to use.
//
class XMLUTIL_EXPORT MacOSTranscoder : public XMLTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
MacOSTranscoder(
const XMLCh* const encodingName,
TECObjectRef textToUnicode,
TECObjectRef unicodeToText,
const XMLSize_t blockSize,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
~MacOSTranscoder();
// -----------------------------------------------------------------------
// The virtual transcoding interface
// -----------------------------------------------------------------------
virtual XMLSize_t transcodeFrom
(
const XMLByte* const srcData
, const XMLSize_t srcCount
, XMLCh* const toFill
, const XMLSize_t maxChars
, XMLSize_t& bytesEaten
, unsigned char* const charSizes
);
virtual XMLSize_t transcodeTo
(
const XMLCh* const srcData
, const XMLSize_t srcCount
, XMLByte* const toFill
, const XMLSize_t maxBytes
, XMLSize_t& charsEaten
, const UnRepOpts options
);
virtual bool canTranscodeTo
(
const unsigned int toCheck
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
MacOSTranscoder(const MacOSTranscoder&);
MacOSTranscoder& operator=(const MacOSTranscoder&);
// -----------------------------------------------------------------------
// Private members
// -----------------------------------------------------------------------
TECObjectRef mTextToUnicode;
TECObjectRef mUnicodeToText;
};
//
// This class is a specialized transcoder that only transcodes between
// the internal XMLCh format and the local code page. It is specialized
// for the very common job of translating data from the client app's
// native code page to the internal format and vice versa.
//
class XMLUTIL_EXPORT MacOSLCPTranscoder : public XMLLCPTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
MacOSLCPTranscoder(XMLTranscoder* const transcoder, MemoryManager* const manager);
~MacOSLCPTranscoder();
// -----------------------------------------------------------------------
// The virtual transcoder API
//
// NOTE: All these APIs don't include null terminator characters in
// their parameters. So calcRequiredSize() returns the number
// of actual chars, not including the null. maxBytes and maxChars
// parameters refer to actual chars, not including the null so
// its assumed that the buffer is physically one char or byte
// larger.
// -----------------------------------------------------------------------
virtual char* transcode(const XMLCh* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLCh* transcode(const char* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
// -----------------------------------------------------------------------
// DEPRECATED old transcode interface
// -----------------------------------------------------------------------
virtual XMLSize_t calcRequiredSize(const char* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLSize_t calcRequiredSize(const XMLCh* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual bool transcode
(
const char* const toTranscode
, XMLCh* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
virtual bool transcode
(
const XMLCh* const toTranscode
, char* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
MacOSLCPTranscoder(const MacOSLCPTranscoder&);
MacOSLCPTranscoder& operator=(const MacOSLCPTranscoder&);
// -----------------------------------------------------------------------
// Private data members
// -----------------------------------------------------------------------
XMLTranscoder* const mTranscoder;
MemoryManager* const mManager;
XMLMutex mMutex; // Mutex to enable reentrancy of LCP transcoder
};
XERCES_CPP_NAMESPACE_END
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,277 @@
/*
* 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
*
* 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: Win32TransService.hpp 676954 2008-07-15 16:29:19Z dbertoni $
*/
#if !defined(XERCESC_INCLUDE_GUARD_WIN32TRANSSERVICE_HPP)
#define XERCESC_INCLUDE_GUARD_WIN32TRANSSERVICE_HPP
#include <xercesc/util/TransService.hpp>
#include <xercesc/util/RefHashTableOf.hpp>
#include <windows.h>
XERCES_CPP_NAMESPACE_BEGIN
class CPMapEntry;
//---------------------------------------------------------------------------
//
// class Win32TransService
//
//---------------------------------------------------------------------------
class XMLUTIL_EXPORT Win32TransService : public XMLTransService
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
Win32TransService(MemoryManager* manager);
virtual ~Win32TransService();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoding service API
// -----------------------------------------------------------------------
virtual int compareIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
);
virtual int compareNIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
, const XMLSize_t maxChars
);
virtual const XMLCh* getId() const;
virtual XMLLCPTranscoder* makeNewLCPTranscoder(MemoryManager* manager);
virtual bool supportsSrcOfs() const;
virtual void upperCase(XMLCh* const toUpperCase);
virtual void lowerCase(XMLCh* const toLowerCase);
protected :
// -----------------------------------------------------------------------
// Protected virtual methods
// -----------------------------------------------------------------------
virtual XMLTranscoder* makeNewXMLTranscoder
(
const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const XMLSize_t blockSize
, MemoryManager* const manager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
Win32TransService(const Win32TransService&);
Win32TransService& operator=(const Win32TransService&);
// This is a hash table of entries which map encoding names to their
// Windows specific code pages. The code page allows us to create
// transcoders for those encodings. The encoding names come from XML
// files.
//
// This map is shared unsynchronized among all threads of the process,
// which is cool since it will be read only once its initialized.
RefHashTableOf<CPMapEntry> *fCPMap;
MemoryManager* fManager;
};
//---------------------------------------------------------------------------
//
// class Win32Transcoder
//
//---------------------------------------------------------------------------
class XMLUTIL_EXPORT Win32Transcoder : public XMLTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
Win32Transcoder
(
const XMLCh* const encodingName
, const unsigned int ieCP
, const XMLSize_t blockSize
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
~Win32Transcoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual XMLSize_t transcodeFrom
(
const XMLByte* const srcData
, const XMLSize_t srcCount
, XMLCh* const toFill
, const XMLSize_t maxChars
, XMLSize_t& bytesEaten
, unsigned char* const charSizes
);
virtual XMLSize_t transcodeTo
(
const XMLCh* const srcData
, const XMLSize_t srcCount
, XMLByte* const toFill
, const XMLSize_t maxBytes
, XMLSize_t& charsEaten
, const UnRepOpts options
);
virtual bool canTranscodeTo
(
const unsigned int toCheck
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
Win32Transcoder(const Win32Transcoder&);
Win32Transcoder& operator=(const Win32Transcoder&);
// -----------------------------------------------------------------------
// Private data members
//
// fIECP
// This is the code page for this encoding.
//
// fUsedDef
// A flag passed into the conversions routines that is set to
// TRUE when a default character was substituted for the actual
// character.
//
// fPtrUsedDef
// A pointer to fUsedDef or a null pointer if the code page does not
// support the parameter that returns whether or not a default
// character was substituted.
//
// fFromFlags
// These are the flags passed to MultiByteToWideChar. For some
// code pages, this must be 0. See the documentation of the function
// for more details.
//
// fToFlags
// These are the flags passed to WideCharToMultiByte. For some
// code pages, this must be 0. See the documentation of the function
// for more details.
//
// -----------------------------------------------------------------------
UINT fIECP;
BOOL fUsedDef;
BOOL* fPtrUsedDef;
DWORD fFromFlags;
DWORD fToFlags;
};
//---------------------------------------------------------------------------
//
// class Win32LCPTranscoder
//
//---------------------------------------------------------------------------
class XMLUTIL_EXPORT Win32LCPTranscoder : public XMLLCPTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
Win32LCPTranscoder();
~Win32LCPTranscoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual char* transcode(const XMLCh* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLCh* transcode(const char* const toTranscode,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
// -----------------------------------------------------------------------
// DEPRECATED old transcode interface
// -----------------------------------------------------------------------
virtual XMLSize_t calcRequiredSize(const char* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual XMLSize_t calcRequiredSize(const XMLCh* const srcText
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
virtual bool transcode
(
const char* const toTranscode
, XMLCh* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
virtual bool transcode
(
const XMLCh* const toTranscode
, char* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
Win32LCPTranscoder(const Win32LCPTranscoder&);
Win32LCPTranscoder& operator=(const Win32LCPTranscoder&);
MemoryManager* fManager;
};
XERCES_CPP_NAMESPACE_END
#endif