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 );
150 xmlCleanupCharEncodingHandlers();
157 /* frees the collected DTD's */
158 static void __xmlBuilderFreeDtdHash( char* key, void* item ) {
164 xmlDocPtr xmlBuilderProcessFile( char* filename, xmlBuilderConfig* config ) {
166 apacheError( "No XML file provided" ); return NULL; }
168 xmlBuilderContext context;
169 context.config = config;
170 context.doc = xmlNewDoc( BAD_CAST "1.0" );
171 context.dtdHash = osrfNewHash();
172 context.entHash = osrfNewHash();
173 context.nodeList = osrfNewList();
174 context.xmlError = 0;
175 context.xmlFile = filename;
176 context.dtdHash->freeItem = &__xmlBuilderFreeDtdHash;
178 /* pre-parse the default dtd if defined */
179 if( config->defaultDtd )
180 xmlBuilderAddDtd( config->defaultDtd, &context );
182 xmlParserCtxtPtr parserCtx;
184 parserCtx = xmlCreatePushParserCtxt(xmlBuilderSaxHandler, &context, "", 0, NULL);
185 xmlCtxtReadFile( parserCtx, filename, NULL, XML_PARSE_RECOVER );
187 xmlFreeParserCtxt( parserCtx );
188 osrfListFree(context.nodeList);
189 osrfHashFree(context.entHash);
190 osrfHashFree(context.dtdHash);
195 void xmlBuilderStartElement( void* context, const xmlChar *name, const xmlChar **atts ) {
196 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
198 xmlNodePtr node = NULL;
200 /* process xincludes as a sub-doc */
201 if( !strcmp( name, "xi:include" ) ) {
203 char* href = strdup(xmlSaxAttr( atts, "href" ));
206 /* find the relative path for the xinclude */
208 int len = strlen(ctx->xmlFile) + strlen(href) + 1;
211 strcpy( buf, ctx->xmlFile );
213 for( i = strlen(buf); i != 0; i-- ) {
214 if( buf[i] == '/' ) break;
223 xmlDocPtr subDoc = xmlBuilderProcessFile( href, ctx->config );
224 node = xmlDocGetRootElement( subDoc );
228 apacheError("Unable to parse xinclude: %s", href );
235 node = xmlNewNode(NULL, name);
236 xmlBuilderAddAtts( ctx, node, atts );
240 xmlNodePtr parent = osrfListGetIndex(
241 ctx->nodeList, ctx->nodeList->size - 1 );
243 if( parent ) xmlAddChild( parent, node );
244 else xmlDocSetRootElement(ctx->doc, node);
246 osrfListPush( ctx->nodeList, node );
250 void xmlBuilderAddAtts( xmlBuilderContext* ctx, xmlNodePtr node, const xmlChar** atts ) {
251 if(!(ctx && node && atts)) return;
253 for(i = 0; (atts[i] != NULL); i++) {
255 const xmlChar* name = atts[i];
256 const xmlChar* prop = atts[i+1];
257 int nl = strlen(prop);
260 if( prop[0] == '&' && prop[nl-1] == ';' ) { /* replace the entity if we are one */
263 strncat(buf, prop + 1, nl - 2);
264 xmlEntityPtr ent = osrfHashGet( ctx->entHash, buf );
265 if(ent && ent->content) _prop = strdup(ent->content);
266 } else { _prop = strdup(prop); }
268 xmlSetProp( node, name, _prop );
275 void xmlBuilderEndElement( void* context, const xmlChar* name ) {
276 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
277 osrfListPop( ctx->nodeList );
281 void xmlBuilderHandleCharacter(void* context, const xmlChar *ch, int len) {
282 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
283 xmlNodePtr node = osrfListGetIndex(
284 ctx->nodeList, ctx->nodeList->size - 1 );
287 xmlNodePtr txt = xmlNewTextLen(ch, len);
288 xmlAddChild( node, txt );
294 void xmlBuilderParseError( void* context, const char* msg, ... ) {
295 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
296 VA_LIST_TO_STRING(msg);
297 apacheDebug( "Parser Error Occurred: %s", VA_BUF);
302 xmlEntityPtr xmlBuilderGetEntity( void* context, const xmlChar* name ) {
303 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
304 xmlEntityPtr ent = osrfHashGet( ctx->entHash, name );
309 void xmlBuilderExtSubset( void* blob,
310 const xmlChar* name, const xmlChar* extId, const xmlChar* sysId ) {
312 xmlBuilderContext* context = (xmlBuilderContext*) blob;
313 if( context->config->defaultDtd ) {
314 apacheDebug("Ignoring DTD [%s] because default DTD is set...", sysId);
318 xmlBuilderAddDtd( sysId, context );
322 void xmlBuilderProcInstruction(
323 void* blob, const xmlChar* name, const xmlChar* data ) {
324 xmlBuilderContext* ctx = (xmlBuilderContext*) blob;
325 //xmlNodePtr node = xmlNewDocPI( ctx->doc, name, data );
326 xmlNodePtr pi = xmlNewDocPI( ctx->doc, name, data );
327 xmlAddChild( (xmlNodePtr) ctx->doc, pi );
332 void xmlBuilderAddDtd( const char* sysId, xmlBuilderContext* context ) {
335 if( osrfHashGet( context->dtdHash, sysId ) ) return; /* already parsed this hash */
337 //apacheDebug("Adding new DTD file to the entity hash: %s", sysId);
339 /* use the dynamic locale if defined... default locale instead */
341 if(__xmlBuilderDynamicLocale) locale = __xmlBuilderDynamicLocale;
342 else locale = context->config->defaultLocale;
344 /* determine the path to the DTD file and load it */
345 int len = strlen(context->config->baseDir) + strlen(locale) + strlen(sysId) + 4;
346 char buf[len]; bzero(buf,len);
347 snprintf( buf, len, "%s/%s/%s", context->config->baseDir, locale, sysId );
349 xmlDtdPtr dtd = xmlParseDTD(NULL, buf);
352 /* cycle through entities and push them into the entity hash */
353 xmlNodePtr node = dtd->children;
355 if( node->type == XML_ENTITY_DECL ) { /* shove the entities into the hash */
356 xmlEntityPtr ent = (xmlEntityPtr) node;
357 osrfHashSet( context->entHash, ent, (char*) ent->name );
362 /* cache the DTD so we can free it later */
363 osrfHashSet( context->dtdHash, dtd, sysId );
367 /* ------------------------------------------------------------------------ */
369 /* register callbacks */
370 static void xmlBuilderRegisterHooks (apr_pool_t *p) {
371 ap_hook_handler(xmlBuilderHandler, NULL, NULL, APR_HOOK_MIDDLE);
372 ap_hook_child_init(xmlBuilderChildInit,NULL,NULL,APR_HOOK_MIDDLE);
376 /* finally, flesh the module */
377 module AP_MODULE_DECLARE_DATA xmlbuilder_module = {
378 STANDARD20_MODULE_STUFF,
381 xmlBuilderCreateConfig,
384 xmlBuilderRegisterHooks,