File: xslt.c

package info (click to toggle)
codelite 17.0.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 136,384 kB
  • sloc: cpp: 491,550; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 805; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (355 lines) | stat: -rw-r--r-- 10,138 bytes parent folder | download | duplicates (5)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
*
*   Copyright (c) 2016, Masatake YAMATO
*   Copyright (c) 2016, Red Hat, K.K.
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License version 2 or (at your option) any later version.
*
*   This module contains functions for generating tags for xslt 1.0.
*   Reference: https://www.w3.org/1999/11/xslt10.dtd
*
*/

#include "general.h"	/* must always come first */

#include <string.h>

#include "entry.h"
#include "options.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
#include "selectors.h"
#include "xml.h"

typedef enum {
	K_STYLESHEET,
	K_PARAMETER,
	K_MATCHED_TEMPLATE,
	K_NAMED_TEMPLATE,
	K_VARIABLE,
	/* TODO: KEY, DECIMAL-FORMAT, ATTRIBUTE, ATTRIBUTE-SET, ... */
} xsltKind;

typedef enum {
	R_STYLESHEET_IMPORTED,
	R_STYLESHEET_INCLUDED,
} xsltStylesheetRole;
static roleDefinition XsltStylesheetRoles [] = {
	{ true, "imported", "imported" },
	{ true, "included", "included" },
};

typedef enum {
	R_PARAMETER_BOUND,
} xsltParameterRole;
static roleDefinition XsltParameterRoles [] = {
	{ true, "bound", "bound to value" },
};

typedef enum {
	R_MATCHED_TEMPLATE_APPLIED,
} xsltMatchedTemplateRole;
static roleDefinition XsltMatchedTemplateRoles [] = {
	{ true, "applied", "applied" },
};

typedef enum {
	R_NAMED_TEMPLATE_CALLED,
} xsltNamedTemplateRole;
static roleDefinition XsltNamedTemplateRoles [] = {
	{ true, "called", "called" },
};


static kindDefinition XsltKinds [] = {
	{ true,  's', "stylesheet", "stylesheets",
	  .referenceOnly = true, ATTACH_ROLES (XsltStylesheetRoles) },
	{ true,  'p', "parameter", "parameters",
	  .referenceOnly = false, ATTACH_ROLES (XsltParameterRoles) },
	{ true,  'm', "matchedTemplate", "matched template",
	  .referenceOnly = false, ATTACH_ROLES (XsltMatchedTemplateRoles) },
	{ true,  'n', "namedTemplate",   "named template",
	  .referenceOnly = false, ATTACH_ROLES (XsltNamedTemplateRoles) },
	{ true,  'v', "variable", "variables" },
};

static void makeTagRecursivelyWithVersionVerification (xmlNode *node,
						       const char *xpath,
						       const tagXpathRecurSpec *spec,
						       xmlXPathContext *ctx,
						       void *userData);
static void makeTagRecursively (xmlNode *node,
				const char *xpath,
				const tagXpathRecurSpec *spec,
				xmlXPathContext *ctx,
				void *userData);

static void makeTagWithProvidingScope (xmlNode *node,
				       const char *xpath,
				       const tagXpathMakeTagSpec *spec,
				       struct sTagEntryInfo *tag,
				       void *userData);

enum xsltXpathTable {
	TABLE_MAIN,
	TABLE_STYLESHEET,
	TABLE_VERSION_VERIFY,
	TABLE_TEMPLATE,
	TABLE_APPLIED_TEMPLATES,
	TABLE_CALL_TEMPLATE,
	TABLE_PARAM,
	TABLE_VARIABLE,
	TABLE_WITH_PARAM,
	TABLE_CALL_TEMPLATE_INTERNAL,
};

#define xsltXpathTemplateTableEntry(PREFIX)				\
	{ PREFIX "/*[local-name()='template']",				\
	  LXPATH_TABLE_DO_RECUR,					\
	  { .recurSpec = { makeTagRecursively, TABLE_TEMPLATE} }}

#define xsltXpathVariableTableEntry(PREFIX)				\
	{ PREFIX "/*[local-name()='variable']",				\
	  LXPATH_TABLE_DO_RECUR,					\
	  { .recurSpec = { makeTagRecursively, TABLE_VARIABLE} }}

