Annotation of parser3/src/main/pa_globals.C, revision 1.152.2.8
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.8! paf 8: static const char* IDENT_GLOBALS_C="$Date: 2003/01/29 12:57: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: #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 RESULT_VAR_NAME "result"
45: #define MATCH_VAR_NAME "match"
46:
47: #define EXCEPTION_VAR_NAME "exception"
48: #define EXCEPTION_TYPE_PART_NAME "type"
49: #define EXCEPTION_SOURCE_PART_NAME "source"
50: #define EXCEPTION_COMMENT_PART_NAME "comment"
51: #define EXCEPTION_HANDLED_PART_NAME "handled"
52:
53:
54: #define DATE_CALENDAR_WEEKNO_NAME "week"
55: #define DATE_CALENDAR_WEEKYEAR_NAME "year"
56:
57: #define CONTENT_DISPOSITION_NAME "content-disposition"
58: #define CONTENT_DISPOSITION_VALUE "attachment"
59: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
60:
61: #define SWITCH_DATA_NAME "SWITCH-DATA"
62:
63: #define CACHE_DATA_NAME "CACHE-DATA"
64:
65: #define CLASS_PATH_NAME "CLASS_PATH"
66:
67: #define SQL_LIMIT_NAME "limit"
68: #define SQL_OFFSET_NAME "offset"
69: #define SQL_DEFAULT_NAME "default"
70: #define SQL_DISTINCT_NAME "distinct"
71:
72: #define HASH_DEFAULT_ELEMENT_NAME "_default"
73:
74: #define HTTP_METHOD_NAME "method"
75: #define HTTP_TIMEOUT_NAME "timeout"
76: #define HTTP_HEADERS_NAME "headers"
77: #define HTTP_ANY_STATUS_NAME "any-status"
78: #define FILE_STATUS_NAME "status"
1.84 parser 79:
1.152.2.3 paf 80: StringPtr body_name(new String(BODY_NAME));
81: StringPtr download_name(new String(DOWNLOAD_NAME));
82:
83: StringPtr content_disposition_name(new String(CONTENT_DISPOSITION_NAME));
84: StringPtr content_disposition_value(new String(CONTENT_DISPOSITION_VALUE));
85: StringPtr content_disposition_filename_name(new String(CONTENT_DISPOSITION_FILENAME_NAME));
86:
87: StringPtr conf_method_name(new String(CONF_METHOD_NAME));
88: StringPtr auto_method_name(new String(AUTO_METHOD_NAME));
89:
90: StringPtr main_class_name(new String(MAIN_CLASS_NAME));
91:
92: StringPtr result_var_name(new String(RESULT_VAR_NAME));
93: StringPtr match_var_name(new String(MATCH_VAR_NAME));
94:
95: StringPtr exception_var_name(new String(EXCEPTION_VAR_NAME));
96: StringPtr exception_type_part_name(new String(EXCEPTION_TYPE_PART_NAME));
97: StringPtr exception_source_part_name(new String(EXCEPTION_SOURCE_PART_NAME));
98: StringPtr exception_comment_part_name(new String(EXCEPTION_COMMENT_PART_NAME));
99: StringPtr exception_handled_part_name(new String(EXCEPTION_HANDLED_PART_NAME));
100:
101: StringPtr charsets_name(new String(CHARSETS_NAME));
102: StringPtr mime_types_name(new String(MIME_TYPES_NAME));
103: StringPtr origins_mode_name(new String(ORIGINS_MODE_NAME));
1.152.2.1 paf 104:
1.152.2.3 paf 105: StringPtr class_path_name(new String(CLASS_PATH_NAME));
1.152.2.1 paf 106:
107: //^switch ^case
1.152.2.3 paf 108: StringPtr switch_data_name(new String(SWITCH_DATA_NAME));
1.152.2.1 paf 109:
110: //^cache
1.152.2.3 paf 111: StringPtr cache_data_name(new String(CACHE_DATA_NAME));
1.152.2.1 paf 112:
113: // sql
1.152.2.3 paf 114: StringPtr sql_limit_name(new String(SQL_LIMIT_NAME));
115: StringPtr sql_offset_name(new String(SQL_OFFSET_NAME));
116: StringPtr sql_default_name(new String(SQL_DEFAULT_NAME));
117: StringPtr sql_distinct_name(new String(SQL_DISTINCT_NAME));
1.152.2.1 paf 118:
119: // hash
1.152.2.3 paf 120: StringPtr hash_default_element_name(new String(HASH_DEFAULT_ELEMENT_NAME));
1.152.2.1 paf 121:
122: // http
1.152.2.3 paf 123: StringPtr http_method_name(new String(HTTP_METHOD_NAME));
124: StringPtr http_timeout_name(new String(HTTP_TIMEOUT_NAME));
125: StringPtr http_headers_name(new String(HTTP_HEADERS_NAME));
126: StringPtr http_any_status_name(new String(HTTP_ANY_STATUS_NAME));
127: StringPtr file_status_name(new String(FILE_STATUS_NAME));
1.8 paf 128:
1.5 paf 129: short hex_value[0x100];
1.111 paf 130:
131: #ifdef XML
132: GdomeDOMImplementation *domimpl;
133: #endif
1.5 paf 134:
135: static void setup_hex_value() {
1.68 parser 136: memset(hex_value, 0, sizeof(hex_value));
1.5 paf 137: hex_value['0'] = 0;
138: hex_value['1'] = 1;
139: hex_value['2'] = 2;
140: hex_value['3'] = 3;
141: hex_value['4'] = 4;
142: hex_value['5'] = 5;
143: hex_value['6'] = 6;
144: hex_value['7'] = 7;
145: hex_value['8'] = 8;
146: hex_value['9'] = 9;
147: hex_value['A'] = 10;
148: hex_value['B'] = 11;
149: hex_value['C'] = 12;
150: hex_value['D'] = 13;
151: hex_value['E'] = 14;
152: hex_value['F'] = 15;
153: hex_value['a'] = 10;
154: hex_value['b'] = 11;
155: hex_value['c'] = 12;
156: hex_value['d'] = 13;
157: hex_value['e'] = 14;
158: hex_value['f'] = 15;
159: }
1.1 paf 160:
1.99 paf 161: #ifdef XML
1.101 paf 162:
163: const int MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS=10;
164:
165: struct XML_Generic_error_info {
166: pa_thread_t thread_id;
167: char *message;
168: } xml_generic_error_infos[MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS];
169:
170: XML_Generic_error_info *xml_generic_error_info(pa_thread_t thread_id) {
171: for(int i=0; i<MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS; i++) {
172: XML_Generic_error_info *p=xml_generic_error_infos+i;
173: if(p->thread_id==thread_id)
174: return p;
175: }
176: return 0;
177: }
178:
1.99 paf 179: static void
1.101 paf 180: xmlParserGenericErrorFunc(void *ctx, const char *msg, ...) {
181: pa_thread_t thread_id=pa_get_thread_id();
182:
183: // infinitely looking for free slot to fill it
184: while(true) {
185: SYNCHRONIZED; // find+fill blocked
186:
187: // first try to get existing for this thread_id
188: XML_Generic_error_info *p=xml_generic_error_info(thread_id);
189: if(!p) { // occupy empty one
190: p=xml_generic_error_info(0);
191: if(!p) // wait for empty for it to appear
192: continue;
193: }
194:
1.102 paf 195: p->thread_id=thread_id;
1.101 paf 196: size_t offset=p->message?strlen(p->message):0;
197: p->message=(char *)realloc(p->message, offset+MAX_STRING);
198: if(!p->message)
199: SAPI::die(
200: "out of memory in 'xmlParserGenericErrorFunc', failed to reallocate to %u bytes",
201: offset+MAX_STRING);
202:
203: va_list args;
204: va_start(args, msg);
205: vsnprintf(p->message+offset, MAX_STRING, msg, args);
206: va_end(args);
207:
208: break;
209: }
210: }
211:
1.102 paf 212: bool xmlHaveGenericErrors() {
213: pa_thread_t thread_id=pa_get_thread_id();
214:
215: SYNCHRONIZED; // find blocked
216:
217: return xml_generic_error_info(thread_id)!=0;
218: }
219:
1.101 paf 220: const char *xmlGenericErrors() {
221: pa_thread_t thread_id=pa_get_thread_id();
222:
223: SYNCHRONIZED; // find+free blocked
224:
225: XML_Generic_error_info *p=xml_generic_error_info(thread_id);
226: if(!p) // no errors for our thread_id registered
227: return 0;
228:
229: const char *result=p->message;
230:
231: // free slot up
232: memset(p, 0, sizeof(*p));
233:
234: // it is up to caller to free it
235: return result;
1.99 paf 236: }
1.110 paf 237:
238: /**
239: * xmlFileMatchWithLocalhostEqDocumentRoot:
240: * filename: the URI for matching
241: *
242: * check if the URI matches an HTTP one
243: *
244: * Returns 1 if matches, 0 otherwise
245: */
246: static int
247: xmlFileMatchLocalhost(const char *filename) {
248: if (!strncmp(filename, "http://localhost", 16))
249: return(1);
250: return(0);
251: }
252:
253:
254: /**
255: * xmlFileOpenHttpLocalhost :
256: * filename: the URI for matching
257: *
258: * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
259: *
260: * input from FILE *, supports compressed input
261: * if filename is " " then the standard input is used
262: *
263: * Returns an I/O context or NULL in case of error
264: */
265: static void *
266: xmlFileOpenLocalhost (const char *filename) {
267: FILE *fd;
268: const char* documentRoot;
269: char path[1000];
270:
271: path[0]=0;
272: strcat(path, (documentRoot=getenv("DOCUMENT_ROOT"))?documentRoot:".");
273: strcat(path, &filename[16]);
274:
275: #ifdef WIN32
276: fd = fopen(path, "rb");
277: #else
278: fd = fopen(path, "r");
279: #endif /* WIN32 */
280: return((void *) fd);
281: }
282:
1.150 paf 283: /**
284: * xmlFileRead:
285: * @context: the I/O context
286: * @buffer: where to drop data
287: * @len: number of bytes to write
288: *
289: * Read @len bytes to @buffer from the I/O channel.
290: *
291: * Returns the number of bytes written
292: */
293: static int
294: pa_xmlFileRead (void * context, char * buffer, int len) {
295: return(fread(&buffer[0], 1, len, (FILE *) context));
296: }
297:
298: /**
299: * xmlFileClose:
300: * @context: the I/O context
301: *
302: * Close an I/O channel
303: */
304: static int
305: pa_xmlFileClose (void * context) {
306: return ( ( fclose((FILE *) context) == EOF ) ? -1 : 0 );
307: }
308:
1.99 paf 309: #endif
310:
1.83 parser 311: void pa_globals_destroy(void *) {
312: try {
1.96 paf 313: #ifdef XML
314: GdomeException exc;
315: gdome_di_unref (domimpl, &exc);
316: #endif
1.83 parser 317: } catch(const Exception& e) {
318: SAPI::die("pa_globals_destroy failed: %s", e.comment());
319: }
320: }
321:
322:
1.152.2.1 paf 323: /// @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
324: void pa_globals_init() {
1.32 paf 325:
1.5 paf 326: // hex value
327: setup_hex_value();
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: