1 /** enc-templ.c  -  XML encoder template, used in code generation
2  ** Copyright (c) 2010 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
3  ** Copyright (c) 2006-2007 Symlabs (symlabs@symlabs.com), All Rights Reserved.
4  ** Author: Sampo Kellomaki (sampo@iki.fi)
5  ** This is confidential unpublished proprietary source code of the author.
6  ** NO WARRANTY, not even implied warranties. Contains trade secrets.
7  ** Distribution prohibited unless authorized in writing.
8  ** Licensed under Apache License 2.0, see file COPYING.
9  ** $Id: enc-templ.c,v 1.27 2007-10-05 22:24:28 sampo Exp $
10  **
11  ** 30.5.2006, created, Sampo Kellomaki (sampo@iki.fi)
12  ** 6.8.2006,  factored data structure walking to aux-templ.c --Sampo
13  ** 8.8.2006,  reworked namespace handling --Sampo
14  ** 26.8.2006, some CSE --Sampo
15  ** 23.9.2006, added WO logic --Sampo
16  ** 30.9.2007, improvements to WO encoding --Sampo
17  ** 8.2.2010,  better handling of schema order encoding of unknown namespace prefixes --Sampo
18  ** 27.10.2010, re-engineered namespace handling --Sampo
19  ** 24.11.2010, this code is sceduled for removal as el_order processing in WO encoder accomplishes the same result. --Sampo
20  **
21  ** N.B: wo=wire order (needed for exc-c14n), so=schema order
22  ** N.B2: This template is meant to be processed by pd/xsd2sg.pl. Beware
23  ** of special markers that xsd2sg.pl expects to find and understand.
24  **/
25 
26 #if 0
27 
28 #ifdef EL_NAME
29 #undef EL_NAME
30 #endif
31 #ifdef EL_STRUCT
32 #undef EL_STRUCT
33 #endif
34 #ifdef EL_NS
35 #undef EL_NS
36 #endif
37 #ifdef EL_TAG
38 #undef EL_TAG
39 #endif
40 
41 #define EL_NAME   ELNAME
42 #define EL_STRUCT ELSTRUCT
43 #define EL_NS     ELNS
44 #define EL_TAG    ELTAG
45 
46 #ifndef MAYBE_UNUSED
47 #define MAYBE_UNUSED   /* May appear as unused variable, but is needed by some generated code. */
48 #endif
49 
50 #if 0
51 #define ENC_LEN_DEBUG(x,tag,len) D("x=%p tag(%s) len=%d",(x),(tag),(len))
52 #define ENC_LEN_DEBUG_BASE char* enc_base = p
53 #else
54 #define ENC_LEN_DEBUG(x,tag,len)
55 #define ENC_LEN_DEBUG_BASE
56 #endif
57 
58 /* FUNC(TXLEN_SO_ELNAME) */
59 
60 /* Compute length of an element (and its subelements). The XML attributes
61  * and elements are processed in schema order. */
62 
63 /* Called by: */
64 int TXLEN_SO_ELNAME(struct zx_ctx* c, struct ELSTRUCT* x SIMPLELENNS)
65 {
66   struct zx_ns_s* pop_seen = 0;
67   struct zx_elem_s* se MAYBE_UNUSED;
68 #if 1 /* NORMALMODE */
69   /* *** in simple_elem case should output ns prefix from ns node. */
70   int len = sizeof("<ELNSCELTAG")-1 + 1 + sizeof("</ELNSCELTAG>")-1;
71   if (c->inc_ns_len)
72     len += zx_len_inc_ns(c, &pop_seen);
73 XMLNS_SO_LEN;
74 ATTRS_SO_LEN;
75 #else
76   /* root node has no begin tag */
77   int len = 0;
78 #endif
79 
80 ELEMS_SO_LEN;
81 
82   len += zx_len_so_common(c, &x->gg, &pop_seen);
83   zx_pop_seen(pop_seen);
84   ENC_LEN_DEBUG(x, "ELNSCELTAG", len);
85   return len;
86 }
87 
88 /* FUNC(TXENC_SO_ELNAME) */
89 
90 /* Render element into string. The XML attributes and elements are
91  * processed in schema order. This is what you generally want for
92  * rendering new data structure to a string. The wo pointers are not used. */
93 
94 /* Called by: */
95 char* TXENC_SO_ELNAME(struct zx_ctx* c, struct ELSTRUCT* x, char* p SIMPLETAGLENNS)
96 {
97   struct zx_elem_s* se MAYBE_UNUSED;
98   struct zx_attr_s* attr MAYBE_UNUSED;
99   struct zx_ns_s* pop_seen = 0;
100   ENC_LEN_DEBUG_BASE;
101 #if 1 /* NORMALMODE */
102   /* *** in simple_elem case should output ns prefix from ns node. */
103   ZX_OUT_TAG(p, "<ELNSCELTAG");
104   if (c->inc_ns)
105     zx_add_inc_ns(c, &pop_seen);
106 XMLNS_SO_SEE;
107   zx_see_attr_ns(c, x->gg.attr, &pop_seen);
108   p = zx_enc_seen(p, pop_seen);
109 ATTRS_SO_ENC;
110   for (attr = x->gg.attr; attr; attr = (struct zx_attr_s*)attr->g.n)
111     if (attr->g.tok == ZX_TOK_ATTR_NOT_FOUND)
112       p = zx_attr_wo_enc(p, attr);
113   ZX_OUT_CH(p, '>');
114 #else
115   /* root node has no begin tag */
116 #endif
117 
118 ELEMS_SO_ENC;
119   p = zx_enc_so_unknown_elems_and_content(c, p, &x->gg);
120 
121 #if 1 /* NORMALMODE */
122   ZX_OUT_CLOSE_TAG(p, "</ELNSCELTAG>");
123   zx_pop_seen(pop_seen);
124 #else
125   /* root node has no end tag either */
126 #endif
127   ENC_LEN_DEBUG(x, "ELNSCELTAG", p-enc_base);
128   return p;
129 }
130 
131 /* FUNC(TXEASY_ENC_SO_ELNAME) */
132 
133 /* Called by: */
134 struct zx_str* TXEASY_ENC_SO_ELNAME(struct zx_ctx* c, struct ELSTRUCT* x SIMPLETAGLENNS)
135 {
136   int len;
137   char* buf;
138   c->ns_tab = ZX_ALLOC(c, sizeof(TXns_tab));      /* *** do we really need to make a copy? Do we still keep list of aliases? */
139   memcpy(c->ns_tab, TXns_tab, sizeof(TXns_tab));
140   len = TXLEN_SO_ELNAME(c, x SIMPLELENNSARG);
141   buf = ZX_ALLOC(c, len+1);
142   return zx_easy_enc_common(c, TXENC_SO_ELNAME(c, x, buf SIMPLETAGLENNSARG), buf, len);
143 }
144 #endif
145 
146 /* EOF */
147