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