/*************************************************************************** xmlpersister.cpp - description ------------------- begin : Mit Aug 14 2002 copyright : (C) 2002 by Alexander Theel email : alex.theel@gmx.net ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "xmlpersister.h" #include "../global.h" #include #include #include "../utilities/base64/CBase64Coder.h" QString XMLPersister::pathOfLastActiveElement(""); bool XMLPersister::bCurrentElementIsBase64Encrypted = false; /************************ loading **********************************/ // ------------------------------------------------------------------------------- CInformationCollection* XMLPersister::createInformationCollection( QFile& xmlFile ) // ------------------------------------------------------------------------------- { QDomDocument xmlDocument( "tuxcards_data_file" ); bool bResult = createDomDocumentFromFile( xmlFile, xmlDocument ); if ( bResult ) { return createInformationCollection( xmlDocument ); } return NULLPTR; } // ------------------------------------------------------------------------------- CInformationCollection* XMLPersister::createInformationCollection( QString xmlString ) // ------------------------------------------------------------------------------- { QDomDocument* xmlDocument = createDomDocumentFromString(xmlString); if ( NULLPTR != xmlDocument ) { return createInformationCollection( *xmlDocument ); } return NULLPTR; } // ------------------------------------------------------------------------------- CInformationCollection* XMLPersister::createInformationCollection( QDomDocument& doc ) // ------------------------------------------------------------------------------- { // create the collection CInformationCollection* collection = new CInformationCollection(); CTreeInformationElement* e = new CTreeInformationElement(); collection->registerAsListenerOf( e ); if ( !doc.documentElement().isNull() ) { QDomElement* pDomElem = parseCollectionDataAndGetRootInformationElement( doc.documentElement() ); if ( NULLPTR != pDomElem ) { parseAttributes(*pDomElem, *e); parseChildrenOf(*pDomElem, *e); } } collection->setRootElement( e ); return collection; } /* if ( !doc->documentElement().isNull() ){ parseAttributes(doc->documentElement(), e); parseChildrenOf(doc->documentElement(), e); } collection->setRootElement(e); */ // ------------------------------------------------------------------------------- QDomElement* XMLPersister::parseCollectionDataAndGetRootInformationElement( QDomNode node ) // ------------------------------------------------------------------------------- { QDomElement* retVal = NULLPTR; if ( node.isElement() ) { QDomElement elem = node.toElement(); //cout<<"element; tagname= "< lastActiveElement = "<getInformation()) ) // informationElement->setInformationFormat(&InformationFormat::RTF); // else // informationElement->setInformationFormat(&InformationFormat::ASCII); } } /** * parses the attributes for a CTreeInformationElement */ // ------------------------------------------------------------------------------- void XMLPersister::parseAttributes( QDomElement& elem, CTreeInformationElement& informationElem ) // ------------------------------------------------------------------------------- { QDomNamedNodeMap attributes = elem.attributes(); QString s = ( attributes.namedItem("informationFormat").isNull() ? QString("ASCII") : attributes.namedItem("informationFormat").toAttr().value() ); informationElem.setInformationFormat(InformationFormat::getByString(s)); //cout<<"XMLP::parseAttr; format= "<setContent( &xmlFile ) ) { // xmlFile.close(); // return 0; // } // xmlFile.close(); // // return doc; } // ------------------------------------------------------------------------------- QDomDocument* XMLPersister::createDomDocumentFromString( QString xmlString ) // ------------------------------------------------------------------------------- { QDomDocument* doc = new QDomDocument( "tuxcards_data_file" ); if ( !doc->setContent(xmlString) ) return NULLPTR; return doc; } // ------------------------------------------------------------------------------- QString XMLPersister::getPathOfLastActiveElement( void ) // ------------------------------------------------------------------------------- { return pathOfLastActiveElement; } /************************ saving ***********************************/ // ------------------------------------------------------------------------------- void XMLPersister::save( CInformationCollection& collection, QString fileName ) // ------------------------------------------------------------------------------- { // TODO: save something like a version string with it (like: "TuxCardsV0.5") QString s = collection.toXML(); QFile f(fileName); if ( f.open(IO_WriteOnly) ) { QTextStream t( &f ); t.setEncoding(QTextStream::UnicodeUTF8); t<