mirror of
https://gitlab.gnome.org/GNOME/libxslt
synced 2025-11-08 11:02:18 +03:00
- libxslt/xslt.[ch]: started parsing stylesheet xsl:stylesheet
Daniel
This commit is contained in:
195
libxslt/xslt.c
195
libxslt/xslt.c
@@ -18,12 +18,60 @@
|
||||
#include <libxslt/xslt.h>
|
||||
#include <libxslt/xsltInternals.h>
|
||||
|
||||
#define DEBUG_PARSING
|
||||
|
||||
/*
|
||||
* There is no XSLT specific error reporting module yet
|
||||
*/
|
||||
#define xsltGenericError xmlGenericError
|
||||
#define xsltGenericErrorContext xmlGenericErrorContext
|
||||
|
||||
/*
|
||||
* Useful macros
|
||||
*/
|
||||
|
||||
#define IS_XSLT_ELEM(n) \
|
||||
((n)->ns != NULL) && (xmlStrEqual(cur->ns->href, XSLT_NAMESPACE))
|
||||
|
||||
#define IS_BLANK(c) (((c) == 0x20) || ((c) == 0x09) || ((c) == 0xA) || \
|
||||
((c) == 0x0D))
|
||||
|
||||
#define IS_BLANK_NODE(n) \
|
||||
(((n)->type == XML_TEXT_NODE) && (xsltIsBlank((n)->content)))
|
||||
|
||||
#define TODO \
|
||||
xsltGenericError(xsltGenericErrorContext, \
|
||||
"Unimplemented block at %s:%d\n", \
|
||||
__FILE__, __LINE__);
|
||||
|
||||
#define STRANGE \
|
||||
xsltGenericError(xsltGenericErrorContext, \
|
||||
"Internal error at %s:%d\n", \
|
||||
__FILE__, __LINE__);
|
||||
|
||||
/************************************************************************
|
||||
* *
|
||||
* Helper functions *
|
||||
* *
|
||||
************************************************************************/
|
||||
|
||||
/**
|
||||
* xsltIsBlank:
|
||||
* @str: a string
|
||||
*
|
||||
* Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
|
||||
*/
|
||||
int
|
||||
xsltIsBlank(xmlChar *str) {
|
||||
if (str == NULL)
|
||||
return(1);
|
||||
while (*str != 0) {
|
||||
if (!(IS_BLANK(*str))) return(0);
|
||||
str++;
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
/************************************************************************
|
||||
* *
|
||||
* Routines to handle XSLT data structures *
|
||||
@@ -73,6 +121,143 @@ xsltFreeStylesheet(xsltStylesheetPtr sheet) {
|
||||
* *
|
||||
************************************************************************/
|
||||
|
||||
/**
|
||||
* xsltParseStylesheetTop:
|
||||
* @style: the XSLT stylesheet
|
||||
* @top: the top level "stylesheet" element
|
||||
*
|
||||
* scan the top level elements of an XSL stylesheet
|
||||
*/
|
||||
|
||||
void
|
||||
xsltParseStylesheetTop(xsltStylesheetPtr style, xmlNodePtr top) {
|
||||
xmlNodePtr cur;
|
||||
|
||||
if (top == NULL)
|
||||
return;
|
||||
cur = top->children;
|
||||
|
||||
while (cur != NULL) {
|
||||
if (IS_BLANK_NODE(cur)) {
|
||||
cur = cur->next;
|
||||
continue;
|
||||
}
|
||||
if (!(IS_XSLT_ELEM(cur))) {
|
||||
#ifdef DEBUG_PARSING
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetTop : found foreign element %s\n",
|
||||
cur->name);
|
||||
#endif
|
||||
cur = cur->next;
|
||||
continue;
|
||||
}
|
||||
if (xmlStrEqual(cur->name, "import")) {
|
||||
TODO /* Handle import */
|
||||
} else
|
||||
break;
|
||||
cur = cur->next;
|
||||
}
|
||||
while (cur != NULL) {
|
||||
if (IS_BLANK_NODE(cur)) {
|
||||
cur = cur->next;
|
||||
continue;
|
||||
}
|
||||
if (!(IS_XSLT_ELEM(cur))) {
|
||||
#ifdef DEBUG_PARSING
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetTop : found foreign element %s\n",
|
||||
cur->name);
|
||||
#endif
|
||||
cur = cur->next;
|
||||
continue;
|
||||
}
|
||||
if (xmlStrEqual(cur->name, "import")) {
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetTop: ignoring misplaced import element\n");
|
||||
} else if (xmlStrEqual(cur->name, "include")) {
|
||||
TODO /* Handle include */
|
||||
} else if (xmlStrEqual(cur->name, "strip-space")) {
|
||||
TODO /* Handle strip-space */
|
||||
} else if (xmlStrEqual(cur->name, "preserve-space")) {
|
||||
TODO /* Handle preserve-space */
|
||||
} else if (xmlStrEqual(cur->name, "output")) {
|
||||
TODO /* Handle output */
|
||||
} else if (xmlStrEqual(cur->name, "key")) {
|
||||
TODO /* Handle key */
|
||||
} else if (xmlStrEqual(cur->name, "decimal-format")) {
|
||||
TODO /* Handle decimal-format */
|
||||
} else if (xmlStrEqual(cur->name, "attribute-set")) {
|
||||
TODO /* Handle attribute-set */
|
||||
} else if (xmlStrEqual(cur->name, "variable")) {
|
||||
TODO /* Handle variable */
|
||||
} else if (xmlStrEqual(cur->name, "param")) {
|
||||
TODO /* Handle param */
|
||||
} else if (xmlStrEqual(cur->name, "template")) {
|
||||
TODO /* Handle template */
|
||||
} else if (xmlStrEqual(cur->name, "namespace-alias")) {
|
||||
TODO /* Handle namespace-alias */
|
||||
} else {
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetTop: ignoring unknown %s element\n",
|
||||
cur->name);
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* xsltParseStylesheetDoc:
|
||||
* @doc: and xmlDoc parsed XML
|
||||
*
|
||||
* parse an XSLT stylesheet building the associated structures
|
||||
*
|
||||
* Returns a new XSLT stylesheet structure.
|
||||
*/
|
||||
|
||||
xsltStylesheetPtr
|
||||
xsltParseStylesheetDoc(xmlDocPtr doc) {
|
||||
xsltStylesheetPtr ret;
|
||||
xmlNodePtr cur;
|
||||
|
||||
if (doc == NULL)
|
||||
return(NULL);
|
||||
|
||||
ret = xsltNewStylesheet();
|
||||
if (ret == NULL)
|
||||
return(NULL);
|
||||
|
||||
/*
|
||||
* First step, locate the xsl:stylesheet element and the
|
||||
* namespace declaration.
|
||||
*/
|
||||
cur = xmlDocGetRootElement(doc);
|
||||
if (cur == NULL) {
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetDoc : empty stylesheet\n");
|
||||
xsltFreeStylesheet(ret);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
if ((IS_XSLT_ELEM(cur)) && (xmlStrEqual(cur->name, "stylesheet"))) {
|
||||
#ifdef DEBUG_PARSING
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetDoc : found stylesheet\n");
|
||||
#endif
|
||||
} else {
|
||||
|
||||
TODO /* lookup the stylesheet element down in the tree */
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetDoc : root is not stylesheet\n");
|
||||
xsltFreeStylesheet(ret);
|
||||
return(NULL);
|
||||
}
|
||||
ret->doc = doc;
|
||||
|
||||
xsltParseStylesheetTop(ret, cur);
|
||||
|
||||
return(ret);
|
||||
}
|
||||
|
||||
/**
|
||||
* xsltParseStylesheetFile:
|
||||
* @filename: the filename/URL to the stylesheet
|
||||
@@ -86,24 +271,28 @@ xsltStylesheetPtr
|
||||
xsltParseStylesheetFile(const xmlChar* filename) {
|
||||
xsltStylesheetPtr ret;
|
||||
xmlDocPtr doc;
|
||||
|
||||
|
||||
if (filename == NULL)
|
||||
return(NULL);
|
||||
|
||||
#ifdef DEBUG_PARSING
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetFile : parse %s\n", filename);
|
||||
#endif
|
||||
|
||||
doc = xmlParseFile(filename);
|
||||
if (doc == NULL) {
|
||||
xsltGenericError(xsltGenericErrorContext,
|
||||
"xsltParseStylesheetFile : cannot parse %s\n", filename);
|
||||
return(NULL);
|
||||
}
|
||||
ret = xsltNewStylesheet();
|
||||
ret = xsltParseStylesheetDoc(doc);
|
||||
if (ret == NULL) {
|
||||
xmlFreeDoc(doc);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
ret->doc = doc;
|
||||
|
||||
return(ret);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user