TransService.hpp

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: TransService.hpp 568078 2007-08-21 11:43:25Z amassari $
00020  */
00021 
00022 #ifndef TRANSSERVICE_HPP
00023 #define TRANSSERVICE_HPP
00024 
00025 #include <xercesc/util/XMemory.hpp>
00026 #include <xercesc/util/PlatformUtils.hpp>
00027 #include <xercesc/framework/XMLRecognizer.hpp>
00028 #include <xercesc/util/RefHashTableOf.hpp>
00029 #include <xercesc/util/RefVectorOf.hpp>
00030 
00031 XERCES_CPP_NAMESPACE_BEGIN
00032 
00033 // Forward references
00034 //class XMLPlatformUtils;
00035 class XMLLCPTranscoder;
00036 class XMLTranscoder;
00037 class ENameMap;
00038 
00039 
00040 //
00041 //  This class is an abstract base class which are used to abstract the
00042 //  transcoding services that Xerces uses. The parser's actual transcoding
00043 //  needs are small so it is desirable to allow different implementations
00044 //  to be provided.
00045 //
00046 //  The transcoding service has to provide a couple of required string
00047 //  and character operations, but its most important service is the creation
00048 //  of transcoder objects. There are two types of transcoders, which are
00049 //  discussed below in the XMLTranscoder class' description.
00050 //
00051 class XMLUTIL_EXPORT XMLTransService : public XMemory
00052 {
00053 public :
00054     // -----------------------------------------------------------------------
00055     //  Class specific types
00056     // -----------------------------------------------------------------------
00057     enum Codes
00058     {
00059         Ok
00060         , UnsupportedEncoding
00061         , InternalFailure
00062         , SupportFilesNotFound
00063     };
00064 
00065     struct TransRec
00066     {
00067         XMLCh       intCh;
00068         XMLByte     extCh;
00069     };
00070 
00071 
00072     // -----------------------------------------------------------------------
00073     //  Public constructors and destructor
00074     // -----------------------------------------------------------------------
00075     virtual ~XMLTransService();
00076 
00077 
00078     // -----------------------------------------------------------------------
00079     //  Non-virtual API
00080     // -----------------------------------------------------------------------
00081     XMLTranscoder* makeNewTranscoderFor
00082     (
00083         const   XMLCh* const            encodingName
00084         ,       XMLTransService::Codes& resValue
00085         , const unsigned int            blockSize
00086         , MemoryManager* const          manager = XMLPlatformUtils::fgMemoryManager
00087     );
00088 
00089     XMLTranscoder* makeNewTranscoderFor
00090     (
00091         const   char* const             encodingName
00092         ,       XMLTransService::Codes& resValue
00093         , const unsigned int            blockSize
00094         , MemoryManager* const          manager = XMLPlatformUtils::fgMemoryManager
00095     );
00096 
00097     XMLTranscoder* makeNewTranscoderFor
00098     (
00099         XMLRecognizer::Encodings        encodingEnum
00100         ,       XMLTransService::Codes& resValue
00101         , const unsigned int            blockSize
00102         , MemoryManager* const          manager = XMLPlatformUtils::fgMemoryManager
00103     );
00104 
00105 
00106     // -----------------------------------------------------------------------
00107     //  The virtual transcoding service API
00108     // -----------------------------------------------------------------------
00109     virtual int compareIString
00110     (
00111         const   XMLCh* const    comp1
00112         , const XMLCh* const    comp2
00113     ) = 0;
00114 
00115     virtual int compareNIString
00116     (
00117         const   XMLCh* const    comp1
00118         , const XMLCh* const    comp2
00119         , const unsigned int    maxChars
00120     ) = 0;
00121 
00122     virtual const XMLCh* getId() const = 0;
00123 
00124     virtual bool isSpace(const XMLCh toCheck) const = 0;
00125 
00126     virtual XMLLCPTranscoder* makeNewLCPTranscoder() = 0;
00127 
00128     virtual bool supportsSrcOfs() const = 0;
00129 
00130     virtual void upperCase(XMLCh* const toUpperCase) const = 0;
00131     virtual void lowerCase(XMLCh* const toLowerCase) const = 0;
00132 
00133     // -----------------------------------------------------------------------
00134     //  Allow users to add their own encodings to the intrinsinc mapping
00135     //  table
00136     //  Usage:
00137     //      XMLTransService::addEncoding (
00138     //          gMyEncodingNameString
00139     //          , new ENameMapFor<MyTransClassType>(gMyEncodingNameString)
00140     //      );
00141     // -----------------------------------------------------------------------
00142     static void addEncoding(const XMLCh* const encoding, ENameMap* const ownMapping);
00143 
00144 
00145 protected :
00146     // -----------------------------------------------------------------------
00147     //  Hidden constructors
00148     // -----------------------------------------------------------------------
00149     XMLTransService();
00150 
00151 
00152     // -----------------------------------------------------------------------
00153     //  Protected virtual methods.
00154     // -----------------------------------------------------------------------
00155 #ifdef OS390
00156     friend class Uniconv390TransService;
00157 #endif
00158     virtual XMLTranscoder* makeNewXMLTranscoder
00159     (
00160         const   XMLCh* const            encodingName
00161         ,       XMLTransService::Codes& resValue
00162         , const unsigned int            blockSize
00163         , MemoryManager* const          manager
00164     ) = 0;
00165 
00166     // -----------------------------------------------------------------------
00167     //  Protected init method for platform utils to call
00168     // -----------------------------------------------------------------------
00169     friend class XMLPlatformUtils;
00170     virtual void initTransService();
00171 
00172     // -----------------------------------------------------------------------
00173     // protected static members
00174     //  gMappings
00175     //      This is a hash table of ENameMap objects. It is created and filled
00176     //      in when the platform init calls our initTransService() method.
00177     //
00178     //  gMappingsRecognizer
00179     //      This is an array of ENameMap objects, predefined for those
00180     //      already recognized by XMLRecognizer::Encodings.
00181     //
00182 
00183     static RefHashTableOf<ENameMap>*    gMappings;
00184     static RefVectorOf<ENameMap>*       gMappingsRecognizer;
00185 
00186 private :
00187     // -----------------------------------------------------------------------
00188     //  Unimplemented constructors and operators
00189     // -----------------------------------------------------------------------
00190     XMLTransService(const XMLTransService&);
00191     XMLTransService& operator=(const XMLTransService&);
00192 
00193     // -----------------------------------------------------------------------
00194     //  Hidden method to enable/disable strict IANA encoding check
00195     //  Caller: XMLPlatformUtils
00196     // -----------------------------------------------------------------------
00197     void strictIANAEncoding(const bool newState);
00198     bool isStrictIANAEncoding();
00199     static void reinitMappings();
00200     static void reinitMappingsRecognizer();
00201 
00202 };
00203 
00204 
00205 
00222 class XMLUTIL_EXPORT XMLTranscoder : public XMemory
00223 {
00224 public :
00225 
00233     enum UnRepOpts
00234     {
00235         UnRep_Throw     
00236         , UnRep_RepChar     
00237     };
00238 
00239 
00242 
00247     virtual ~XMLTranscoder();
00249 
00250 
00251 
00254 
00274     virtual unsigned int transcodeFrom
00275     (
00276         const   XMLByte* const          srcData
00277         , const unsigned int            srcCount
00278         ,       XMLCh* const            toFill
00279         , const unsigned int            maxChars
00280         ,       unsigned int&           bytesEaten
00281         ,       unsigned char* const    charSizes
00282     ) = 0;
00283 
00297     virtual unsigned int transcodeTo
00298     (
00299         const   XMLCh* const    srcData
00300         , const unsigned int    srcCount
00301         ,       XMLByte* const  toFill
00302         , const unsigned int    maxBytes
00303         ,       unsigned int&   charsEaten
00304         , const UnRepOpts       options
00305     ) = 0;
00306 
00312     virtual bool canTranscodeTo
00313     (
00314         const   unsigned int    toCheck
00315     )   const = 0;
00316 
00318 
00321 
00326     unsigned int getBlockSize() const;
00327 
00333     const XMLCh* getEncodingName() const;
00335 
00338 
00346     MemoryManager* getMemoryManager() const;
00347 
00349 
00350 protected :
00351     // -----------------------------------------------------------------------
00352     //  Hidden constructors
00353     // -----------------------------------------------------------------------
00354     XMLTranscoder
00355     (
00356         const   XMLCh* const    encodingName
00357         , const unsigned int    blockSize
00358         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00359     );
00360 
00361 
00362     // -----------------------------------------------------------------------
00363     //  Protected helper methods
00364     // -----------------------------------------------------------------------
00365     // As the body of this function is commented out it could be removed.
00366     // However, currently all calls to it are guarded by #if defined(XERCES_DEBUG)
00367     // so will leave it for now.
00368     void checkBlockSize(const unsigned int toCheck);
00369 
00370 
00371 private :
00372     // -----------------------------------------------------------------------
00373     //  Unimplemented constructors and operators
00374     // -----------------------------------------------------------------------
00375     XMLTranscoder(const XMLTranscoder&);
00376     XMLTranscoder& operator=(const XMLTranscoder&);
00377 
00378     // -----------------------------------------------------------------------
00379     //  Private data members
00380     //
00381     //  fBlockSize
00382     //      This is the block size indicated in the constructor.
00383     //
00384     //  fEncodingName
00385     //      This is the name of the encoding this encoder is for. All basic
00386     //      XML transcoder's are for named encodings.
00387     // -----------------------------------------------------------------------
00388     unsigned int    fBlockSize;
00389     XMLCh*          fEncodingName;
00390     MemoryManager*  fMemoryManager;
00391 };
00392 
00393 
00394 //
00395 //  This class is a specialized transcoder that only transcodes between
00396 //  the internal XMLCh format and the local code page. It is specialized
00397 //  for the very common job of translating data from the client app's
00398 //  native code page to the internal format and vice versa.
00399 //
00400 class XMLUTIL_EXPORT XMLLCPTranscoder : public XMemory
00401 {
00402 public :
00403     // -----------------------------------------------------------------------
00404     //  Public constructors and destructor
00405     // -----------------------------------------------------------------------
00406     virtual ~XMLLCPTranscoder();
00407 
00408 
00409     // -----------------------------------------------------------------------
00410     //  The virtual transcoder API
00411     //
00412     //  NOTE:   All these APIs don't include null terminator characters in
00413     //          their parameters. So calcRequiredSize() returns the number
00414     //          of actual chars, not including the null. maxBytes and maxChars
00415     //          parameters refer to actual chars, not including the null so
00416     //          its assumed that the buffer is physically one char or byte
00417     //          larger.
00418     // -----------------------------------------------------------------------
00419     virtual unsigned int calcRequiredSize(const char* const srcText
00420         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) = 0;
00421 
00422     virtual unsigned int calcRequiredSize(const XMLCh* const srcText
00423         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) = 0;
00424 
00425     virtual char* transcode(const XMLCh* const toTranscode) = 0;
00426     virtual char* transcode(const XMLCh* const toTranscode,
00427                             MemoryManager* const manager) = 0;
00428 
00429     virtual XMLCh* transcode(const char* const toTranscode) = 0;
00430     virtual XMLCh* transcode(const char* const toTranscode,
00431                              MemoryManager* const manager) = 0;
00432 
00433     virtual bool transcode
00434     (
00435         const   char* const     toTranscode
00436         ,       XMLCh* const    toFill
00437         , const unsigned int    maxChars
00438         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00439     ) = 0;
00440 
00441     virtual bool transcode
00442     (
00443         const   XMLCh* const    toTranscode
00444         ,       char* const     toFill
00445         , const unsigned int    maxBytes
00446         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00447     ) = 0;
00448 
00449 
00450 protected :
00451     // -----------------------------------------------------------------------
00452     //  Hidden constructors
00453     // -----------------------------------------------------------------------
00454     XMLLCPTranscoder();
00455 
00456 
00457 private :
00458     // -----------------------------------------------------------------------
00459     //  Unimplemented constructors and operators
00460     // -----------------------------------------------------------------------
00461     XMLLCPTranscoder(const XMLLCPTranscoder&);
00462     XMLLCPTranscoder& operator=(const XMLLCPTranscoder&);
00463 };
00464 
00465 
00466 // ---------------------------------------------------------------------------
00467 //  XMLTranscoder: Getter methods
00468 // ---------------------------------------------------------------------------
00469 inline MemoryManager* XMLTranscoder::getMemoryManager() const
00470 {
00471     return fMemoryManager;
00472 }
00473 
00474 // ---------------------------------------------------------------------------
00475 //  XMLTranscoder: Protected helper methods
00476 // ---------------------------------------------------------------------------
00477 inline unsigned int XMLTranscoder::getBlockSize() const
00478 {
00479     return fBlockSize;
00480 }
00481 
00482 inline const XMLCh* XMLTranscoder::getEncodingName() const
00483 {
00484     return fEncodingName;
00485 }
00486 
00487 XERCES_CPP_NAMESPACE_END
00488 
00489 #endif

Generated on Thu Feb 19 11:35:43 2009 for Xerces-C++ by  doxygen 1.5.4