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