1 | /*
|
---|
2 | * libxslt_pipes.c: a program for performing a series of XSLT
|
---|
3 | * transformations
|
---|
4 | *
|
---|
5 | * Writen by Panos Louridas, based on libxslt_tutorial.c by John Fleck.
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * Sun GPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
25 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
26 | * the General Public License version 2 (GPLv2) at this time for any software where
|
---|
27 | * a choice of GPL license versions is made available with the language indicating
|
---|
28 | * that GPLv2 or any later version may be used, or where a choice of which version
|
---|
29 | * of the GPL is applied is otherwise unspecified.
|
---|
30 | */
|
---|
31 |
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <string.h>
|
---|
34 | #include <stdlib.h>
|
---|
35 |
|
---|
36 | #include <libxslt/transform.h>
|
---|
37 | #include <libxslt/xsltutils.h>
|
---|
38 |
|
---|
39 | extern int xmlLoadExtDtdDefaultValue;
|
---|
40 |
|
---|
41 | static void usage(const char *name) {
|
---|
42 | printf("Usage: %s [options] stylesheet [stylesheet ...] file [file ...]\n",
|
---|
43 | name);
|
---|
44 | printf(" --out file: send output to file\n");
|
---|
45 | printf(" --param name value: pass a (parameter,value) pair\n");
|
---|
46 | }
|
---|
47 |
|
---|
48 | int main(int argc, char **argv) {
|
---|
49 | int arg_indx;
|
---|
50 | const char *params[16 + 1];
|
---|
51 | int params_indx = 0;
|
---|
52 | int stylesheet_indx = 0;
|
---|
53 | int file_indx = 0;
|
---|
54 | int i, j, k;
|
---|
55 | FILE *output_file = stdout;
|
---|
56 | xsltStylesheetPtr *stylesheets =
|
---|
57 | (xsltStylesheetPtr *) calloc(argc, sizeof(xsltStylesheetPtr));
|
---|
58 | xmlDocPtr *files = (xmlDocPtr *) calloc(argc, sizeof(xmlDocPtr));
|
---|
59 | xmlDocPtr doc, res;
|
---|
60 | int return_value = 0;
|
---|
61 |
|
---|
62 | if (argc <= 1) {
|
---|
63 | usage(argv[0]);
|
---|
64 | return_value = 1;
|
---|
65 | goto finish;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* Collect arguments */
|
---|
69 | for (arg_indx = 1; arg_indx < argc; arg_indx++) {
|
---|
70 | if (argv[arg_indx][0] != '-')
|
---|
71 | break;
|
---|
72 | if ((!strcmp(argv[arg_indx], "-param"))
|
---|
73 | || (!strcmp(argv[arg_indx], "--param"))) {
|
---|
74 | arg_indx++;
|
---|
75 | params[params_indx++] = argv[arg_indx++];
|
---|
76 | params[params_indx++] = argv[arg_indx];
|
---|
77 | if (params_indx >= 16) {
|
---|
78 | fprintf(stderr, "too many params\n");
|
---|
79 | return_value = 1;
|
---|
80 | goto finish;
|
---|
81 | }
|
---|
82 | } else if ((!strcmp(argv[arg_indx], "-o"))
|
---|
83 | || (!strcmp(argv[arg_indx], "--out"))) {
|
---|
84 | arg_indx++;
|
---|
85 | output_file = fopen(argv[arg_indx], "w");
|
---|
86 | } else {
|
---|
87 | fprintf(stderr, "Unknown option %s\n", argv[arg_indx]);
|
---|
88 | usage(argv[0]);
|
---|
89 | return_value = 1;
|
---|
90 | goto finish;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | params[params_indx] = 0;
|
---|
94 |
|
---|
95 | /* Collect and parse stylesheets and files to be transformed */
|
---|
96 | for (; arg_indx < argc; arg_indx++) {
|
---|
97 | char *argument =
|
---|
98 | (char *) malloc(sizeof(char) * (strlen(argv[arg_indx]) + 1));
|
---|
99 | strcpy(argument, argv[arg_indx]);
|
---|
100 | if (strtok(argument, ".")) {
|
---|
101 | char *suffix = strtok(0, ".");
|
---|
102 | if (suffix && !strcmp(suffix, "xsl")) {
|
---|
103 | stylesheets[stylesheet_indx++] =
|
---|
104 | xsltParseStylesheetFile((const xmlChar *)argv[arg_indx]);;
|
---|
105 | } else {
|
---|
106 | files[file_indx++] = xmlParseFile(argv[arg_indx]);
|
---|
107 | }
|
---|
108 | } else {
|
---|
109 | files[file_indx++] = xmlParseFile(argv[arg_indx]);
|
---|
110 | }
|
---|
111 | free(argument);
|
---|
112 | }
|
---|
113 |
|
---|
114 | xmlSubstituteEntitiesDefault(1);
|
---|
115 | xmlLoadExtDtdDefaultValue = 1;
|
---|
116 |
|
---|
117 | /* Process files */
|
---|
118 | for (i = 0; files[i]; i++) {
|
---|
119 | doc = files[i];
|
---|
120 | res = doc;
|
---|
121 | for (j = 0; stylesheets[j]; j++) {
|
---|
122 | res = xsltApplyStylesheet(stylesheets[j], doc, params);
|
---|
123 | xmlFreeDoc(doc);
|
---|
124 | doc = res;
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (stylesheets[0]) {
|
---|
128 | xsltSaveResultToFile(output_file, res, stylesheets[j-1]);
|
---|
129 | } else {
|
---|
130 | xmlDocDump(output_file, res);
|
---|
131 | }
|
---|
132 | xmlFreeDoc(res);
|
---|
133 | }
|
---|
134 |
|
---|
135 | fclose(output_file);
|
---|
136 |
|
---|
137 | for (k = 0; stylesheets[k]; k++) {
|
---|
138 | xsltFreeStylesheet(stylesheets[k]);
|
---|
139 | }
|
---|
140 |
|
---|
141 | xsltCleanupGlobals();
|
---|
142 | xmlCleanupParser();
|
---|
143 |
|
---|
144 | finish:
|
---|
145 | free(stylesheets);
|
---|
146 | free(files);
|
---|
147 | return(return_value);
|
---|
148 | }
|
---|