File indexing completed on 2024-05-12 05:10:15

0001 /***************************************************************************
0002     Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #ifndef TELLICO_XSLTHANDLER_H
0026 #define TELLICO_XSLTHANDLER_H
0027 
0028 #include <QByteArray>
0029 #include <QHash>
0030 #include <QString>
0031 
0032 // for xmlDocPtr
0033 #include <libxml/tree.h>
0034 
0035 extern "C" {
0036 // for xsltStyleSheetPtr
0037 #include <libxslt/xsltInternals.h>
0038 }
0039 
0040 class QUrl;
0041 class QDomDocument;
0042 
0043 namespace Tellico {
0044 
0045 /**
0046  * The XSLTHandler contains all the code which uses XSLT processing to generate HTML or to
0047  * translate to other formats.
0048  *
0049  * @author Robby Stephenson
0050  */
0051 class XSLTHandler {
0052 
0053 public:
0054   class XMLOutputBuffer {
0055   public:
0056     XMLOutputBuffer();
0057     ~XMLOutputBuffer();
0058     bool isValid() const { return (m_buf != nullptr); }
0059     xmlOutputBuffer* buffer() const { return m_buf; }
0060     QString result() const { return m_res; }
0061   private:
0062     xmlOutputBuffer* m_buf;
0063     QString m_res;
0064   };
0065 
0066   /**
0067    * @param xsltFile The XSLT file
0068    */
0069   XSLTHandler(const QByteArray& xsltFile);
0070   /**
0071    * @param xsltURL The XSLT URL
0072    */
0073   XSLTHandler(const QUrl& xsltURL);
0074   /**
0075    * @param xsltDoc The XSLT DOM document
0076    * @param xsltFile The XSLT file, should be a url?
0077    */
0078   XSLTHandler(const QDomDocument& xsltDoc, const QByteArray& xsltFile, bool translate=false);
0079   /**
0080    */
0081   ~XSLTHandler();
0082 
0083   bool isValid() const;
0084 
0085   /**
0086    * Set the XSLT text
0087    *
0088    * @param dom The XSLT DOM document
0089    * @param xsltFile The XSLT file, should be a url?
0090    */
0091   void setXSLTDoc(const QDomDocument& dom, const QByteArray& xsltFile, bool translate=false);
0092   /**
0093    * Adds a param
0094    */
0095   void addParam(const QByteArray& name, const QByteArray& value);
0096   /**
0097    * Adds a string param
0098    */
0099   void addStringParam(const QByteArray& name, const QByteArray& value);
0100   void removeParam(const QByteArray& name);
0101   const QByteArray& param(const QByteArray& name);
0102   /**
0103    * Processes text through the XSLT transformation.
0104    *
0105    * @param text The text to be transformed
0106    * @return The transformed text
0107    */
0108   QString applyStylesheet(const QString& text);
0109 
0110   static QDomDocument& setLocaleEncoding(QDomDocument& dom);
0111 
0112 private:
0113   void init();
0114   QString process(xmlDocPtr docIn);
0115 
0116   xsltStylesheetPtr m_stylesheet;
0117 
0118   QHash<QByteArray, QByteArray> m_params;
0119 
0120   static int s_initCount;
0121 };
0122 
0123 } // end namespace
0124 #endif