Beispiel #1
0
static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
{
    if(!node) return 1;

    info->sheet = xsltNewStylesheet();
    if (!info->sheet)
        return 0;

    info->ctxt = xsltNewTransformContext( info->sheet, node->doc );
    if (!info->ctxt)
        return 0;

    info->pattern = xsltCompilePattern( str, node->doc,
                                        node, info->sheet, info->ctxt );
    if (!info->pattern)
        return 0;
    return 1;
}
Beispiel #2
0
String::C xdoc2buf(Request& r, VXdoc& vdoc, 
					XDocOutputOptions& oo,
					const String* file_spec,
					bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) {
	Charset* render=0;
	Charset* header=0;
	if(use_source_charset_to_render_and_client_charset_to_write_to_header) {
		render=&r.charsets.source();
		header=&r.charsets.client();
	} else {
		header=render=&charsets.get(oo.encoding->change_case(r.charsets.source(), String::CC_UPPER));
	}
	const char* render_encoding=render->NAME_CSTR();
	const char* header_encoding=header->NAME_CSTR();

	xmlCharEncodingHandler *renderer=xmlFindCharEncodingHandler(render_encoding);
	// UTF-8 renderer contains empty input/output converters, 
	// which is wrong for xmlOutputBufferCreateIO
	// while zero renderer goes perfectly 
	if(render->isUTF8())
		renderer=0;

	xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer));

	xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet());
	if(!stylesheet.get())
		throw Exception(0,
			0,
			"xsltNewStylesheet failed");

	#define OOSTRING2STYLE(name) \
		stylesheet->name=oo.name?BAD_CAST xmlMemStrdup((const char*)r.transcode(*oo.name)):0
	#define OOBOOL2STYLE(name) \
		if(oo.name>=0) stylesheet->name=oo.name

	OOSTRING2STYLE(method);
	OOSTRING2STYLE(encoding);
	OOSTRING2STYLE(mediaType);
//	OOSTRING2STYLE(doctypeSystem);
//	OOSTRING2STYLE(doctypePublic);
	OOBOOL2STYLE(indent);
	OOSTRING2STYLE(version);
	OOBOOL2STYLE(standalone);
	OOBOOL2STYLE(omitXmlDeclaration);

	xmlDoc& xmldoc=vdoc.get_xmldoc();
	xmldoc.encoding=BAD_CAST xmlMemStrdup(render_encoding);
	if(header_encoding)
		stylesheet->encoding=BAD_CAST xmlMemStrdup(header_encoding);
	if(xsltSaveResultTo(outputBuffer.get(), &xmldoc, stylesheet.get())<0
		|| xmlHaveGenericErrors())
		throw XmlException(0, r);

	// write out result
	char *gnome_str;
	size_t gnome_length;
#ifdef LIBXML2_NEW_BUFFER
	if(outputBuffer->conv) {
		gnome_length=xmlBufUse(outputBuffer->conv);
		gnome_str=(char *)xmlBufContent(outputBuffer->conv);
	} else {
		gnome_length=xmlOutputBufferGetSize(&(*outputBuffer));
		gnome_str=(char *)xmlOutputBufferGetContent(&(*outputBuffer));
	}
#else
	if(outputBuffer->conv) {
		gnome_length=outputBuffer->conv->use;
		gnome_str=(char *)outputBuffer->conv->content;
	} else {
		gnome_length=outputBuffer->buffer->use;
		gnome_str=(char *)outputBuffer->buffer->content;
	}
#endif

	if(file_spec){
		file_write(r.charsets,
			*file_spec,
			gnome_str,
			gnome_length, 
			true/*as_text*/);
		return String::C(); // actually, we don't need this output at all
	} else
		return String::C(gnome_length ? pa_strdup(gnome_str, gnome_length) : 0, gnome_length);
}
Beispiel #3
0
static void
xslt_yelp_document (xsltTransformContextPtr ctxt,
		    xmlNodePtr              node,
		    xmlNodePtr              inst,
		    xsltStylePreCompPtr     comp)
{
    YelpTransform *transform;
    xmlChar *page_id = NULL;
    gchar   *temp;
    xmlChar *page_buf;
    gint     buf_size;
    xsltStylesheetPtr style = NULL;
    const char *old_outfile;
    xmlDocPtr   new_doc = NULL;
    xmlDocPtr   old_doc;
    xmlNodePtr  old_insert;

    debug_print (DB_FUNCTION, "entering\n");

    if (ctxt->state == XSLT_STATE_STOPPED)
	return;

    if (!ctxt || !node || !inst || !comp)
	return;

    transform = (YelpTransform *) ctxt->_private;

    page_id = xsltEvalAttrValueTemplate (ctxt, inst,
					 (const xmlChar *) "href",
					 NULL);
    if (page_id == NULL || *page_id == '\0') {
	if (page_id)
	    xmlFree (page_id);
	else
	    xsltTransformError (ctxt, NULL, inst,
				_("No href attribute found on "
				  "yelp:document\n"));
	/* FIXME: put a real error here */
	goto done;
    }
    debug_print (DB_ARG, "  page_id = \"%s\"\n", page_id);

    old_outfile = ctxt->outputFile;
    old_doc     = ctxt->output;
    old_insert  = ctxt->insert;
    ctxt->outputFile = (const char *) page_id;

    style = xsltNewStylesheet ();
    if (style == NULL) {
	xsltTransformError (ctxt, NULL, inst,
			    _("Out of memory"));
	goto done;
    }

    style->omitXmlDeclaration = TRUE;

    new_doc = xmlNewDoc (BAD_CAST "1.0");
    new_doc->charset = XML_CHAR_ENCODING_UTF8;
    new_doc->dict = ctxt->dict;
    xmlDictReference (new_doc->dict);

    ctxt->output = new_doc;
    ctxt->insert = (xmlNodePtr) new_doc;

    xsltApplyOneTemplate (ctxt, node, inst->children, NULL, NULL);
    xsltSaveResultToString (&page_buf, &buf_size, new_doc, style);

    ctxt->outputFile = old_outfile;
    ctxt->output     = old_doc;
    ctxt->insert     = old_insert;

    g_mutex_lock (transform->mutex);

    temp = g_strdup ((gchar *) page_id);
    xmlFree (page_id);

    g_async_queue_push (transform->queue, g_strdup ((gchar *) temp));
    g_hash_table_insert (transform->chunks, temp, page_buf);
    transform->idle_funcs++;
    g_idle_add ((GSourceFunc) transform_chunk, transform);

    g_mutex_unlock (transform->mutex);

 done:
    if (new_doc)
	xmlFreeDoc (new_doc);
    if (style)
	xsltFreeStylesheet (style);
}