00001 /*
00002 * The Apache Software License, Version 1.1
00003 *
00004 * Copyright (c) 1999-2000 The Apache Software Foundation. All rights
00005 * reserved.
00006 *
00007 * Redistribution and use in source and binary forms, with or without
00008 * modification, are permitted provided that the following conditions
00009 * are met:
00010 *
00011 * 1. Redistributions of source code must retain the above copyright
00012 * notice, this list of conditions and the following disclaimer.
00013 *
00014 * 2. Redistributions in binary form must reproduce the above copyright
00015 * notice, this list of conditions and the following disclaimer in
00016 * the documentation and/or other materials provided with the
00017 * distribution.
00018 *
00019 * 3. The end-user documentation included with the redistribution,
00020 * if any, must include the following acknowledgment:
00021 * "This product includes software developed by the
00022 * Apache Software Foundation (http://www.apache.org/)."
00023 * Alternately, this acknowledgment may appear in the software itself,
00024 * if and wherever such third-party acknowledgments normally appear.
00025 *
00026 * 4. The names "Xerces" and "Apache Software Foundation" must
00027 * not be used to endorse or promote products derived from this
00028 * software without prior written permission. For written
00029 * permission, please contact apache\@apache.org.
00030 *
00031 * 5. Products derived from this software may not be called "Apache",
00032 * nor may "Apache" appear in their name, without prior written
00033 * permission of the Apache Software Foundation.
00034 *
00035 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00036 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00037 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00038 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00039 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00040 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00041 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00042 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00043 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00044 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00045 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00046 * SUCH DAMAGE.
00047 * ====================================================================
00048 *
00049 * This software consists of voluntary contributions made by many
00050 * individuals on behalf of the Apache Software Foundation, and was
00051 * originally based on software copyright (c) 1999, International
00052 * Business Machines, Inc., http://www.ibm.com . For more information
00053 * on the Apache Software Foundation, please see
00054 * <http://www.apache.org/>.
00055 */
00056
00057 /*
00058 * $Log: TransService.hpp,v $
00059 * Revision 1.11 2000/04/12 22:57:45 roddey
00060 * A couple of fixes to comments and parameter names to make them
00061 * more correct.
00062 *
00063 * Revision 1.10 2000/03/28 19:43:19 roddey
00064 * Fixes for signed/unsigned warnings. New work for two way transcoding
00065 * stuff.
00066 *
00067 * Revision 1.9 2000/03/17 23:59:54 roddey
00068 * Initial updates for two way transcoding support
00069 *
00070 * Revision 1.8 2000/03/02 19:54:46 roddey
00071 * This checkin includes many changes done while waiting for the
00072 * 1.1.0 code to be finished. I can't list them all here, but a list is
00073 * available elsewhere.
00074 *
00075 * Revision 1.7 2000/02/24 20:05:25 abagchi
00076 * Swat for removing Log from API docs
00077 *
00078 * Revision 1.6 2000/02/06 07:48:04 rahulj
00079 * Year 2K copyright swat.
00080 *
00081 * Revision 1.5 2000/01/25 22:49:55 roddey
00082 * Moved the supportsSrcOfs() method from the individual transcoder to the
00083 * transcoding service, where it should have been to begin with.
00084 *
00085 * Revision 1.4 2000/01/25 19:19:07 roddey
00086 * Simple addition of a getId() method to the xcode and netacess abstractions to
00087 * allow each impl to give back an id string.
00088 *
00089 * Revision 1.3 1999/12/18 00:18:10 roddey
00090 * More changes to support the new, completely orthagonal support for
00091 * intrinsic encodings.
00092 *
00093 * Revision 1.2 1999/12/15 19:41:28 roddey
00094 * Support for the new transcoder system, where even intrinsic encodings are
00095 * done via the same transcoder abstraction as external ones.
00096 *
00097 * Revision 1.1.1.1 1999/11/09 01:05:16 twl
00098 * Initial checkin
00099 *
00100 * Revision 1.2 1999/11/08 20:45:16 rahul
00101 * Swat for adding in Product name and CVS comment log variable.
00102 *
00103 */
00104
00105 #ifndef TRANSSERVICE_HPP
00106 #define TRANSSERVICE_HPP
00107
00108 #include <util/XercesDefs.hpp>
00109
00110
00111 // Forward references
00112 class XMLPlatformUtils;
00113 class XMLLCPTranscoder;
00114 class XMLTranscoder;
00115
00116
00117 //
00118 // This class is an abstract base class which are used to abstract the
00119 // transcoding services that Xerces uses. The parser's actual transcoding
00120 // needs are small so it is desirable to allow different implementations
00121 // to be provided.
00122 //
00123 // The transcoding service has to provide a couple of required string
00124 // and character operations, but its most important service is the creation
00125 // of transcoder objects. There are two types of transcoders, which are
00126 // discussed below in the XMLTranscoder class' description.
00127 //
00128 class XMLTransService
00129 {
00130 public :
00131 // -----------------------------------------------------------------------
00132 // Class specific types
00133 // -----------------------------------------------------------------------
00134 enum Codes
00135 {
00136 Ok
00137 , UnsupportedEncoding
00138 , InternalFailure
00139 , SupportFilesNotFound
00140 };
00141
00142 struct TransRec
00143 {
00144 XMLCh intCh;
00145 XMLByte extCh;
00146 };
00147
00148
00149 // -----------------------------------------------------------------------
00150 // Public constructors and destructor
00151 // -----------------------------------------------------------------------
00152 virtual ~XMLTransService();
00153
00154
00155 // -----------------------------------------------------------------------
00156 // Non-virtual API
00157 // -----------------------------------------------------------------------
00158 XMLTranscoder* makeNewTranscoderFor
00159 (
00160 const XMLCh* const encodingName
00161 , XMLTransService::Codes& resValue
00162 , const unsigned int blockSize
00163 );
00164
00165 XMLTranscoder* makeNewTranscoderFor
00166 (
00167 const char* const encodingName
00168 , XMLTransService::Codes& resValue
00169 , const unsigned int blockSize
00170 );
00171
00172
00173 // -----------------------------------------------------------------------
00174 // The virtual transcoding service API
00175 // -----------------------------------------------------------------------
00176 virtual int compareIString
00177 (
00178 const XMLCh* const comp1
00179 , const XMLCh* const comp2
00180 ) = 0;
00181
00182 virtual int compareNIString
00183 (
00184 const XMLCh* const comp1
00185 , const XMLCh* const comp2
00186 , const unsigned int maxChars
00187 ) = 0;
00188
00189 virtual const XMLCh* getId() const = 0;
00190
00191 virtual bool isSpace(const XMLCh toCheck) const = 0;
00192
00193 virtual XMLLCPTranscoder* makeNewLCPTranscoder() = 0;
00194
00195 virtual bool supportsSrcOfs() const = 0;
00196
00197 virtual void upperCase(XMLCh* const toUpperCase) const = 0;
00198
00199
00200 protected :
00201 // -----------------------------------------------------------------------
00202 // Hidden constructors
00203 // -----------------------------------------------------------------------
00204 XMLTransService();
00205
00206
00207 // -----------------------------------------------------------------------
00208 // Protected virtual methods.
00209 // -----------------------------------------------------------------------
00210 virtual XMLTranscoder* makeNewXMLTranscoder
00211 (
00212 const XMLCh* const encodingName
00213 , XMLTransService::Codes& resValue
00214 , const unsigned int blockSize
00215 ) = 0;
00216
00217
00218 private :
00219 // -----------------------------------------------------------------------
00220 // Unimplemented constructors and operators
00221 // -----------------------------------------------------------------------
00222 XMLTransService(const XMLTransService&);
00223 void operator=(const XMLTransService&);
00224
00225
00226 // -----------------------------------------------------------------------
00227 // Hidden init method for platform utils to call
00228 // -----------------------------------------------------------------------
00229 friend class XMLPlatformUtils;
00230 void initTransService();
00231 };
00232
00233
00234
00235 //
00236 // This type of transcoder is for non-local code page encodings, i.e.
00237 // named encodings. These are used internally by the scanner to internalize
00238 // raw XML into the internal Unicode format, and by writer classes to
00239 // convert that internal Unicode format (which comes out of the parser)
00240 // back out to a format that the receiving client code wants to use.
00241 //
00242 class XMLTranscoder
00243 {
00244 public :
00245 // -----------------------------------------------------------------------
00246 // This enum is used by the transcodeTo() method to indicate how to
00247 // react to unrepresentable characters. The transcodeFrom() method always
00248 // works the same. It will consider any invalid data to be an error and
00249 // throw.
00250 //
00251 // The options mean:
00252 // Throw - Throw an exception
00253 // RepChar - Use the replacement char
00254 // -----------------------------------------------------------------------
00255 enum UnRepOpts
00256 {
00257 UnRep_Throw
00258 , UnRep_RepChar
00259 };
00260
00261
00262 // -----------------------------------------------------------------------
00263 // Public constructors and destructor
00264 // -----------------------------------------------------------------------
00265 virtual ~XMLTranscoder();
00266
00267
00268 // -----------------------------------------------------------------------
00269 // The virtual transcoding interface
00270 // -----------------------------------------------------------------------
00271 virtual unsigned int transcodeFrom
00272 (
00273 const XMLByte* const srcData
00274 , const unsigned int srcCount
00275 , XMLCh* const toFill
00276 , const unsigned int maxChars
00277 , unsigned int& bytesEaten
00278 , unsigned char* const charSizes
00279 ) = 0;
00280
00281 virtual unsigned int transcodeTo
00282 (
00283 const XMLCh* const srcData
00284 , const unsigned int srcCount
00285 , XMLByte* const toFill
00286 , const unsigned int maxBytes
00287 , unsigned int& charsEaten
00288 , const UnRepOpts options
00289 ) = 0;
00290
00291 virtual bool canTranscodeTo
00292 (
00293 const unsigned int toCheck
00294 ) const = 0;
00295
00296
00297 // -----------------------------------------------------------------------
00298 // Getter methods
00299 // -----------------------------------------------------------------------
00300 unsigned int getBlockSize() const;
00301
00302 const XMLCh* getEncodingName() const;
00303
00304
00305 protected :
00306 // -----------------------------------------------------------------------
00307 // Hidden constructors
00308 // -----------------------------------------------------------------------
00309 XMLTranscoder
00310 (
00311 const XMLCh* const encodingName
00312 , const unsigned int blockSize
00313 );
00314
00315
00316 // -----------------------------------------------------------------------
00317 // Protected helper methods
00318 // -----------------------------------------------------------------------
00319 void checkBlockSize(const unsigned int toCheck);
00320
00321
00322 private :
00323 // -----------------------------------------------------------------------
00324 // Unimplemented constructors and operators
00325 // -----------------------------------------------------------------------
00326 XMLTranscoder(const XMLTranscoder&);
00327 void operator=(const XMLTranscoder&);
00328
00329
00330 // -----------------------------------------------------------------------
00331 // Private data members
00332 //
00333 // fBlockSize
00334 // This is the block size indicated in the constructor. This lets
00335 // the derived class preallocate appopriately sized buffers. This
00336 // sets the maximum number of characters which can be internalized
00337 // per call to transcodeFrom() and transcodeTo().
00338 //
00339 // fEncodingName
00340 // This is the name of the encoding this encoder is for. All basic
00341 // XML transcoder's are for named encodings.
00342 // -----------------------------------------------------------------------
00343 unsigned int fBlockSize;
00344 XMLCh* fEncodingName;
00345 };
00346
00347
00348 //
00349 // This class is a specialized transcoder that only transcodes between
00350 // the internal XMLCh format and the local code page. It is specialized
00351 // for the very common job of translating data from the client app's
00352 // native code page to the internal format and vice versa.
00353 //
00354 class XMLLCPTranscoder
00355 {
00356 public :
00357 // -----------------------------------------------------------------------
00358 // Public constructors and destructor
00359 // -----------------------------------------------------------------------
00360 virtual ~XMLLCPTranscoder();
00361
00362
00363 // -----------------------------------------------------------------------
00364 // The virtual transcoder API
00365 //
00366 // NOTE: All these APIs don't include null terminator characters in
00367 // their parameters. So calcRequiredSize() returns the number
00368 // of actual chars, not including the null. maxBytes and maxChars
00369 // parameters refer to actual chars, not including the null so
00370 // its assumed that the buffer is physically one char or byte
00371 // larger.
00372 // -----------------------------------------------------------------------
00373 virtual unsigned int calcRequiredSize(const char* const srcText) = 0;
00374
00375 virtual unsigned int calcRequiredSize(const XMLCh* const srcText) = 0;
00376
00377 virtual char* transcode(const XMLCh* const toTranscode) = 0;
00378
00379 virtual XMLCh* transcode(const char* const toTranscode) = 0;
00380
00381 virtual bool transcode
00382 (
00383 const char* const toTranscode
00384 , XMLCh* const toFill
00385 , const unsigned int maxChars
00386 ) = 0;
00387
00388 virtual bool transcode
00389 (
00390 const XMLCh* const toTranscode
00391 , char* const toFill
00392 , const unsigned int maxBytes
00393 ) = 0;
00394
00395
00396 protected :
00397 // -----------------------------------------------------------------------
00398 // Hidden constructors
00399 // -----------------------------------------------------------------------
00400 XMLLCPTranscoder();
00401
00402
00403 private :
00404 // -----------------------------------------------------------------------
00405 // Unimplemented constructors and operators
00406 // -----------------------------------------------------------------------
00407 XMLLCPTranscoder(const XMLLCPTranscoder&);
00408 void operator=(const XMLLCPTranscoder&);
00409 };
00410
00411
00412 // ---------------------------------------------------------------------------
00413 // XMLTranscoder: Protected helper methods
00414 // ---------------------------------------------------------------------------
00415 inline unsigned int XMLTranscoder::getBlockSize() const
00416 {
00417 return fBlockSize;
00418 }
00419
00420 inline const XMLCh* XMLTranscoder::getEncodingName() const
00421 {
00422 return fEncodingName;
00423 }
00424
00425 #endif