From 998f21e02a725cd553d7c278819f67cd81295af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sl=C3=A1vek=20Banko?= Date: Mon, 24 Jun 2013 02:08:15 +0200 Subject: Initial import --- src/fileexporterxml.cpp | 228 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 src/fileexporterxml.cpp (limited to 'src/fileexporterxml.cpp') diff --git a/src/fileexporterxml.cpp b/src/fileexporterxml.cpp new file mode 100644 index 0000000..4dcbee4 --- /dev/null +++ b/src/fileexporterxml.cpp @@ -0,0 +1,228 @@ +/*************************************************************************** +* Copyright (C) 2004-2009 by Thomas Fischer * +* fischer@unix-ag.uni-kl.de * +* * +* 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. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ +#include +#include + +#include +#include +#include +#include +#include +#include +#include "fileexporterxml.h" + +namespace BibTeX +{ + + FileExporterXML::FileExporterXML() : FileExporter() + { + // nothing + } + + + FileExporterXML::~FileExporterXML() + { + // nothing + } + + bool FileExporterXML::save( QIODevice* iodevice, const File* bibtexfile, QStringList * /*errorLog*/ ) + { + m_mutex.lock(); + bool result = TRUE; + m_cancelFlag = FALSE; + QTextStream stream( iodevice ); + stream.setEncoding( QTextStream::UnicodeUTF8 ); + + stream << "" << endl; + stream << "" << endl; + + int numElements = (int) bibtexfile->count(), i = 0; + emit progress( 0, numElements ); + for ( File::ElementList::const_iterator it = bibtexfile->elements.begin(); it != bibtexfile->elements.end() && result && !m_cancelFlag; it++ ) + { + Element *element = *it; + write( stream, element, bibtexfile ); + emit progress( ++i, numElements ); + } + + stream << "" << endl; + + m_mutex.unlock(); + return result && !m_cancelFlag; + } + + bool FileExporterXML::save( QIODevice* iodevice, const Element* element, QStringList * /*errorLog*/ ) + { + QTextStream stream( iodevice ); + stream.setEncoding( QTextStream::UnicodeUTF8 ); + + stream << "" << endl; + return write( stream, element ); + } + + void FileExporterXML::cancel() + { + m_cancelFlag = TRUE; + } + + bool FileExporterXML::write( QTextStream&stream, const Element *element, const File* bibtexfile ) + { + bool result = FALSE; + + const Entry *entry = dynamic_cast( element ); + if ( entry != NULL ) + { + if ( bibtexfile != NULL ) + { + entry = bibtexfile->completeReferencedFieldsConst( entry ); + } + result |= writeEntry( stream, entry ); + if ( bibtexfile != NULL ) + delete entry; + } + else + { + const Macro * macro = dynamic_cast( element ); + if ( macro != NULL ) + result |= writeMacro( stream, macro ); + else + { + const Comment * comment = dynamic_cast( element ); + if ( comment != NULL ) + result |= writeComment( stream, comment ); + else + { + // preambles are ignored, make no sense in XML files + } + } + } + + return result; + } + + bool FileExporterXML::writeEntry( QTextStream &stream, const Entry* entry ) + { + stream << " encode( entry->id() ) << "\" type=\"" << entry->entryTypeString().lower() << "\">" << endl; + for ( Entry::EntryFields::const_iterator it = entry->begin(); it != entry->end(); it++ ) + { + EntryField *field = *it; + switch ( field->fieldType() ) + { + case EntryField::ftAuthor: + case EntryField::ftEditor: + { + QString tag = field->fieldTypeName().lower(); + stream << " <" << tag << "s>" << endl; + QStringList persons = QStringList::split( QRegExp( "\\s+(,|and|&)+\\s+", FALSE ), EncoderXML::currentEncoderXML() ->encode( valueToString( field->value() ) ) ); + for ( QStringList::Iterator it = persons.begin(); it != persons.end(); it++ ) + stream << " " << *it << "" << endl; + stream << " " << endl; + } + break; + case EntryField::ftMonth: + { + stream << " ::ConstIterator it = field->value()->items.begin(); it != field->value()->items.end(); ++it ) + { + if ( dynamic_cast( *it ) != NULL ) + { + for ( int i = 0; i < 12; i++ ) + if ( QString::compare(( *it )->text(), MonthsTriple[ i ] ) == 0 ) + { + if ( month < 1 ) + { + tag = MonthsTriple[ i ]; + month = i + 1; + } + content.append( Months[ i ] ); + ok = TRUE; + break; + } + } + else + content.append( EncoderXML::currentEncoderXML() ->encode(( *it )->text() ) ); + } + + if ( !ok ) + content = EncoderXML::currentEncoderXML() ->encode( field->value()->simplifiedText() ) ; + if ( !tag.isEmpty() ) + stream << " tag=\"" << tag << "\""; + if ( month > 0 ) + stream << " month=\"" << month << "\""; + stream << '>' << content; + stream << "" << endl; + } + break; + default: + { + QString tag = field->fieldTypeName().lower(); + stream << " <" << tag << ">" << EncoderXML::currentEncoderXML() ->encode( valueToString( field->value() ) ) << "" << endl; + } + break; + } + + } + stream << " " << endl; + + return TRUE; + } + + bool FileExporterXML::writeMacro( QTextStream &stream, const Macro *macro ) + { + stream << " key() << "\">"; + stream << EncoderXML::currentEncoderXML() ->encode( valueToString( macro->value() ) ); + stream << "" << endl; + + return TRUE; + } + + bool FileExporterXML::writeComment( QTextStream &stream, const Comment *comment ) + { + stream << " " ; + stream << EncoderXML::currentEncoderXML() ->encode( comment->text() ); + stream << "" << endl; + + return TRUE; + } + + QString FileExporterXML::valueToString( Value *value ) + { + QString result; + bool isFirst = TRUE; + + for ( QValueList::ConstIterator it = value->items.begin(); it != value->items.end(); it++ ) + { + if ( !isFirst ) + result.append( ' ' ); + isFirst = FALSE; + + result.append(( *it ) ->simplifiedText() ); + } + + return result; + } + + +} -- cgit v1.2.3