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

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.19! paf         8: static const char* IDENT_GLOBALS_C="$Date: 2003/03/06 16:15:25 $";
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.5       paf        25: short hex_value[0x100];
1.111     paf        26: 
                     27: #ifdef XML
                     28: GdomeDOMImplementation *domimpl;
                     29: #endif
1.5       paf        30: 
                     31: static void setup_hex_value() {
1.68      parser     32:        memset(hex_value, 0, sizeof(hex_value));
1.5       paf        33:        hex_value['0'] = 0;     
                     34:        hex_value['1'] = 1;     
                     35:        hex_value['2'] = 2;     
                     36:        hex_value['3'] = 3;     
                     37:        hex_value['4'] = 4;     
                     38:        hex_value['5'] = 5;     
                     39:        hex_value['6'] = 6;     
                     40:        hex_value['7'] = 7;     
                     41:        hex_value['8'] = 8;     
                     42:        hex_value['9'] = 9;
                     43:        hex_value['A'] = 10;
                     44:        hex_value['B'] = 11;
                     45:        hex_value['C'] = 12;
                     46:        hex_value['D'] = 13;
                     47:        hex_value['E'] = 14;
                     48:        hex_value['F'] = 15;
                     49:        hex_value['a'] = 10;
                     50:        hex_value['b'] = 11;
                     51:        hex_value['c'] = 12;
                     52:        hex_value['d'] = 13;
                     53:        hex_value['e'] = 14;
                     54:        hex_value['f'] = 15;
                     55: }
1.1       paf        56: 
1.99      paf        57: #ifdef XML
1.101     paf        58: 
                     59: const int MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS=10;
                     60: 
1.152.2.17  paf        61: class XML_Generic_error_info {
                     62: public:
1.101     paf        63:        pa_thread_t thread_id;
1.152.2.17  paf        64:        char buf[MAX_STRING];
                     65:        size_t used;
                     66: public:
                     67:        XML_Generic_error_info() {
                     68:                reset();
                     69:        }
                     70:        void reset() { 
                     71:                thread_id=0; 
                     72:                buf[used=0]=0;
                     73:        }
                     74:        CharPtr get_and_reset() {
                     75:                CharPtr result(new char[used+1]);
                     76:                memcpy(result.get(), buf, used+1);
                     77:                reset();
                     78:                return result;
                     79:        }
1.101     paf        80: } xml_generic_error_infos[MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS];
                     81: 
                     82: XML_Generic_error_info *xml_generic_error_info(pa_thread_t thread_id) {
                     83:        for(int i=0; i<MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS; i++) {
                     84:                XML_Generic_error_info *p=xml_generic_error_infos+i;
                     85:                if(p->thread_id==thread_id)
                     86:                        return p;
                     87:        }
                     88:        return 0;
                     89: }
                     90: 
1.99      paf        91: static void
1.152.2.10  paf        92: xmlParserGenericErrorFunc(void *ctx, const char* msg, ...) { 
1.101     paf        93:     pa_thread_t thread_id=pa_get_thread_id();
                     94: 
                     95:        // infinitely looking for free slot to fill it
                     96:        while(true) {
                     97:                SYNCHRONIZED;  // find+fill blocked
                     98: 
                     99:                // first try to get existing for this thread_id
                    100:                XML_Generic_error_info *p=xml_generic_error_info(thread_id);
                    101:                if(!p) { // occupy empty one
                    102:                        p=xml_generic_error_info(0);
                    103:                        if(!p) // wait for empty for it to appear
                    104:                                continue;
                    105:                }
                    106: 
1.102     paf       107:                p->thread_id=thread_id;
1.101     paf       108:                
                    109:                va_list args;
                    110:                va_start(args, msg);
1.152.2.19! paf       111:                p->used+=vsnprintf(p->buf+p->used, sizeof(p->buf)-p->used, msg, args);
1.101     paf       112:                va_end(args);
                    113: 
                    114:                break;
                    115:        }
                    116: }
                    117: 
1.102     paf       118: bool xmlHaveGenericErrors() {
                    119:     pa_thread_t thread_id=pa_get_thread_id();
                    120: 
                    121:        SYNCHRONIZED;  // find blocked
                    122: 
                    123:        return xml_generic_error_info(thread_id)!=0;
                    124: }
                    125: 
1.152.2.17  paf       126: CharPtr xmlGenericErrors() {
1.101     paf       127:     pa_thread_t thread_id=pa_get_thread_id();
                    128: 
                    129:        SYNCHRONIZED;  // find+free blocked
                    130: 
                    131:        XML_Generic_error_info *p=xml_generic_error_info(thread_id);
                    132:        if(!p) // no errors for our thread_id registered
1.152.2.17  paf       133:                return CharPtr(0);
1.101     paf       134: 
1.152.2.17  paf       135:        return p->get_and_reset();
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) {
1.152.2.16  paf       218:                SAPI::abort("pa_globals_destroy failed: %s", e.comment());
1.83      parser    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:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       274:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105     paf       275: //     FILE *f=fopen("y:\\xslt.log", "wt");
                    276: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       277: 
                    278:        // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    279:        xmlRegisterInputCallbacks(
                    280:                xmlFileMatchLocalhost, xmlFileOpenLocalhost,
1.150     paf       281:                pa_xmlFileRead, pa_xmlFileClose);
1.76      parser    282: #endif
1.1       paf       283: }
1.76      parser    284: 
                    285: #if defined(XML) && defined(_MSC_VER)
1.132     paf       286: #      define GNOME_LIBS "/parser3project/win32xml/win32/gnome"
1.131     paf       287: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76      parser    288: #      ifdef _DEBUG
1.131     paf       289: #              pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/dsp/libxml2_so_debug/libxml2.lib")
                    290: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_so_debug/libexslt.lib")
                    291: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_so_debug/libxslt.lib")
                    292: #              pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/Debug/libgdome.lib")
1.76      parser    293: #      else
1.131     paf       294: #              pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/dsp/libxml2_so_release/libxml2.lib")
                    295: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_so_release/libexslt.lib")
                    296: #              pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_so_release/libxslt.lib")
                    297: #              pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/Release/libgdome.lib")
1.85      paf       298: #      endif
                    299: #endif

E-mail: