Debian Codegraph @

src/xml_pyx.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* $Id: xml_pyx.c,v 1.9 2005/03/12 03:24:23 mgrouch Exp $ */
2
3 /**
4 * Based on xmln from pyxie project
5 *
6 * The PYX format is a line-oriented representation of
7 * XML documents that is derived from the SGML ESIS format.
8 * (see ESIS - ISO 8879 Element Structure Information Set spec,
9 * ISO/IEC JTC1/SC18/WG8 N931 (ESIS))
10 *
11 * A non-validating, ESIS generating tool
12 * ESIS Generation by Sean Mc Grath http://www.digitome.com/sean.html
13 */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include <libxml/parser.h>
20 #include <libxml/parserInternals.h>
21
22 #include "xmlstar.h"
23
24 /**
25 * Output newline and tab characters as escapes
26 * Required both for attribute values and character data (#PCDATA)
27 */
28 static void
29 SanitizeData(const xmlChar *s, int len)
30 {
31 while (len--)
32 {
33 switch (*s)
34 {
35 case 10:
36 printf("\\n");
37 break;
38 case 13:
39 break;
40 case 9:
41 printf ("\\t");
42 break;
43 case '\\':
44 printf ("\\\\");
45 break;
46 default:
47 putchar (*s);
48 }
49 s++;
50 }
51 }
52
53 static void
54 print_qname(const xmlChar *prefix, const xmlChar *localname)
55 {
56 if (prefix)
57 printf("%s:", prefix);
58 printf("%s", localname);
59 }
60
61 int
62 CompareAttributes(const void *a1,const void *a2)
63 {
64 typedef xmlChar const *const xmlCStr;
65 xmlCStr *attr1 = *(xmlCStr* const* const)a1;
66 xmlCStr *attr2 = *(xmlCStr* const* const)a2;
67 return xmlStrcmp(*attr1, *attr2);
68 }
69
70 void
71 pyxStartElement (void * ctx,
72 const xmlChar * localname,
73 const xmlChar * prefix,
74 const xmlChar * URI,
75 int nb_namespaces,
76 const xmlChar ** namespaces,
77 int nb_attributes,
78 int nb_defaulted,
79 const xmlChar ** attributes)
80 {
81 int i;
82 /* DON'T modify the attributes array, ever. */
83 const xmlChar*** atts = &attributes;
84
85 fprintf(stdout,"(");
86 print_qname(prefix, localname);
87 fprintf(stdout, "\n");
88
89 if (nb_attributes > 1) {
90 atts = calloc(nb_attributes, sizeof(*atts));
91 for (i = 0; i < nb_attributes; ++i) {
92 atts[i] = &(attributes[i * 5]);
93 }
94 /* Sort the pairs based on the name part of the pair */
95 qsort (atts,
96 nb_attributes,
97 sizeof(*atts),
98 CompareAttributes);
99 }
100
101 for (i = 0; i < nb_namespaces; i++) {
102 int aidx = i * 2;
103 const xmlChar
104 *prefix = namespaces[aidx],
105 *uri = namespaces[aidx+1];
106 /* namespace definitions take the form xmlns:prefix=uri*/
107 putchar('A');
108 if (xmlStrlen(prefix) > 0)
109 print_qname(BAD_CAST "xmlns", prefix);
110 else
111 fputs("xmlns", stdout);
112 putchar(' ');
113 SanitizeData(uri, xmlStrlen(uri));
114 putchar('\n');
115 }
116
117 for (i = 0; i < nb_attributes; i++) {
118 const xmlChar *localname = atts[i][0],
119 *prefix = atts[i][1],
120 /* *nsURI = attributes[atts][2], */
121 *valueBegin = atts[i][3],
122 *valueEnd = atts[i][4];
123 int valueLen = valueEnd - valueBegin;
124
125 /* Attribute Name */
126 putchar('A');
127 print_qname(prefix, localname);
128 putchar(' ');
129 /* value - can contain literal "\n" so escape */
130 SanitizeData(valueBegin, valueLen);
131 putchar('\n');
132 }
133
134 /* we did only allocate memory if nb_attributes > 1 */
135 if (nb_attributes > 1) free(atts);
136 }
137
138 void
139 pyxEndElement(void *userData, const xmlChar *localname, const xmlChar *prefix,
140 const xmlChar *URI)
141 {
142 fprintf(stdout,")");
143 print_qname(prefix, localname);
144 putchar('\n');
145 }
146
147 void
148 pyxCharacterData(void *userData, const xmlChar *s, int len)
149 {
150 fprintf(stdout, "-");
151 SanitizeData(s, len);
152 putchar('\n');
153 }
154
155 void
156 pyxProcessingInstruction(void *userData,
157 const xmlChar *target,
158 const xmlChar *data)
159 {
160 fprintf(stdout,"?%s ",target);
161 SanitizeData(data, xmlStrlen(data));
162 fprintf(stdout,"\n");
163 }
164
165 void
166 pyxUnparsedEntityDeclHandler(void *userData,
167 const xmlChar *entityName,
168 const xmlChar *publicId,
169 const xmlChar *systemId,
170 const xmlChar *notationName)
171 {
172 fprintf(stdout, "U%s %s %s%s%s\n",
173 (char *)entityName, (char *)notationName, (char *)systemId,
174 (publicId == NULL? "": " "),
175 (publicId == NULL? "": (char *) publicId));
176 }
177
178 void
179 pyxNotationDeclHandler(void *userData,
180 const xmlChar *notationName,
181 const xmlChar *publicId,
182 const xmlChar *systemId)
183 {
184 fprintf(stdout, "N%s %s%s%s\n", (char*) notationName, (char*) systemId,
185 (publicId == NULL? "": " "),
186 (publicId == NULL? "": (const char*) publicId));
187 }
188
189 void
190 pyxExternalEntityReferenceHandler(void* userData,
191 const xmlChar *name)
192 {
193 const xmlChar *p = name;
194 fprintf (stdout, "&");
195 /* Up to space is the name of the referenced entity */
196 while (*p && (*p != ' ')) {
197 putchar (*p);
198 p++;
199 }
200 }
201
202 static void
203 pyxExternalSubsetHandler(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
204 const xmlChar *ExternalID, const xmlChar *SystemID)
205 {
206 fprintf(stdout, "D %s PUBLIC", name); /* TODO: re-check */
207 if (ExternalID == NULL)
208 fprintf(stdout, " ");
209 else
210 fprintf(stdout, " \"%s\"", ExternalID);
211 if (SystemID == NULL)
212 fprintf(stdout, "\n");
213 else
214 fprintf(stdout, " \"%s\"\n", SystemID);
215 }
216
217 static void
218 pyxCommentHandler(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
219 {
220 fprintf(stdout,"C");
221 SanitizeData(value, xmlStrlen(value));
222 fprintf(stdout,"\n");
223 }
224
225 static void
226 pyxCdataBlockHandler(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
227 {
228 fprintf(stdout,"[");
229 SanitizeData(value, len);
230 fprintf(stdout,"\n");
231 }
232
233 static void
234 pyxUsage(const char *argv0, exit_status status)
235 {
236 extern void fprint_pyx_usage(FILE* o, const char* argv0);
237 extern const char more_info[];
238 FILE *o = (status == EXIT_SUCCESS)? stdout : stderr;
239 fprint_pyx_usage(o, argv0);
240 fprintf(o, "%s", more_info);
241 exit(status);
242 }
243
244 static xmlSAXHandler pyxSAX;
245
246 static int
247 pyx_process_file(const char *filename)
248 {
249 int ret;
250 xmlParserCtxtPtr ctxt;
251
252 ctxt = xmlCreateFileParserCtxt(filename);
253 if (!ctxt) /* assume it failed because of filename */
254 return EXIT_BAD_FILE;
255
256 ctxt->sax = &pyxSAX;
257 ret = xmlParseDocument(ctxt);
258
259 ctxt->sax = NULL; /* don't try to free pyxSAX */
260 xmlFreeParserCtxt(ctxt);
261
262 return (ret == 0)? 0 : EXIT_LIB_ERROR;
263 }
264
265 int
266 pyxMain(int argc,const char *argv[])
267 {
268 int status = 0;
269
270 if ((argc > 2) &&
271 (
272 (strcmp(argv[2],"-h") == 0) ||
273 (strcmp(argv[2],"-H") == 0) ||
274 (strcmp(argv[2],"-Z") == 0) ||
275 (strcmp(argv[2],"-?") == 0) ||
276 (strcmp(argv[2],"--help") == 0)
277 ))
278 {
279 pyxUsage(argv[0], EXIT_SUCCESS);
280 }
281
282 xmlInitParser();
283 /* Establish Event Handlers */
284 pyxSAX.startElementNs = pyxStartElement;
285 pyxSAX.endElementNs = pyxEndElement;
286 pyxSAX.processingInstruction = pyxProcessingInstruction;
287 pyxSAX.characters = pyxCharacterData;
288 pyxSAX.notationDecl = pyxNotationDeclHandler;
289 pyxSAX.reference = pyxExternalEntityReferenceHandler;
290 pyxSAX.unparsedEntityDecl = pyxUnparsedEntityDeclHandler;
291 pyxSAX.externalSubset = pyxExternalSubsetHandler;
292 pyxSAX.comment = pyxCommentHandler;
293 pyxSAX.cdataBlock = pyxCdataBlockHandler;
294 pyxSAX.initialized = XML_SAX2_MAGIC;
295
296 if (argc == 2) {
297 status = pyx_process_file("-");
298 }
299 else {
300 argv++;
301 argc--;
302 for (++argv; argc>1; argc--,argv++) {
303 int ret = pyx_process_file(*argv);
304 if (ret != 0) status = ret;
305 }
306 }
307 xmlCleanupParser();
308 return status;
309 }