opc_text.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /*
  30. Extract all text of an Word document as HTML.
  31. Ussage:
  32. opc_text FILENAME
  33. Sample:
  34. opc_text OOXMLI1.docx
  35. */
  36. #include <opc/opc.h>
  37. #include <stdio.h>
  38. #include <time.h>
  39. #ifdef WIN32
  40. #include <crtdbg.h>
  41. #endif
  42. static void dumpText(mceTextReader_t *reader) {
  43. mce_skip_attributes(reader);
  44. mce_start_children(reader) {
  45. mce_start_element(reader, _X("http://schemas.openxmlformats.org/wordprocessingml/2006/main"), _X("t")) {
  46. mce_skip_attributes(reader);
  47. mce_start_children(reader) {
  48. mce_start_text(reader) {
  49. for(const xmlChar *txt=xmlTextReaderConstValue(reader->reader);0!=*txt;txt++) {
  50. switch(*txt) {
  51. case '<':
  52. printf("&lt;");
  53. break;
  54. case '>':
  55. printf("&gt;");
  56. break;
  57. case '&':
  58. printf("&amp;");
  59. break;
  60. default:
  61. putc(*txt, stdout);
  62. break;
  63. }
  64. }
  65. } mce_end_text(reader);
  66. } mce_end_children(reader);
  67. } mce_end_element(reader);
  68. mce_start_element(reader, _X("http://schemas.openxmlformats.org/wordprocessingml/2006/main"), _X("p")) {
  69. printf("<p>");
  70. dumpText(reader);
  71. printf("</p>\n");
  72. } mce_end_element(reader);
  73. mce_start_element(reader, NULL, NULL) {
  74. dumpText(reader);
  75. } mce_end_element(reader);
  76. } mce_end_children(reader);
  77. }
  78. int main( int argc, const char* argv[] )
  79. {
  80. #ifdef WIN32
  81. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  82. #endif
  83. opcInitLibrary();
  84. opcContainer *c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_ONLY, NULL, NULL);
  85. if (NULL!=c) {
  86. mceTextReader_t reader;
  87. if (OPC_ERROR_NONE==opcXmlReaderOpen(c, &reader, _X("/word/document.xml"), NULL, 0, 0)) {
  88. mce_start_document(&reader) {
  89. mce_start_element(&reader, NULL, NULL) {
  90. printf("<html>\n");
  91. printf("<head>\n");
  92. printf("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
  93. printf("</head>\n");
  94. printf("<body>\n");
  95. dumpText(&reader);
  96. printf("<body>\n");
  97. printf("</html>\n");
  98. } mce_end_element(&reader);
  99. } mce_end_document(&reader);
  100. mceTextReaderCleanup(&reader);
  101. }
  102. opcContainerClose(c, OPC_CLOSE_NOW);
  103. }
  104. opcFreeLibrary();
  105. #ifdef WIN32
  106. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  107. #endif
  108. return 0;
  109. }