/**
 * Copyright 2009  Michael Leupold <lemma@confuego.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3, or any
 * later version accepted by the membership of KDE e.V. (or its
 * successor approved by the membership of KDE e.V.), which shall
 * act as a proxy defined in Section 6 of version 3 of the license.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "parserprivate.h"

ParserPrivate::ParserPrivate() : m_allowLocal(false)
{
}

ParserPrivate::~ParserPrivate()
{
}

QList<void*> ParserPrivate::s_knownPrivates;

void ParserPrivate::docCreatePrivate(xmlDocPtr doc)
{
    ParserPrivate *priv = new ParserPrivate;
    ParserPrivate::s_knownPrivates.append((void*)priv);
    doc->_private = (void*)priv;
}

void ParserPrivate::docFreePrivate(xmlDocPtr doc)
{
    ParserPrivate::s_knownPrivates.removeAll(doc->_private);
    ParserPrivate *priv = (ParserPrivate*)(doc->_private);
    delete priv;
}

QList<TransformationError> ParserPrivate::docGetErrors(xmlDocPtr doc)
{
    if (doc->_private && ParserPrivate::s_knownPrivates.contains(doc->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(doc->_private);
        return priv->m_errors;
    }
    return QList<TransformationError>();
}

void ParserPrivate::docAddErrors(xmlDocPtr doc, const QList<TransformationError> &errors)
{
    if (doc->_private && ParserPrivate::s_knownPrivates.contains(doc->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(doc->_private);
        priv->m_errors.append(errors);
    }
}

void ParserPrivate::docAddSecurityError(xmlDocPtr doc, const KUrl &url)
{
    if (doc->_private && ParserPrivate::s_knownPrivates.contains(doc->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(doc->_private);
        TransformationError error;
        error.m_errorType = TransformationError::ErrorSecurity;
        error.m_file = url;
        priv->m_errors.append(error);
    }
}

xsltTransformContextPtr ParserPrivate::createTransformContext(xsltStylesheetPtr style,
                                                              xmlDocPtr doc)
{
   xsltTransformContextPtr ctxt = xsltNewTransformContext(style, doc);
   if (ctxt == 0) {
       return 0;
   }
   ParserPrivate *priv = new ParserPrivate;
   ParserPrivate::s_knownPrivates.append((void*)priv);
   ctxt->_private = (void*)priv;
   return ctxt;
}

void ParserPrivate::freeTransformContext(xsltTransformContextPtr ctxt)
{
    ParserPrivate::s_knownPrivates.removeAll(ctxt->_private);
    ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);
    delete priv;
    xsltFreeTransformContext(ctxt);
}

QList<TransformationError> ParserPrivate::contextGetErrors(xsltTransformContextPtr ctxt)
{
    if (ctxt->_private && ParserPrivate::s_knownPrivates.contains(ctxt->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);
        return priv->m_errors;
    }
    return QList<TransformationError>();
}

void ParserPrivate::contextAddErrors(xsltTransformContextPtr ctxt,
                                     const QList<TransformationError> &errors)
{
    if (ctxt->_private && ParserPrivate::s_knownPrivates.contains(ctxt->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);
        priv->m_errors.append(errors);
    }
}

void ParserPrivate::contextAddSecurityError(xsltTransformContextPtr ctxt, const KUrl &url)
{
    if (ctxt->_private && ParserPrivate::s_knownPrivates.contains(ctxt->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);
        TransformationError error;
        error.m_errorType = TransformationError::ErrorSecurity;
        error.m_file = url;
        priv->m_errors.append(error);
    }
}

xmlParserCtxtPtr ParserPrivate::createParserContext(bool allowLocal, xmlDictPtr dict,
                                                    int options)
{
    // allocate parser, set dict and options
    xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
    if (ctxt == 0) {
        return 0;
    }
    if (dict != 0) {
        if (ctxt->dict != 0) {
            xmlDictFree(ctxt->dict);
        }
        ctxt->dict = dict;
        xmlDictReference(ctxt->dict);
    }
    if (options != 0) {
        xmlCtxtUseOptions(ctxt, options);
    }

    // add _private to control loading external entities
    ParserPrivate *priv = new ParserPrivate;
    priv->m_allowLocal = allowLocal;
    ParserPrivate::s_knownPrivates.append((void*)priv);
    ctxt->_private = (void*)priv;
    return ctxt;
}

QList<TransformationError> ParserPrivate::contextGetErrors(xmlParserCtxtPtr ctxt)
{
    if (ctxt->_private && ParserPrivate::s_knownPrivates.contains(ctxt->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);
        return priv->m_errors;
    }
    return QList<TransformationError>();
}

xmlDocPtr ParserPrivate::freeParserContext(xmlParserCtxtPtr ctxt)
{
    xmlDocPtr doc = 0;
    if (ctxt->wellFormed) {
        doc = ctxt->myDoc;
    } else {
        xmlFreeDoc(ctxt->myDoc);
        ctxt->myDoc = 0;
    }
    ParserPrivate::s_knownPrivates.removeAll(ctxt->_private);
    ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);
    delete priv;
    xmlFreeParserCtxt(ctxt);
    return doc;
}

bool ParserPrivate::securityLoadCheck(const KUrl &url, xmlParserCtxtPtr ctxt)
{
    if (ctxt->_private && ParserPrivate::s_knownPrivates.contains(ctxt->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);

        // trying to load local document in non-local context
        if (!priv->m_allowLocal && !url.hasHost()) {
            return false;
        }

        // current document is remote - all other external entities have to
        // be remote as well.
        if (url.hasHost()) {
            priv->m_allowLocal = false;
        }
    }

    return true;
}

void ParserPrivate::contextAddSecurityError(xmlParserCtxtPtr ctxt, const KUrl &url)
{
    if (ctxt->_private && ParserPrivate::s_knownPrivates.contains(ctxt->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);
        TransformationError error;
        error.m_errorType = TransformationError::ErrorSecurity;
        error.m_file = url;
        priv->m_errors.append(error);
    }
}

void ParserPrivate::contextAddNotFoundError(xmlParserCtxtPtr ctxt, const KUrl &url)
{
    if (ctxt->_private && ParserPrivate::s_knownPrivates.contains(ctxt->_private)) {
        ParserPrivate *priv = (ParserPrivate*)(ctxt->_private);
        TransformationError error;
        error.m_errorType = TransformationError::ErrorNotFound;
        error.m_file = url;
        priv->m_errors.append(error);
    }
}