#define xsltXpathParamTableEntry(PREFIX)			\
	{ PREFIX "/*[local-name()='param']",			\
	  LXPATH_TABLE_DO_RECUR,				\
	  { .recurSpec = { makeTagRecursively, TABLE_PARAM} }}

#define xsltXpathWithParamTableEntry(PREFIX)				\
	{ PREFIX "/*[local-name()='with-param']",			\
	  LXPATH_TABLE_DO_RECUR,					\
	  { .recurSpec = { makeTagRecursively, TABLE_WITH_PARAM} }}	\

static tagXpathTable xsltXpathMainTable[] = {
	{ "(/*[local-name()='stylesheet']|/*[local-name()='transform'])",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { makeTagRecursivelyWithVersionVerification } }},
};

static tagXpathTable xsltXpathStylesheetTable[] = {
	{ "./*[local-name()='import']/@href",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_STYLESHEET, R_STYLESHEET_IMPORTED} }},
	{ "./*[local-name()='include']/@href",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_STYLESHEET, R_STYLESHEET_INCLUDED} }},

	xsltXpathTemplateTableEntry ("."),
	xsltXpathVariableTableEntry ("."),
	xsltXpathParamTableEntry    ("."),
};

static tagXpathTable xsltXpathTemplateTable[] = {
	{ "./@match",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_MATCHED_TEMPLATE, ROLE_DEFINITION_INDEX,
			    makeTagWithProvidingScope } }},
	{ "./@name",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_NAMED_TEMPLATE, ROLE_DEFINITION_INDEX,
			    makeTagWithProvidingScope } }},
	{ ".",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { makeTagRecursively, TABLE_CALL_TEMPLATE_INTERNAL } }},
};

static tagXpathTable xsltXpathAppliedTemplatesTable [] = {
	{ "./@select",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_MATCHED_TEMPLATE, R_MATCHED_TEMPLATE_APPLIED,
			    makeTagWithProvidingScope } }},
	xsltXpathWithParamTableEntry("."),
};

static tagXpathTable xsltXpathCallTemplateTable [] = {
	{ "./@name",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_NAMED_TEMPLATE, R_NAMED_TEMPLATE_CALLED,
			    makeTagWithProvidingScope } }},
	xsltXpathWithParamTableEntry("."),
};

static tagXpathTable xsltXpathParamTable [] = {
	{ "./@name",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_PARAMETER, ROLE_DEFINITION_INDEX,
			    makeTagWithProvidingScope} }},
	xsltXpathTemplateTableEntry ("."),
};

static tagXpathTable xsltXpathVariableTable [] = {
	{ "./@name",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_VARIABLE, ROLE_DEFINITION_INDEX,
			    makeTagWithProvidingScope} }},
	xsltXpathTemplateTableEntry ("."),
};

static tagXpathTable xsltXpathWithParamTable [] = {
	{ "./@name",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = {K_PARAMETER, R_PARAMETER_BOUND,
			    makeTagWithProvidingScope} }},
	xsltXpathTemplateTableEntry ("."),
};

static tagXpathTable xsltXpathTemplateInternalTable [] = {
	{ "./*[local-name()='apply-templates']",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { makeTagRecursively, TABLE_APPLIED_TEMPLATES } }},
	{ "./*[local-name()='call-template']",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { makeTagRecursively, TABLE_CALL_TEMPLATE } }},
	xsltXpathVariableTableEntry ("."),
	xsltXpathParamTableEntry ("."),
	{ "./node()[not ("
	  "local-name()='apply-templates'"
	  " or "
	  "local-name()='call-template'"
	  " or "
	  "local-name()='variable'"
	  " or "
	  "local-name()='param'"
	  ")]",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { makeTagRecursively, TABLE_CALL_TEMPLATE_INTERNAL } }},
};

static void verifyVersion (xmlNode *node,
			   const char *xpath CTAGS_ATTR_UNUSED,
			   const tagXpathRecurSpec *spec CTAGS_ATTR_UNUSED,
			   xmlXPathContext *ctx CTAGS_ATTR_UNUSED,
			   void *userData)
{
	bool *acceptable = userData;
	char *version = (char *)xmlNodeGetContent (node);

	if (version)
	{
		if (strcmp (version, "1.0") == 0)
		{
			verbose ("xslt: accept version: %s\n", version);
			*acceptable = true;
		}
		else
			verbose ("xslt: unsupported version: %s\n", version);

		eFree (version);
	}
	else
		verbose ("xslt: version unknown\n");
}

