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

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

E-mail: