[2674] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 4 | * this work for additional information regarding copyright ownership. |
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 6 | * (the "License"); you may not use this file except in compliance with |
---|
| 7 | * the License. You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /* |
---|
| 19 | * $Id: TransService.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #ifndef TRANSSERVICE_HPP |
---|
| 23 | #define TRANSSERVICE_HPP |
---|
| 24 | |
---|
| 25 | #include <xercesc/util/XMemory.hpp> |
---|
| 26 | #include <xercesc/util/PlatformUtils.hpp> |
---|
| 27 | #include <xercesc/framework/XMLRecognizer.hpp> |
---|
| 28 | #include <xercesc/util/RefHashTableOf.hpp> |
---|
| 29 | #include <xercesc/util/RefVectorOf.hpp> |
---|
| 30 | |
---|
| 31 | XERCES_CPP_NAMESPACE_BEGIN |
---|
| 32 | |
---|
| 33 | // Forward references |
---|
| 34 | //class XMLPlatformUtils; |
---|
| 35 | class XMLLCPTranscoder; |
---|
| 36 | class XMLTranscoder; |
---|
| 37 | class ENameMap; |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | // |
---|
| 41 | // This class is an abstract base class which are used to abstract the |
---|
| 42 | // transcoding services that Xerces uses. The parser's actual transcoding |
---|
| 43 | // needs are small so it is desirable to allow different implementations |
---|
| 44 | // to be provided. |
---|
| 45 | // |
---|
| 46 | // The transcoding service has to provide a couple of required string |
---|
| 47 | // and character operations, but its most important service is the creation |
---|
| 48 | // of transcoder objects. There are two types of transcoders, which are |
---|
| 49 | // discussed below in the XMLTranscoder class' description. |
---|
| 50 | // |
---|
| 51 | class XMLUTIL_EXPORT XMLTransService : public XMemory |
---|
| 52 | { |
---|
| 53 | public : |
---|
| 54 | // ----------------------------------------------------------------------- |
---|
| 55 | // Class specific types |
---|
| 56 | // ----------------------------------------------------------------------- |
---|
| 57 | enum Codes |
---|
| 58 | { |
---|
| 59 | Ok |
---|
| 60 | , UnsupportedEncoding |
---|
| 61 | , InternalFailure |
---|
| 62 | , SupportFilesNotFound |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | struct TransRec |
---|
| 66 | { |
---|
| 67 | XMLCh intCh; |
---|
| 68 | XMLByte extCh; |
---|
| 69 | }; |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | // ----------------------------------------------------------------------- |
---|
| 73 | // Public constructors and destructor |
---|
| 74 | // ----------------------------------------------------------------------- |
---|
| 75 | virtual ~XMLTransService(); |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | // ----------------------------------------------------------------------- |
---|
| 79 | // Non-virtual API |
---|
| 80 | // ----------------------------------------------------------------------- |
---|
| 81 | XMLTranscoder* makeNewTranscoderFor |
---|
| 82 | ( |
---|
| 83 | const XMLCh* const encodingName |
---|
| 84 | , XMLTransService::Codes& resValue |
---|
| 85 | , const unsigned int blockSize |
---|
| 86 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
| 87 | ); |
---|
| 88 | |
---|
| 89 | XMLTranscoder* makeNewTranscoderFor |
---|
| 90 | ( |
---|
| 91 | const char* const encodingName |
---|
| 92 | , XMLTransService::Codes& resValue |
---|
| 93 | , const unsigned int blockSize |
---|
| 94 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
| 95 | ); |
---|
| 96 | |
---|
| 97 | XMLTranscoder* makeNewTranscoderFor |
---|
| 98 | ( |
---|
| 99 | XMLRecognizer::Encodings encodingEnum |
---|
| 100 | , XMLTransService::Codes& resValue |
---|
| 101 | , const unsigned int blockSize |
---|
| 102 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
| 103 | ); |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | // ----------------------------------------------------------------------- |
---|
| 107 | // The virtual transcoding service API |
---|
| 108 | // ----------------------------------------------------------------------- |
---|
| 109 | virtual int compareIString |
---|
| 110 | ( |
---|
| 111 | const XMLCh* const comp1 |
---|
| 112 | , const XMLCh* const comp2 |
---|
| 113 | ) = 0; |
---|
| 114 | |
---|
| 115 | virtual int compareNIString |
---|
| 116 | ( |
---|
| 117 | const XMLCh* const comp1 |
---|
| 118 | , const XMLCh* const comp2 |
---|
| 119 | , const unsigned int maxChars |
---|
| 120 | ) = 0; |
---|
| 121 | |
---|
| 122 | virtual const XMLCh* getId() const = 0; |
---|
| 123 | |
---|
| 124 | virtual bool isSpace(const XMLCh toCheck) const = 0; |
---|
| 125 | |
---|
| 126 | virtual XMLLCPTranscoder* makeNewLCPTranscoder() = 0; |
---|
| 127 | |
---|
| 128 | virtual bool supportsSrcOfs() const = 0; |
---|
| 129 | |
---|
| 130 | virtual void upperCase(XMLCh* const toUpperCase) const = 0; |
---|
| 131 | virtual void lowerCase(XMLCh* const toLowerCase) const = 0; |
---|
| 132 | |
---|
| 133 | // ----------------------------------------------------------------------- |
---|
| 134 | // Allow users to add their own encodings to the intrinsinc mapping |
---|
| 135 | // table |
---|
| 136 | // Usage: |
---|
| 137 | // XMLTransService::addEncoding ( |
---|
| 138 | // gMyEncodingNameString |
---|
| 139 | // , new ENameMapFor<MyTransClassType>(gMyEncodingNameString) |
---|
| 140 | // ); |
---|
| 141 | // ----------------------------------------------------------------------- |
---|
| 142 | static void addEncoding(const XMLCh* const encoding, ENameMap* const ownMapping); |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | protected : |
---|
| 146 | // ----------------------------------------------------------------------- |
---|
| 147 | // Hidden constructors |
---|
| 148 | // ----------------------------------------------------------------------- |
---|
| 149 | XMLTransService(); |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | // ----------------------------------------------------------------------- |
---|
| 153 | // Protected virtual methods. |
---|
| 154 | // ----------------------------------------------------------------------- |
---|
| 155 | #ifdef OS390 |
---|
| 156 | friend class Uniconv390TransService; |
---|
| 157 | #endif |
---|
| 158 | virtual XMLTranscoder* makeNewXMLTranscoder |
---|
| 159 | ( |
---|
| 160 | const XMLCh* const encodingName |
---|
| 161 | , XMLTransService::Codes& resValue |
---|
| 162 | , const unsigned int blockSize |
---|
| 163 | , MemoryManager* const manager |
---|
| 164 | ) = 0; |
---|
| 165 | |
---|
| 166 | // ----------------------------------------------------------------------- |
---|
| 167 | // Protected init method for platform utils to call |
---|
| 168 | // ----------------------------------------------------------------------- |
---|
| 169 | friend class XMLPlatformUtils; |
---|
| 170 | virtual void initTransService(); |
---|
| 171 | |
---|
| 172 | // ----------------------------------------------------------------------- |
---|
| 173 | // protected static members |
---|
| 174 | // gMappings |
---|
| 175 | // This is a hash table of ENameMap objects. It is created and filled |
---|
| 176 | // in when the platform init calls our initTransService() method. |
---|
| 177 | // |
---|
| 178 | // gMappingsRecognizer |
---|
| 179 | // This is an array of ENameMap objects, predefined for those |
---|
| 180 | // already recognized by XMLRecognizer::Encodings. |
---|
| 181 | // |
---|
| 182 | |
---|
| 183 | static RefHashTableOf<ENameMap>* gMappings; |
---|
| 184 | static RefVectorOf<ENameMap>* gMappingsRecognizer; |
---|
| 185 | |
---|
| 186 | private : |
---|
| 187 | // ----------------------------------------------------------------------- |
---|
| 188 | // Unimplemented constructors and operators |
---|
| 189 | // ----------------------------------------------------------------------- |
---|
| 190 | XMLTransService(const XMLTransService&); |
---|
| 191 | XMLTransService& operator=(const XMLTransService&); |
---|
| 192 | |
---|
| 193 | // ----------------------------------------------------------------------- |
---|
| 194 | // Hidden method to enable/disable strict IANA encoding check |
---|
| 195 | // Caller: XMLPlatformUtils |
---|
| 196 | // ----------------------------------------------------------------------- |
---|
| 197 | void strictIANAEncoding(const bool newState); |
---|
| 198 | bool isStrictIANAEncoding(); |
---|
| 199 | static void reinitMappings(); |
---|
| 200 | static void reinitMappingsRecognizer(); |
---|
| 201 | |
---|
| 202 | }; |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * <code>DOMString</code> is the generic string class that stores all strings |
---|
| 208 | * used in the DOM C++ API. |
---|
| 209 | * |
---|
| 210 | * Though this class supports most of the common string operations to manipulate |
---|
| 211 | * strings, it is not meant to be a comphrehensive string class. |
---|
| 212 | */ |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * <code>XMLTranscoder</code> is for transcoding non-local code |
---|
| 216 | * page encodings, i.e. named encodings. These are used internally |
---|
| 217 | * by the scanner to internalize raw XML into the internal Unicode |
---|
| 218 | * format, and by writer classes to convert that internal Unicode |
---|
| 219 | * format (which comes out of the parser) back out to a format that |
---|
| 220 | * the receiving client code wants to use. |
---|
| 221 | */ |
---|
| 222 | class XMLUTIL_EXPORT XMLTranscoder : public XMemory |
---|
| 223 | { |
---|
| 224 | public : |
---|
| 225 | |
---|
| 226 | /** |
---|
| 227 | * This enum is used by the <code>transcodeTo()</code> method |
---|
| 228 | * to indicate how to react to unrepresentable characters. The |
---|
| 229 | * <code>transcodeFrom()</code> method always works the |
---|
| 230 | * same. It will consider any invalid data to be an error and |
---|
| 231 | * throw. |
---|
| 232 | */ |
---|
| 233 | enum UnRepOpts |
---|
| 234 | { |
---|
| 235 | UnRep_Throw /**< Throw an exception */ |
---|
| 236 | , UnRep_RepChar /**< Use the replacement char */ |
---|
| 237 | }; |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | /** @name Destructor. */ |
---|
| 241 | //@{ |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | * Destructor for XMLTranscoder |
---|
| 245 | * |
---|
| 246 | */ |
---|
| 247 | virtual ~XMLTranscoder(); |
---|
| 248 | //@} |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | /** @name The virtual transcoding interface */ |
---|
| 253 | //@{ |
---|
| 254 | |
---|
| 255 | /** Converts from the encoding of the service to the internal XMLCh* encoding |
---|
| 256 | * |
---|
| 257 | * @param srcData the source buffer to be transcoded |
---|
| 258 | * @param srcCount number of bytes in the source buffer |
---|
| 259 | * @param toFill the destination buffer |
---|
| 260 | * @param maxChars the max number of characters in the destination buffer |
---|
| 261 | * @param bytesEaten after transcoding, this will hold the number of bytes |
---|
| 262 | * that were processed from the source buffer |
---|
| 263 | * @param charSizes an array which must be at least as big as maxChars |
---|
| 264 | * into which will be inserted values that indicate how many |
---|
| 265 | * bytes from the input went into each XMLCh that was created |
---|
| 266 | * into toFill. Since many encodings use variable numbers of |
---|
| 267 | * byte per character, this provides a means to find out what |
---|
| 268 | * bytes in the input went into making a particular output |
---|
| 269 | * UTF-16 character. |
---|
| 270 | * @return Returns the number of chars put into the target buffer |
---|
| 271 | */ |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | virtual unsigned int transcodeFrom |
---|
| 275 | ( |
---|
| 276 | const XMLByte* const srcData |
---|
| 277 | , const unsigned int srcCount |
---|
| 278 | , XMLCh* const toFill |
---|
| 279 | , const unsigned int maxChars |
---|
| 280 | , unsigned int& bytesEaten |
---|
| 281 | , unsigned char* const charSizes |
---|
| 282 | ) = 0; |
---|
| 283 | |
---|
| 284 | /** Converts from the internal XMLCh* encoding to the encoding of the service |
---|
| 285 | * |
---|
| 286 | * @param srcData the source buffer to be transcoded |
---|
| 287 | * @param srcCount number of characters in the source buffer |
---|
| 288 | * @param toFill the destination buffer |
---|
| 289 | * @param maxBytes the max number of bytes in the destination buffer |
---|
| 290 | * @param charsEaten after transcoding, this will hold the number of chars |
---|
| 291 | * that were processed from the source buffer |
---|
| 292 | * @param options options to pass to the transcoder that explain how to |
---|
| 293 | * respond to an unrepresentable character |
---|
| 294 | * @return Returns the number of chars put into the target buffer |
---|
| 295 | */ |
---|
| 296 | |
---|
| 297 | virtual unsigned int transcodeTo |
---|
| 298 | ( |
---|
| 299 | const XMLCh* const srcData |
---|
| 300 | , const unsigned int srcCount |
---|
| 301 | , XMLByte* const toFill |
---|
| 302 | , const unsigned int maxBytes |
---|
| 303 | , unsigned int& charsEaten |
---|
| 304 | , const UnRepOpts options |
---|
| 305 | ) = 0; |
---|
| 306 | |
---|
| 307 | /** Query whether the transcoder can handle a given character |
---|
| 308 | * |
---|
| 309 | * @param toCheck the character code point to check |
---|
| 310 | */ |
---|
| 311 | |
---|
| 312 | virtual bool canTranscodeTo |
---|
| 313 | ( |
---|
| 314 | const unsigned int toCheck |
---|
| 315 | ) const = 0; |
---|
| 316 | |
---|
| 317 | //@} |
---|
| 318 | |
---|
| 319 | /** @name Getter methods */ |
---|
| 320 | //@{ |
---|
| 321 | |
---|
| 322 | /** Get the internal block size |
---|
| 323 | * |
---|
| 324 | * @return The block size indicated in the constructor. |
---|
| 325 | */ |
---|
| 326 | unsigned int getBlockSize() const; |
---|
| 327 | |
---|
| 328 | /** Get the encoding name |
---|
| 329 | * |
---|
| 330 | * @return the name of the encoding that this |
---|
| 331 | * <code>XMLTranscoder</code> object is for |
---|
| 332 | */ |
---|
| 333 | const XMLCh* getEncodingName() const; |
---|
| 334 | //@} |
---|
| 335 | |
---|
| 336 | /** @name Getter methods*/ |
---|
| 337 | //@{ |
---|
| 338 | |
---|
| 339 | /** Get the plugged-in memory manager |
---|
| 340 | * |
---|
| 341 | * This method returns the plugged-in memory manager user for dynamic |
---|
| 342 | * memory allocation/deallocation. |
---|
| 343 | * |
---|
| 344 | * @return the plugged-in memory manager |
---|
| 345 | */ |
---|
| 346 | MemoryManager* getMemoryManager() const; |
---|
| 347 | |
---|
| 348 | //@} |
---|
| 349 | |
---|
| 350 | protected : |
---|
| 351 | // ----------------------------------------------------------------------- |
---|
| 352 | // Hidden constructors |
---|
| 353 | // ----------------------------------------------------------------------- |
---|
| 354 | XMLTranscoder |
---|
| 355 | ( |
---|
| 356 | const XMLCh* const encodingName |
---|
| 357 | , const unsigned int blockSize |
---|
| 358 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
| 359 | ); |
---|
| 360 | |
---|
| 361 | |
---|
| 362 | // ----------------------------------------------------------------------- |
---|
| 363 | // Protected helper methods |
---|
| 364 | // ----------------------------------------------------------------------- |
---|
| 365 | // As the body of this function is commented out it could be removed. |
---|
| 366 | // However, currently all calls to it are guarded by #if defined(XERCES_DEBUG) |
---|
| 367 | // so will leave it for now. |
---|
| 368 | void checkBlockSize(const unsigned int toCheck); |
---|
| 369 | |
---|
| 370 | |
---|
| 371 | private : |
---|
| 372 | // ----------------------------------------------------------------------- |
---|
| 373 | // Unimplemented constructors and operators |
---|
| 374 | // ----------------------------------------------------------------------- |
---|
| 375 | XMLTranscoder(const XMLTranscoder&); |
---|
| 376 | XMLTranscoder& operator=(const XMLTranscoder&); |
---|
| 377 | |
---|
| 378 | // ----------------------------------------------------------------------- |
---|
| 379 | // Private data members |
---|
| 380 | // |
---|
| 381 | // fBlockSize |
---|
| 382 | // This is the block size indicated in the constructor. |
---|
| 383 | // |
---|
| 384 | // fEncodingName |
---|
| 385 | // This is the name of the encoding this encoder is for. All basic |
---|
| 386 | // XML transcoder's are for named encodings. |
---|
| 387 | // ----------------------------------------------------------------------- |
---|
| 388 | unsigned int fBlockSize; |
---|
| 389 | XMLCh* fEncodingName; |
---|
| 390 | MemoryManager* fMemoryManager; |
---|
| 391 | }; |
---|
| 392 | |
---|
| 393 | |
---|
| 394 | // |
---|
| 395 | // This class is a specialized transcoder that only transcodes between |
---|
| 396 | // the internal XMLCh format and the local code page. It is specialized |
---|
| 397 | // for the very common job of translating data from the client app's |
---|
| 398 | // native code page to the internal format and vice versa. |
---|
| 399 | // |
---|
| 400 | class XMLUTIL_EXPORT XMLLCPTranscoder : public XMemory |
---|
| 401 | { |
---|
| 402 | public : |
---|
| 403 | // ----------------------------------------------------------------------- |
---|
| 404 | // Public constructors and destructor |
---|
| 405 | // ----------------------------------------------------------------------- |
---|
| 406 | virtual ~XMLLCPTranscoder(); |
---|
| 407 | |
---|
| 408 | |
---|
| 409 | // ----------------------------------------------------------------------- |
---|
| 410 | // The virtual transcoder API |
---|
| 411 | // |
---|
| 412 | // NOTE: All these APIs don't include null terminator characters in |
---|
| 413 | // their parameters. So calcRequiredSize() returns the number |
---|
| 414 | // of actual chars, not including the null. maxBytes and maxChars |
---|
| 415 | // parameters refer to actual chars, not including the null so |
---|
| 416 | // its assumed that the buffer is physically one char or byte |
---|
| 417 | // larger. |
---|
| 418 | // ----------------------------------------------------------------------- |
---|
| 419 | virtual unsigned int calcRequiredSize(const char* const srcText |
---|
| 420 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) = 0; |
---|
| 421 | |
---|
| 422 | virtual unsigned int calcRequiredSize(const XMLCh* const srcText |
---|
| 423 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) = 0; |
---|
| 424 | |
---|
| 425 | virtual char* transcode(const XMLCh* const toTranscode) = 0; |
---|
| 426 | virtual char* transcode(const XMLCh* const toTranscode, |
---|
| 427 | MemoryManager* const manager) = 0; |
---|
| 428 | |
---|
| 429 | virtual XMLCh* transcode(const char* const toTranscode) = 0; |
---|
| 430 | virtual XMLCh* transcode(const char* const toTranscode, |
---|
| 431 | MemoryManager* const manager) = 0; |
---|
| 432 | |
---|
| 433 | virtual bool transcode |
---|
| 434 | ( |
---|
| 435 | const char* const toTranscode |
---|
| 436 | , XMLCh* const toFill |
---|
| 437 | , const unsigned int maxChars |
---|
| 438 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
| 439 | ) = 0; |
---|
| 440 | |
---|
| 441 | virtual bool transcode |
---|
| 442 | ( |
---|
| 443 | const XMLCh* const toTranscode |
---|
| 444 | , char* const toFill |
---|
| 445 | , const unsigned int maxBytes |
---|
| 446 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
| 447 | ) = 0; |
---|
| 448 | |
---|
| 449 | |
---|
| 450 | protected : |
---|
| 451 | // ----------------------------------------------------------------------- |
---|
| 452 | // Hidden constructors |
---|
| 453 | // ----------------------------------------------------------------------- |
---|
| 454 | XMLLCPTranscoder(); |
---|
| 455 | |
---|
| 456 | |
---|
| 457 | private : |
---|
| 458 | // ----------------------------------------------------------------------- |
---|
| 459 | // Unimplemented constructors and operators |
---|
| 460 | // ----------------------------------------------------------------------- |
---|
| 461 | XMLLCPTranscoder(const XMLLCPTranscoder&); |
---|
| 462 | XMLLCPTranscoder& operator=(const XMLLCPTranscoder&); |
---|
| 463 | }; |
---|
| 464 | |
---|
| 465 | |
---|
| 466 | // --------------------------------------------------------------------------- |
---|
| 467 | // XMLTranscoder: Getter methods |
---|
| 468 | // --------------------------------------------------------------------------- |
---|
| 469 | inline MemoryManager* XMLTranscoder::getMemoryManager() const |
---|
| 470 | { |
---|
| 471 | return fMemoryManager; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | // --------------------------------------------------------------------------- |
---|
| 475 | // XMLTranscoder: Protected helper methods |
---|
| 476 | // --------------------------------------------------------------------------- |
---|
| 477 | inline unsigned int XMLTranscoder::getBlockSize() const |
---|
| 478 | { |
---|
| 479 | return fBlockSize; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | inline const XMLCh* XMLTranscoder::getEncodingName() const |
---|
| 483 | { |
---|
| 484 | return fEncodingName; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | XERCES_CPP_NAMESPACE_END |
---|
| 488 | |
---|
| 489 | #endif |
---|