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

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

E-mail: