1 | /* copyright (C) 2001 Sun Microsystems */ |
---|
2 | |
---|
3 | /* |
---|
4 | * This library is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU Lesser General Public |
---|
6 | * License as published by the Free Software Foundation; either |
---|
7 | * version 2.1 of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This library is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | * Lesser General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU Lesser General Public |
---|
15 | * License along with this library; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | #include <locale.h> |
---|
21 | #include <libintl.h> |
---|
22 | #include <libxslt/xslt.h> |
---|
23 | #include <libxslt/xsltInternals.h> |
---|
24 | #include <libxslt/transform.h> |
---|
25 | #include <libxslt/xsltutils.h> |
---|
26 | #include <libxml/xmlmemory.h> |
---|
27 | #include <libxml/DOCBparser.h> |
---|
28 | #include <libxml/xinclude.h> |
---|
29 | #include <scrollkeeper.h> |
---|
30 | #include <string.h> |
---|
31 | #include <stdlib.h> |
---|
32 | #include <unistd.h> |
---|
33 | #include <errno.h> |
---|
34 | #include <sys/stat.h> |
---|
35 | |
---|
36 | extern int xmlLoadExtDtdDefaultValue; |
---|
37 | |
---|
38 | int apply_stylesheets (char *input_file, char *type, int stylesheet_num, |
---|
39 | char **stylesheets, char **outputs, char outputprefs) |
---|
40 | { |
---|
41 | xmlDocPtr res; |
---|
42 | docbDocPtr doc; |
---|
43 | xsltStylesheetPtr cur; |
---|
44 | int i; |
---|
45 | int returnval = 1; |
---|
46 | FILE *fid; |
---|
47 | struct stat buf; |
---|
48 | #ifndef SOLARIS |
---|
49 | char line[1024], *start, *end; |
---|
50 | int num; |
---|
51 | FILE *res_fid; |
---|
52 | char *doctype; |
---|
53 | char command[1024]; |
---|
54 | char temp1[PATHLEN], temp2[PATHLEN], errors[PATHLEN]; |
---|
55 | int temp1_fd, temp2_fd, errors_fd; |
---|
56 | #endif |
---|
57 | |
---|
58 | if (input_file == NULL || |
---|
59 | stylesheets == NULL || |
---|
60 | outputs == NULL) { |
---|
61 | return 0; |
---|
62 | } |
---|
63 | |
---|
64 | xmlSubstituteEntitiesDefault(1); |
---|
65 | xmlLoadExtDtdDefaultValue = 1; |
---|
66 | xmlIndentTreeOutput = 1; |
---|
67 | |
---|
68 | if (strcmp(type, "sgml") == 0) { |
---|
69 | |
---|
70 | #ifdef SOLARIS |
---|
71 | doc = docbParseFile(input_file, NULL); |
---|
72 | #else |
---|
73 | snprintf(temp1, PATHLEN, SCROLLKEEPER_STATEDIR "/tmp/scrollkeeper-extract-1.xml.XXXXXX"); |
---|
74 | snprintf(temp2, PATHLEN, SCROLLKEEPER_STATEDIR "/tmp/scrollkeeper-extract-2.xml.XXXXXX"); |
---|
75 | snprintf(errors, PATHLEN, SCROLLKEEPER_STATEDIR "/tmp/scrollkeeper-extract-errors.XXXXXX"); |
---|
76 | |
---|
77 | temp1_fd = mkstemp(temp1); |
---|
78 | printf ("%s\n", temp1); |
---|
79 | if (temp1_fd == -1) { |
---|
80 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Cannot create temporary file: %s : %s\n"),temp1, strerror(errno)); |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|
84 | errors_fd = mkstemp(errors); |
---|
85 | if (errors_fd == -1) { |
---|
86 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Cannot create temporary file: %s : %s\n"),errors, strerror(errno)); |
---|
87 | return 0; |
---|
88 | } |
---|
89 | close(errors_fd); |
---|
90 | |
---|
91 | snprintf(command, 1024, "sgml2xml -xlower -f%s %s > %s", errors, input_file, temp1); |
---|
92 | system(command); |
---|
93 | |
---|
94 | unlink(errors); |
---|
95 | |
---|
96 | fid = fopen(input_file, "r"); |
---|
97 | if (fid == NULL) { |
---|
98 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Cannot read file: %s : %s\n"),input_file, strerror(errno)); |
---|
99 | close(temp1_fd); |
---|
100 | return 0; |
---|
101 | } |
---|
102 | |
---|
103 | doctype = NULL; |
---|
104 | while(fgets(line, 1024, fid) != NULL) { |
---|
105 | if ((start = strstr(line, "DOCTYPE")) != NULL) { |
---|
106 | start += 7; |
---|
107 | while (*start == ' ') { |
---|
108 | start++; |
---|
109 | } |
---|
110 | end = start; |
---|
111 | while (*end != ' ') { |
---|
112 | end++; |
---|
113 | } |
---|
114 | doctype = malloc(end-start+1); |
---|
115 | check_ptr(doctype, ""); |
---|
116 | strncpy(doctype, start, end-start); |
---|
117 | doctype[end-start] = '\0'; |
---|
118 | break; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | fclose (fid); |
---|
123 | |
---|
124 | if (doctype == NULL) { |
---|
125 | close(temp1_fd); |
---|
126 | unlink(temp1); |
---|
127 | return 0; |
---|
128 | } |
---|
129 | |
---|
130 | temp2_fd = mkstemp(temp2); |
---|
131 | if (temp2_fd == -1) { |
---|
132 | close(temp1_fd); |
---|
133 | unlink(temp1); |
---|
134 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Cannot create temporary file: %s : %s\n"),temp2, strerror(errno)); |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | fid = fdopen(temp1_fd, "r"); |
---|
139 | res_fid = fdopen(temp2_fd, "w"); |
---|
140 | if (fid == NULL || res_fid == NULL) { |
---|
141 | close(temp1_fd); |
---|
142 | unlink(temp1); |
---|
143 | close(temp2_fd); |
---|
144 | unlink(temp2); |
---|
145 | if (fid) |
---|
146 | fclose (fid); |
---|
147 | if (res_fid) |
---|
148 | fclose (res_fid); |
---|
149 | return 0; |
---|
150 | } |
---|
151 | |
---|
152 | num = 0; |
---|
153 | while (fgets(line, 1024, fid) != NULL) { |
---|
154 | fputs(line, res_fid); |
---|
155 | if (num == 0) { |
---|
156 | num = 1; |
---|
157 | fprintf(res_fid, "<!DOCTYPE %s PUBLIC " DB_PUBLIC_ID DB_SYSTEM_ID ">\n", doctype); |
---|
158 | } |
---|
159 | } |
---|
160 | fclose(fid); |
---|
161 | fclose(res_fid); |
---|
162 | |
---|
163 | doc = xmlParseFile(temp2); |
---|
164 | unlink(temp1); |
---|
165 | unlink(temp2); |
---|
166 | if (doc == NULL) { |
---|
167 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Document is not well-formed XML: %s\n"), temp2); |
---|
168 | return 0; |
---|
169 | } |
---|
170 | #endif /*SOLARIS */ |
---|
171 | |
---|
172 | } |
---|
173 | else if (strcmp(type, "xml") == 0) { |
---|
174 | if (stat(input_file, &buf) == -1) { |
---|
175 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Cannot stat file: %s : %s\n"), input_file, strerror(errno)); |
---|
176 | return 0; |
---|
177 | } |
---|
178 | |
---|
179 | doc = xmlParseFile(input_file); |
---|
180 | xmlXIncludeProcess(doc); |
---|
181 | if (doc == NULL) { |
---|
182 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Document is not well-formed XML: %s\n"), input_file); |
---|
183 | return 0; |
---|
184 | } |
---|
185 | } else { |
---|
186 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Cannot apply stylesheet to document of type: %s\n"), type); |
---|
187 | return 0; |
---|
188 | } |
---|
189 | |
---|
190 | for(i = 0; i < stylesheet_num; i++) { |
---|
191 | if (stylesheets[i] == NULL || |
---|
192 | outputs[i] == NULL) { |
---|
193 | continue; |
---|
194 | } |
---|
195 | |
---|
196 | fid = fopen(outputs[i], "w"); |
---|
197 | if (fid == NULL) { |
---|
198 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Cannot open output file: %s : %s \n"), outputs[i], strerror(errno)); |
---|
199 | returnval = 0; |
---|
200 | continue; |
---|
201 | } |
---|
202 | |
---|
203 | if (stat(stylesheets[i], &buf) == -1) { |
---|
204 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "(apply_stylesheets)", _("Cannot stat stylesheet file: %s : %s\n"), stylesheets[i], strerror(errno)); |
---|
205 | returnval = 0; |
---|
206 | fclose(fid); |
---|
207 | continue; |
---|
208 | } |
---|
209 | |
---|
210 | cur = xsltParseStylesheetFile((const xmlChar *)(stylesheets[i])); |
---|
211 | res = xsltApplyStylesheet(cur, doc, NULL); |
---|
212 | xsltSaveResultToFile(fid, res, cur); |
---|
213 | xmlFreeDoc(res); |
---|
214 | xsltFreeStylesheet(cur); |
---|
215 | fclose(fid); |
---|
216 | } |
---|
217 | |
---|
218 | xmlFreeDoc(doc); |
---|
219 | |
---|
220 | xmlCleanupParser(); |
---|
221 | xmlMemoryDump(); |
---|
222 | |
---|
223 | return returnval; |
---|
224 | } |
---|