]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/apachemods/mod_xmltools.c
OpenILS apache xml processing stuff, other mods can go in this dir as well
[Evergreen.git] / Open-ILS / src / apachemods / mod_xmltools.c
1 #include "apachetools.h"
2 #include "xmltools.h"
3
4 #define MODULE_NAME             "mod_xmltools" /* our module name */
5 #define PARAM_LOCALE            "locale"                        /* the URL param for the local directory */
6 #define LANG_DTD                        "lang.dtd"              /* the DTD for the test entities */
7
8 /* these should be config directives */
9 #define LOCALE_DIR              "/home/erickson/sandbox/apachemods/locale"              /* The root directory where the local files are stored */
10 #define DEFAULT_LOCALE  "en-US"                 /* If no locale data is provided */
11
12
13 /* Child Init */
14 static void mod_xmltools_child_init(apr_pool_t *p, server_rec *s) {
15 }
16
17 /* allocates a char* to hold the name of the DTD language file 
18         Prints to stderr and returns NULL if there was an error loading the file 
19         */
20
21 static char* get_dtd_lang_file(string_array* params) {
22
23         char* localedir = apacheGetFirstParamValue(params, PARAM_LOCALE);
24         if(!localedir) localedir = strdup(DEFAULT_LOCALE);
25
26         int len = strlen(LANG_DTD) + strlen(localedir) + strlen(LOCALE_DIR) + 1;
27         char dtdfile[len];
28         bzero(dtdfile, len);
29
30         if(localedir)
31                 sprintf(dtdfile, "%s/%s/%s",  LOCALE_DIR, localedir, LANG_DTD );
32
33         return strdup(dtdfile);
34 }
35
36 static int mod_xmltools_handler (request_rec* r) {
37
38         /* make sure we're needed first thing*/
39         if (strcmp(r->handler, MODULE_NAME )) 
40                 return DECLINED;
41
42         /* we accept get/post requests */
43         r->allowed |= (AP_METHOD_BIT << M_GET);
44         r->allowed |= (AP_METHOD_BIT << M_POST);
45
46         ap_set_content_type(r, "text/html");
47
48         string_array* params = apacheParseParms(r);
49
50         char* file = r->filename;
51         char* dtdfile = get_dtd_lang_file(params);
52
53         xmlDocPtr doc;
54
55         /* be explicit */
56         xmlSubstituteEntitiesDefault(0);
57
58         /* parse the doc */
59         if( (doc = xmlParseFile(file)) == NULL) {
60                 fprintf(stderr, "\n ^-- Error parsing XML file %s\n", file);
61                 fflush(stderr);
62                 return HTTP_INTERNAL_SERVER_ERROR;
63         }
64
65         /* process xincludes */
66         if( xmlXIncludeProcess(doc) < 0 ) {
67                 fprintf(stderr, "\n ^-- Error processing XIncludes for file %s\n", file);
68                 fflush(stderr);
69                 return HTTP_INTERNAL_SERVER_ERROR;
70         }
71
72
73         /* replace the DTD */
74         if(xmlReplaceDtd(doc, dtdfile) < 0) {
75                 fprintf(stderr, "Error replacing DTD file with file %s\n", dtdfile);
76                 fflush(stderr);
77                 return HTTP_INTERNAL_SERVER_ERROR;
78         }
79
80
81         /* force DTD entity replacement */
82         doc = xmlProcessDtdEntities(doc);
83
84         /* stringify */
85         char* xml = xmlDocToString(doc, 0);
86
87         /* print the doc */
88         ap_rputs(xml, r);
89
90         /* deallocate */
91         free(dtdfile);
92         free(xml);
93         xmlFreeDoc(doc);
94         xmlCleanupCharEncodingHandlers();
95         xmlCleanupParser();
96         
97         return OK;
98
99 }
100
101
102 static void mod_xmltools_register_hooks (apr_pool_t *p) {
103         ap_hook_handler(mod_xmltools_handler, NULL, NULL, APR_HOOK_MIDDLE);
104         ap_hook_child_init(mod_xmltools_child_init,NULL,NULL,APR_HOOK_MIDDLE);
105 }
106
107 module AP_MODULE_DECLARE_DATA mod_xmltools = {
108         STANDARD20_MODULE_STUFF,
109         NULL,
110         NULL,
111         NULL,
112         NULL,
113         NULL,
114         mod_xmltools_register_hooks,
115 };
116