Annotation of parser3/src/main/pa_globals.C, revision 1.152.2.17

1.15      paf         1: /** @file
1.16      paf         2:        Parser: globals.
                      3: 
1.152.2.10  paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.113     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.133     paf         6: */
1.16      paf         7: 
1.152.2.17! paf         8: static const char* IDENT_GLOBALS_C="$Date: 2003/02/26 11:51:15 $";
1.1       paf         9: 
1.102     paf        10: #include "pa_config_includes.h"
                     11: 
                     12: #ifdef XML
                     13: #include "libxslt/extensions.h"
                     14: #include "libxslt/xsltutils.h"
1.116     paf        15: extern "C" {
1.102     paf        16: #include "libexslt/exslt.h"
1.116     paf        17: };
1.102     paf        18: #endif
                     19: 
1.1       paf        20: #include "pa_globals.h"
1.32      paf        21: #include "pa_string.h"
1.83      parser     22: #include "pa_sapi.h"
1.101     paf        23: #include "pa_threads.h"
1.152.2.2  paf        24: 
1.152.2.4  paf        25: #ifdef XML
                     26: #include "pa_stylesheet_manager.h"
                     27: #endif
1.8       paf        28: 
1.5       paf        29: short hex_value[0x100];
1.111     paf        30: 
                     31: #ifdef XML
                     32: GdomeDOMImplementation *domimpl;
                     33: #endif
1.5       paf        34: 
                     35: static void setup_hex_value() {
1.68      parser     36:        memset(hex_value, 0, sizeof(hex_value));
1.5       paf        37:        hex_value['0'] = 0;     
                     38:        hex_value['1'] = 1;     
                     39:        hex_value['2'] = 2;     
                     40:        hex_value['3'] = 3;     
                     41:        hex_value['4'] = 4;     
                     42:        hex_value['5'] = 5;     
                     43:        hex_value['6'] = 6;     
                     44:        hex_value['7'] = 7;     
                     45:        hex_value['8'] = 8;     
                     46:        hex_value['9'] = 9;
                     47:        hex_value['A'] = 10;
                     48:        hex_value['B'] = 11;
                     49:        hex_value['C'] = 12;
                     50:        hex_value['D'] = 13;
                     51:        hex_value['E'] = 14;
                     52:        hex_value['F'] = 15;
                     53:        hex_value['a'] = 10;
                     54:        hex_value['b'] = 11;
                     55:        hex_value['c'] = 12;
                     56:        hex_value['d'] = 13;
                     57:        hex_value['e'] = 14;
                     58:        hex_value['f'] = 15;
                     59: }
1.1       paf        60: 
1.99      paf        61: #ifdef XML
1.101     paf        62: 
                     63: const int MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS=10;
                     64: 
1.152.2.17! paf        65: class XML_Generic_error_info {
        !            66: public:
1.101     paf        67:        pa_thread_t thread_id;
1.152.2.17! paf        68:        char buf[MAX_STRING];
        !            69:        size_t used;
        !            70: public:
        !            71:        XML_Generic_error_info() {
        !            72:                reset();
        !            73:        }
        !            74:        void reset() { 
        !            75:                thread_id=0; 
        !            76:                buf[used=0]=0;
        !            77:        }
        !            78:        CharPtr get_and_reset() {
        !            79:                CharPtr result(new char[used+1]);
        !            80:                memcpy(result.get(), buf, used+1);
        !            81:                reset();
        !            82:                return result;
        !            83:        }
1.101     paf        84: } xml_generic_error_infos[MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS];
                     85: 
                     86: XML_Generic_error_info *xml_generic_error_info(pa_thread_t thread_id) {
                     87:        for(int i=0; i<MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS; i++) {
                     88:                XML_Generic_error_info *p=xml_generic_error_infos+i;
                     89:                if(p->thread_id==thread_id)
                     90:                        return p;
                     91:        }
                     92:        return 0;
                     93: }
                     94: 
