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
287
288
289
290
291
292
293
|
/* $Id: xml_pyx.c,v 1.9 2005/03/12 03:24:23 mgrouch Exp $ */
/**
* Based on xmln from pyxie project
*
* The PYX format is a line-oriented representation of
* XML documents that is derived from the SGML ESIS format.
* (see ESIS - ISO 8879 Element Structure Information Set spec,
* ISO/IEC JTC1/SC18/WG8 N931 (ESIS))
*
* A non-validating, ESIS generating tool
* ESIS Generation by Sean Mc Grath http://www.digitome.com/sean.html
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include "xmlstar.h"
/**
* Output newline and tab characters as escapes
* Required both for attribute values and character data (#PCDATA)
*/
static void
SanitizeData(const xmlChar *s, int len)
{
while (len--)
{
switch (*s)
{
case 10:
printf("\\n");
break;
case 13:
break;
case 9:
printf ("\\t");
break;
case '\\':
printf ("\\\\");
break;
default:
putchar (*s);
}
s++;
}
}
static void
print_qname(const xmlChar *prefix, const xmlChar *localname)
{
if (prefix)
printf("%s:", prefix);
printf("%s", localname);
}
int
CompareAttributes(const void *a1,const void *a2)
{
typedef xmlChar const *const xmlCStr;
xmlCStr *attr1 = a1, *attr2 = a2;
return xmlStrcmp(*attr1, *attr2);
}
void
pyxStartElement (void * ctx,
const xmlChar * localname,
const xmlChar * prefix,
const xmlChar * URI,
int nb_namespaces,
const xmlChar ** namespaces,
int nb_attributes,
int nb_defaulted,
const xmlChar ** attributes)
{
int i;
fprintf(stdout,"(");
print_qname(prefix, localname);
fprintf(stdout, "\n");
if (nb_attributes > 1)
/* Sort the pairs based on the name part of the pair */
qsort ((void *)attributes,
nb_attributes,
sizeof(xmlChar *)*5,
CompareAttributes);
for (i = 0; i < nb_namespaces; i++) {
int aidx = i * 2;
const xmlChar
*prefix = namespaces[aidx],
*uri = namespaces[aidx+1];
/* namespace definitions take the form xmlns:prefix=uri*/
putchar('A');
if (xmlStrlen(prefix) > 0)
print_qname(BAD_CAST "xmlns", prefix);
else
fputs("xmlns", stdout);
putchar(' ');
SanitizeData(uri, xmlStrlen(uri));
putchar('\n');
}
for (i = 0; i < nb_attributes; i++) {
int aidx = i * 5;
const xmlChar *localname = attributes[aidx],
*prefix = attributes[aidx+1],
/* *nsURI = attributes[aidx+2], */
*valueBegin = attributes[aidx+3],
*valueEnd = attributes[aidx+4];
int valueLen = valueEnd - valueBegin;
/* Attribute Name */
putchar('A');
print_qname(prefix, localname);
putchar(' ');
/* value - can contain literal "\n" so escape */
SanitizeData(valueBegin, valueLen);
putchar('\n');
}
}
void
pyxEndElement(void *userData, const xmlChar *localname, const xmlChar *prefix,
const xmlChar *URI)
{
fprintf(stdout,")");
print_qname(prefix, localname);
putchar('\n');
}
void
pyxCharacterData(void *userData, const xmlChar *s, int len)
{
fprintf(stdout, "-");
SanitizeData(s, len);
putchar('\n');
}
void
pyxProcessingInstruction(void *userData,
const xmlChar *target,
const xmlChar *data)
{
fprintf(stdout,"?%s ",target);
SanitizeData(data, xmlStrlen(data));
fprintf(stdout,"\n");
}
void
pyxUnparsedEntityDeclHandler(void *userData,
const xmlChar *entityName,
const xmlChar *publicId,
const xmlChar *systemId,
const xmlChar *notationName)
{
fprintf(stdout, "U%s %s %s%s%s\n",
(char *)entityName, (char *)notationName, (char *)systemId,
(publicId == NULL? "": " "),
(publicId == NULL? "": (char *) publicId));
}
void
pyxNotationDeclHandler(void *userData,
const xmlChar *notationName,
const xmlChar *publicId,
const xmlChar *systemId)
{
fprintf(stdout, "N%s %s%s%s\n", (char*) notationName, (char*) systemId,
(publicId == NULL? "": " "),
(publicId == NULL? "": (const char*) publicId));
}
void
pyxExternalEntityReferenceHandler(void* userData,
const xmlChar *name)
{
const xmlChar *p = name;
fprintf (stdout, "&");
/* Up to space is the name of the referenced entity */
while (*p && (*p != ' ')) {
putchar (*p);
p++;
}
}
static void
pyxExternalSubsetHandler(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
const xmlChar *ExternalID, const xmlChar *SystemID)
{
fprintf(stdout, "D %s PUBLIC", name); /* TODO: re-check */
if (ExternalID == NULL)
fprintf(stdout, " ");
else
fprintf(stdout, " \"%s\"", ExternalID);
if (SystemID == NULL)
fprintf(stdout, "\n");
else
fprintf(stdout, " \"%s\"\n", SystemID);
}
static void
pyxCommentHandler(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
{
fprintf(stdout,"C");
SanitizeData(value, xmlStrlen(value));
fprintf(stdout,"\n");
}
static void
pyxCdataBlockHandler(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
{
fprintf(stdout,"[");
SanitizeData(value, len);
fprintf(stdout,"\n");
}
static void
pyxUsage(const char *argv0, exit_status status)
{
extern void fprint_pyx_usage(FILE* o, const char* argv0);
extern const char more_info[];
FILE *o = (status == EXIT_SUCCESS)? stdout : stderr;
fprint_pyx_usage(o, argv0);
fprintf(o, "%s", more_info);
exit(status);
}
int
pyx_process_file(const char *filename)
{
int ret;
xmlParserCtxtPtr ctxt;
xmlInitParser();
ctxt = xmlCreateFileParserCtxt(filename);
memset(ctxt->sax, 0, sizeof(*ctxt->sax));
/* Establish Event Handlers */
ctxt->sax->initialized = XML_SAX2_MAGIC;
ctxt->sax->startElementNs = pyxStartElement;
ctxt->sax->endElementNs = pyxEndElement;
ctxt->sax->processingInstruction = pyxProcessingInstruction;
ctxt->sax->characters = pyxCharacterData;
ctxt->sax->notationDecl = pyxNotationDeclHandler;
ctxt->sax->reference = pyxExternalEntityReferenceHandler;
ctxt->sax->unparsedEntityDecl = pyxUnparsedEntityDeclHandler;
ctxt->sax->externalSubset = pyxExternalSubsetHandler;
ctxt->sax->comment = pyxCommentHandler;
ctxt->sax->cdataBlock = pyxCdataBlockHandler;
ret = xmlParseDocument(ctxt);
xmlFreeParserCtxt(ctxt);
xmlCleanupParser();
return ret;
}
int
pyxMain(int argc,const char *argv[])
{
int status = 0;
if ((argc > 2) &&
(
(strcmp(argv[2],"-h") == 0) ||
(strcmp(argv[2],"-H") == 0) ||
(strcmp(argv[2],"-Z") == 0) ||
(strcmp(argv[2],"-?") == 0) ||
(strcmp(argv[2],"--help") == 0)
))
{
pyxUsage(argv[0], EXIT_SUCCESS);
}
if (argc == 2) {
status = pyx_process_file("-");
}
else {
argv++;
argc--;
for (++argv; argc>1; argc--,argv++) {
int ret = pyx_process_file(*argv);
if (ret != 0) status = ret;
}
}
return status;
}
|