00001
00032
00033
00034
00035
00036
00037 #include "linden_common.h"
00038
00039 #include "llxmlparser.h"
00040 #include "llerror.h"
00041
00042
00043 LLXmlParser::LLXmlParser()
00044 :
00045 mParser( NULL ),
00046 mDepth( 0 )
00047 {
00048 strcpy( mAuxErrorString, "no error" );
00049
00050
00051 mParser = XML_ParserCreate(NULL);
00052
00053 XML_SetUserData(mParser, this);
00054 XML_SetElementHandler( mParser, startElementHandler, endElementHandler);
00055 XML_SetCharacterDataHandler( mParser, characterDataHandler);
00056 XML_SetProcessingInstructionHandler( mParser, processingInstructionHandler);
00057 XML_SetCommentHandler( mParser, commentHandler);
00058
00059 XML_SetCdataSectionHandler( mParser, startCdataSectionHandler, endCdataSectionHandler);
00060
00061
00062
00063 XML_SetDefaultHandlerExpand( mParser, defaultDataHandler);
00064
00065 XML_SetUnparsedEntityDeclHandler( mParser, unparsedEntityDeclHandler);
00066 }
00067
00068 LLXmlParser::~LLXmlParser()
00069 {
00070 XML_ParserFree( mParser );
00071 }
00072
00073
00074 BOOL LLXmlParser::parseFile(const std::string &path)
00075 {
00076 llassert( !mDepth );
00077
00078 BOOL success = TRUE;
00079
00080 FILE* file = LLFile::fopen(path.c_str(), "rb");
00081 if( !file )
00082 {
00083 snprintf( mAuxErrorString, sizeof(mAuxErrorString), "Couldn't open file %s", path.c_str());
00084 success = FALSE;
00085 }
00086 else
00087 {
00088 S32 bytes_read = 0;
00089
00090 fseek(file, 0L, SEEK_END);
00091 S32 buffer_size = ftell(file);
00092 fseek(file, 0L, SEEK_SET);
00093
00094 void* buffer = XML_GetBuffer(mParser, buffer_size);
00095 if( !buffer )
00096 {
00097 snprintf( mAuxErrorString, sizeof(mAuxErrorString), "Unable to allocate XML buffer while reading file %s", path.c_str() );
00098 success = FALSE;
00099 goto exit_label;
00100 }
00101
00102 bytes_read = (S32)fread(buffer, 1, buffer_size, file);
00103 if( bytes_read <= 0 )
00104 {
00105 snprintf( mAuxErrorString, sizeof(mAuxErrorString), "Error while reading file %s", path.c_str() );
00106 success = FALSE;
00107 goto exit_label;
00108 }
00109
00110 if( !XML_ParseBuffer(mParser, bytes_read, TRUE ) )
00111 {
00112 snprintf( mAuxErrorString, sizeof(mAuxErrorString), "Error while parsing file %s", path.c_str() );
00113 success = FALSE;
00114 }
00115
00116 exit_label:
00117 fclose( file );
00118 }
00119
00120
00121 if( success )
00122 {
00123 llassert( !mDepth );
00124 }
00125 mDepth = 0;
00126
00127 if( !success )
00128 {
00129 llwarns << mAuxErrorString << llendl;
00130 }
00131
00132 return success;
00133 }
00134
00135
00136
00137
00138
00139 S32 LLXmlParser::parse( const char* buf, int len, int isFinal )
00140 {
00141 return XML_Parse(mParser, buf, len, isFinal);
00142 }
00143
00144 const char* LLXmlParser::getErrorString()
00145 {
00146 const char* error_string = XML_ErrorString(XML_GetErrorCode( mParser ));
00147 if( !error_string )
00148 {
00149 error_string = mAuxErrorString;
00150 }
00151 return error_string;
00152 }
00153
00154 S32 LLXmlParser::getCurrentLineNumber()
00155 {
00156 return XML_GetCurrentLineNumber( mParser );
00157 }
00158
00159 S32 LLXmlParser::getCurrentColumnNumber()
00160 {
00161 return XML_GetCurrentColumnNumber(mParser);
00162 }
00163
00165
00166
00167
00168 void LLXmlParser::startElementHandler(
00169 void *userData,
00170 const XML_Char *name,
00171 const XML_Char **atts)
00172 {
00173 LLXmlParser* self = (LLXmlParser*) userData;
00174 self->startElement( name, atts );
00175 self->mDepth++;
00176 }
00177
00178
00179 void LLXmlParser::endElementHandler(
00180 void *userData,
00181 const XML_Char *name)
00182 {
00183 LLXmlParser* self = (LLXmlParser*) userData;
00184 self->mDepth--;
00185 self->endElement( name );
00186 }
00187
00188
00189
00190 void LLXmlParser::characterDataHandler(
00191 void *userData,
00192 const XML_Char *s,
00193 int len)
00194 {
00195 LLXmlParser* self = (LLXmlParser*) userData;
00196 self->characterData( s, len );
00197 }
00198
00199
00200
00201 void LLXmlParser::processingInstructionHandler(
00202 void *userData,
00203 const XML_Char *target,
00204 const XML_Char *data)
00205 {
00206 LLXmlParser* self = (LLXmlParser*) userData;
00207 self->processingInstruction( target, data );
00208 }
00209
00210
00211
00212 void LLXmlParser::commentHandler(void *userData, const XML_Char *data)
00213 {
00214 LLXmlParser* self = (LLXmlParser*) userData;
00215 self->comment( data );
00216 }
00217
00218
00219 void LLXmlParser::startCdataSectionHandler(void *userData)
00220 {
00221 LLXmlParser* self = (LLXmlParser*) userData;
00222 self->mDepth++;
00223 self->startCdataSection();
00224 }
00225
00226
00227 void LLXmlParser::endCdataSectionHandler(void *userData)
00228 {
00229 LLXmlParser* self = (LLXmlParser*) userData;
00230 self->endCdataSection();
00231 self->mDepth++;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 void LLXmlParser::defaultDataHandler(
00249 void *userData,
00250 const XML_Char *s,
00251 int len)
00252 {
00253 LLXmlParser* self = (LLXmlParser*) userData;
00254 self->defaultData( s, len );
00255 }
00256
00257
00258
00259
00260
00261
00262 void LLXmlParser::unparsedEntityDeclHandler(
00263 void *userData,
00264 const XML_Char *entityName,
00265 const XML_Char *base,
00266 const XML_Char *systemId,
00267 const XML_Char *publicId,
00268 const XML_Char *notationName)
00269 {
00270 LLXmlParser* self = (LLXmlParser*) userData;
00271 self->unparsedEntityDecl( entityName, base, systemId, publicId, notationName );
00272 }
00273
00274
00275
00276
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421