00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <libCMMLParse/CMMLParser.h>
00037
00038
00039
00040
00041
00042
00043
00044
00045 using namespace std;
00046
00047
00048
00049
00050
00051 CMMLParser::CMMLParser(void)
00052 {
00053 }
00054
00055 CMMLParser::~CMMLParser(void)
00056 {
00057 }
00058
00059 bool CMMLParser::parseDocFromBuffer(wstring locCMMLFileWString, C_CMMLDoc* outCMMLDoc, C_CMMLError* outCMMLError)
00060 {
00061
00062 bool locReturnValue = false;
00063
00064
00065 if (!outCMMLDoc) {
00066 return false;
00067 }
00068
00069
00070
00071
00072 size_t locCMMLTagIndex = locCMMLFileWString.find(L"<cmml", 0);
00073 if (locCMMLTagIndex != string::npos) {
00074 wstring locCMMLFileWString1 = locCMMLFileWString.substr(locCMMLTagIndex);
00075
00076
00077 C_CMMLRootTag* locRootTag = new C_CMMLRootTag;
00078 locReturnValue = parseCMMLRootTag(locCMMLFileWString1, locRootTag, outCMMLError);
00079 if (locReturnValue) {
00080
00081 outCMMLDoc->setRoot(locRootTag);
00082 } else {
00083
00084 outCMMLDoc = NULL;
00085 }
00086 } else {
00087
00088 outCMMLDoc = NULL;
00089 }
00090
00091 return locReturnValue;
00092 }
00093
00094 bool CMMLParser::parseDocFromFile(wstring inFilename, C_CMMLDoc* outCMMLDoc, C_CMMLError* outCMMLError)
00095 {
00096
00097 bool locReturnValue = false;
00098
00099
00100 if (!outCMMLDoc) {
00101 return false;
00102 }
00103
00104
00105
00106 fstream locFile;
00107
00108 locFile.open(StringHelper::toNarrowStr(inFilename).c_str(), ios_base::in | ios_base::binary);
00109
00110 if (!locFile.is_open()) {
00111
00112
00113 return false;
00114 }
00115
00116
00117 locFile.seekg(0, ios::end);
00118 size_t locCMMLFileSize = locFile.tellg();
00119 locFile.clear();
00120
00121
00122 locFile.seekg(0);
00123
00124 unsigned short BUFFER_SIZE = 8192;
00125 char *locBuffer = new char[locCMMLFileSize];
00126 size_t locBytesRead = 0;
00127
00128 while (!locFile.eof()) {
00129 locFile.read(locBuffer + locBytesRead, BUFFER_SIZE);
00130 locBytesRead += locFile.gcount();
00131 }
00132
00133 locFile.close();
00134
00135
00136 wstring locCMMLFileWString = StringHelper::toWStr(locBuffer);
00137
00138
00139
00140
00141 size_t locCMMLTagIndex = locCMMLFileWString.find(L"<cmml", 0);
00142 if (locCMMLTagIndex != string::npos) {
00143 locCMMLFileWString = locCMMLFileWString.substr(locCMMLTagIndex);
00144 }
00145
00146
00147 C_CMMLRootTag* locRootTag = new C_CMMLRootTag;
00148 locReturnValue = parseCMMLRootTag(locCMMLFileWString, locRootTag, outCMMLError);
00149 if (locReturnValue) {
00150
00151 outCMMLDoc->setRoot(locRootTag);
00152 } else {
00153
00154 outCMMLDoc = NULL;
00155 }
00156
00157
00158 delete [] locBuffer;
00159
00160 return locReturnValue;
00161 }
00162
00163 bool CMMLParser::parseCMMLRootTag(wstring inCMMLRootText, C_CMMLRootTag* outCMMLRoot, C_CMMLError* outCMMLError )
00164 {
00165
00166 bool locReturnValue = false;
00167
00168
00169 if (!outCMMLRoot) {
00170 return false;
00171 }
00172
00173
00174
00175
00176 string locCMMLRootText = StringHelper::toUTF8Str(inCMMLRootText);
00177
00178
00179 XTag *locRootParser = NULL;
00180 int ErrorOffset = 0;
00181 locRootParser = xtag_new_parse(locCMMLRootText.c_str(), (int)locCMMLRootText.size(), &ErrorOffset);
00182 if (locRootParser) {
00183
00184 if (strcmp(xtag_get_name(locRootParser), "cmml") == 0) {
00185
00186 locReturnValue = parseRootTag(locRootParser, outCMMLRoot);
00187 }
00188 }
00189 else
00190 {
00191
00192 int LineNumber = 0;
00193 size_t Offset=0;
00194 for ( ;Offset != string::npos, Offset < (size_t)ErrorOffset; Offset++)
00195 {
00196 Offset = locCMMLRootText.find("\n", (size_t)Offset);
00197 LineNumber++;
00198 if (Offset == string::npos)
00199 {
00200 break;
00201 }
00202 }
00203
00204 if (outCMMLError != NULL)
00205 {
00206 outCMMLError->SetLineNumber(LineNumber);
00207 }
00208 }
00209
00210 if (locRootParser) {
00211 xtag_free(locRootParser);
00212 }
00213
00214 return locReturnValue;
00215 }
00216
00217
00218 bool CMMLParser::parseClipTag(wstring inClipText, C_ClipTag* outClip)
00219 {
00220
00221
00222 bool locReturnValue = false;
00223
00224
00225 if (!outClip) {
00226 return false;
00227 }
00228
00229
00230
00231
00232 string locClipText = StringHelper::toUTF8Str(inClipText);
00233
00234
00235 XTag *locClipParser = NULL;
00236 int ErrorOffset = 0;
00237 locClipParser = xtag_new_parse(locClipText.c_str(), (int)locClipText.size(), &ErrorOffset);
00238 if (locClipParser) {
00239
00240 if (strcmp(xtag_get_name(locClipParser), "clip") == 0) {
00241
00242 locReturnValue = parseClipTag(locClipParser, outClip);
00243 }
00244 }
00245
00246 if (locClipParser) {
00247 xtag_free(locClipParser);
00248 }
00249
00250 return locReturnValue;
00251 }
00252
00253
00254 bool CMMLParser::parseHeadTag(wstring inHeadText, C_HeadTag* outHead)
00255 {
00256
00257
00258 bool locReturnValue = false;
00259
00260
00261 if (!outHead) {
00262 return false;
00263 }
00264
00265
00266
00267
00268 string locHeadText = StringHelper::toUTF8Str(inHeadText);
00269
00270
00271 XTag *locHeadParser = NULL;
00272 int ErrorOffset = 0;
00273 locHeadParser = xtag_new_parse(locHeadText.c_str(), (int)locHeadText.size(), &ErrorOffset);
00274 if (locHeadParser) {
00275 if (strcmp(xtag_get_name(locHeadParser), "head") == 0) {
00276 locReturnValue = parseHeadTag(locHeadParser, outHead);
00277 }
00278 }
00279
00280 if (locHeadParser) {
00281 xtag_free(locHeadParser);
00282 }
00283
00284 return locReturnValue;
00285 }
00286
00287
00288
00289
00290 #define XTAG_PARSE_INTO(tagParser, parseMethod, TagType, parentTagSetter, parentTag) \
00291 { \
00292 TagType *locTag = new TagType; \
00293 if (!parseMethod(tagParser, locTag)) { \
00294 return false; \
00295 } \
00296 parentTag->parentTagSetter(locTag); \
00297 };
00298
00299 #define XTAG_SET_ATTRIBUTE(tagParser, attributeName, tag, attributeSetter) \
00300 { \
00301 const char *locAttributeCString = xtag_get_attribute(tagParser, attributeName); \
00302 if (locAttributeCString) { \
00303 tag->attributeSetter(StringHelper::toWStr(locAttributeCString)); \
00304 \
00305 } \
00306 };
00307
00308 #define XTAG_REQUIRED_ATTRIBUTE(tagParser, attributeName, tag) \
00309 { \
00310 const char *locAttributeCString = xtag_get_attribute(tagParser, attributeName); \
00311 if (!locAttributeCString) { \
00312 return false; \
00313 } else { \
00314 \
00315 } \
00316 };
00317
00318 #define XTAG_PARSE_CHILD(parentParser, tagName, tagParser, tagType, setterMethod, parentTag) \
00319 { \
00320 XTag *locParser = NULL; \
00321 locParser = xtag_first_child(parentParser, tagName); \
00322 if (locParser) { \
00323 XTAG_PARSE_INTO(locParser, tagParser, tagType, setterMethod, parentTag); \
00324 } \
00325 };
00326
00327 #define XTAG_EXACTLY_ONE_CHILD(parentParser, tagName) \
00328 { \
00329 XTag *locParser = xtag_first_child(parentParser, tagName); \
00330 if (locParser != NULL) { \
00331 \
00332 locParser = xtag_next_child(parentParser, tagName); \
00333 if (locParser) { \
00334 \
00335 return false; \
00336 } \
00337 } else { \
00338 \
00339 return false; \
00340 } \
00341 };
00342
00343 #define XTAG_PARSE_LIST(TagType, listTagName, tagParser, parentParser, parentTag, parentGetListMethod) \
00344 { \
00345 XTag *locTagListParser = NULL; \
00346 for ( locTagListParser = xtag_first_child(parentParser, listTagName); \
00347 locTagListParser != NULL; \
00348 locTagListParser = xtag_next_child(parentParser, listTagName)) { \
00349 XTAG_PARSE_INTO(locTagListParser, tagParser, TagType, addTag, parentTag->parentGetListMethod()); \
00350 } \
00351 };
00352
00353 #define XTAG_SET_CDATA(tagParser, tag) \
00354 { \
00355 const char *locCData = xtag_get_pcdata(tagParser); \
00356 if (locCData) { \
00357 \
00358 \
00359 tag->setText(StringHelper::fromUTF8Str(locCData)); \
00360 \
00361 } \
00362 };
00363
00364
00365
00366
00367 bool CMMLParser::parseStreamTag(XTag* inStreamParser, C_StreamTag* outStream)
00368 {
00369 XTAG_SET_ATTRIBUTE(inStreamParser, "id", outStream, setId);
00370 XTAG_SET_ATTRIBUTE(inStreamParser, "timebase", outStream, setTimebase);
00371 XTAG_SET_ATTRIBUTE(inStreamParser, "utc", outStream, setUtc);
00372
00373 XTAG_PARSE_LIST(C_ImportTag, "import", parseImportTag,
00374 inStreamParser, outStream, importList);
00375
00376 return true;
00377 }
00378
00379
00380 bool CMMLParser::parseRootTag(XTag* inCMMLRootParser, C_CMMLRootTag* outCMMLRoot)
00381 {
00382 XTAG_SET_ATTRIBUTE(inCMMLRootParser, "id", outCMMLRoot, setId);
00383
00384 XTAG_EXACTLY_ONE_CHILD(inCMMLRootParser, "head");
00385 XTAG_PARSE_CHILD(inCMMLRootParser, "head", parseHeadTag, C_HeadTag, setHead, outCMMLRoot);
00386 XTAG_PARSE_CHILD(inCMMLRootParser, "stream", parseStreamTag, C_StreamTag, setStream, outCMMLRoot);
00387
00388 XTAG_PARSE_LIST(C_ClipTag, "clip", parseClipTag, inCMMLRootParser, outCMMLRoot, clipList);
00389
00390
00391 XTAG_SET_ATTRIBUTE(inCMMLRootParser, "lang", outCMMLRoot, setLang);
00392 XTAG_SET_ATTRIBUTE(inCMMLRootParser, "dir", outCMMLRoot, setDirn);
00393
00394 return true;
00395 }
00396
00397 bool CMMLParser::parseHeadTag(XTag* inHeadParser, C_HeadTag* outHead)
00398 {
00399 XTAG_SET_ATTRIBUTE(inHeadParser, "id", outHead, setId);
00400 XTAG_SET_ATTRIBUTE(inHeadParser, "profile", outHead, setProfile);
00401
00402 XTAG_EXACTLY_ONE_CHILD(inHeadParser, "title");
00403 XTAG_PARSE_CHILD(inHeadParser, "title", parseTitleTag, C_TitleTag, setTitle, outHead);
00404 XTAG_PARSE_CHILD(inHeadParser, "base", parseBaseTag, C_BaseTag, setBase, outHead);
00405
00406 XTAG_PARSE_LIST(C_MetaTag, "meta", parseMetaTag, inHeadParser, outHead, metaList);
00407
00408
00409 XTAG_SET_ATTRIBUTE(inHeadParser, "lang", outHead, setLang);
00410 XTAG_SET_ATTRIBUTE(inHeadParser, "dir", outHead, setDirn);
00411
00412 return true;
00413 }
00414
00415 bool CMMLParser::parseTitleTag(XTag* inTitleParser, C_TitleTag* outTitle)
00416 {
00417 XTAG_SET_ATTRIBUTE(inTitleParser, "id", outTitle, setId);
00418
00419 XTAG_SET_CDATA(inTitleParser, outTitle);
00420
00421
00422 XTAG_SET_ATTRIBUTE(inTitleParser, "lang", outTitle, setLang);
00423 XTAG_SET_ATTRIBUTE(inTitleParser, "dir", outTitle, setDirn);
00424
00425 return true;
00426 }
00427
00428 bool CMMLParser::parseBaseTag(XTag* inBaseParser, C_BaseTag* outBase)
00429 {
00430 XTAG_SET_ATTRIBUTE(inBaseParser, "id", outBase, setId);
00431 XTAG_SET_ATTRIBUTE(inBaseParser, "href", outBase, setHref);
00432 XTAG_REQUIRED_ATTRIBUTE(inBaseParser, "href", outBase);
00433
00434 return true;
00435 }
00436
00437 bool CMMLParser::parseMetaTag(XTag* inMetaParser, C_MetaTag* outMeta)
00438 {
00439 XTAG_SET_ATTRIBUTE(inMetaParser, "scheme", outMeta, setScheme);
00440 XTAG_SET_ATTRIBUTE(inMetaParser, "content", outMeta, setContent);
00441 XTAG_SET_ATTRIBUTE(inMetaParser, "id", outMeta, setId);
00442 XTAG_SET_ATTRIBUTE(inMetaParser, "name", outMeta, setName);
00443
00444
00445 XTAG_SET_ATTRIBUTE(inMetaParser, "lang", outMeta, setLang);
00446 XTAG_SET_ATTRIBUTE(inMetaParser, "dir", outMeta, setDirn);
00447
00448 return true;
00449 }
00450
00451 bool CMMLParser::parseClipTag(XTag* inClipParser, C_ClipTag* outClip)
00452 {
00453 XTAG_SET_ATTRIBUTE(inClipParser, "track", outClip, setTrack);
00454 XTAG_SET_ATTRIBUTE(inClipParser, "id", outClip, setId);
00455 XTAG_SET_ATTRIBUTE(inClipParser, "start", outClip, setStart);
00456 XTAG_REQUIRED_ATTRIBUTE(inClipParser, "start", outClip);
00457 XTAG_SET_ATTRIBUTE(inClipParser, "end", outClip, setEnd);
00458
00459 XTAG_PARSE_LIST(C_MetaTag, "meta", parseMetaTag, inClipParser, outClip, metaList);
00460
00461 XTAG_PARSE_CHILD(inClipParser, "a", parseAnchorTag, C_AnchorTag, setAnchor, outClip);
00462 XTAG_PARSE_CHILD(inClipParser, "img", parseImageTag, C_ImageTag, setImage, outClip);
00463 XTAG_PARSE_CHILD(inClipParser, "desc", parseDescTag, C_DescTag, setDesc, outClip);
00464
00465
00466 XTAG_SET_ATTRIBUTE(inClipParser, "lang", outClip, setLang);
00467 XTAG_SET_ATTRIBUTE(inClipParser, "dir", outClip, setDirn);
00468
00469 return true;
00470 }
00471
00472 bool CMMLParser::parseAnchorTag(XTag* inAnchorParser, C_AnchorTag* outAnchor)
00473 {
00474 XTAG_SET_ATTRIBUTE(inAnchorParser, "id", outAnchor, setId);
00475 XTAG_SET_ATTRIBUTE(inAnchorParser, "class", outAnchor, setCls);
00476 XTAG_SET_ATTRIBUTE(inAnchorParser, "href", outAnchor, setHref);
00477 XTAG_REQUIRED_ATTRIBUTE(inAnchorParser, "href", outAnchor);
00478
00479 XTAG_SET_CDATA(inAnchorParser, outAnchor);
00480
00481
00482 XTAG_SET_ATTRIBUTE(inAnchorParser, "lang", outAnchor, setLang);
00483 XTAG_SET_ATTRIBUTE(inAnchorParser, "dir", outAnchor, setDirn);
00484
00485 return true;
00486 }
00487
00488 bool CMMLParser::parseImageTag(XTag* inImageParser, C_ImageTag* outImage)
00489 {
00490 XTAG_SET_ATTRIBUTE(inImageParser, "id", outImage, setId);
00491 XTAG_SET_ATTRIBUTE(inImageParser, "src", outImage, setSrc);
00492 XTAG_REQUIRED_ATTRIBUTE(inImageParser, "src", outImage);
00493 XTAG_SET_ATTRIBUTE(inImageParser, "alt", outImage, setAlt);
00494
00495
00496 XTAG_SET_ATTRIBUTE(inImageParser, "lang", outImage, setLang);
00497 XTAG_SET_ATTRIBUTE(inImageParser, "dir", outImage, setDirn);
00498
00499 return true;
00500 }
00501
00502 bool CMMLParser::parseDescTag(XTag* inDescParser, C_DescTag* outDesc)
00503 {
00504 XTAG_SET_ATTRIBUTE(inDescParser, "id", outDesc, setId);
00505
00506 XTAG_SET_CDATA(inDescParser, outDesc);
00507
00508
00509 XTAG_SET_ATTRIBUTE(inDescParser, "lang", outDesc, setLang);
00510 XTAG_SET_ATTRIBUTE(inDescParser, "dir", outDesc, setDirn);
00511
00512 return true;
00513 }
00514
00515 bool CMMLParser::parseImportTag(XTag* inImportParser, C_ImportTag* outImport)
00516 {
00517 XTAG_SET_ATTRIBUTE(inImportParser, "granulerate", outImport, setGranuleRate);
00518 XTAG_SET_ATTRIBUTE(inImportParser, "contenttype", outImport, setContentType);
00519 XTAG_SET_ATTRIBUTE(inImportParser, "src", outImport, setSrc);
00520 XTAG_SET_ATTRIBUTE(inImportParser, "start", outImport, setStart);
00521 XTAG_SET_ATTRIBUTE(inImportParser, "end", outImport, setEnd);
00522 XTAG_SET_ATTRIBUTE(inImportParser, "title", outImport, setTitle);
00523
00524 XTAG_PARSE_LIST(C_ParamTag, "param", parseParamTag, inImportParser, outImport, paramList);
00525
00526 return true;
00527 }
00528
00529 bool CMMLParser::parseParamTag(XTag* inParamParser, C_ParamTag* outParam)
00530 {
00531 XTAG_SET_ATTRIBUTE(inParamParser, "id", outParam, setId);
00532 XTAG_SET_ATTRIBUTE(inParamParser, "name", outParam, setName);
00533 XTAG_REQUIRED_ATTRIBUTE(inParamParser, "name", outParam);
00534 XTAG_SET_ATTRIBUTE(inParamParser, "value", outParam, setContent);
00535 XTAG_REQUIRED_ATTRIBUTE(inParamParser, "value", outParam);
00536
00537 return true;
00538 }
00539
00540 #undef XTAG_REQUIRED_ATTRIBUTE
00541
00542 #undef XTAG_SET_ATTRIBUTE
00543
00544 #undef XTAG_PARSE_INTO
00545
00546 #undef XTAG_SET_CDATA