ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

xmlparser.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/xmlparser.h"
00024 #include "common/archive.h"
00025 #include "common/fs.h"
00026 #include "common/memstream.h"
00027 #include "common/system.h"
00028 
00029 namespace Common {
00030 
00031 XMLParser::~XMLParser() {
00032     while (!_activeKey.empty())
00033         freeNode(_activeKey.pop());
00034 
00035     delete _XMLkeys;
00036     delete _stream;
00037 
00038     for (List<XMLKeyLayout *>::iterator i = _layoutList.begin();
00039         i != _layoutList.end(); ++i)
00040         delete *i;
00041 
00042     _layoutList.clear();
00043 }
00044 
00045 bool XMLParser::loadFile(const String &filename) {
00046     _stream = SearchMan.createReadStreamForMember(filename);
00047     if (!_stream)
00048         return false;
00049 
00050     _fileName = filename;
00051     return true;
00052 }
00053 
00054 bool XMLParser::loadFile(const FSNode &node) {
00055     _stream = node.createReadStream();
00056     if (!_stream)
00057         return false;
00058 
00059     _fileName = node.getName();
00060     return true;
00061 }
00062 
00063 bool XMLParser::loadBuffer(const byte *buffer, uint32 size, DisposeAfterUse::Flag disposable) {
00064     _stream = new MemoryReadStream(buffer, size, disposable);
00065     _fileName = "Memory Stream";
00066     return true;
00067 }
00068 
00069 bool XMLParser::loadStream(SeekableReadStream *stream) {
00070     _stream = stream;
00071     _fileName = "File Stream";
00072     return _stream != nullptr;
00073 }
00074 
00075 void XMLParser::close() {
00076     delete _stream;
00077     _stream = nullptr;
00078 }
00079 
00080 bool XMLParser::parserError(const String &errStr) {
00081     _state = kParserError;
00082 
00083     const int startPosition = _stream->pos();
00084     int currentPosition = startPosition;
00085     int lineCount = 1;
00086     char c = 0;
00087 
00088     _stream->seek(0, SEEK_SET);
00089 
00090     while (currentPosition--) {
00091         c = _stream->readByte();
00092 
00093         if (c == '\n' || c == '\r')
00094             lineCount++;
00095     }
00096 
00097     assert(_stream->pos() == startPosition);
00098     currentPosition = startPosition;
00099 
00100     Common::String errorMessage = Common::String::format("\n  File <%s>, line %d:\n", _fileName.c_str(), lineCount);
00101 
00102     if (startPosition > 1) {
00103         int keyOpening = 0;
00104         int keyClosing = 0;
00105 
00106         while (currentPosition-- && keyOpening == 0) {
00107             _stream->seek(-2, SEEK_CUR);
00108             c = _stream->readByte();
00109 
00110             if (c == '<')
00111                 keyOpening = currentPosition - 1;
00112             else if (c == '>')
00113                 keyClosing = currentPosition;
00114         }
00115 
00116         _stream->seek(startPosition, SEEK_SET);
00117         currentPosition = startPosition;
00118         while (keyClosing == 0 && c && currentPosition++) {
00119             c = _stream->readByte();
00120 
00121             if (c == '>')
00122                 keyClosing = currentPosition;
00123         }
00124 
00125         currentPosition = (keyClosing - keyOpening);
00126         _stream->seek(keyOpening, SEEK_SET);
00127 
00128         while (currentPosition--)
00129             errorMessage += (char)_stream->readByte();
00130     }
00131 
00132     errorMessage += "\n\nParser error: ";
00133     errorMessage += errStr;
00134     errorMessage += "\n\n";
00135 
00136     g_system->logMessage(LogMessageType::kError, errorMessage.c_str());
00137 
00138     return false;
00139 }
00140 
00141 bool XMLParser::parseXMLHeader(ParserNode *node) {
00142     assert(node->header);
00143 
00144     if (_activeKey.size() != 1)
00145         return parserError("XML Header is expected in the global scope.");
00146 
00147     if (!node->values.contains("version"))
00148         return parserError("Missing XML version in XML header.");
00149 
00150     if (node->values["version"] != "1.0")
00151         return parserError("Unsupported XML version.");
00152 
00153     return true;
00154 }
00155 
00156 bool XMLParser::parseActiveKey(bool closed) {
00157     bool ignore = false;
00158     assert(_activeKey.empty() == false);
00159 
00160     ParserNode *key = _activeKey.top();
00161 
00162     if (key->name == "xml" && key->header == true) {
00163         assert(closed);
00164         return parseXMLHeader(key) && closeKey();
00165     }
00166 
00167     XMLKeyLayout *layout = (_activeKey.size() == 1) ? _XMLkeys : getParentNode(key)->layout;
00168 
00169     if (layout->children.contains(key->name)) {
00170         key->layout = layout->children[key->name];
00171 
00172         StringMap localMap = key->values;
00173         int keyCount = localMap.size();
00174 
00175         for (List<XMLKeyLayout::XMLKeyProperty>::const_iterator i = key->layout->properties.begin(); i != key->layout->properties.end(); ++i) {
00176             if (i->required && !localMap.contains(i->name))
00177                 return parserError("Missing required property '" + i->name + "' inside key '" + key->name + "'");
00178             else if (localMap.contains(i->name))
00179                 keyCount--;
00180         }
00181 
00182         if (keyCount > 0)
00183             return parserError("Unhandled property inside key '" + key->name + "'.");
00184 
00185     } else {
00186         return parserError("Unexpected key in the active scope ('" + key->name + "').");
00187     }
00188 
00189     // check if any of the parents must be ignored.
00190     // if a parent is ignored, all children are too.
00191     for (int i = _activeKey.size() - 1; i >= 0; --i) {
00192         if (_activeKey[i]->ignore)
00193             ignore = true;
00194     }
00195 
00196     if (ignore == false && keyCallback(key) == false) {
00197         // HACK:  People may be stupid and overlook the fact that
00198         // when keyCallback() fails, a parserError() must be set.
00199         // We set it manually in that case.
00200         if (_state != kParserError)
00201             parserError("Unhandled exception when parsing '" + key->name + "' key.");
00202 
00203         return false;
00204     }
00205 
00206     if (closed)
00207         return closeKey();
00208 
00209     return true;
00210 }
00211 
00212 bool XMLParser::parseKeyValue(String keyName) {
00213     assert(_activeKey.empty() == false);
00214 
00215     if (_activeKey.top()->values.contains(keyName))
00216         return false;
00217 
00218     _token.clear();
00219     char stringStart;
00220 
00221     if (_char == '"' || _char == '\'') {
00222         stringStart = _char;
00223         _char = _stream->readByte();
00224 
00225         while (_char && _char != stringStart) {
00226             _token += _char;
00227             _char = _stream->readByte();
00228         }
00229 
00230         if (_char == 0)
00231             return false;
00232 
00233         _char = _stream->readByte();
00234 
00235     } else if (!parseToken()) {
00236         return false;
00237     }
00238 
00239     _activeKey.top()->values[keyName] = _token;
00240     return true;
00241 }
00242 
00243 bool XMLParser::parseIntegerKey(const char *key, int count, ...) {
00244     bool result;
00245     va_list args;
00246     va_start(args, count);
00247     result = vparseIntegerKey(key, count, args);
00248     va_end(args);
00249     return result;
00250 }
00251 
00252 bool XMLParser::parseIntegerKey(const String &key, int count, ...) {
00253     bool result;
00254     va_list args;
00255     va_start(args, count);
00256     result = vparseIntegerKey(key.c_str(), count, args);
00257     va_end(args);
00258     return result;
00259 }
00260 
00261 bool XMLParser::vparseIntegerKey(const char *key, int count, va_list args) {
00262     char *parseEnd;
00263     int *num_ptr;
00264 
00265     while (count--) {
00266         while (isSpace(*key))
00267             key++;
00268 
00269         num_ptr = va_arg(args, int*);
00270         *num_ptr = strtol(key, &parseEnd, 10);
00271 
00272         key = parseEnd;
00273 
00274         while (isSpace(*key))
00275             key++;
00276 
00277         if (count && *key++ != ',')
00278             return false;
00279     }
00280 
00281     return (*key == 0);
00282 }
00283 
00284 bool XMLParser::closeKey() {
00285     bool ignore = false;
00286     bool result = true;
00287 
00288     for (int i = _activeKey.size() - 1; i >= 0; --i) {
00289         if (_activeKey[i]->ignore)
00290             ignore = true;
00291     }
00292 
00293     if (ignore == false)
00294         result = closedKeyCallback(_activeKey.top());
00295 
00296     freeNode(_activeKey.pop());
00297 
00298     return result;
00299 }
00300 
00301 bool XMLParser::parse() {
00302     if (_stream == nullptr)
00303         return false;
00304 
00305     // Make sure we are at the start of the stream.
00306     _stream->seek(0, SEEK_SET);
00307 
00308     if (_XMLkeys == nullptr)
00309         buildLayout();
00310 
00311     while (!_activeKey.empty())
00312         freeNode(_activeKey.pop());
00313 
00314     cleanup();
00315 
00316     bool activeClosure = false;
00317     bool activeHeader = false;
00318     bool selfClosure;
00319 
00320     _state = kParserNeedHeader;
00321     _activeKey.clear();
00322 
00323     _char = _stream->readByte();
00324 
00325     while (_char && _state != kParserError) {
00326         if (skipSpaces())
00327             continue;
00328 
00329         if (skipComments())
00330             continue;
00331 
00332         switch (_state) {
00333         case kParserNeedHeader:
00334         case kParserNeedKey:
00335             if (_char != '<') {
00336                 parserError("Parser expecting key start.");
00337                 break;
00338             }
00339 
00340             if ((_char = _stream->readByte()) == 0) {
00341                 parserError("Unexpected end of file.");
00342                 break;
00343             }
00344 
00345             if (_state == kParserNeedHeader) {
00346                 if (_char != '?') {
00347                     parserError("Expecting XML header.");
00348                     break;
00349                 }
00350 
00351                 _char = _stream->readByte();
00352                 activeHeader = true;
00353             } else if (_char == '/') {
00354                 _char = _stream->readByte();
00355                 activeClosure = true;
00356             } else if (_char == '?') {
00357                 parserError("Unexpected header. There may only be one XML header per file.");
00358                 break;
00359             }
00360 
00361             _state = kParserNeedKeyName;
00362             break;
00363 
00364         case kParserNeedKeyName:
00365             if (!parseToken()) {
00366                 parserError("Invalid key name.");
00367                 break;
00368             }
00369 
00370             if (activeClosure) {
00371                 if (_activeKey.empty() || _token != _activeKey.top()->name) {
00372                     parserError("Unexpected closure.");
00373                     break;
00374                 }
00375             } else {
00376                 ParserNode *node = allocNode(); // new ParserNode;
00377                 node->name = _token;
00378                 node->ignore = false;
00379                 node->header = activeHeader;
00380                 node->depth = _activeKey.size();
00381                 node->layout = nullptr;
00382                 _activeKey.push(node);
00383             }
00384 
00385             _state = kParserNeedPropertyName;
00386             break;
00387 
00388         case kParserNeedPropertyName:
00389             if (activeClosure) {
00390                 if (!closeKey()) {
00391                     parserError("Missing data when closing key '" + _activeKey.top()->name + "'.");
00392                     break;
00393                 }
00394 
00395                 activeClosure = false;
00396 
00397                 if (_char != '>')
00398                     parserError("Invalid syntax in key closure.");
00399                 else
00400                     _state = kParserNeedKey;
00401 
00402                 _char = _stream->readByte();
00403                 break;
00404             }
00405 
00406             selfClosure = false;
00407 
00408             if (_char == '/' || (_char == '?' && activeHeader)) {
00409                 selfClosure = true;
00410                 _char = _stream->readByte();
00411             }
00412 
00413             if (_char == '>') {
00414                 if (activeHeader && !selfClosure) {
00415                     parserError("XML Header must be self-closed.");
00416                 } else if (parseActiveKey(selfClosure)) {
00417                     _char = _stream->readByte();
00418                     _state = kParserNeedKey;
00419                 }
00420 
00421                 activeHeader = false;
00422                 break;
00423             }
00424 
00425             if (selfClosure)
00426                 parserError("Expecting key closure after '/' symbol.");
00427             else if (!parseToken())
00428                 parserError("Error when parsing key value.");
00429             else
00430                 _state = kParserNeedPropertyOperator;
00431 
00432             break;
00433 
00434         case kParserNeedPropertyOperator:
00435             if (_char != '=')
00436                 parserError("Syntax error after key name.");
00437             else
00438                 _state = kParserNeedPropertyValue;
00439 
00440             _char = _stream->readByte();
00441             break;
00442 
00443         case kParserNeedPropertyValue:
00444             if (!parseKeyValue(_token))
00445                 parserError("Invalid key value.");
00446             else
00447                 _state = kParserNeedPropertyName;
00448 
00449             break;
00450 
00451         default:
00452             break;
00453         }
00454     }
00455 
00456     if (_state == kParserError)
00457         return false;
00458 
00459     if (_state != kParserNeedKey || !_activeKey.empty())
00460         return parserError("Unexpected end of file.");
00461 
00462     return true;
00463 }
00464 
00465 bool XMLParser::skipSpaces() {
00466     if (!isSpace(_char))
00467         return false;
00468 
00469     while (_char && isSpace(_char))
00470         _char = _stream->readByte();
00471 
00472     return true;
00473 }
00474 
00475 bool XMLParser::skipComments() {
00476     if (_char == '<') {
00477         _char = _stream->readByte();
00478 
00479         if (_char != '!') {
00480             _stream->seek(-1, SEEK_CUR);
00481             _char = '<';
00482             return false;
00483         }
00484 
00485         if (_stream->readByte() != '-' || _stream->readByte() != '-')
00486             return parserError("Malformed comment syntax.");
00487 
00488         _char = _stream->readByte();
00489 
00490         while (_char) {
00491             if (_char == '-') {
00492                 if (_stream->readByte() == '-') {
00493 
00494                     if (_stream->readByte() != '>')
00495                         return parserError("Malformed comment (double-hyphen inside comment body).");
00496 
00497                     _char = _stream->readByte();
00498                     return true;
00499                 }
00500             }
00501 
00502             _char = _stream->readByte();
00503         }
00504 
00505         return parserError("Comment has no closure.");
00506     }
00507 
00508     return false;
00509 }
00510 
00511 bool XMLParser::parseToken() {
00512     _token.clear();
00513 
00514     while (isValidNameChar(_char)) {
00515         _token += _char;
00516         _char = _stream->readByte();
00517     }
00518 
00519     return isSpace(_char) != 0 || _char == '>' || _char == '=' || _char == '/';
00520 }
00521 
00522 } // End of namespace Common


Generated on Sat Apr 20 2019 05:04:20 for ResidualVM by doxygen 1.7.1
curved edge   curved edge