textwriter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. Copyright (c) 2010, Florian Reuter
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Florian Reuter nor the names of its contributors
  14. may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <mce/textwriter.h>
  30. struct MCE_TEXTWRITER_STRUCT {
  31. xmlTextWriterPtr writer;
  32. puint32_t level;
  33. mceQNameLevelSet_t registered_set;
  34. mceQNameLevelSet_t processcontent_set;
  35. const xmlChar *ns_mce;
  36. };
  37. mceTextWriter *mceTextWriterCreateIO(xmlOutputWriteCallback iowrite, xmlOutputCloseCallback ioclose, void *ioctx, xmlCharEncodingHandlerPtr encoder) {
  38. mceTextWriter *w=(mceTextWriter*)xmlMalloc(sizeof(mceTextWriter));
  39. if (NULL!=w) {
  40. memset(w, 0, sizeof(*w));
  41. xmlOutputBufferPtr out=xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, encoder);
  42. w->writer=xmlNewTextWriter(out);
  43. if (NULL==w->writer) {
  44. // creation failed
  45. xmlOutputBufferClose(out);
  46. xmlFree(w);
  47. w=NULL;
  48. } else {
  49. // creation OK
  50. w->ns_mce=mceTextWriterRegisterNamespace(w, _X("http://schemas.openxmlformats.org/markup-compatibility/2006"), _X("mce"), 0);
  51. mceTextWriterRegisterNamespace(w, _X("http://www.w3.org/XML/1998/namespace"), _X("xml"), 0);
  52. }
  53. }
  54. return w;
  55. }
  56. int mceTextWriterFree(mceTextWriter *w) {
  57. int ret=0;
  58. if (NULL!=w) {
  59. xmlFreeTextWriter(w->writer);
  60. if (NULL!=w->registered_set.list_array) xmlFree(w->registered_set.list_array);
  61. if (NULL!=w->processcontent_set.list_array) xmlFree(w->processcontent_set.list_array);
  62. xmlFree(w);
  63. ret=1;
  64. }
  65. return ret;
  66. }
  67. int mceTextWriterStartDocument(mceTextWriter *w) {
  68. int ret=0;
  69. ret=xmlTextWriterStartDocument(w->writer, NULL, NULL, NULL);
  70. PASSERT(0==w->level);
  71. return ret;
  72. }
  73. int mceTextWriterEndDocument(mceTextWriter *w) {
  74. int ret=0;
  75. PASSERT(0==w->level);
  76. mceQNameLevelCleanup(&w->registered_set, w->level);
  77. ret=xmlTextWriterEndDocument(w->writer);
  78. return ret;
  79. }
  80. int mceTextWriterStartElement(mceTextWriter *w, const xmlChar *ns, const xmlChar *ln) {
  81. int ret=0;
  82. PASSERT(w->level>=w->registered_set.max_level);
  83. mceQNameLevel_t* qName=mceQNameLevelLookup(&w->registered_set, ns, NULL, PTRUE);
  84. if (NULL!=qName) {
  85. if (NULL==qName->ln) {
  86. ret=xmlTextWriterStartElement(w->writer, ln);
  87. } else {
  88. ret=xmlTextWriterStartElementNS(w->writer, qName->ln, ln, (w->level==w->registered_set.max_level?ns:NULL));
  89. }
  90. if (w->level==w->registered_set.max_level) {
  91. // register new namespaces
  92. puint32_t ignorables=0;
  93. for(puint32_t i=0;i<w->registered_set.list_items;i++) {
  94. if (w->registered_set.list_array[i].level==w->level) {
  95. if ((w->registered_set.list_array[i].flag&MCE_IGNORABLE)==MCE_IGNORABLE) {
  96. ignorables++;
  97. }
  98. if (w->registered_set.list_array[i].ns!=qName->ns) {
  99. if (!(w->registered_set.list_array[i].ln[0]=='x'
  100. && w->registered_set.list_array[i].ln[1]=='m'
  101. && w->registered_set.list_array[i].ln[2]=='l'
  102. && w->registered_set.list_array[i].ln[3]==0)) {
  103. xmlTextWriterWriteAttributeNS(w->writer,
  104. _X("xmlns"),
  105. w->registered_set.list_array[i].ln,
  106. NULL,
  107. w->registered_set.list_array[i].ns);
  108. }
  109. }
  110. }
  111. }
  112. if (ignorables>0) {
  113. mceQNameLevel_t* mceQName=mceQNameLevelLookup(&w->registered_set, w->ns_mce, NULL, PTRUE);
  114. PASSERT(NULL!=mceQName);
  115. if (NULL!=mceQName) {
  116. xmlTextWriterStartAttributeNS(w->writer,
  117. mceQName->ln,
  118. _X("Ignorable"),
  119. NULL);
  120. puint32_t j=0;
  121. for(puint32_t i=0;i<w->registered_set.list_items;i++) {
  122. if (w->registered_set.list_array[i].level==w->level && MCE_IGNORABLE==(w->registered_set.list_array[i].flag&MCE_IGNORABLE)) {
  123. xmlTextWriterWriteString(w->writer, w->registered_set.list_array[i].ln);
  124. if (++j<ignorables) {
  125. xmlTextWriterWriteString(w->writer, _X(" "));
  126. }
  127. }
  128. }
  129. PASSERT(j==ignorables);
  130. xmlTextWriterEndAttribute(w->writer);
  131. }
  132. }
  133. }
  134. if (w->level==w->processcontent_set.max_level) {
  135. // write process content attribute
  136. puint32_t v_len=0;
  137. xmlChar *v=NULL;
  138. for(puint32_t i=0;i<w->processcontent_set.list_items;i++) {
  139. if (w->processcontent_set.list_array[i].level==w->level) {
  140. mceQNameLevel_t* qName=mceQNameLevelLookup(&w->registered_set, w->processcontent_set.list_array[i].ns, NULL, PTRUE);
  141. PASSERT(NULL!=qName); // namespace not registered?
  142. if (NULL!=qName) {
  143. puint32_t len=(v_len>0?xmlStrlen(_X(" ")):0)+xmlStrlen(qName->ln)+xmlStrlen(_X(":"))+xmlStrlen(w->processcontent_set.list_array[i].ln)+1;
  144. v=(xmlChar *)xmlRealloc(v, v_len+len);
  145. if (v_len>0) { v[v_len++]=' '; len--; }
  146. int l=xmlStrPrintf(v+v_len, len, _X("%s:%s"), qName->ln, w->processcontent_set.list_array[i].ln);
  147. PASSERT(1+l==len); // cause by terminating "0"
  148. v_len+=l;
  149. }
  150. }
  151. }
  152. if (NULL!=v) {
  153. mceQNameLevel_t* mceQName=mceQNameLevelLookup(&w->registered_set, w->ns_mce, NULL, PTRUE);
  154. PASSERT(NULL!=mceQName);
  155. if (NULL!=mceQName) {
  156. xmlTextWriterWriteAttributeNS(w->writer, mceQName->ln, _X("ProcessContent"), NULL, v);
  157. }
  158. xmlFree(v); v=NULL;
  159. }
  160. }
  161. } else {
  162. // namespace not registered => not good!
  163. PASSERT(0==ret);
  164. }
  165. w->level++;
  166. return ret;
  167. }
  168. int mceTextWriterEndElement(mceTextWriter *w, const xmlChar *ns, const xmlChar *ln) {
  169. int ret=0;
  170. ret=xmlTextWriterEndElement(w->writer);
  171. mceQNameLevelCleanup(&w->registered_set, w->level);
  172. mceQNameLevelCleanup(&w->processcontent_set, w->level);
  173. PASSERT(w->level>0);
  174. w->level--;
  175. return ret;
  176. }
  177. int mceTextWriterWriteString(mceTextWriter *w, const xmlChar *content) {
  178. int ret=0;
  179. ret=xmlTextWriterWriteString(w->writer, content);
  180. return ret;
  181. }
  182. const xmlChar *mceTextWriterRegisterNamespace(mceTextWriter *w, const xmlChar *ns, const xmlChar *prefix, int flags) {
  183. mceQNameLevelAdd(&w->registered_set, ns, prefix, w->level);
  184. mceQNameLevel_t *ret=mceQNameLevelLookup(&w->registered_set, ns, prefix, PFALSE);
  185. PASSERT(NULL!=ret); // not inserted? why?
  186. if (NULL!=ret) {
  187. ret->flag=flags;
  188. return ret->ns;
  189. } else {
  190. return NULL;
  191. }
  192. }
  193. int mceTextWriterProcessContent(mceTextWriter *w, const xmlChar *ns, const xmlChar *ln) {
  194. return mceQNameLevelAdd(&w->processcontent_set, ns, ln, w->level)?0:-1;
  195. }
  196. int mceTextWriterAttributeF(mceTextWriter *w, const xmlChar *ns, const xmlChar *ln, const char *value, ...) {
  197. va_list args;
  198. int ret=0;
  199. va_start(args, value);
  200. PASSERT(w->level>=w->registered_set.max_level);
  201. mceQNameLevel_t* qName=mceQNameLevelLookup(&w->registered_set, ns, NULL, PTRUE);
  202. if (NULL!=qName) {
  203. if (NULL==qName->ln) {
  204. ret=xmlTextWriterWriteVFormatAttribute(w->writer, ln, value, args);
  205. } else {
  206. ret=xmlTextWriterWriteVFormatAttributeNS(w->writer, qName->ln, ln, NULL, value, args);
  207. }
  208. } else {
  209. PASSERT(0); // hmm namespace not registered?
  210. }
  211. va_end(args);
  212. return ret;
  213. }
  214. int mceTextWriterStartAlternateContent(mceTextWriter *w) {
  215. return xmlTextWriterStartElementNS(w->writer, _X("mce"), _X("AlternateContent"), NULL);
  216. }
  217. int mceTextWriterEndAlternateContent(mceTextWriter *w) {
  218. return xmlTextWriterEndElement(w->writer);
  219. }
  220. int mceTextWriterStartChoice(mceTextWriter *w, const xmlChar *ns) {
  221. int ret=xmlTextWriterStartElementNS(w->writer, _X("mce"), _X("Choice"), NULL);
  222. if (-1!=ret) {
  223. mceQNameLevel_t* qName=mceQNameLevelLookup(&w->registered_set, ns, NULL, PTRUE);
  224. if (NULL!=qName) {
  225. xmlTextWriterWriteAttribute(w->writer, _X("Requires"), qName->ln);
  226. } else {
  227. PASSERT(0); // namespace not registered
  228. ret=-1;
  229. }
  230. }
  231. return ret;
  232. }
  233. int mceTextWriterEndChoice(mceTextWriter *w) {
  234. return xmlTextWriterEndElement(w->writer);
  235. }
  236. int mceTextWriterStartFallback(mceTextWriter *w) {
  237. return xmlTextWriterStartElementNS(w->writer, _X("mce"), _X("AlternateContent"), NULL);
  238. }
  239. int mceTextWriterEndFallback(mceTextWriter *w) {
  240. return xmlTextWriterEndElement(w->writer);
  241. }
  242. static int xmlOutputWrite(void * context, const char * buffer, int len) {
  243. FILE *out=(FILE*)context;
  244. return fwrite(buffer, sizeof(char), len, out);
  245. }
  246. static int xmlOutputClose(void * context) {
  247. return 0;
  248. }
  249. xmlTextWriterPtr xmlNewTextWriterFile(FILE *file) {
  250. xmlTextWriter *writer=NULL;
  251. if (NULL!=file) {
  252. xmlOutputBuffer *out=xmlOutputBufferCreateIO(xmlOutputWrite, xmlOutputClose, file, NULL);
  253. if (NULL!=out) {
  254. writer=xmlNewTextWriter(out);
  255. }
  256. } else {
  257. xmlOutputBuffer *out=xmlOutputBufferCreateIO(xmlOutputWrite, xmlOutputClose, stdout, NULL);
  258. if (NULL!=out) {
  259. writer=xmlNewTextWriter(out);
  260. }
  261. }
  262. return writer;
  263. }
  264. mceTextWriter *mceNewTextWriterFile(FILE *file) {
  265. return mceTextWriterCreateIO(xmlOutputWrite, xmlOutputClose, file, NULL);
  266. }
  267. xmlTextWriterPtr mceTextWriterIntern(mceTextWriter *w) {
  268. return w->writer;
  269. }