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

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

E-mail: