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;
31 static const char* xmlBuilderSetLocaleParam(
32 cmd_parms* params, void* cfg, const char* arg ) {
33 xmlBuilderConfig* config = ap_get_module_config(
34 params->server->module_config, &xmlbuilder_module );
35 config->localeParam = (char*) arg;
40 static const char* xmlBuilderSetPostXSL(
41 cmd_parms* params, void* cfg, const char* arg ) {
42 xmlBuilderConfig* config = ap_get_module_config(
43 params->server->module_config, &xmlbuilder_module );
44 config->postXSL = xsltParseStylesheetFile((xmlChar*) arg);
45 if( config->postXSL == NULL )
46 apacheDebug("Unable to parse postXSL stylesheet: %s. No postXSL will be performed", arg);
50 static const command_rec xmlBuilderCommands[] = {
51 AP_INIT_TAKE1( MODXMLB_CONFIG_LOCALE,
52 xmlBuilderSetDefaultLocale, NULL, ACCESS_CONF, "Default Locale"),
53 AP_INIT_TAKE1( MODXMLB_CONFIG_BASE_DIR,
54 xmlBuilderSetBaseDir, NULL, ACCESS_CONF, "Base Directory"),
55 AP_INIT_TAKE1( MODXMLB_CONFIG_POST_XSL,
56 xmlBuilderSetPostXSL, NULL, ACCESS_CONF, "Post XSL"),
57 AP_INIT_TAKE1( MODXMLB_CONFIG_DEFAULT_DTD,
58 xmlBuilderSetDefaultDtd, NULL, ACCESS_CONF, "Default DTD"),
59 AP_INIT_TAKE1( MODXMLB_CONFIG_LOCALE_PARAM,
60 xmlBuilderSetLocaleParam, NULL, ACCESS_CONF, "Default DTD"),
64 static void* xmlBuilderCreateConfig( apr_pool_t* p, server_rec* s ) {
65 xmlBuilderConfig* config =
66 (xmlBuilderConfig*) apr_palloc( p, sizeof(xmlBuilderConfig) );
67 config->baseDir = MODXMLB_DEFAULT_BASE_DIR;
68 config->defaultLocale = MODXMLB_DEFAULT_LOCALE;
69 config->defaultDtd = NULL;
70 config->postXSL = NULL;
71 config->localeParam = MODXMLB_DEFAULT_LOCALE_PARAM;
72 return (void*) config;
76 /* Child Init handler ----------------------------------------------------------- */
77 static void xmlBuilderChildInit( apr_pool_t *p, server_rec *s ) {
80 static int xmlBuilderHandler( request_rec* r ) {
82 if( strcmp(r->handler, MODULE_NAME ) ) return DECLINED;
84 xmlBuilderConfig* config = ap_get_module_config(
85 r->server->module_config, &xmlbuilder_module );
87 r->allowed |= (AP_METHOD_BIT << M_GET);
88 r->allowed |= (AP_METHOD_BIT << M_POST);
89 ap_set_content_type(r, "text/html; charset=utf-8");
91 string_array* params = apacheParseParms(r);
92 char* locale = apacheGetFirstParamValue(params, config->localeParam);
93 if(locale) __xmlBuilderDynamicLocale = locale;
94 char* XMLFile = r->filename;
96 xmlDocPtr doc = xmlBuilderProcessFile( XMLFile, config );
97 if(!doc) return apacheError( "Unable to parse XML file %s", XMLFile );
99 /* apply the post XSL */
100 if(config->postXSL) {
102 newdoc = xsltApplyStylesheet(config->postXSL, doc, NULL );
105 apacheDebug("Error applying postXSL... skipping.");
112 char* docXML = xmlDocToString( doc, 1 );
117 xmlCleanupCharEncodingHandlers();
124 /* frees the collected DTD's */
125 static void __xmlBuilderFreeDtdHash( char* key, void* item ) {
131 xmlDocPtr xmlBuilderProcessFile( char* filename, xmlBuilderConfig* config ) {
133 apacheError( "No XML file provided" ); return NULL; }
135 xmlBuilderContext context;
136 context.config = config;
137 context.doc = xmlNewDoc( BAD_CAST "1.0" );
138 context.dtdHash = osrfNewHash();
139 context.entHash = osrfNewHash();
140 context.nodeList = osrfNewList();
141 context.xmlError = 0;
142 context.xmlFile = filename;
143 context.dtdHash->freeItem = &__xmlBuilderFreeDtdHash;
145 /* pre-parse the default dtd if defined */
146 if( config->defaultDtd )
147 xmlBuilderAddDtd( config->defaultDtd, &context );
149 xmlParserCtxtPtr parserCtx;
151 parserCtx = xmlCreatePushParserCtxt(xmlBuilderSaxHandler, &context, "", 0, NULL);
152 xmlCtxtReadFile( parserCtx, filename, NULL, XML_PARSE_RECOVER );
154 xmlFreeParserCtxt( parserCtx );
155 osrfListFree(context.nodeList);
156 osrfHashFree(context.entHash);
157 osrfHashFree(context.dtdHash);
162 void xmlBuilderStartElement( void* context, const xmlChar *name, const xmlChar **atts ) {
163 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
165 xmlNodePtr node = NULL;
167 /* process xincludes as a sub-doc */
168 if( !strcmp( name, "xi:include" ) ) {
170 char* href = strdup(xmlSaxAttr( atts, "href" ));
173 /* find the relative path for the xinclude */
175 int len = strlen(ctx->xmlFile) + strlen(href) + 1;
178 strcpy( buf, ctx->xmlFile );
180 for( i = strlen(buf); i != 0; i-- ) {
181 if( buf[i] == '/' ) break;
190 xmlDocPtr subDoc = xmlBuilderProcessFile( href, ctx->config );
191 node = xmlDocGetRootElement( subDoc );
195 apacheError("Unable to parse xinclude: %s", href );
202 node = xmlNewNode(NULL, name);
203 xmlAddAttrs( node, atts );
207 xmlNodePtr parent = osrfListGetIndex(
208 ctx->nodeList, ctx->nodeList->size - 1 );
210 if( parent ) xmlAddChild( parent, node );
211 else xmlDocSetRootElement(ctx->doc, node);
213 osrfListPush( ctx->nodeList, node );
216 void xmlBuilderEndElement( void* context, const xmlChar* name ) {
217 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
218 osrfListPop( ctx->nodeList );
222 void xmlBuilderHandleCharacter(void* context, const xmlChar *ch, int len) {
223 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
224 xmlNodePtr node = osrfListGetIndex(
225 ctx->nodeList, ctx->nodeList->size - 1 );
228 xmlNodePtr txt = xmlNewTextLen(ch, len);
229 xmlAddChild( node, txt );
235 void xmlBuilderParseError( void* context, const char* msg, ... ) {
236 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
237 VA_LIST_TO_STRING(msg);
238 apacheDebug( "Parser Error Occurred: %s", VA_BUF);
243 xmlEntityPtr xmlBuilderGetEntity( void* context, const xmlChar* name ) {
244 xmlBuilderContext* ctx = (xmlBuilderContext*) context;
245 return osrfHashGet( ctx->entHash, name );
249 void xmlBuilderExtSubset( void* blob,
250 const xmlChar* name, const xmlChar* extId, const xmlChar* sysId ) {
252 xmlBuilderContext* context = (xmlBuilderContext*) blob;
253 if( context->config->defaultDtd ) return; /* only use the default if defined */
254 xmlBuilderAddDtd( sysId, context );
259 void xmlBuilderAddDtd( const char* sysId, xmlBuilderContext* context ) {
262 if( osrfHashGet( context->dtdHash, sysId ) ) return; /* already parsed this hash */
264 /* use the dynamic locale if defined... default locale instead */
266 if(__xmlBuilderDynamicLocale) locale = __xmlBuilderDynamicLocale;
267 else locale = context->config->defaultLocale;
269 /* determine the path to the DTD file and load it */
270 int len = strlen(context->config->baseDir) + strlen(locale) + strlen(sysId) + 4;
271 char buf[len]; bzero(buf,len);
272 snprintf( buf, len, "%s/%s/%s", context->config->baseDir, locale, sysId );
274 xmlDtdPtr dtd = xmlParseDTD(NULL, buf);
277 /* cycle through entities and push them into the entity hash */
278 xmlNodePtr node = dtd->children;
280 if( node->type == XML_ENTITY_DECL ) { /* shove the entities into the hash */
281 xmlEntityPtr ent = (xmlEntityPtr) node;
282 osrfHashSet( context->entHash, ent, (char*) ent->name );
287 /* cache the DTD so we can free it later */
288 osrfHashSet( context->dtdHash, dtd, sysId );
292 /* ------------------------------------------------------------------------ */
294 /* register callbacks */
295 static void xmlBuilderRegisterHooks (apr_pool_t *p) {
296 ap_hook_handler(xmlBuilderHandler, NULL, NULL, APR_HOOK_MIDDLE);
297 ap_hook_child_init(xmlBuilderChildInit,NULL,NULL,APR_HOOK_MIDDLE);
301 /* finally, flesh the module */
302 module AP_MODULE_DECLARE_DATA xmlbuilder_module = {
303 STANDARD20_MODULE_STUFF,
306 xmlBuilderCreateConfig,
309 xmlBuilderRegisterHooks,