Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 03a0aa9

Browse files
committed
added numargs check to xsltLoadStyleSheet
1 parent aa17d68 commit 03a0aa9

File tree

2 files changed

+66
-38
lines changed

2 files changed

+66
-38
lines changed

revxml/src/revxml.cpp

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,13 +2685,16 @@ void XML_XPathDataFromQuery(char *args[], int nargs, char **retstring, Bool *pas
26852685
}
26862686

26872687
// MDW-2013-08-09: [[ RevXmlXslt ]]
2688+
// XML stylesheet transformation functions
2689+
26882690
/**
26892691
* XML_xsltLoadStylesheet
2690-
* @pStylesheet : stylesheet contents
2692+
* @pStylesheetDocID : xml document id
26912693
*
2692-
* returns an xsltStylesheetPtr which can be used in xsltApplyStyleSheet
2694+
* returns an xsltStylesheetPtr which can be used in xsltApplyStyleSheet.
2695+
* it's up to the user to free the stylesheet pointer after processing.
26932696
*
2694-
* put xsltLoadStylesheet(tStylesheet)
2697+
* put xsltLoadStylesheet(tStylesheetDocID)
26952698
*/
26962699
void XML_xsltLoadStylesheet(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
26972700
{
@@ -2715,12 +2718,15 @@ void XML_xsltLoadStylesheet(char *args[], int nargs, char **retstring, Bool *pas
27152718
*retstring = istrdup(result);
27162719
free(result);
27172720
}
2721+
// couldn't dereference the xml document
27182722
else
27192723
*retstring = istrdup(xmlerrors[XPATHERR_BADDOCPOINTER]);
27202724
}
2725+
// couldn't dereference the xml id
27212726
else
27222727
*retstring = istrdup(xmlerrors[XMLERR_BADDOCID]);
27232728
}
2729+
// only one argument permitted
27242730
else
27252731
*retstring = istrdup(xmlerrors[XMLERR_BADARGUMENTS]);
27262732
}
@@ -2730,6 +2736,7 @@ void XML_xsltLoadStylesheet(char *args[], int nargs, char **retstring, Bool *pas
27302736
* @pStylesheetPath : path to stylesheet file
27312737
*
27322738
* returns an xsltStylesheetPtr which can be used in xsltApplyStyleSheet
2739+
* it's up to the user to free the stylesheet pointer after processing.
27332740
*
27342741
* put xsltLoadStylesheetFromFile(tPathToStylesheet)
27352742
*/
@@ -2748,6 +2755,7 @@ void XML_xsltLoadStylesheetFromFile(char *args[], int nargs, char **retstring, B
27482755
*retstring = istrdup(result);
27492756
free(result);
27502757
}
2758+
// only one argument permitted
27512759
else
27522760
*retstring = istrdup(xmlerrors[XMLERR_BADARGUMENTS]);
27532761
}
@@ -2758,7 +2766,7 @@ void XML_xsltLoadStylesheetFromFile(char *args[], int nargs, char **retstring, B
27582766
*
27592767
* frees a xsltStylesheetPtr created by xsltLoadStylesheet
27602768
*
2761-
* put xsltFreeStylesheet(tStylesheetID)
2769+
* xsltFreeStylesheet tStylesheetID
27622770
*/
27632771
void XML_xsltFreeStylesheet(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
27642772
{
@@ -2772,6 +2780,7 @@ void XML_xsltFreeStylesheet(char *args[], int nargs, char **retstring, Bool *pas
27722780
xsltFreeStylesheet(cur);
27732781
*retstring = (char *)calloc(1,1);
27742782
}
2783+
// only one argument permitted
27752784
else
27762785
*retstring = istrdup(xmlerrors[XMLERR_BADARGUMENTS]);
27772786
}
@@ -2780,10 +2789,13 @@ void XML_xsltFreeStylesheet(char *args[], int nargs, char **retstring, Bool *pas
27802789
* XML_xsltApplyStylesheet
27812790
* @pDocID : xml tree id (already parsed)
27822791
* @pStylesheet : xsltStylesheetPtr from xsltLoadStylesheet
2783-
* @pParamList : ]optional] delimiter between data elements (default="\n")
2792+
* @pParamList : [optional] delimiter between data elements (default="\n")
27842793
*
2794+
* Returns the transformed xml data after applying the stylesheet.
2795+
* Note that the user must free the stylesheet document after processing.
27852796
*
27862797
* put xsltApplyStylesheet(tDocID, tStylesheet, tParamList)
2798+
* xsltFreeStylesheet tStylesheet
27872799
*/
27882800
void XML_xsltApplyStylesheet(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
27892801
{
@@ -2800,7 +2812,6 @@ void XML_xsltApplyStylesheet(char *args[], int nargs, char **retstring, Bool *pa
28002812

28012813
if (2 <= nargs)
28022814
{
2803-
// XML_NewDocumentFromFile(args, nargs, retstring, pass, error);
28042815
int docID = atoi(args[0]);
28052816
CXMLDocument *xmlDocument = doclist.find(docID);
28062817
if (NULL != xmlDocument)
@@ -2811,14 +2822,20 @@ void XML_xsltApplyStylesheet(char *args[], int nargs, char **retstring, Bool *pa
28112822
cur = (xsltStylesheetPtr)atoi(args[1]);
28122823
if (NULL != cur)
28132824
{
2814-
params[nbparams] = NULL;
2825+
if (nargs > 2)
2826+
{
2827+
}
2828+
else
2829+
{
2830+
params[nbparams] = NULL;
2831+
}
28152832
res = xsltApplyStylesheet(cur, xmlDoc, params);
28162833

28172834
xsltSaveResultToString(&doc_txt_ptr, &doc_txt_len, res, cur);
28182835

28192836
*retstring = istrdup((char *)doc_txt_ptr);
28202837

2821-
xsltFreeStylesheet(cur);
2838+
// free the xml document - we're done with it
28222839
xmlFreeDoc(res);
28232840

28242841
xsltCleanupGlobals();
@@ -2833,18 +2850,20 @@ void XML_xsltApplyStylesheet(char *args[], int nargs, char **retstring, Bool *pa
28332850
else
28342851
*retstring = istrdup(xmlerrors[XMLERR_BADDOCID]);
28352852
}
2853+
// requires at least docID and stylesheetPtr arguments
28362854
else
28372855
*retstring = istrdup(xmlerrors[XMLERR_BADARGUMENTS]);
28382856
}
28392857

28402858
/**
28412859
* XML_xsltApplyStylesheetFile
28422860
* @pDocID : xml tree id (already parsed)
2843-
* @pStylesheet : stylesheet to apply against the xml document
2844-
* @pParamList : ]optional] delimiter between data elements (default="\n")
2845-
*
2861+
* @pStylesheetPath : path to stylesheet file
2862+
* @pParamList : [optional] delimiter between data elements (default="\n")
28462863
*
2847-
* put xsltApplyStylesheet(tDocID, tStylesheet, tParamList)
2864+
* Returns the transformed xml data after applying the stylesheet.
2865+
*
2866+
* put xsltApplyStylesheet(tDocID, tStylesheetPath, tParamList)
28482867
*/
28492868
void XML_xsltApplyStylesheetFile(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
28502869
{
@@ -2854,49 +2873,58 @@ void XML_xsltApplyStylesheetFile(char *args[], int nargs, char **retstring, Bool
28542873
xsltStylesheetPtr cur = NULL;
28552874
int nbparams = 0;
28562875
const char *params[16 + 1];
2857-
char *result;
28582876

28592877
xmlChar *doc_txt_ptr;
28602878
int doc_txt_len;
28612879

2862-
// XML_NewDocumentFromFile(args, nargs, retstring, pass, error);
2863-
int docID = atoi(args[0]);
2864-
CXMLDocument *xmlDocument = doclist.find(docID);
2865-
if (NULL != xmlDocument)
2880+
if (2 <= nargs)
28662881
{
2867-
xmlDocPtr xmlDoc = xmlDocument->GetDocPtr();
2868-
if (NULL != xmlDoc)
2882+
int docID = atoi(args[0]);
2883+
CXMLDocument *xmlDocument = doclist.find(docID);
2884+
if (NULL != xmlDocument)
28692885
{
2870-
cur = xsltParseStylesheetFile((const xmlChar *)args[1]);
2871-
if (NULL != cur)
2886+
xmlDocPtr xmlDoc = xmlDocument->GetDocPtr();
2887+
if (NULL != xmlDoc)
28722888
{
2873-
params[nbparams] = NULL;
2874-
res = xsltApplyStylesheet(cur, xmlDoc, params);
2889+
cur = xsltParseStylesheetFile((const xmlChar *)args[1]);
2890+
if (NULL != cur)
2891+
{
2892+
if (nargs > 2)
2893+
{
2894+
}
2895+
else
2896+
{
2897+
params[nbparams] = NULL;
2898+
}
2899+
res = xsltApplyStylesheet(cur, xmlDoc, params);
28752900

2876-
// xsltSaveResultToFile(stdout, res, cur);
2877-
xsltSaveResultToString(&doc_txt_ptr, &doc_txt_len, res, cur);
2901+
xsltSaveResultToString(&doc_txt_ptr, &doc_txt_len, res, cur);
28782902

2879-
// result = (char *)malloc(INTSTRSIZE);
2880-
// sprintf(result,"%ld",res);
2881-
// *retstring = (result != NULL ? istrdup(result) : (char *)calloc(1,1));
2882-
*retstring = istrdup((char *)doc_txt_ptr);
2883-
// free(result);
2903+
*retstring = istrdup((char *)doc_txt_ptr);
28842904

2885-
xsltFreeStylesheet(cur);
2886-
xmlFreeDoc(res);
2887-
// xmlFreeDoc(doc);
2905+
// free the xsltStylesheetPtr we just created
2906+
xsltFreeStylesheet(cur);
2907+
// free the xml document - we're done with it
2908+
xmlFreeDoc(res);
28882909

2889-
xsltCleanupGlobals();
2890-
xmlCleanupParser();
2910+
xsltCleanupGlobals();
2911+
xmlCleanupParser();
2912+
}
2913+
// couldn't dereference the stylesheet document
2914+
else
2915+
*retstring = istrdup(xmlerrors[XPATHERR_BADDOCPOINTER]);
28912916
}
2917+
// couldn't dereference the xml document
28922918
else
28932919
*retstring = istrdup(xmlerrors[XPATHERR_BADDOCPOINTER]);
28942920
}
2921+
// couldn't dereference the xml id
28952922
else
2896-
*retstring = istrdup(xmlerrors[XPATHERR_BADDOCPOINTER]);
2923+
*retstring = istrdup(xmlerrors[XMLERR_BADDOCID]);
28972924
}
2925+
// requires at least docID and stylesheetPath arguments
28982926
else
2899-
*retstring = istrdup(xmlerrors[XMLERR_BADDOCID]);
2927+
*retstring = istrdup(xmlerrors[XMLERR_BADARGUMENTS]);
29002928
}
29012929

29022930
EXTERNAL_BEGIN_DECLARATIONS("revXML")

thirdparty

0 commit comments

Comments
 (0)