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