1.99      paf        95: static void
1.152.2.10  paf        96: xmlParserGenericErrorFunc(void *ctx, const char* msg, ...) { 
1.101     paf        97:     pa_thread_t thread_id=pa_get_thread_id();
                     98: 
                     99:        // infinitely looking for free slot to fill it
                    100:        while(true) {
                    101:                SYNCHRONIZED;  // find+fill blocked
                    102: 
                    103:                // first try to get existing for this thread_id
                    104:                XML_Generic_error_info *p=xml_generic_error_info(thread_id);
                    105:                if(!p) { // occupy empty one
                    106:                        p=xml_generic_error_info(0);
                    107:                        if(!p) // wait for empty for it to appear
                    108:                                continue;
                    109:                }
                    110: 
1.102     paf       111:                p->thread_id=thread_id;
1.101     paf       112:                
                    113:                va_list args;
                    114:                va_start(args, msg);
1.152.2.17! paf       115:                vsnprintf(p->buf+p->used, sizeof(p->buf)-p->used, msg, args);
1.101     paf       116:                va_end(args);
                    117: 
                    118:                break;
                    119:        }
                    120: }
                    121: 
1.102     paf       122: bool xmlHaveGenericErrors() {
                    123:     pa_thread_t thread_id=pa_get_thread_id();
                    124: 
                    125:        SYNCHRONIZED;  // find blocked
                    126: 
                    127:        return xml_generic_error_info(thread_id)!=0;
                    128: }
                    129: 
1.152.2.17! paf       130: CharPtr xmlGenericErrors() {
1.101     paf       131:     pa_thread_t thread_id=pa_get_thread_id();
                    132: 
                    133:        SYNCHRONIZED;  // find+free blocked
                    134: 
                    135:        XML_Generic_error_info *p=xml_generic_error_info(thread_id);
                    136:        if(!p) // no errors for our thread_id registered
1.152.2.17! paf       137:                return CharPtr(0);
1.101     paf       138: 
1.152.2.17! paf       139:        return p->get_and_reset();
1.99      paf       140: }
1.110     paf       141: 
                    142: /**
                    143:  * xmlFileMatchWithLocalhostEqDocumentRoot:
                    144:  * filename:  the URI for matching
                    145:  *
                    146:  * check if the URI matches an HTTP one
                    147:  *
                    148:  * Returns 1 if matches, 0 otherwise
                    149:  */
                    150: static int
1.152.2.10  paf       151: xmlFileMatchLocalhost(const char* filename) {
1.110     paf       152:     if (!strncmp(filename, "http://localhost", 16))
                    153:        return(1);
                    154:     return(0);
                    155: }
                    156: 
                    157: 
                    158: /**
                    159:  * xmlFileOpenHttpLocalhost :
                    160:  * filename:  the URI for matching
                    161:  *
                    162:  * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    163:  *
                    164:  * input from FILE *, supports compressed input
                    165:  * if filename is " " then the standard input is used
                    166:  *
                    167:  * Returns an I/O context or NULL in case of error
                    168:  */
                    169: static void *
1.152.2.10  paf       170: xmlFileOpenLocalhost (const char* filename) {
1.110     paf       171:     FILE *fd;
                    172:     const char* documentRoot;
                    173:     char path[1000];
                    174: 
                    175:        path[0]=0;
                    176:        strcat(path, (documentRoot=getenv("DOCUMENT_ROOT"))?documentRoot:".");
                    177:        strcat(path, &filename[16]);
                    178: 
                    179: #ifdef WIN32
                    180:     fd = fopen(path, "rb");
                    181: #else
                    182:     fd = fopen(path, "r");
                    183: #endif /* WIN32 */
                    184:     return((void *) fd);
                    185: }
                    186: 
1.150     paf       187: /**
                    188:  * xmlFileRead:
                    189:  * @context:  the I/O context
                    190:  * @buffer:  where to drop data
                    191:  * @len:  number of bytes to write
                    192:  *
                    193:  * Read @len bytes to @buffer from the I/O channel.
                    194:  *
                    195:  * Returns the number of bytes written
                    196:  */
                    197: static int
                    198: pa_xmlFileRead (void * context, char * buffer, int len) {
                    199:     return(fread(&buffer[0], 1,  len, (FILE *) context));
                    200: }
                    201: 
                    202: /**
                    203:  * xmlFileClose:
                    204:  * @context:  the I/O context
                    205:  *
                    206:  * Close an I/O channel
                    207:  */
                    208: static int
                    209: pa_xmlFileClose (void * context) {
                    210:     return ( ( fclose((FILE *) context) == EOF ) ? -1 : 0 );
                    211: }
                    212: 
1.99      paf       213: #endif
                    214: 
1.83      parser    215: void pa_globals_destroy(void *) {
                    216:        try {
1.96      paf       217: #ifdef XML
                    218:                GdomeException exc;
                    219:                gdome_di_unref (domimpl, &exc);
                    220: #endif
1.83      parser    221:        } catch(const Exception& e) {
1.152.2.16  paf       222:                SAPI::abort("pa_globals_destroy failed: %s", e.comment());
1.83      parser    223:        }
                    224: }
                    225: 
                    226: 
1.152.2.1  paf       227: /// @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
                    228: void pa_globals_init() {
1.32      paf       229: 
1.5       paf       230:        // hex value
                    231:        setup_hex_value();
1.74      parser    232: 
1.76      parser    233: #ifdef XML
1.96      paf       234:        // initializing xml libs
                    235: 
                    236:        /* First I get a DOMImplementation reference */
                    237:        domimpl = gdome_di_mkref ();
                    238:     /*
                    239:      * Register the EXSLT extensions and the test module
                    240:      */
                    241:     exsltRegisterAll();
                    242:     xsltRegisterTestModule();
                    243:     xmlDefaultSAXHandlerInit();
                    244:     /*
                    245:      * disable CDATA from being built in the document tree
                    246:      */
1.101     paf       247:     // never added yet  xmlDefaultSAXHandler.cdataBlock = NULL;
1.99      paf       248: 
                    249:        /*
                    250:         * Initialization function for the XML parser.
                    251:         * This is not reentrant. Call once before processing in case of
                    252:         * use in multithreaded programs.
                    253:        */
                    254:        xmlInitParser();
1.107     paf       255: 
                    256:        // 1. this is needed for proper parsing of stylesheets
                    257:        // there were a situation where honest entity ruined innocent xpath compilation
                    258:        // doc says "you sould turn it on on stylesheet load" without deepening into details
                    259:        // 2. when dom tree with entites goes under transform text nodes 
                    260:        // got [erroreosly] cut on first entity occurance
1.109     paf       261:        // --
1.107     paf       262:        // that is why this is:
                    263:        xmlSubstituteEntitiesDefault(1);
1.100     paf       264:        
                    265:        // Bit in the loadsubset context field to tell to do ID/REFs lookups 
                    266:        xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
                    267:        // Bit in the loadsubset context field to tell to do complete the elements attributes lists 
                    268:        // with the ones defaulted from the DTDs 
1.138     paf       269:     xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
                    270: 
                    271:        // validate each document after load/create (?)
                    272:        //xmlDoValidityCheckingDefaultValue = 1;
1.99      paf       273: 
1.104     paf       274: //regretfully this not only replaces entities on parse, but also on generate   xmlSubstituteEntitiesDefault(1);
1.105     paf       275:        // never switched this on xmlIndentTreeOutput=1;
1.104     paf       276: 
1.101     paf       277:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       278:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105     paf       279: //     FILE *f=fopen("y:\\xslt.log", "wt");
                    280: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       281: 
                    282:        // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    283:        xmlRegisterInputCallbacks(
                    284:                xmlFileMatchLocalhost, xmlFileOpenLocalhost,
1.150     paf       285:                pa_xmlFileRead, pa_xmlFileClose);
1.96      paf       286: 
                    287:        // XSLT stylesheet manager
1.90      paf       288:        cache_managers->put(*NEW String(pool, "stylesheet"), 
                    289:                stylesheet_manager=NEW Stylesheet_manager(pool));
1.76      parser    290: #endif
1.1       paf       291: }
1.76      parser    292: 
                    293: #if defined(XML) && defined(_MSC_VER)
1.132     paf       294: #      define GNOME_LIBS "/parser3project/win32xml/win32/gnome"
1.131     paf       295: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76      parser    296: #      ifdef _DEBUG
1.131     paf       297: #              pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/dsp/libxml2_so_debug/libxml2.lib")
                    298: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_so_debug/libexslt.lib")
                    299: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_so_debug/libxslt.lib")
                    300: #              pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/Debug/libgdome.lib")
1.76      parser    301: #      else
1.131     paf       302: #              pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/dsp/libxml2_so_release/libxml2.lib")
                    303: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_so_release/libexslt.lib")
                    304: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_so_release/libxslt.lib")
                    305: #              pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/Release/libgdome.lib")
1.85      paf       306: #      endif
                    307: #endif

E-mail: