helper.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /** @file mce/helper.h
  30. Helper functions needed by mce/textreader.h and mce/textwriter.h to implement MCE:
  31. - mceQNameLevelAdd(), mceQNameLevelLookup() and mceQNameLevelCleanup() maintain a set of mceQNameLevel_t tuples.
  32. - mceQNameLevelPush() and mceQNameLevelPopIfMatch() maintain a stack of mceQNameLevel_t tuples.
  33. - mceCtxInit(), mceCtxCleanup() and mceCtxUnderstandsNamespace() manage a context which holds all information needed to do MCE proprocessing.
  34. */
  35. #include <mce/config.h>
  36. #ifndef MCE_HELPER_H
  37. #define MCE_HELPER_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /**
  42. Tiple (ns, ln, level).
  43. */
  44. typedef struct MCE_QNAME_LEVEL {
  45. xmlChar *ns;
  46. xmlChar *ln;
  47. puint32_t level;
  48. puint32_t flag; // used by mceTextWriter
  49. } mceQNameLevel_t;
  50. /**
  51. */
  52. typedef enum MCE_SKIP_STATE_ENUM {
  53. MCE_SKIP_STATE_IGNORE,
  54. MCE_SKIP_STATE_ALTERNATE_CONTENT,
  55. MCE_SKIP_STATE_CHOICE_MATCHED
  56. } mceSkipState_t;
  57. /**
  58. Represents an intervall of levels which are "skipped" i.e. ignored.
  59. */
  60. typedef struct MCE_SKIP_ITEM {
  61. puint32_t level_start;
  62. puint32_t level_end;
  63. mceSkipState_t state;
  64. } mceSkipItem_t;
  65. /**
  66. Either represents a set of (ns, ln, level) triples.
  67. */
  68. typedef struct MCE_QNAME_LEVEL_SET {
  69. mceQNameLevel_t *list_array;
  70. puint32_t list_items;
  71. puint32_t max_level;
  72. } mceQNameLevelSet_t;
  73. /**
  74. The skip stack.
  75. */
  76. typedef struct MCE_SKIP_STACK {
  77. mceSkipItem_t *stack_array;
  78. puint32_t stack_items;
  79. } mceSkipStack_t;
  80. typedef enum MCE_ERROR_ENUM {
  81. MCE_ERROR_NONE,
  82. MCE_ERROR_XML,
  83. MCE_ERROR_MUST_UNDERSTAND,
  84. MCE_ERROR_VALIDATION,
  85. MCE_ERROR_MEMORY
  86. } mceError_t;
  87. /**
  88. Holds all information to do MCE preprocessing.
  89. */
  90. typedef struct MCE_CONTEXT {
  91. mceQNameLevelSet_t ignorable_set;
  92. mceQNameLevelSet_t understands_set;
  93. mceQNameLevelSet_t processcontent_set;
  94. mceQNameLevelSet_t suspended_set;
  95. #if (MCE_NAMESPACE_SUBSUMPTION_ENABLED)
  96. mceQNameLevelSet_t subsume_namespace_set;
  97. mceQNameLevelSet_t subsume_exclude_set;
  98. mceQNameLevelSet_t subsume_prefix_set;
  99. #endif
  100. mceSkipStack_t skip_stack;
  101. mceError_t error;
  102. pbool_t mce_disabled;
  103. puint32_t suspended_level;
  104. } mceCtx_t;
  105. /**
  106. Add a new tiple (ns, ln, level) to the triple set \c qname_level_set.
  107. The \c ns_sub string is optional and will not be touched.
  108. */
  109. pbool_t mceQNameLevelAdd(mceQNameLevelSet_t *qname_level_set, const xmlChar *ns, const xmlChar *ln, puint32_t level);
  110. /**
  111. Lookup a tiple (ns, ln, level) via \c ns and \c ln. If \c ignore_ln is PTRUE then the first tiple matching \c ns will be returned.
  112. */
  113. mceQNameLevel_t* mceQNameLevelLookup(mceQNameLevelSet_t *qname_level_set, const xmlChar *ns, const xmlChar *ln, pbool_t ignore_ln);
  114. /**
  115. Remove all triples (ns, ln, level) where the level greater or equal to \c level.
  116. */
  117. pbool_t mceQNameLevelCleanup(mceQNameLevelSet_t *qname_level_set, puint32_t level);
  118. /**
  119. Push a new skip intervall (level_start, level_end, state) on the stack \c skip_stack.
  120. */
  121. pbool_t mceSkipStackPush(mceSkipStack_t *skip_stack, puint32_t level_start, puint32_t level_end, mceSkipState_t state);
  122. /**
  123. Pop the intervall (ns, ln, level) from the stack \c qname_level_array.
  124. */
  125. void mceSkipStackPop(mceSkipStack_t *skip_stack);
  126. /**
  127. Returns top item or NULL.
  128. */
  129. mceSkipItem_t *mceSkipStackTop(mceSkipStack_t *skip_stack);
  130. /**
  131. Returns TRUE, if the \c level is in the top skip intervall.
  132. */
  133. pbool_t mceSkipStackSkip(mceSkipStack_t *skip_stack, puint32_t level);
  134. /**
  135. Initialize the mceCtx_t \c ctx.
  136. */
  137. pbool_t mceCtxInit(mceCtx_t *ctx);
  138. /**
  139. Cleanup, i.e. release all resourced from the mceCtx_t \c ctx.
  140. */
  141. pbool_t mceCtxCleanup(mceCtx_t *ctx);
  142. /**
  143. Register the namespace \ns in \c ctx.
  144. */
  145. pbool_t mceCtxUnderstandsNamespace(mceCtx_t *ctx, const xmlChar *ns);
  146. /**
  147. Register the namespace \ns in \c ctx.
  148. */
  149. pbool_t mceCtxSuspendProcessing(mceCtx_t *ctx, const xmlChar *ns, const xmlChar *ln);
  150. #if (MCE_NAMESPACE_SUBSUMPTION_ENABLED)
  151. /**
  152. Subsume namespace \c ns_new with \c ns_old.
  153. */
  154. pbool_t mceCtxSubsumeNamespace(mceCtx_t *ctx, const xmlChar *prefix_new, const xmlChar *ns_new, const xmlChar *ns_old);
  155. #endif
  156. #ifdef __cplusplus
  157. } /* extern "C" */
  158. #endif
  159. #endif /* MCE_HELPER_H */