<commit_before>/*##############################################################################

    Copyright (C) 2011 HPCC Systems.

    All rights reserved. This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
############################################################################## */

#ifndef XSLPROCESSOR_IPP_INCL
#define XSLPROCESSOR_IPP_INCL

#ifndef _WIN32
//undefine the stricmp and strnicmp macros defined by platform.h
//since xerces implements these functions
#undef stricmp
#undef strnicmp
#endif

#include <xercesc/util/PlatformUtils.hpp>
#include <xalanc/XalanTransformer/XalanTransformer.hpp>
#include <xalanc/XPath/Function.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/sax/EntityResolver.hpp>
#include <xercesc/sax/InputSource.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/sax/HandlerBase.hpp>

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include "jliball.hpp"
#include "xslcache.hpp"

#ifdef _WIN32
#undef new
#undef delete
#define URLPREFIX ""
#else
#define URLPREFIX "file://"
#endif

#include "xslprocessor.hpp"

XALAN_USING_XERCES(EntityResolver)
XALAN_USING_XERCES(InputSource)
XALAN_USING_XERCES(XMLString)
XALAN_USING_XERCES(MemBufInputSource)
XALAN_USING_XERCES(XMLPlatformUtils)
XALAN_USING_XALAN(XalanDOMString)
XALAN_USING_XALAN(XalanParsedSource)
XALAN_USING_XALAN(XSLTResultTarget)
//XALAN_USING_XALAN(StaticStringToDOMString)
XALAN_USING_XALAN(XalanTransformer)
XALAN_USING_XALAN(XalanCompiledStylesheet)
XALAN_USING_XALAN(XSLTInputSource)
XALAN_USING_XALAN(Function)
XALAN_USING_XALAN(XObjectPtr)
XALAN_USING_XALAN(XPathExecutionContext)
XALAN_USING_XALAN(MemoryManagerType)
XALAN_USING_XALAN(XalanCopyConstruct)
XALAN_USING_XALAN(XalanNode)
XALAN_USING_XERCES(Locator)

class MemSourceResolver : public EntityResolver, public CInterface, implements IInterface
{
private:
    Owned<IIncludeHandler> m_includehandler;
    StringArray* m_includes;

public:
    IMPLEMENT_IINTERFACE;

    MemSourceResolver()
    {
        m_includes = NULL;
    }

    MemSourceResolver(StringArray* includes)
    {
        m_includes = includes;
    }

    int setHandler(IIncludeHandler* handler)
    {
        if(handler != NULL)
            m_includehandler.set(handler);
        return 0;
    }

