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

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

E-mail: