1 #include "mod_xmlbuilder.h"
3 char* __xmlBuilderDynamicLocale = NULL;
6 /* set the base DTD directory */
7 static const char* xmlBuilderSetBaseDir(cmd_parms *params, void *cfg, const char *arg) {
8 xmlBuilderConfig* config = ap_get_module_config(
9 params->server->module_config, &xmlbuilder_module );
10 config->baseDir = (char*) arg;
14 static const char* xmlBuilderSetDefaultLocale(
15 cmd_parms* params, void* cfg, const char* arg ) {
16 xmlBuilderConfig* config = ap_get_module_config(
17 params->server->module_config, &xmlbuilder_module );
18 config->defaultLocale = (char*) arg;
22 static const char* xmlBuilderSetDefaultDtd(
23 cmd_parms* params, void* cfg, const char* arg ) {
24 xmlBuilderConfig* config = ap_get_module_config(
25 params->server->module_config, &xmlbuilder_module );
26 config->defaultDtd = (char*) arg;
27 if( config->defaultDtd ) {
28 if(!strcmp(config->defaultDtd,"NULL"))
29 config->defaultDtd = NULL;
35 static const char* xmlBuilderSetLocaleParam(
36 cmd_parms* params, void* cfg, const char* arg ) {
37 xmlBuilderConfig* config = ap_get_module_config(
38 params->server->module_config, &xmlbuilder_module );
39 config->localeParam = (char*) arg;
44 static const char* xmlBuilderSetPostXSL(
45 cmd_parms* params, void* cfg, const char* arg ) {
46 xmlBuilderConfig* config = ap_get_module_config(
47 params->server->module_config, &xmlbuilder_module );
48 config->postXSL = xsltParseStylesheetFile((xmlChar*) arg);
49 if( config->postXSL == NULL )
50 apacheDebug("Unable to parse postXSL stylesheet: %s. No postXSL will be performed", arg);
54 static const char* xmlBuilderSetContentType(
55 cmd_parms* params, void* cfg, const char* arg ) {
56 xmlBuilderConfig* config = ap_get_module_config(
57 params->server->module_config, &xmlbuilder_module );
58 config->contentType = (char*) arg;
62 static const command_rec xmlBuilderCommands[] = {
63 AP_INIT_TAKE1( MODXMLB_CONFIG_LOCALE,
64 xmlBuilderSetDefaultLocale, NULL, ACCESS_CONF, "Default Locale"),
65 AP_INIT_TAKE1( MODXMLB_CONFIG_BASE_DIR,
66 xmlBuilderSetBaseDir, NULL, ACCESS_CONF, "Base Directory"),
67 AP_INIT_TAKE1( MODXMLB_CONFIG_POST_XSL,
68 xmlBuilderSetPostXSL, NULL, ACCESS_CONF, "Post XSL"),
69 AP_INIT_TAKE1( MODXMLB_CONFIG_DEFAULT_DTD,
70 xmlBuilderSetDefaultDtd, NULL, ACCESS_CONF, "Default DTD"),
71 AP_INIT_TAKE1( MODXMLB_CONFIG_LOCALE_PARAM,
72 xmlBuilderSetLocaleParam, NULL, ACCESS_CONF, "Default DTD"),
73 AP_INIT_TAKE1( MODXMLB_CONFIG_CONTENT_TYPE,
74 xmlBuilderSetContentType, NULL, ACCESS_CONF, "Content Type"),
78 static void* xmlBuilderCreateConfig( apr_pool_t* p, server_rec* s ) {
79 xmlBuilderConfig* config =
80 (xmlBuilderConfig*) apr_palloc( p, sizeof(xmlBuilderConfig) );
81 config->baseDir = MODXMLB_DEFAULT_BASE_DIR;
82 config->defaultLocale = MODXMLB_DEFAULT_LOCALE;
83 config->defaultDtd = NULL;
84 config->postXSL = NULL;
85 config->contentType = NULL;
86 config->localeParam = MODXMLB_DEFAULT_LOCALE_PARAM;
87 return (void*) config;
91 /* Child Init handler ----------------------------------------------------------- */
92 static void xmlBuilderChildInit( apr_pool_t *p, server_rec *s ) {
95 static int xmlBuilderHandler( request_rec* r ) {
97 if( strcmp(r->handler, MODULE_NAME ) ) return DECLINED;
99 xmlBuilderConfig* config = ap_get_module_config(
100 r->server->module_config, &xmlbuilder_module );
102 r->allowed |= (AP_METHOD_BIT << M_GET);
103 r->allowed |= (AP_METHOD_BIT << M_POST);
104 char* ct = config->contentType;
105 if(!config->contentType) ct = "text/html; charset=utf-8";
106 ap_set_content_type(r, ct);
108 //ap_table_set(r->headers_out, "Cache-Control", "max-age=15552000");
110 char* dates = apr_pcalloc(r->pool, MAX_STRING_LEN);
111 apr_rfc822_date(dates, apr_time_now() + 604800000000); /* cache for one week */
112 ap_table_set(r->headers_out, "Expires", dates);
114 apr_rfc822_date(dates, apr_time_now()); /* cache for one week */
115 ap_table_set(r->headers_out, "Date", dates);
119 char expire_hdr[APR_RFC822_DATE_LEN];
120 apr_rfc822_date(expire_hdr, (apr_time_t)(time(NULL) + 15552000));
121 ap_table_set(r->headers_out, "Expires", expire_hdr);
124 string_array* params = apacheParseParms(r);
125 char* locale = apacheGetFirstParamValue(params, config->localeParam);
126 if(locale) __xmlBuilderDynamicLocale = locale;
127 char* XMLFile = r->filename;
129 xmlDocPtr doc = xmlBuilderProcessFile( XMLFile, config );
130 if(!doc) return apacheError( "Unable to parse XML file %s", XMLFile );
132 /* apply the post XSL */
133 if(config->postXSL) {
135 newdoc = xsltApplyStylesheet(config->postXSL, doc, NULL );
138 apacheDebug("Error applying postXSL... skipping.");
145 char* docXML = xmlDocToString( doc, 1 );
146 //apacheDebug("DOC:\n%s\n%s", docXML);
151 //xmlCleanupCharEncodingHandlers();
152 //xmlCleanupParser();
158 /* frees the collected DTD's */
159 static void __xmlBuilderFreeDtdHash( char* key, void* item ) {
165 xmlDocPtr xmlBuilderProcessFile( char* filename, xmlBuilderConfig* config ) {
167 apacheError( "No XML file provided" ); return NULL; }
169 xmlBuilderContext context;
170 context.config = config;
171 context.doc = xmlNewDoc( BAD_CAST "1.0" );
172 context.dtdHash = osrfNewHash();
173 context.entHash = osrfNewHash();
174 context.nodeList = osrfNewList();
175 context.xmlError = 0;
176 context.xmlFile = filename;
177 context.dtdHash->freeItem = &__xmlBuilderFreeDtdHash;
179 /* pre-parse the default dtd if defined */
180 if( config->defaultDtd )
181 xmlBuilderAddDtd( config->defaultDtd, &context );
183 xmlParserCtxtPtr parserCtx;
185 parserCtx = xmlCreatePushParserCtxt(xmlBuilderSaxHandler, &context, "", 0, NULL);
186 xmlCtxtReadFile( parserCtx, filename, NULL, XML_PARSE_RECOVER );
188 xmlFreeParserCtxt( parserCtx );
189 osrfListFree(context.nodeList);
190 osrfHashFree(context.entHash);
191 osrfHashFree(context.dtdHash);
196 void xmlBuilderStartElement( void* context, const xmlChar *name, const xmlChar **atts ) {
197 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
199 xmlNodePtr node = NULL;
201 /* process xincludes as a sub-doc */
202 if( !strcmp( name, "xi:include" ) ) {
204 char* href = strdup(xmlSaxAttr( atts, "href" ));
207 /* find the relative path for the xinclude */
209 int len = strlen(ctx->xmlFile) + strlen(href) + 1;
212 strcpy( buf, ctx->xmlFile );
214 for( i = strlen(buf); i != 0; i-- ) {
215 if( buf[i] == '/' ) break;
224 xmlDocPtr subDoc = xmlBuilderProcessFile( href, ctx->config );
225 node = xmlDocGetRootElement( subDoc );
229 apacheError("Unable to parse xinclude: %s", href );
236 node = xmlNewNode(NULL, name);
237 xmlBuilderAddAtts( ctx, node, atts );
241 xmlNodePtr parent = osrfListGetIndex(
242 ctx->nodeList, ctx->nodeList->size - 1 );
244 if( parent ) xmlAddChild( parent, node );
245 else xmlDocSetRootElement(ctx->doc, node);
247 osrfListPush( ctx->nodeList, node );
251 void xmlBuilderAddAtts( xmlBuilderContext* ctx, xmlNodePtr node, const xmlChar** atts ) {
252 if(!(ctx && node && atts)) return;
255 for(i = 0; (atts[i] != NULL); i++) {
259 const xmlChar* name = atts[i];
260 const xmlChar* prop = atts[i+1];
261 int nl = strlen(prop);
264 if( prop[0] == '&' && prop[nl-1] == ';' ) { /* replace the entity if we are one */
267 strncat(buf, prop + 1, nl - 2);
268 xmlEntityPtr ent = osrfHashGet( ctx->entHash, buf );
269 if(ent && ent->content) _prop = ent->content;
270 } else { _prop = (char*) prop; }
272 xmlSetProp( node, name, _prop );
278 void xmlBuilderEndElement( void* context, const xmlChar* name ) {
279 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
280 osrfListPop( ctx->nodeList );
284 void xmlBuilderHandleCharacter(void* context, const xmlChar *ch, int len) {
285 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
286 xmlNodePtr node = osrfListGetIndex(
287 ctx->nodeList, ctx->nodeList->size - 1 );
290 xmlNodePtr txt = xmlNewTextLen(ch, len);
291 xmlAddChild( node, txt );
297 void xmlBuilderParseError( void* context, const char* msg, ... ) {
298 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
299 VA_LIST_TO_STRING(msg);
300 apacheDebug( "Parser Error Occurred: %s", VA_BUF);
305 xmlEntityPtr xmlBuilderGetEntity( void* context, const xmlChar* name ) {
306 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
307 xmlEntityPtr ent = osrfHashGet( ctx->entHash, name );
312 void xmlBuilderExtSubset( void* blob,
313 const xmlChar* name, const xmlChar* extId, const xmlChar* sysId ) {
315 xmlBuilderContext* context = (xmlBuilderContext*) blob;
316 if( context->config->defaultDtd ) {
317 apacheDebug("Ignoring DTD [%s] because default DTD is set...", sysId);
321 xmlBuilderAddDtd( sysId, context );
325 void xmlBuilderProcInstruction(
326 void* blob, const xmlChar* name, const xmlChar* data ) {
327 xmlBuilderContext* ctx = (xmlBuilderContext*) blob;
328 //xmlNodePtr node = xmlNewDocPI( ctx->doc, name, data );
329 xmlNodePtr pi = xmlNewDocPI( ctx->doc, name, data );
330 xmlAddChild( (xmlNodePtr) ctx->doc, pi );
335 void xmlBuilderAddDtd( const char* sysId, xmlBuilderContext* context ) {
338 if( osrfHashGet( context->dtdHash, sysId ) ) return; /* already parsed this hash */
340 //apacheDebug("Adding new DTD file to the entity hash: %s", sysId);
342 /* use the dynamic locale if defined... default locale instead */
344 if(__xmlBuilderDynamicLocale) locale = __xmlBuilderDynamicLocale;
345 else locale = context->config->defaultLocale;
347 /* determine the path to the DTD file and load it */
348 int len = strlen(context->config->baseDir) + strlen(locale) + strlen(sysId) + 4;
349 char buf[len]; bzero(buf,len);
350 snprintf( buf, len, "%s/%s/%s", context->config->baseDir, locale, sysId );
352 xmlDtdPtr dtd = xmlParseDTD(NULL, buf);
355 /* cycle through entities and push them into the entity hash */
356 xmlNodePtr node = dtd->children;
358 if( node->type == XML_ENTITY_DECL ) { /* shove the entities into the hash */
359 xmlEntityPtr ent = (xmlEntityPtr) node;
360 osrfHashSet( context->entHash, ent, (char*) ent->name );
365 /* cache the DTD so we can free it later */
366 osrfHashSet( context->dtdHash, dtd, sysId );
370 /* ------------------------------------------------------------------------ */
372 /* register callbacks */
373 static void xmlBuilderRegisterHooks (apr_pool_t *p) {
374 ap_hook_handler(xmlBuilderHandler, NULL, NULL, APR_HOOK_MIDDLE);
375 ap_hook_child_init(xmlBuilderChildInit,NULL,NULL,APR_HOOK_MIDDLE);
379 /* finally, flesh the module */
380 module AP_MODULE_DECLARE_DATA xmlbuilder_module = {
381 STANDARD20_MODULE_STUFF,
384 xmlBuilderCreateConfig,
387 xmlBuilderRegisterHooks,