Reader.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025
00026 #ifndef _PreComp_
00027 # include <xercesc/sax/SAXParseException.hpp>
00028 # include <xercesc/sax/SAXException.hpp>
00029 # include <xercesc/sax2/XMLReaderFactory.hpp>
00030 # include <xercesc/sax2/SAX2XMLReader.hpp>
00031 #endif
00032
00033 #include <locale>
00034
00036 #include "Reader.h"
00037 #include "Exception.h"
00038 #include "Persistence.h"
00039 #include "InputSource.h"
00040 #include "Console.h"
00041 #include "Sequencer.h"
00042
00043 #include <zipios++/zipios-config.h>
00044 #include <zipios++/zipfile.h>
00045 #include <zipios++/zipinputstream.h>
00046 #include <zipios++/zipoutputstream.h>
00047 #include <zipios++/meta-iostreams.h>
00048
00049 #include "XMLTools.h"
00050
00051 XERCES_CPP_NAMESPACE_USE
00052
00053 using namespace std;
00054
00055
00056
00057
00058
00059
00060
00061 Base::XMLReader::XMLReader(const char* FileName, std::istream& str)
00062 : DocumentSchema(0), Level(0), _File(FileName)
00063 {
00064 #ifdef _MSC_VER
00065 str.imbue(std::locale::empty());
00066 #else
00067
00068 str.imbue(std::locale::classic());
00069 #endif
00070
00071
00072 parser = XMLReaderFactory::createXMLReader();
00073
00074
00075
00076
00077
00078
00079
00080
00081 parser->setContentHandler(this);
00082 parser->setErrorHandler(this);
00083
00084 try {
00085 StdInputSource file(str, _File.filePath().c_str());
00086 _valid = parser->parseFirst( file,token);
00087 }
00088 catch (const XMLException& toCatch) {
00089 char* message = XMLString::transcode(toCatch.getMessage());
00090 cerr << "Exception message is: \n"
00091 << message << "\n";
00092 XMLString::release(&message);
00093 }
00094 catch (const SAXParseException& toCatch) {
00095 char* message = XMLString::transcode(toCatch.getMessage());
00096 cerr << "Exception message is: \n"
00097 << message << "\n";
00098 XMLString::release(&message);
00099 }
00100 #ifndef FC_DEBUG
00101 catch (...) {
00102 cerr << "Unexpected Exception \n";
00103 }
00104 #endif
00105 }
00106
00107 Base::XMLReader::~XMLReader()
00108 {
00109
00110 delete parser;
00111 }
00112
00113 const char* Base::XMLReader::localName(void) const
00114 {
00115 return LocalName.c_str();
00116 }
00117
00118 unsigned int Base::XMLReader::getAttributeCount(void) const
00119 {
00120 return (unsigned int)AttrMap.size();
00121 }
00122
00123 long Base::XMLReader::getAttributeAsInteger(const char* AttrName) const
00124 {
00125 AttrMapType::const_iterator pos = AttrMap.find(AttrName);
00126
00127 if (pos != AttrMap.end())
00128 return atol(pos->second.c_str());
00129 else
00130
00131 assert(0);
00132
00133 return 0;
00134 }
00135
00136 unsigned long Base::XMLReader::getAttributeAsUnsigned(const char* AttrName) const
00137 {
00138 AttrMapType::const_iterator pos = AttrMap.find(AttrName);
00139
00140 if (pos != AttrMap.end())
00141 return strtoul(pos->second.c_str(),0,10);
00142 else
00143
00144 assert(0);
00145
00146 return 0;
00147 }
00148
00149 double Base::XMLReader::getAttributeAsFloat (const char* AttrName) const
00150 {
00151 AttrMapType::const_iterator pos = AttrMap.find(AttrName);
00152
00153 if (pos != AttrMap.end())
00154 return atof(pos->second.c_str());
00155 else
00156
00157 assert(0);
00158
00159 return 0.0;
00160 }
00161
00162 const char* Base::XMLReader::getAttribute (const char* AttrName) const
00163 {
00164 AttrMapType::const_iterator pos = AttrMap.find(AttrName);
00165
00166 if (pos != AttrMap.end())
00167 return pos->second.c_str();
00168 else
00169
00170 assert(0);
00171
00172 return "";
00173 }
00174
00175 bool Base::XMLReader::hasAttribute (const char* AttrName) const
00176 {
00177 return AttrMap.find(AttrName) != AttrMap.end();
00178 }
00179
00180 bool Base::XMLReader::read(void)
00181 {
00182 ReadType = None;
00183
00184 try {
00185 parser->parseNext(token);
00186 }
00187 catch (const XMLException& toCatch) {
00188 #if 0
00189 char* message = XMLString::transcode(toCatch.getMessage());
00190 cerr << "Exception message is: \n"
00191 << message << "\n";
00192 XMLString::release(&message);
00193 return false;
00194 #else
00195 char* message = XMLString::transcode(toCatch.getMessage());
00196 std::string what = message;
00197 XMLString::release(&message);
00198 throw Base::Exception(what);
00199 #endif
00200 }
00201 catch (const SAXParseException& toCatch) {
00202 #if 0
00203 char* message = XMLString::transcode(toCatch.getMessage());
00204 cerr << "Exception message is: \n"
00205 << message << "\n";
00206 XMLString::release(&message);
00207 return false;
00208 #else
00209 char* message = XMLString::transcode(toCatch.getMessage());
00210 std::string what = message;
00211 XMLString::release(&message);
00212 throw Base::XMLParseException(what);
00213 #endif
00214 }
00215 catch (...) {
00216 #if 0
00217 cerr << "Unexpected Exception \n" ;
00218 return false;
00219 #else
00220 throw Base::Exception("Unexpected XML exception");
00221 #endif
00222 }
00223
00224 return true;
00225 }
00226
00227 void Base::XMLReader::readElement(const char* ElementName)
00228 {
00229 bool ok;
00230 int currentLevel = Level;
00231 std::string currentName = LocalName;
00232 do {
00233 ok = read(); if (!ok) break;
00234 if (ReadType == EndElement && currentName == LocalName && currentLevel >= Level) {
00235
00236
00237 break;
00238 }
00239 } while ((ReadType != StartElement && ReadType != StartEndElement) ||
00240 (ElementName && LocalName != ElementName));
00241 }
00242
00243 void Base::XMLReader::readEndElement(const char* ElementName)
00244 {
00245
00246 if (ReadType == EndElement && LocalName == ElementName)
00247 return;
00248 bool ok;
00249 do {
00250 ok = read(); if (!ok) break;
00251 } while (ReadType != EndElement || (ElementName && LocalName != ElementName));
00252 }
00253
00254 void Base::XMLReader::readCharacters(void)
00255 {
00256 }
00257
00258 void Base::XMLReader::readFiles(zipios::ZipInputStream &zipstream) const
00259 {
00260
00261
00262
00263
00264
00265
00266
00267
00268 zipios::ConstEntryPointer entry;
00269 try {
00270 entry = zipstream.getNextEntry();
00271 }
00272 catch (const std::exception&) {
00273
00274
00275 return;
00276 }
00277 std::vector<FileEntry>::const_iterator it = FileList.begin();
00278 Base::SequencerLauncher seq("Importing project files...", FileList.size());
00279 while (entry->isValid() && it != FileList.end()) {
00280 std::vector<FileEntry>::const_iterator jt = it;
00281
00282
00283 while (jt != FileList.end() && entry->getName() != jt->FileName)
00284 ++jt;
00285
00286
00287 if (jt != FileList.end()) {
00288 try {
00289 jt->Object->RestoreDocFile(zipstream);
00290 }
00291 catch(...) {
00292
00293
00294
00295
00296
00297 Base::Console().Error("Reading failed from embedded file: %s\n", entry->toString().c_str());
00298 }
00299
00300 it = jt + 1;
00301 }
00302
00303 seq.next();
00304
00305
00306 try {
00307 entry = zipstream.getNextEntry();
00308 }
00309 catch (const std::exception&) {
00310
00311 break;
00312 }
00313 }
00314 }
00315
00316 const char *Base::XMLReader::addFile(const char* Name, Base::Persistence *Object)
00317 {
00318 FileEntry temp;
00319 temp.FileName = Name;
00320 temp.Object = Object;
00321
00322 FileList.push_back(temp);
00323 FileNames.push_back( temp.FileName );
00324
00325 return Name;
00326 }
00327
00328 const std::vector<std::string>& Base::XMLReader::getFilenames() const
00329 {
00330 return FileNames;
00331 }
00332
00333 bool Base::XMLReader::isRegistered(Base::Persistence *Object) const
00334 {
00335 if (Object) {
00336 for (std::vector<FileEntry>::const_iterator it = FileList.begin(); it != FileList.end(); ++it) {
00337 if (it->Object == Object)
00338 return true;
00339 }
00340 }
00341
00342 return false;
00343 }
00344
00345
00346
00347
00348 void Base::XMLReader::startElement(const XMLCh* const , const XMLCh* const localname, const XMLCh* const , const XERCES_CPP_NAMESPACE_QUALIFIER Attributes& attrs)
00349 {
00350 Level++;
00351 LocalName = StrX(localname).c_str();
00352
00353
00354 AttrMap.clear();
00355 for (unsigned int i = 0; i < attrs.getLength(); i++) {
00356 AttrMap[StrX(attrs.getQName(i)).c_str()] = StrXUTF8(attrs.getValue(i)).c_str();
00357 }
00358
00359 ReadType = StartElement;
00360 }
00361
00362 void Base::XMLReader::endElement (const XMLCh* const , const XMLCh *const localname, const XMLCh *const )
00363 {
00364 Level--;
00365 LocalName = StrX(localname).c_str();
00366
00367 if (ReadType == StartElement)
00368 ReadType = StartEndElement;
00369 else
00370 ReadType = EndElement;
00371 }
00372
00373
00374 void Base::XMLReader::characters(const XMLCh* const chars, const unsigned int length)
00375 {
00376 Characters = StrX(chars).c_str();
00377 ReadType = Chars;
00378 CharacterCount += length;
00379 }
00380
00381 void Base::XMLReader::ignorableWhitespace( const XMLCh* const , const unsigned int )
00382 {
00383
00384 }
00385
00386 void Base::XMLReader::resetDocument()
00387 {
00388
00389
00390
00391
00392 }
00393
00394
00395
00396
00397
00398 void Base::XMLReader::error(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& e)
00399 {
00400
00401
00402 cerr << "Error at file " << StrX(e.getSystemId())
00403 << ", line " << e.getLineNumber()
00404 << ", char " << e.getColumnNumber() << endl;
00405 throw e;
00406 }
00407
00408 void Base::XMLReader::fatalError(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& e)
00409 {
00410
00411
00412 cerr << "Fatal Error at file " << StrX(e.getSystemId())
00413 << ", line " << e.getLineNumber()
00414 << ", char " << e.getColumnNumber() << endl;
00415 throw e;
00416 }
00417
00418 void Base::XMLReader::warning(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& e)
00419 {
00420
00421
00422 cerr << "Warning at file " << StrX(e.getSystemId())
00423 << ", line " << e.getLineNumber()
00424 << ", char " << e.getColumnNumber() << endl;
00425 throw e;
00426 }
00427
00428 void Base::XMLReader::resetErrors()
00429 {
00430 }