properties.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <opc/properties.h>
  30. #include <libxml/xmlmemory.h>
  31. #include <opc/xmlreader.h>
  32. #include <mce/textreader.h>
  33. #include <mce/textwriter.h>
  34. opc_error_t opcCorePropertiesInit(opcProperties_t *cp) {
  35. opc_bzero_mem(cp, sizeof(*cp));
  36. return OPC_ERROR_NONE;
  37. }
  38. static void opcDCSimpleTypeFree(opcDCSimpleType_t *v) {
  39. if (NULL!=v) {
  40. if (NULL!=v->lang) xmlFree(v->lang);
  41. if (NULL!=v->str) xmlFree(v->str);
  42. }
  43. }
  44. opc_error_t opcCorePropertiesCleanup(opcProperties_t *cp) {
  45. if (NULL!=cp->category) xmlFree(cp->category);
  46. if (NULL!=cp->contentStatus) xmlFree(cp->contentStatus);
  47. if (NULL!=cp->created) xmlFree(cp->created);
  48. opcDCSimpleTypeFree(&cp->creator);
  49. opcDCSimpleTypeFree(&cp->description);
  50. opcDCSimpleTypeFree(&cp->identifier);
  51. for(opc_uint32_t i=0;i<cp->keyword_items;i++) {
  52. opcDCSimpleTypeFree(&cp->keyword_array[i]);
  53. }
  54. if (NULL!=cp->keyword_array) {
  55. xmlFree(cp->keyword_array);
  56. }
  57. opcDCSimpleTypeFree(&cp->language);
  58. if (NULL!=cp->lastModifiedBy) xmlFree(cp->lastModifiedBy);
  59. if (NULL!=cp->lastPrinted) xmlFree(cp->lastPrinted);
  60. if (NULL!=cp->modified) xmlFree(cp->modified);
  61. if (NULL!=cp->revision) xmlFree(cp->revision);
  62. opcDCSimpleTypeFree(&cp->subject);
  63. opcDCSimpleTypeFree(&cp->title);
  64. if (NULL!=cp->version) xmlFree(cp->version);
  65. return OPC_ERROR_NONE;
  66. }
  67. opc_error_t opcCorePropertiesSetString(xmlChar **prop, const xmlChar *str) {
  68. if (NULL!=prop) {
  69. if (NULL!=*prop) xmlFree(*prop);
  70. *prop=xmlStrdup(str);
  71. }
  72. return OPC_ERROR_NONE;
  73. }
  74. opc_error_t opcCorePropertiesSetStringLang(opcDCSimpleType_t *prop, const xmlChar *str, const xmlChar *lang) {
  75. if (NULL!=prop) {
  76. if (NULL!=prop->str) xmlFree(prop->str);
  77. if (NULL!=prop->lang) xmlFree(prop->lang);
  78. prop->str=(NULL!=str?xmlStrdup(str):NULL);
  79. prop->lang=(NULL!=lang?xmlStrdup(lang):NULL);
  80. }
  81. return OPC_ERROR_NONE;
  82. }
  83. static const char cp_ns[]="http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
  84. static const char dc_ns[]="http://purl.org/dc/elements/1.1/";
  85. static const char dcterms_ns[]="http://purl.org/dc/terms/";
  86. static const char xml_ns[]="http://www.w3.org/XML/1998/namespace";
  87. static const char xsi_ns[]="http://www.w3.org/2001/XMLSchema-instance";
  88. static void _opcCorePropertiesAddKeywords(opcProperties_t *cp, const xmlChar *keywords, const xmlChar *lang) {
  89. int ofs=0;
  90. do {
  91. while(0!=keywords[ofs] && (' '==keywords[ofs] || '\t'==keywords[ofs] || '\r'==keywords[ofs] || '\n'==keywords[ofs])) ofs++;
  92. int start=ofs;
  93. while(0!=keywords[ofs] && ';'!=keywords[ofs]) ofs++;
  94. int end=ofs;
  95. while(end>start && (' '==keywords[end-1] || '\t'==keywords[end-1] || '\r'==keywords[end-1] || '\n'==keywords[end-1])) end--;
  96. if (end>start) {
  97. cp->keyword_array=(opcDCSimpleType_t*)xmlRealloc(cp->keyword_array, (cp->keyword_items+1)*sizeof(opcDCSimpleType_t));
  98. cp->keyword_array[cp->keyword_items].lang=(NULL!=lang?xmlStrdup(lang):NULL);
  99. cp->keyword_array[cp->keyword_items].str=xmlStrndup(keywords+start, end-start);
  100. cp->keyword_items++;
  101. }
  102. ofs++;
  103. } while(keywords[ofs-1]!=0);
  104. }
  105. static void _opcCorePropertiesReadString(mceTextReader_t *r, xmlChar **v) {
  106. mce_skip_attributes(r);
  107. mce_start_children(r) {
  108. mce_start_text(r) {
  109. if (NULL!=v && NULL!=*v) xmlFree(*v);
  110. *v=xmlStrdup(xmlTextReaderConstValue(r->reader));
  111. } mce_end_text(r);
  112. } mce_end_children(r);
  113. }
  114. static void _opcCorePropertiesReadSimpleType(mceTextReader_t *r, opcDCSimpleType_t *v) {
  115. opc_bzero_mem(v, sizeof(*v));
  116. mce_start_attributes(r) {
  117. mce_start_attribute(r, _X(xml_ns), _X("lang")) {
  118. if (NULL==v->lang) {
  119. v->lang=xmlStrdup(xmlTextReaderConstValue(r->reader));
  120. }
  121. } mce_end_attribute(r);
  122. } mce_end_attributes(r);
  123. mce_start_children(r) {
  124. mce_start_text(r) {
  125. if (NULL==v->str) {
  126. v->str=xmlStrdup(xmlTextReaderConstValue(r->reader));
  127. }
  128. } mce_end_text(r);
  129. } mce_end_children(r);
  130. }
  131. opc_error_t opcCorePropertiesRead(opcProperties_t *cp, opcContainer *c) {
  132. mceTextReader_t r;
  133. if (OPC_ERROR_NONE==opcXmlReaderOpen(c, &r, _X("docProps/core.xml"), NULL, NULL, 0)) {
  134. mce_start_document(&r) {
  135. mce_start_element(&r, _X(cp_ns), _X("coreProperties")) {
  136. mce_skip_attributes(&r);
  137. mce_start_children(&r) {
  138. mce_start_element(&r, _X(cp_ns), _X("category")) {
  139. _opcCorePropertiesReadString(&r, &cp->category);
  140. } mce_end_element(&r);
  141. mce_start_element(&r, _X(cp_ns), _X("contentStatus")) {
  142. _opcCorePropertiesReadString(&r, &cp->contentStatus);
  143. } mce_end_element(&r);
  144. mce_start_element(&r, _X(dcterms_ns), _X("created")) {
  145. _opcCorePropertiesReadString(&r, &cp->created);
  146. } mce_end_element(&r);
  147. mce_start_element(&r, _X(dc_ns), _X("creator")) {
  148. _opcCorePropertiesReadSimpleType(&r, &cp->creator);
  149. } mce_end_element(&r);
  150. mce_start_element(&r, _X(dc_ns), _X("description")) {
  151. _opcCorePropertiesReadSimpleType(&r, &cp->description);
  152. } mce_end_element(&r);
  153. mce_start_element(&r, _X(dc_ns), _X("identifier")) {
  154. _opcCorePropertiesReadSimpleType(&r, &cp->identifier);
  155. } mce_end_element(&r);
  156. mce_start_element(&r, _X(cp_ns), _X("keywords")) {
  157. xmlChar *xml_lang=NULL;
  158. mce_start_attributes(&r) {
  159. mce_start_attribute(&r, _X(xml_ns), _X("lang")) {
  160. xml_lang=xmlStrdup(xmlTextReaderConstValue(r.reader));
  161. } mce_end_attribute(&r);
  162. } mce_end_attributes(&r);
  163. mce_start_children(&r) {
  164. mce_start_element(&r, _X(cp_ns), _X("value")) {
  165. xmlChar *value_xml_lang=NULL;
  166. mce_start_attributes(&r) {
  167. mce_start_attribute(&r, _X(xml_ns), _X("lang")) {
  168. value_xml_lang=xmlStrdup(xmlTextReaderConstValue(r.reader));
  169. } mce_end_attribute(&r);
  170. } mce_end_attributes(&r);
  171. mce_start_children(&r) {
  172. mce_start_text(&r) {
  173. _opcCorePropertiesAddKeywords(cp, xmlTextReaderConstValue(r.reader), value_xml_lang);
  174. } mce_end_text(&r);
  175. } mce_end_children(&r);
  176. if (NULL!=value_xml_lang) { xmlFree(value_xml_lang); value_xml_lang=NULL; };
  177. } mce_end_element(&r);
  178. mce_start_text(&r) {
  179. _opcCorePropertiesAddKeywords(cp, xmlTextReaderConstValue(r.reader), xml_lang);
  180. } mce_end_text(&r);
  181. } mce_end_children(&r);
  182. if (NULL!=xml_lang) { xmlFree(xml_lang); xml_lang=NULL; };
  183. } mce_end_element(&r);
  184. mce_start_element(&r, _X(dc_ns), _X("language")) {
  185. _opcCorePropertiesReadSimpleType(&r, &cp->language);
  186. } mce_end_element(&r);
  187. mce_start_element(&r, _X(cp_ns), _X("lastModifiedBy")) {
  188. _opcCorePropertiesReadString(&r, &cp->lastModifiedBy);
  189. } mce_end_element(&r);
  190. mce_start_element(&r, _X(cp_ns), _X("lastPrinted")) {
  191. _opcCorePropertiesReadString(&r, &cp->lastPrinted);
  192. } mce_end_element(&r);
  193. mce_start_element(&r, _X(dcterms_ns), _X("modified")) {
  194. _opcCorePropertiesReadString(&r, &cp->modified);
  195. } mce_end_element(&r);
  196. mce_start_element(&r, _X(cp_ns), _X("revision")) {
  197. _opcCorePropertiesReadString(&r, &cp->revision);
  198. } mce_end_element(&r);
  199. mce_start_element(&r, _X(dc_ns), _X("subject")) {
  200. _opcCorePropertiesReadSimpleType(&r, &cp->subject);
  201. } mce_end_element(&r);
  202. mce_start_element(&r, _X(dc_ns), _X("title")) {
  203. _opcCorePropertiesReadSimpleType(&r, &cp->title);
  204. } mce_end_element(&r);
  205. mce_start_element(&r, _X(cp_ns), _X("version")) {
  206. _opcCorePropertiesReadString(&r, &cp->version);
  207. } mce_end_element(&r);
  208. } mce_end_children(&r);
  209. } mce_end_element(&r);
  210. } mce_end_document(&r);
  211. mceTextReaderCleanup(&r);
  212. }
  213. return OPC_ERROR_NONE;
  214. }
  215. static void _opcCorePropertiesWriteString(mceTextWriter *w, const xmlChar *ns, const xmlChar *name, const xmlChar *value, const xmlChar *type) {
  216. if (NULL!=value) {
  217. mceTextWriterStartElement(w, ns, name);
  218. if (NULL!=type) {
  219. mceTextWriterAttributeF(w, _X(xsi_ns), _X("type"), (const char *)type);
  220. }
  221. mceTextWriterWriteString(w, value);
  222. mceTextWriterEndElement(w, ns, name);
  223. }
  224. }
  225. static void _opcCorePropertiesWriteSimpleType(mceTextWriter *w, const xmlChar *ns, const xmlChar *name, const opcDCSimpleType_t*value) {
  226. if (NULL!=value->str) {
  227. mceTextWriterStartElement(w, ns, name);
  228. if (NULL!=value->lang) {
  229. mceTextWriterAttributeF(w, _X(xml_ns), _X("lang"), (const char *)value->lang);
  230. }
  231. mceTextWriterWriteString(w, value->str);
  232. mceTextWriterEndElement(w, ns, name);
  233. }
  234. }
  235. opc_error_t opcCorePropertiesWrite(opcProperties_t *cp, opcContainer *c) {
  236. opcPart part=opcPartCreate(c, _X("docProps/core.xml"), _X("application/vnd.openxmlformats-package.core-properties+xml"), 0);
  237. if (NULL!=part) {
  238. mceTextWriter *w=mceTextWriterOpen(c, part, OPC_COMPRESSIONOPTION_SUPERFAST);
  239. if (NULL!=w) {
  240. mceTextWriterStartDocument(w);
  241. mceTextWriterRegisterNamespace(w, _X(cp_ns), _X("cp"), MCE_DEFAULT);
  242. mceTextWriterRegisterNamespace(w, _X(dc_ns), _X("dc"), MCE_DEFAULT);
  243. mceTextWriterRegisterNamespace(w, _X(dcterms_ns), _X("dcterms"), MCE_DEFAULT);
  244. mceTextWriterRegisterNamespace(w, _X(xsi_ns), _X("xsi"), MCE_DEFAULT);
  245. mceTextWriterStartElement(w, _X(cp_ns), _X("coreProperties"));
  246. _opcCorePropertiesWriteString(w, _X(cp_ns), _X("category"), cp->category, NULL);
  247. _opcCorePropertiesWriteString(w, _X(cp_ns), _X("contentStatus"), cp->contentStatus, NULL);
  248. _opcCorePropertiesWriteString(w, _X(dcterms_ns), _X("created"), cp->created, _X("dcterms:W3CDTF"));
  249. _opcCorePropertiesWriteSimpleType(w, _X(dc_ns), _X("creator"), &cp->creator);
  250. _opcCorePropertiesWriteSimpleType(w, _X(dc_ns), _X("description"), &cp->description);
  251. _opcCorePropertiesWriteSimpleType(w, _X(dc_ns), _X("identifier"), &cp->identifier);
  252. if (cp->keyword_items>0) {
  253. mceTextWriterStartElement(w, _X(cp_ns), _X("keywords"));
  254. for(opc_uint32_t i=0;i<cp->keyword_items;i++) {
  255. _opcCorePropertiesWriteSimpleType(w, _X(cp_ns), _X("value"), &cp->keyword_array[i]);
  256. if (i+1<cp->keyword_items) {
  257. mceTextWriterWriteString(w, _X(";"));
  258. }
  259. }
  260. mceTextWriterEndElement(w, _X(cp_ns), _X("keywords"));
  261. }
  262. _opcCorePropertiesWriteSimpleType(w, _X(dc_ns), _X("language"), &cp->language);
  263. _opcCorePropertiesWriteString(w, _X(cp_ns), _X("lastModifiedBy"), cp->lastModifiedBy, NULL);
  264. _opcCorePropertiesWriteString(w, _X(cp_ns), _X("lastPrinted"), cp->lastPrinted, _X("dcterms:W3CDTF"));
  265. _opcCorePropertiesWriteString(w, _X(dcterms_ns), _X("modified"), cp->modified, _X("dcterms:W3CDTF"));
  266. _opcCorePropertiesWriteString(w, _X(cp_ns), _X("revision"), cp->revision, NULL);
  267. _opcCorePropertiesWriteSimpleType(w, _X(dc_ns), _X("subject"), &cp->subject);
  268. _opcCorePropertiesWriteSimpleType(w, _X(dc_ns), _X("title"), &cp->title);
  269. _opcCorePropertiesWriteString(w, _X(cp_ns), _X("version"), cp->version, NULL);
  270. mceTextWriterEndElement(w, _X(cp_ns), _X("coreProperties"));
  271. mceTextWriterEndDocument(w);
  272. mceTextWriterFree(w);
  273. }
  274. }
  275. return OPC_ERROR_NONE;
  276. }