File indexing completed on 2025-01-05 03:52:04

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-04-04
0007  * Description : a tool to generate HTML image galleries
0008  *
0009  * SPDX-FileCopyrightText: 2006-2010 by Aurelien Gateau <aurelien dot gateau at free dot fr>
0010  * SPDX-FileCopyrightText: 2012-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "galleryxmlutils.h"
0017 
0018 // Local includes
0019 
0020 #include "digikam_debug.h"
0021 
0022 namespace DigikamGenericHtmlGalleryPlugin
0023 {
0024 
0025 bool XMLWriter::open(const QString& name)
0026 {
0027     xmlTextWriterPtr ptr = xmlNewTextWriterFilename(name.toUtf8().constData(), 0);
0028 
0029     if (!ptr)
0030     {
0031         return false;
0032     }
0033 
0034     m_writer.assign(ptr);
0035 
0036     int rc = xmlTextWriterStartDocument(ptr, nullptr, "UTF-8", nullptr);
0037 
0038     if (rc < 0)
0039     {
0040         m_writer.assign(nullptr);
0041 
0042         return false;
0043     }
0044 
0045     xmlTextWriterSetIndent(ptr, 1);
0046 
0047     return true;
0048 }
0049 
0050 XMLWriter::operator xmlTextWriterPtr() const
0051 {
0052     return m_writer;
0053 }
0054 
0055 void XMLWriter::writeElement(const char* element, const QString& value)
0056 {
0057     int rc = xmlTextWriterWriteElement(m_writer,
0058                                        BAD_CAST element,
0059                                        BAD_CAST value.toUtf8().data());
0060 
0061     if (rc < 0)
0062     {
0063         qCWarning(DIGIKAM_DPLUGIN_GENERIC_LOG) << "Error while calling Libxml2::xmlTextWriterWriteElement()";
0064     }
0065 }
0066 
0067 void XMLWriter::writeElement(const char* element, int value)
0068 {
0069     writeElement(element, QString::number(value));
0070 }
0071 
0072 // ------------------------------------------------------
0073 
0074 void XMLAttributeList::write(XMLWriter& writer) const
0075 {
0076     Map::const_iterator it  = m_map.begin();
0077     Map::const_iterator end = m_map.end();
0078 
0079     for ( ; it != end ; ++it)
0080     {
0081         int rc = xmlTextWriterWriteAttribute(writer,
0082                                              BAD_CAST it.key().toLatin1().data(),
0083                                              BAD_CAST it.value().toLatin1().data());
0084         if (rc < 0)
0085         {
0086             qCWarning(DIGIKAM_DPLUGIN_GENERIC_LOG) << "Error while calling Libxml2::xmlTextWriterWriteAttribute()";
0087         }
0088     }
0089 }
0090 
0091 void XMLAttributeList::append(const QString& key, const QString& value)
0092 {
0093     m_map[key] = value;
0094 }
0095 
0096 void XMLAttributeList::append(const QString& key, int value)
0097 {
0098     m_map[key] = QString::number(value);
0099 }
0100 
0101 // ------------------------------------------------------
0102 
0103 XMLElement::XMLElement(XMLWriter& writer,
0104                        const QString& element,
0105                        const XMLAttributeList* attributeList)
0106     : m_writer(writer)
0107 {
0108     int rc = xmlTextWriterStartElement(writer,
0109                                        BAD_CAST element.toLatin1().data());
0110 
0111     if (rc < 0)
0112     {
0113         qCWarning(DIGIKAM_DPLUGIN_GENERIC_LOG) << "Error while calling Libxml2::xmlTextWriterStartElement()";
0114     }
0115 
0116     if (attributeList)
0117     {
0118         attributeList->write(writer);
0119     }
0120 }
0121 
0122 XMLElement::~XMLElement()
0123 {
0124     xmlTextWriterEndElement(m_writer);
0125 }
0126 
0127 } // namespace DigikamGenericHtmlGalleryPlugin