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

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

E-mail: