1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/* $Id: trans.c,v 1.19 2004/11/22 02:28:21 mgrouch Exp $ */
#include <config.h>
#include "trans.h"
#include "xmlstar.h"
/*
* This code is based on xsltproc by Daniel Veillard (daniel@veillard.com)
* (see also http://xmlsoft.org/)
*/
int errorno = 0;
/**
* Initialize global command line options
*/
void
xsltInitOptions(xsltOptionsPtr ops)
{
ops->noval = 1;
ops->nonet = 1;
ops->omit_decl = 0;
ops->show_extensions = 0;
ops->noblanks = 0;
ops->embed = 0;
#ifdef LIBXML_XINCLUDE_ENABLED
ops->xinclude = 0;
#endif
#ifdef LIBXML_HTML_ENABLED
ops->html = 0;
#endif
#ifdef LIBXML_CATALOG_ENABLED
ops->catalogs = 0;
#endif
}
/**
* Initialize LibXML
*/
void
xsltInitLibXml(xsltOptionsPtr ops)
{
/*
* Initialize library memory
*/
xmlInitMemory();
LIBXML_TEST_VERSION
/*
* Store line numbers in the document tree
*/
xmlLineNumbersDefault(1);
/*
* Register the EXSLT extensions
*/
exsltRegisterAll();
/*
* Register the test module
*/
xsltRegisterTestModule();
if (ops->show_extensions)
{
xsltDebugDumpExtensions(stderr);
exit(EXIT_SUCCESS);
}
xmlKeepBlanksDefault(1);
if (ops->noblanks) xmlKeepBlanksDefault(0);
xmlPedanticParserDefault(0);
xmlGetWarningsDefaultValue = 1;
/*xmlDoValidityCheckingDefaultValue = 0;*/
xmlLoadExtDtdDefaultValue = 1;
/*
* DTD validation options
*/
if (ops->noval == 0)
{
xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
}
else
{
xmlLoadExtDtdDefaultValue = 0;
}
#ifdef LIBXML_XINCLUDE_ENABLED
/*
* enable XInclude
*/
if (ops->xinclude)
xsltSetXIncludeDefault(1);
#endif
#ifdef LIBXML_CATALOG_ENABLED
/*
* enable SGML catalogs
*/
if (ops->catalogs)
{
char *catalogs = getenv("SGML_CATALOG_FILES");
if (catalogs == NULL)
fprintf(stderr, "%s", "Variable $SGML_CATALOG_FILES not set\n");
else
xmlLoadCatalogs(catalogs);
}
#endif
}
/* get result of XSL transformation */
xmlDocPtr
xsltTransform(xsltOptionsPtr ops, xmlDocPtr doc, const char** params,
xsltStylesheetPtr cur, const char *filename)
{
xsltTransformContextPtr ctxt;
xmlDocPtr res;
if (ops->omit_decl)
{
cur->omitXmlDeclaration = 1;
}
#ifdef LIBXML_XINCLUDE_ENABLED
if (ops->xinclude) xmlXIncludeProcess(doc);
#endif
ctxt = xsltNewTransformContext(cur, doc);
if (ctxt == NULL) return NULL;
res = xsltApplyStylesheetUser(cur, doc, params, NULL, NULL, ctxt);
if (ctxt->state == XSLT_STATE_ERROR)
errorno = 9;
if (ctxt->state == XSLT_STATE_STOPPED)
errorno = 10;
xsltFreeTransformContext(ctxt);
xmlFreeDoc(doc);
if (res == NULL)
{
fprintf(stderr, "no result for %s\n", filename);
}
return res;
}
/**
* Run stylesheet on XML document
*/
void
xsltProcess(xsltOptionsPtr ops, xmlDocPtr doc, const char** params,
xsltStylesheetPtr cur, const char *filename)
{
xmlDocPtr res = xsltTransform(ops, doc, params, cur, filename);
if (res && xsltSaveResultToFile(stdout, res, cur) < 0)
{
errorno = EXIT_LIB_ERROR;
}
xmlFreeDoc(res);
}
/**
* run XSLT on documents
*/
int xsltRun(xsltOptionsPtr ops, char* xsl, const char** params,
int count, char **docs)
{
xsltStylesheetPtr cur = NULL;
xmlDocPtr doc, style;
int i, options = 0;
options = XSLT_PARSE_OPTIONS;
/*
* Compile XSLT Sylesheet
*/
style = xmlReadFile((const char *) xsl, NULL, options);
if (style == NULL)
{
fprintf(stderr, "cannot parse %s\n", xsl);
cur = NULL;
errorno = 4;
}
else
{
if (ops->embed)
{
cur = xsltLoadStylesheetPI(style);
if (cur != NULL)
{
/* it is an embedded stylesheet */
xsltProcess(ops, style, params, cur, xsl);
xsltFreeStylesheet(cur);
cur = NULL;
}
for (i=0; i<count; i++)
{
style = xmlReadFile((const char *) docs[i], NULL, options);
if (style == NULL)
{
fprintf(stderr, "cannot parse %s\n", docs[i]);
cur = NULL;
goto done;
}
cur = xsltLoadStylesheetPI(style);
if (cur != NULL)
{
/* it is an embedded stylesheet */
xsltProcess(ops, style, params, cur, docs[i]);
xsltFreeStylesheet(cur);
cur = NULL;
}
}
goto done;
}
cur = xsltParseStylesheetDoc(style);
if (cur != NULL)
{
if (cur->errors != 0)
{
errorno = 5;
goto done;
}
if (cur->indent == 1) xmlIndentTreeOutput = 1;
else xmlIndentTreeOutput = 0;
}
else
{
xmlFreeDoc(style);
errorno = 5;
goto done;
}
}
/*
* run XSLT
*/
if ((cur != NULL) && (cur->errors == 0))
{
for (i=0; i<count; i++)
{
doc = NULL;
#ifdef LIBXML_HTML_ENABLED
if (ops->html) doc = htmlReadFile(docs[i], NULL, options);
else
#endif
{
doc = xmlReadFile((const char *) docs[i], NULL, options);
}
if (doc == NULL)
{
fprintf(stderr, "unable to parse %s\n", docs[i]);
errorno = 6;
continue;
}
xsltProcess(ops, doc, params, cur, docs[i]);
}
if (count == 0)
{
/* stdin */
doc = NULL;
#ifdef LIBXML_HTML_ENABLED
if (ops->html) doc = htmlParseFile("-", NULL);
else
#endif
doc = xmlReadFile("-", NULL, options);
xsltProcess(ops, doc, params, cur, "-");
}
}
done:
/*
* Clean up
*/
if (cur != NULL) xsltFreeStylesheet(cur);
return(errorno);
}
|