1 #include "apachetools.h"
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 */
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 */
14 static void mod_xmltools_child_init(apr_pool_t *p, server_rec *s) {
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
21 static char* get_dtd_lang_file(string_array* params) {
23 char* localedir = apacheGetFirstParamValue(params, PARAM_LOCALE);
24 if(!localedir) localedir = strdup(DEFAULT_LOCALE);
26 int len = strlen(LANG_DTD) + strlen(localedir) + strlen(LOCALE_DIR) + 1;
31 sprintf(dtdfile, "%s/%s/%s", LOCALE_DIR, localedir, LANG_DTD );
33 return strdup(dtdfile);
36 static int mod_xmltools_handler (request_rec* r) {
38 /* make sure we're needed first thing*/
39 if (strcmp(r->handler, MODULE_NAME ))
42 /* we accept get/post requests */
43 r->allowed |= (AP_METHOD_BIT << M_GET);
44 r->allowed |= (AP_METHOD_BIT << M_POST);
46 ap_set_content_type(r, "text/html");
48 string_array* params = apacheParseParms(r);
50 char* file = r->filename;
51 char* dtdfile = get_dtd_lang_file(params);
56 xmlSubstituteEntitiesDefault(0);
59 if( (doc = xmlParseFile(file)) == NULL) {
60 fprintf(stderr, "\n ^-- Error parsing XML file %s\n", file);
62 return HTTP_INTERNAL_SERVER_ERROR;
65 /* process xincludes */
66 if( xmlXIncludeProcess(doc) < 0 ) {
67 fprintf(stderr, "\n ^-- Error processing XIncludes for file %s\n", file);
69 return HTTP_INTERNAL_SERVER_ERROR;
74 if(xmlReplaceDtd(doc, dtdfile) < 0) {
75 fprintf(stderr, "Error replacing DTD file with file %s\n", dtdfile);
77 return HTTP_INTERNAL_SERVER_ERROR;
81 /* force DTD entity replacement */
82 doc = xmlProcessDtdEntities(doc);
85 char* xml = xmlDocToString(doc, 0);
94 xmlCleanupCharEncodingHandlers();
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);
107 module AP_MODULE_DECLARE_DATA mod_xmltools = {
108 STANDARD20_MODULE_STUFF,
114 mod_xmltools_register_hooks,