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