textreader.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/textreader.h>
  30. int mceTextReaderInit(mceTextReader_t *mceTextReader, xmlTextReaderPtr reader) {
  31. memset(mceTextReader, 0, sizeof(*mceTextReader));
  32. mceCtxInit(&mceTextReader->mceCtx);
  33. mceTextReader->reader=reader;
  34. return (NULL!=mceTextReader->reader?0:-1);
  35. }
  36. int mceTextReaderCleanup(mceTextReader_t *mceTextReader) {
  37. mceCtxCleanup(&mceTextReader->mceCtx);
  38. xmlTextReaderClose(mceTextReader->reader);
  39. xmlFreeTextReader(mceTextReader->reader);
  40. return 0;
  41. }
  42. static xmlChar *xmlStrDupArray(const xmlChar *value) {
  43. opc_uint32_t len=xmlStrlen(value);
  44. xmlChar *ret=(xmlChar *)xmlMalloc((2+len)*sizeof(xmlChar));
  45. opc_uint32_t j=0;
  46. for(opc_uint32_t i=0;i<len;i++) {
  47. while(i<len && (value[i]==' ' || value[i]=='\t' || value[i]=='\r' || value[i]=='\n')) i++; // skip preceeding spaces
  48. while(i<len && value[i]!=' ' && value[i]!='\t' && value[i]!='\r' && value[i]!='\n') ret[j++]=value[i++];
  49. ret[j++]=0;
  50. }
  51. ret[j]=0;
  52. return ret;
  53. }
  54. static xmlChar *xmlStrArrayFirst(xmlChar *a, int *entry_len) {
  55. PASSERT(NULL!=entry_len);
  56. *entry_len=xmlStrlen(a);
  57. return a;
  58. }
  59. static pbool_t xmlStrArrayValid(const xmlChar *e) {
  60. return NULL!=e && *e!=0;
  61. }
  62. static xmlChar *xmlStrArrayNext(xmlChar *e, int *entry_len) {
  63. PASSERT(NULL!=entry_len);
  64. PASSERT(xmlStrArrayValid(e));
  65. xmlChar *ret=e+1+*entry_len;
  66. *entry_len=xmlStrlen(ret);
  67. return ret;
  68. }
  69. void mceRaiseError(xmlTextReader *reader, mceCtx_t *ctx, mceError_t error, const xmlChar *str, ...) {
  70. va_list args;
  71. va_start(args, str);
  72. PASSERT(MCE_ERROR_NONE==ctx->error); // called twice? why?
  73. ctx->error=error;
  74. xmlChar buf[1024];
  75. xmlStrVPrintf(buf, sizeof(buf), str, args);
  76. xmlTextReaderErrorFunc f=NULL;
  77. void *arg=NULL;
  78. xmlTextReaderGetErrorHandler(reader, &f, &arg);
  79. if (NULL!=f) {
  80. f(arg, (const char *)buf, XML_PARSER_SEVERITY_ERROR, (xmlTextReaderLocatorPtr)reader);
  81. } else {
  82. xmlGenericError(xmlGenericErrorContext, "%s!\n", (const char *)buf);
  83. }
  84. va_end(args);
  85. }
  86. static char ns_mce[]="http://schemas.openxmlformats.org/markup-compatibility/2006";
  87. static const char ns_xml[]="http://www.w3.org/2000/xmlns/";
  88. static const char _xmlns[]="xmlns";
  89. static void mceTextReaderProcessAttributes(xmlTextReader *reader, mceCtx_t *ctx, puint32_t level) {
  90. xmlNodePtr c=xmlTextReaderCurrentNode(reader);
  91. if (NULL!=c) { // make sure to inherit any namespace declaration on a parent MCE element
  92. for(xmlNodePtr n=c->parent;
  93. NULL!=n && XML_ELEMENT_NODE==n->type
  94. && NULL!=n->ns && 0==xmlStrcmp(n->ns->href, _X(ns_mce))
  95. && NULL!=n->name
  96. && (0==xmlStrcmp(_X("AlternateContent"), n->name)
  97. || 0==xmlStrcmp(_X("Choice"), n->name)
  98. || 0==xmlStrcmp(_X("Fallback"), n->name));
  99. n=n->parent) {
  100. for(xmlNs *nsDef=n->nsDef;NULL!=nsDef;nsDef=nsDef->next) {
  101. xmlAttrPtr a=xmlHasNsProp(c, nsDef->prefix, _X(ns_xml));
  102. if (NULL==a) { // only add a namespace if the prefix is not overwritten in the current node
  103. xmlNsPtr xmlns_ns=xmlNewNs(c, nsDef->href, nsDef->prefix);
  104. }
  105. }
  106. }
  107. }
  108. if (1==xmlTextReaderHasAttributes(reader)) {
  109. if (1==xmlTextReaderMoveToFirstAttribute(reader)) {
  110. do {
  111. if (0==xmlStrcmp(_X("Ignorable"), xmlTextReaderConstLocalName(reader)) &&
  112. 0==xmlStrcmp(_X(ns_mce), xmlTextReaderConstNamespaceUri(reader))) {
  113. xmlChar *v=xmlStrDupArray(xmlTextReaderConstValue(reader));
  114. int prefix_len=0;
  115. for(xmlChar *prefix=xmlStrArrayFirst(v, &prefix_len);xmlStrArrayValid(prefix);prefix=xmlStrArrayNext(prefix, &prefix_len)) {
  116. xmlChar *ns_=xmlTextReaderLookupNamespace(reader, prefix);
  117. if (NULL!=ns_ && NULL==mceQNameLevelLookup(&ctx->understands_set, ns_, NULL, PFALSE)) {
  118. PENSURE(mceQNameLevelAdd(&ctx->ignorable_set, ns_, NULL, level));
  119. }
  120. xmlFree(ns_);
  121. }
  122. xmlFree(v);
  123. } else if (0==xmlStrcmp(_X("ProcessContent"), xmlTextReaderConstLocalName(reader)) &&
  124. 0==xmlStrcmp(_X(ns_mce), xmlTextReaderConstNamespaceUri(reader))) {
  125. xmlChar *v=xmlStrDupArray(xmlTextReaderConstValue(reader));
  126. int qname_len=0;
  127. for(xmlChar *qname=xmlStrArrayFirst(v, &qname_len);xmlStrArrayValid(qname);qname=xmlStrArrayNext(qname, &qname_len)) {
  128. int prefix=0; while(qname[prefix]!=':' && qname[prefix]!=0) prefix++;
  129. OPC_ASSERT(prefix<=qname_len);
  130. int ln=(prefix<qname_len?prefix+1:0);
  131. if (prefix<qname_len) {
  132. qname[prefix]=0;
  133. prefix=0;
  134. };
  135. xmlChar *ns_=xmlTextReaderLookupNamespace(reader, qname+prefix);
  136. if (NULL!=ns_ && NULL==mceQNameLevelLookup(&ctx->understands_set, ns_, NULL, PFALSE)) {
  137. PENSURE(mceQNameLevelAdd(&ctx->processcontent_set, ns_, xmlStrdup(qname+ln), level));
  138. }
  139. }
  140. xmlFree(v);
  141. } else if (0==xmlStrcmp(_X("MustUnderstand"), xmlTextReaderConstLocalName(reader)) &&
  142. 0==xmlStrcmp(_X(ns_mce), xmlTextReaderConstNamespaceUri(reader))) {
  143. xmlChar *v=xmlStrDupArray(xmlTextReaderConstValue(reader));
  144. int prefix_len=0;
  145. for(xmlChar *prefix=xmlStrArrayFirst(v, &prefix_len);xmlStrArrayValid(prefix);prefix=xmlStrArrayNext(prefix, &prefix_len)) {
  146. xmlChar *ns_=xmlTextReaderLookupNamespace(reader, prefix);
  147. if (NULL!=ns_ && NULL==mceQNameLevelLookup(&ctx->understands_set, ns_, NULL, PFALSE)) {
  148. mceRaiseError(reader, ctx, MCE_ERROR_MUST_UNDERSTAND, _X("MustUnderstand namespace \"%s\""), ns_);
  149. }
  150. }
  151. xmlFree(v);
  152. #if (MCE_NAMESPACE_SUBSUMPTION_ENABLED)
  153. } else if (0==xmlStrcmp(_X(ns_xml), xmlTextReaderConstNamespaceUri(reader))) {
  154. mceQNameLevel_t *qnl=mceQNameLevelLookup(&ctx->subsume_prefix_set, xmlTextReaderConstValue(reader), NULL, PTRUE);
  155. if (NULL!=qnl) {
  156. const xmlChar *prefix=xmlTextReaderConstLocalName(reader);
  157. if (0!=xmlStrcmp(prefix, qnl->ln)) {
  158. // different binding!
  159. printf("prefix=%s\n", prefix);
  160. PASSERT(0); //@TODO add store prefix for subsumption namespaces...
  161. }
  162. }
  163. } else {
  164. const xmlChar *ns_orig=xmlTextReaderConstNamespaceUri(reader);
  165. const xmlChar *ns=mceTextReaderSubsumeNS(ctx, ns_orig, xmlTextReaderConstLocalName(reader));
  166. if (ns_orig!=ns) {
  167. // namespace needs to be subsumed...
  168. }
  169. #endif
  170. }
  171. } while (1==xmlTextReaderMoveToNextAttribute(reader));
  172. }
  173. xmlAttrPtr remove=NULL;
  174. if (1==xmlTextReaderMoveToFirstAttribute(reader)) {
  175. do {
  176. if (NULL!=remove) {
  177. xmlRemoveProp(remove); remove=NULL;
  178. }
  179. if (0==xmlStrcmp(_X(ns_mce), xmlTextReaderConstNamespaceUri(reader))) {
  180. OPC_ASSERT(XML_ATTRIBUTE_NODE==xmlTextReaderCurrentNode(reader)->type);
  181. remove=(xmlAttrPtr)xmlTextReaderCurrentNode(reader);
  182. } else if (NULL!=mceQNameLevelLookup(&ctx->ignorable_set,
  183. xmlTextReaderConstNamespaceUri(reader),
  184. NULL,
  185. PFALSE)) {
  186. OPC_ASSERT(XML_ATTRIBUTE_NODE==xmlTextReaderCurrentNode(reader)->type);
  187. remove=(xmlAttrPtr)xmlTextReaderCurrentNode(reader);
  188. }
  189. } while (1==xmlTextReaderMoveToNextAttribute(reader));
  190. OPC_ENSURE(1==xmlTextReaderMoveToElement(reader));
  191. if (NULL!=remove) {
  192. xmlRemoveProp(remove); remove=NULL;
  193. }
  194. OPC_ASSERT(NULL==remove);
  195. }
  196. }
  197. }
  198. static pbool_t mceTextReaderProcessStartElement(xmlTextReader *reader, mceCtx_t *ctx, puint32_t level, const xmlChar *ns, const xmlChar *ln) {
  199. if (!mceSkipStackSkip(&ctx->skip_stack, level)) {
  200. mceTextReaderProcessAttributes(reader, ctx, level);
  201. if (0==xmlStrcmp(_X(ns_mce), ns) && 0==xmlStrcmp(_X("AlternateContent"), ln)) {
  202. mceSkipStackPush(&ctx->skip_stack, level, level+1, MCE_SKIP_STATE_ALTERNATE_CONTENT);
  203. } else if (0==xmlStrcmp(_X(ns_mce), ns) && 0==xmlStrcmp(_X("Choice"), ln)) {
  204. xmlChar *req_ns=NULL;
  205. if (1==xmlTextReaderMoveToAttribute(reader, _X("Requires"))) {
  206. req_ns=xmlTextReaderLookupNamespace(reader, xmlTextReaderConstValue(reader));
  207. OPC_ENSURE(1==xmlTextReaderMoveToElement(reader));
  208. } else if (1==xmlTextReaderMoveToAttributeNs(reader, _X("Requires"), _X(ns_mce))) {
  209. req_ns=xmlTextReaderLookupNamespace(reader, xmlTextReaderConstValue(reader));
  210. OPC_ENSURE(1==xmlTextReaderMoveToElement(reader));
  211. }
  212. if (NULL==req_ns) {
  213. mceRaiseError(reader, ctx, MCE_ERROR_XML, _X("Missing \"Requires\" attribute"));
  214. } else if (NULL==mceSkipStackTop(&ctx->skip_stack)
  215. || !(mceSkipStackTop(&ctx->skip_stack)->state==MCE_SKIP_STATE_ALTERNATE_CONTENT || mceSkipStackTop(&ctx->skip_stack)->state==MCE_SKIP_STATE_CHOICE_MATCHED)
  216. || mceSkipStackTop(&ctx->skip_stack)->level_start+1!=level
  217. || mceSkipStackTop(&ctx->skip_stack)->level_end!=level) {
  218. mceRaiseError(reader, ctx, MCE_ERROR_XML, _X("Choice element does not appear at a valid position."));
  219. } else if (mceQNameLevelLookup(&ctx->understands_set, req_ns, NULL, PFALSE) && mceSkipStackTop(&ctx->skip_stack)->state!=MCE_SKIP_STATE_CHOICE_MATCHED) {
  220. mceSkipStackTop(&ctx->skip_stack)->state=MCE_SKIP_STATE_CHOICE_MATCHED;
  221. mceSkipStackPush(&ctx->skip_stack, level, level+1, MCE_SKIP_STATE_IGNORE);
  222. } else {
  223. mceSkipStackPush(&ctx->skip_stack, level, PUINT32_MAX, MCE_SKIP_STATE_IGNORE);
  224. }
  225. if (NULL!=req_ns) xmlFree(req_ns);
  226. } else if (0==xmlStrcmp(_X(ns_mce), ns) && 0==xmlStrcmp(_X("Fallback"), ln)) {
  227. if (NULL==mceSkipStackTop(&ctx->skip_stack)
  228. || !(mceSkipStackTop(&ctx->skip_stack)->state==MCE_SKIP_STATE_ALTERNATE_CONTENT || mceSkipStackTop(&ctx->skip_stack)->state==MCE_SKIP_STATE_CHOICE_MATCHED)
  229. || mceSkipStackTop(&ctx->skip_stack)->level_start+1!=level
  230. || mceSkipStackTop(&ctx->skip_stack)->level_end!=level) {
  231. mceRaiseError(reader, ctx, MCE_ERROR_XML, _X("Fallback element does not appear at a valid position."));
  232. } else if (mceSkipStackTop(&ctx->skip_stack)->state!=MCE_SKIP_STATE_CHOICE_MATCHED) {
  233. mceSkipStackPush(&ctx->skip_stack, level, level+1, MCE_SKIP_STATE_IGNORE);
  234. } else {
  235. mceSkipStackPush(&ctx->skip_stack, level, PUINT32_MAX, MCE_SKIP_STATE_IGNORE);
  236. }
  237. } else if (mceQNameLevelLookup(&ctx->ignorable_set, ns, NULL, PFALSE)
  238. && !mceQNameLevelLookup(&ctx->understands_set, ns, NULL, PFALSE)) {
  239. if (mceQNameLevelLookup(&ctx->processcontent_set, ns, ln, PFALSE)) {
  240. mceSkipStackPush(&ctx->skip_stack, level, level+1, MCE_SKIP_STATE_IGNORE);
  241. } else {
  242. mceSkipStackPush(&ctx->skip_stack, level, PUINT32_MAX, MCE_SKIP_STATE_IGNORE);
  243. }
  244. }
  245. }
  246. return mceSkipStackSkip(&ctx->skip_stack, level);
  247. }
  248. static pbool_t mceTextReaderProcessEndElement(xmlTextReader *reader, mceCtx_t *ctx, puint32_t level, const xmlChar *ns, const xmlChar *ln) {
  249. pbool_t skiped=PFALSE;
  250. if (mceSkipStackSkip(&ctx->skip_stack, level)) {
  251. if (mceSkipStackTop(&ctx->skip_stack)->level_start==level) {
  252. mceSkipStackPop(&ctx->skip_stack);
  253. } else if (mceSkipStackTop(&ctx->skip_stack)->level_end==level) {
  254. mceSkipStackTop(&ctx->skip_stack)->level_end--;
  255. PASSERT(mceSkipStackTop(&ctx->skip_stack)->level_start<mceSkipStackTop(&ctx->skip_stack)->level_end);
  256. }
  257. skiped=PTRUE;
  258. }
  259. mceQNameLevelCleanup(&ctx->ignorable_set, level);
  260. mceQNameLevelCleanup(&ctx->processcontent_set, level);
  261. mceQNameLevelCleanup(&ctx->understands_set, level);
  262. mceQNameLevelCleanup(&ctx->suspended_set, level);
  263. return skiped;
  264. }
  265. int mceTextReaderPostprocess(xmlTextReader *reader, mceCtx_t *ctx, int ret) {
  266. pbool_t suspend=ctx->mce_disabled;
  267. const xmlChar *ns=NULL;
  268. const xmlChar *ln=NULL;
  269. if (MCE_ERROR_NONE!=ctx->error) {
  270. ret=-1;
  271. } else {
  272. ns=xmlTextReaderConstNamespaceUri(reader);
  273. ln=xmlTextReaderConstLocalName(reader);
  274. if (XML_READER_TYPE_ELEMENT==xmlTextReaderNodeType(reader)) {
  275. if (ctx->suspended_level>0 || NULL!=mceQNameLevelLookup(&ctx->suspended_set, ns, ln, PFALSE)) {
  276. suspend=PTRUE;
  277. if (!xmlTextReaderIsEmptyElement(reader)) {
  278. ctx->suspended_level++;
  279. }
  280. }
  281. } else if (ctx->suspended_level>0) {
  282. suspend=PTRUE;
  283. if (XML_READER_TYPE_END_ELEMENT==xmlTextReaderNodeType(reader)) {
  284. ctx->suspended_level--;
  285. }
  286. }
  287. }
  288. pbool_t skip=!suspend;
  289. while(1==ret && skip) {
  290. if (XML_READER_TYPE_ELEMENT==xmlTextReaderNodeType(reader)) {
  291. skip=mceTextReaderProcessStartElement(reader, ctx, xmlTextReaderDepth(reader), ns, ln);
  292. if (xmlTextReaderIsEmptyElement(reader)) {
  293. PENSURE(mceTextReaderProcessEndElement(reader, ctx, xmlTextReaderDepth(reader), ns, ln)==skip);
  294. }
  295. } else if (XML_READER_TYPE_END_ELEMENT==xmlTextReaderNodeType(reader)) {
  296. skip=mceTextReaderProcessEndElement(reader, ctx, xmlTextReaderDepth(reader), ns, ln);
  297. } else {
  298. skip=mceSkipStackSkip(&ctx->skip_stack, xmlTextReaderDepth(reader));
  299. }
  300. if (skip) {
  301. if (-1!=(ret=xmlTextReaderRead(reader))) { // skip element
  302. ns=xmlTextReaderConstNamespaceUri(reader); // get next ns
  303. ln=xmlTextReaderConstLocalName(reader); // get net local name
  304. }
  305. }
  306. if (MCE_ERROR_NONE!=ctx->error) {
  307. ret=-1;
  308. }
  309. }
  310. if (-1==ret && MCE_ERROR_NONE==ctx->error) {
  311. ctx->error=MCE_ERROR_XML; //@TODO be more specific about the type of libxml2 error.
  312. }
  313. return ret;
  314. }
  315. mceError_t mceTextReaderGetError(mceTextReader_t *mceTextReader) {
  316. return mceTextReader->mceCtx.error;
  317. }
  318. int mceTextReaderRead(mceTextReader_t *mceTextReader) {
  319. return mceTextReaderPostprocess(mceTextReader->reader, &mceTextReader->mceCtx, xmlTextReaderRead(mceTextReader->reader));
  320. }
  321. int mceTextReaderNext(mceTextReader_t *mceTextReader) {
  322. return mceTextReaderPostprocess(mceTextReader->reader, &mceTextReader->mceCtx, xmlTextReaderNext(mceTextReader->reader));
  323. }
  324. int mceTextReaderDump(mceTextReader_t *mceTextReader, xmlTextWriter *writer, pbool_t fragment) {
  325. int ret=-1;
  326. if (XML_READER_TYPE_ELEMENT==xmlTextReaderNodeType(mceTextReader->reader)) {
  327. const xmlChar *ns=xmlTextReaderConstNamespaceUri(mceTextReader->reader);
  328. const xmlChar *ln=xmlTextReaderConstLocalName(mceTextReader->reader);
  329. if (NULL!=ns) {
  330. xmlTextWriterStartElementNS(writer, xmlTextReaderConstPrefix(mceTextReader->reader), ln, NULL);
  331. } else {
  332. xmlTextWriterStartElement(writer, ln);
  333. }
  334. if (xmlTextReaderHasAttributes(mceTextReader->reader)) {
  335. if (1==xmlTextReaderMoveToFirstAttribute(mceTextReader->reader)) {
  336. do {
  337. const xmlChar *attr_ns=xmlTextReaderConstNamespaceUri(mceTextReader->reader);
  338. const xmlChar *attr_ln=xmlTextReaderConstLocalName(mceTextReader->reader);
  339. const xmlChar *attr_val=xmlTextReaderConstValue(mceTextReader->reader);
  340. if (NULL!=attr_ns && 0==xmlStrcmp(attr_ns, _X(ns_xml))) {
  341. if (0==xmlStrcmp(attr_ln, _X(_xmlns))) {
  342. xmlTextWriterWriteAttribute(writer, attr_ln, attr_val);
  343. } else {
  344. xmlTextWriterWriteAttributeNS(writer, _X(_xmlns), attr_ln, NULL, attr_val);
  345. }
  346. } else {
  347. if (NULL!=ns) {
  348. xmlTextWriterWriteAttributeNS(writer, xmlTextReaderConstPrefix(mceTextReader->reader), attr_ln, NULL, attr_val);
  349. } else {
  350. xmlTextWriterWriteAttribute(writer, attr_ln, attr_val);
  351. }
  352. }
  353. } while (1==xmlTextReaderMoveToNextAttribute(mceTextReader->reader));
  354. }
  355. PENSURE(1==xmlTextReaderMoveToElement(mceTextReader->reader));
  356. }
  357. if (!xmlTextReaderIsEmptyElement(mceTextReader->reader)) {
  358. ret=mceTextReaderRead(mceTextReader); // read start element
  359. while (1==ret && XML_READER_TYPE_END_ELEMENT!=xmlTextReaderNodeType(mceTextReader->reader)) {
  360. ret=mceTextReaderDump(mceTextReader, writer, fragment);
  361. }
  362. PASSERT(-1==ret || XML_READER_TYPE_END_ELEMENT==xmlTextReaderNodeType(mceTextReader->reader));
  363. ret=mceTextReaderRead(mceTextReader); // read end element
  364. } else {
  365. ret=mceTextReaderRead(mceTextReader); // read empty element
  366. }
  367. xmlTextWriterEndElement(writer);
  368. } else if (XML_READER_TYPE_TEXT==xmlTextReaderNodeType(mceTextReader->reader)) {
  369. xmlTextWriterWriteString(writer, xmlTextReaderConstValue(mceTextReader->reader));
  370. ret=mceTextReaderRead(mceTextReader); // read end element
  371. } else if (XML_READER_TYPE_SIGNIFICANT_WHITESPACE==xmlTextReaderNodeType(mceTextReader->reader)) {
  372. xmlTextWriterWriteString(writer, xmlTextReaderConstValue(mceTextReader->reader));
  373. ret=mceTextReaderRead(mceTextReader); // read end element
  374. } else if (XML_READER_TYPE_NONE==xmlTextReaderNodeType(mceTextReader->reader)) {
  375. ret=mceTextReaderRead(mceTextReader);
  376. if (1==ret && XML_READER_TYPE_NONE!=xmlTextReaderNodeType(mceTextReader->reader)) {
  377. if (!fragment) xmlTextWriterStartDocument(writer, NULL, NULL, NULL);
  378. ret=mceTextReaderDump(mceTextReader, writer, fragment);
  379. if (!fragment) xmlTextWriterEndDocument(writer);
  380. }
  381. } else {
  382. if (1==(ret=mceTextReaderNext(mceTextReader))) { // skip element
  383. ret=mceTextReaderDump(mceTextReader, writer, fragment);
  384. }
  385. }
  386. return ret;
  387. }
  388. int mceTextReaderUnderstandsNamespace(mceTextReader_t *mceTextReader, const xmlChar *ns) {
  389. return (mceCtxUnderstandsNamespace(&mceTextReader->mceCtx, ns)?0:-1);
  390. }
  391. pbool_t mceTextReaderDisableMCE(mceTextReader_t *mceTextReader, pbool_t flag) {
  392. pbool_t ret=mceTextReader->mceCtx.mce_disabled>0;
  393. mceTextReader->mceCtx.mce_disabled=flag;
  394. return ret;
  395. }