static tagXpathTable xsltXpathVersionVerifyTable [] = {
	{ "./@version",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { verifyVersion } }},
};


static tagXpathTableTable xsltXpathTableTable[] = {
	[TABLE_MAIN]              = { ARRAY_AND_SIZE (xsltXpathMainTable) },
	[TABLE_STYLESHEET]        = { ARRAY_AND_SIZE (xsltXpathStylesheetTable) },
	[TABLE_VERSION_VERIFY]    = { ARRAY_AND_SIZE (xsltXpathVersionVerifyTable) },
	[TABLE_TEMPLATE]          = { ARRAY_AND_SIZE (xsltXpathTemplateTable) },
	[TABLE_APPLIED_TEMPLATES] = { ARRAY_AND_SIZE (xsltXpathAppliedTemplatesTable) },
	[TABLE_CALL_TEMPLATE]     = { ARRAY_AND_SIZE (xsltXpathCallTemplateTable) },
	[TABLE_PARAM]             = { ARRAY_AND_SIZE (xsltXpathParamTable) },
	[TABLE_VARIABLE]          = { ARRAY_AND_SIZE (xsltXpathVariableTable) },
	[TABLE_WITH_PARAM]        = { ARRAY_AND_SIZE (xsltXpathWithParamTable) },
	[TABLE_CALL_TEMPLATE_INTERNAL] = { ARRAY_AND_SIZE (xsltXpathTemplateInternalTable) },
};


static void makeTagRecursivelyWithVersionVerification (xmlNode *node,
						       const char *xpath CTAGS_ATTR_UNUSED,
						       const tagXpathRecurSpec *spec CTAGS_ATTR_UNUSED,
						       xmlXPathContext *ctx,
						       void *userData)
{
	bool acceptable = false;
	int backup;

	findXMLTags (ctx, node, TABLE_VERSION_VERIFY, &acceptable);
	if (!acceptable)
		return;

	backup = *(int *)userData;
	findXMLTags (ctx, node, TABLE_STYLESHEET, userData);

	*(int *)userData = backup;
}

static void makeTagRecursively (xmlNode *node,
				const char *xpath CTAGS_ATTR_UNUSED,
				const tagXpathRecurSpec *spec,
				xmlXPathContext *ctx,
				void *userData)
{
	int backup = *(int *)userData;

	findXMLTags (ctx, node, spec->nextTable, userData);

	*(int *)userData = backup;
}

static void makeTagWithProvidingScope (xmlNode *node CTAGS_ATTR_UNUSED,
				       const char *xpath CTAGS_ATTR_UNUSED,
				       const tagXpathMakeTagSpec *spec CTAGS_ATTR_UNUSED,
				       struct sTagEntryInfo *tag,
				       void *userData)
{
	int *index = (int *)userData;

	tag->extensionFields.scopeIndex = *index;
	*index = makeTagEntry (tag);
}

static void
findXsltTags (void)
{
	scheduleRunningBaseparser (RUN_DEFAULT_SUBPARSERS);
}

static void
runXPathEngine(xmlSubparser *s,
			   xmlXPathContext *ctx, xmlNode *root)
{
	int corkIndex = CORK_NIL;

	findXMLTags (ctx, root, TABLE_MAIN, &corkIndex);
}

static xmlSubparser xsltSubparser = {
	.subparser = {
		.direction = SUBPARSER_BI_DIRECTION,
	},
	.runXPathEngine = runXPathEngine,
};

extern parserDefinition*
XsltParser (void)
{
	static const char *const extensions [] = { "xsl", "xslt", NULL };
	parserDefinition* const def = parserNew ("XSLT");
	static parserDependency dependencies [] = {
		[0] = { DEPTYPE_SUBPARSER, "XML", &xsltSubparser },
	};

	def->kindTable         = XsltKinds;
	def->kindCount     = ARRAY_SIZE (XsltKinds);
	def->extensions    = extensions;
	def->parser        = findXsltTags;
	def->tagXpathTableTable = xsltXpathTableTable;
	def->tagXpathTableCount = ARRAY_SIZE (xsltXpathTableTable);
	def->useCork = CORK_QUEUE;
	def->dependencies = dependencies;
	def->dependencyCount = ARRAY_SIZE (dependencies);

	return def;
}