    InputSource* resolveEntity (const XMLCh* const publicId, 
                                        const XMLCh* const systemId)
    {
        if(m_includehandler.get() == NULL)
            throw MakeStringException(-1, "MemSourceResolver::resolveEntity() - m_includehandler is NULL");

        InputSource* inputsrc=NULL;
        MemoryBuffer buf;
        char includename[1025];
        XMLString::transcode(systemId, includename, 1024);
        bool pathonly = false;
        if (m_includehandler->getInclude(includename, buf, pathonly))
        {
            if(pathonly)
            {
                if(m_includes)
                {
                    StringBuffer pbuf;
                    pbuf.append(buf.length(), buf.toByteArray());
                    m_includes->append(pbuf.str());
                }
                StringBuffer path;
                if(!isAbsolutePath((const char *)buf.toByteArray()))
                {
                    char baseurl[1025];
                    GetCurrentDirectory(1024, baseurl);
                    path.append(URLPREFIX).append(baseurl).append(PATHSEPSTR);
                }
                path.append(buf.length(), buf.toByteArray());
                inputsrc = new XSLTInputSource(path.str()); 
            }
            else
            {
                MemBufInputSource* memsrc = new MemBufInputSource((const XMLByte*)buf.detach(), buf.length(), (const XMLCh*)NULL, false);
                memsrc->setCopyBufToStream(false);<commit_msg>gh-342 - Fix order of evaluation issue in libxml xslprocessor include handling<commit_after>/*##############################################################################

    Copyright (C) 2011 HPCC Systems.

    All rights reserved. This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
############################################################################## */

#ifndef XSLPROCESSOR_IPP_INCL
#define XSLPROCESSOR_IPP_INCL

#ifndef _WIN32
//undefine the stricmp and strnicmp macros defined by platform.h
//since xerces implements these functions
#undef stricmp
#undef strnicmp
#endif

#include <xercesc/util/PlatformUtils.hpp>
#include <xalanc/XalanTransformer/XalanTransformer.hpp>
#include <xalanc/XPath/Function.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/sax/EntityResolver.hpp>
#include <xercesc/sax/InputSource.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/sax/HandlerBase.hpp>

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include "jliball.hpp"
#include "xslcache.hpp"

#ifdef _WIN32
#undef new
#undef delete
#define URLPREFIX ""
#else
#define URLPREFIX "file://"
#endif

#include "xslprocessor.hpp"

XALAN_USING_XERCES(EntityResolver)
XALAN_USING_XERCES(InputSource)
XALAN_USING_XERCES(XMLString)
XALAN_USING_XERCES(MemBufInputSource)
XALAN_USING_XERCES(XMLPlatformUtils)
XALAN_USING_XALAN(XalanDOMString)
XALAN_USING_XALAN(XalanParsedSource)
XALAN_USING_XALAN(XSLTResultTarget)
//XALAN_USING_XALAN(StaticStringToDOMString)
XALAN_USING_XALAN(XalanTransformer)
XALAN_USING_XALAN(XalanCompiledStylesheet)
XALAN_USING_XALAN(XSLTInputSource)
XALAN_USING_XALAN(Function)
XALAN_USING_XALAN(XObjectPtr)
XALAN_USING_XALAN(XPathExecutionContext)
XALAN_USING_XALAN(MemoryManagerType)
XALAN_USING_XALAN(XalanCopyConstruct)
XALAN_USING_XALAN(XalanNode)
XALAN_USING_XERCES(Locator)

class MemSourceResolver : public EntityResolver, public CInterface, implements IInterface
{
private:
    Owned<IIncludeHandler> m_includehandler;
    StringArray* m_includes;

public:
    IMPLEMENT_IINTERFACE;

    MemSourceResolver()
    {
        m_includes = NULL;
    }

    MemSourceResolver(StringArray* includes)
    {
        m_includes = includes;
    }

    int setHandler(IIncludeHandler* handler)
    {
        if(handler != NULL)
            m_includehandler.set(handler);
        return 0;
    }

    InputSource* resolveEntity (const XMLCh* const publicId, 
                                        const XMLCh* const systemId)
    {
        if(m_includehandler.get() == NULL)
            throw MakeStringException(-1, "MemSourceResolver::resolveEntity() - m_includehandler is NULL");

        InputSource* inputsrc=NULL;
        MemoryBuffer buf;
        char includename[1025];
        XMLString::transcode(systemId, includename, 1024);
        bool pathonly = false;
        if (m_includehandler->getInclude(includename, buf, pathonly))
        {
            if(pathonly)
            {
                if(m_includes)
                {
                    StringBuffer pbuf;
                    pbuf.append(buf.length(), buf.toByteArray());
                    m_includes->append(pbuf.str());
                }
                StringBuffer path;
                if(!isAbsolutePath((const char *)buf.toByteArray()))
                {
                    char baseurl[1025];
                    GetCurrentDirectory(1024, baseurl);
                    path.append(URLPREFIX).append(baseurl).append(PATHSEPSTR);
                }
                path.append(buf.length(), buf.toByteArray());
                inputsrc = new XSLTInputSource(path.str()); 
            }
            else
            {
                size32_t buflen = buf.length();
                MemBufInputSource* memsrc = new MemBufInputSource((const XMLByte*)buf.detach(), buflen, (const XMLCh*)NULL, false);
                memsrc->setCopyBufToStream(false);
{'fraction_non_alphanumeric': 0.1174781936623606, 'fraction_numerical': 0.006514298332781274, 'mean_word_length': 3.280718336483932, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2622367', 'n_tokens_mistral': 2865, 'n_tokens_neox': 2722, 'n_words': 610}