| 1 | |
| 2 | /* |
| 3 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * Authors: |
| 21 | * Lukas Kuklinek <[email protected]> |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * text.c |
| 26 | * |
| 27 | * Created on: Jan 14, 2010 |
| 28 | * Author: david.niemoller |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <string.h> |
| 36 | #include <stdio.h> |
| 37 | |
| 38 | #include "text_priv.h" |
| 39 | #include "util.h" |
| 40 | #include "list.h" |
| 41 | #include "oscap_helpers.h" |
| 42 | |
| 43 | const char * const OSCAP_LANG_ENGLISH = "en"; |
| 44 | const char * const OSCAP_LANG_ENGLISH_US = "en-US"; |
| 45 | const char * const OSCAP_LANG_DEFAULT = "en-US"; |
| 46 | |
| 47 | const struct oscap_text_traits OSCAP_TEXT_TRAITS_PLAIN = { .html = false }; |
| 48 | const struct oscap_text_traits OSCAP_TEXT_TRAITS_HTML = { .html = true }; |
| 49 | |
| 50 | |
| 51 | OSCAP_ACCESSOR_STRING(oscap_text, text) |
| 52 | OSCAP_ACCESSOR_STRING(oscap_text, lang) |
| 53 | OSCAP_GENERIC_GETTER(bool, oscap_text, is_html, traits.html) |
| 54 | OSCAP_GENERIC_GETTER(bool, oscap_text, can_substitute, traits.can_substitute) |
| 55 | OSCAP_GENERIC_GETTER(bool, oscap_text, can_override, traits.can_override) |
| 56 | OSCAP_GENERIC_GETTER(bool, oscap_text, overrides, traits.overrides) |
| 57 | OSCAP_ITERATOR_GEN_T(struct oscap_text *, oscap_text) |
| 58 | /*OSCAP_ITERATOR_RESET(oscap_text)*/ |
| 59 | OSCAP_ITERATOR_REMOVE_T(struct oscap_text *, oscap_text, oscap_text_free) |
| 60 | |
| 61 | |
| 62 | bool oscap_text_set_overrides(struct oscap_text *text, bool overrides) |
| 63 | { |
| 64 | text->traits.overrides = overrides; |
| 65 | return text->traits.can_override; |
| 66 | } |
| 67 | |
| 68 | void oscap_text_free(struct oscap_text *text) |
| 69 | { |
| 70 | if (text != NULL) { |
| 71 | free(text->lang); |
| 72 | free(text->text); |
| 73 | free(text); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | struct oscap_text *oscap_text_new_full(struct oscap_text_traits traits, const char *string, const char *lang) |
| 78 | { |
| 79 | struct oscap_text *text = calloc(1, sizeof(struct oscap_text)); |
| 80 | text->traits = traits; |
| 81 | text->text = oscap_strdup(string); |
| 82 | text->lang = oscap_strdup(lang); |
| 83 | return text; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | struct oscap_text *oscap_text_new(void) |
| 88 | { |
| 89 | return oscap_text_new_full(OSCAP_TEXT_TRAITS_PLAIN, NULL, NULL); |
| 90 | } |
| 91 | |
| 92 | struct oscap_text * oscap_text_clone(const struct oscap_text * text) |
| 93 | { |
| 94 | return oscap_text_new_full(text->traits, text->text, text->lang); |
| 95 | } |
| 96 | |
| 97 | struct oscap_text *oscap_text_new_html(void) |
| 98 | { |
| 99 | //assert(false); // TODO implement |
| 100 | return oscap_text_new_full(OSCAP_TEXT_TRAITS_HTML, NULL, NULL); |
| 101 | } |
| 102 | |
| 103 | struct oscap_text *oscap_text_new_parse(struct oscap_text_traits traits, xmlTextReaderPtr reader) |
| 104 | { |
| 105 | assert(reader != NULL); |
| 106 | |
| 107 | // new text |
| 108 | struct oscap_text *text = oscap_text_new_full(traits, NULL, NULL); |
| 109 | |
| 110 | // extract 'override' attribute |
| 111 | if (text->traits.can_override) { |
| 112 | xmlTextReaderMoveToAttribute(reader, BAD_CAST "override"); |
| 113 | text->traits.overrides = oscap_string_to_enum(OSCAP_BOOL_MAP, (const char *) xmlTextReaderConstValue(reader)); |
| 114 | if (xmlTextReaderConstValue(reader) != NULL) text->traits.override_given = true; |
| 115 | } |
| 116 | |
| 117 | // extract language |
| 118 | text->lang = (char *) xmlTextReaderXmlLang(reader); |
| 119 | |
| 120 | xmlTextReaderMoveToElement(reader); |
| 121 | |
| 122 | // extract content |
| 123 | if (text->traits.html || text->traits.can_substitute) |
| 124 | text->text = oscap_get_xml(reader); |
| 125 | else text->text = oscap_element_string_copy(reader); |
| 126 | |
| 127 | return text; |
| 128 | } |
| 129 | |
| 130 | xmlNode *oscap_text_to_dom(struct oscap_text *text, xmlNode *parent, const char *elname) |
| 131 | { |
| 132 | if (!text) return NULL; |
| 133 | |
| 134 | xmlNode *text_node = NULL; |
| 135 | |
| 136 | if (text->traits.html || text->traits.can_substitute) { |
| 137 | text_node = oscap_xmlstr_to_dom(parent, elname, text->text); |
| 138 | // make sure we use parent's namespace |
| 139 | xmlSetNs(text_node, parent->ns); |
| 140 | } |
| 141 | else { |
| 142 | // NULL as ns here means that namespace is inherited from parent |
| 143 | text_node = xmlNewTextChild(parent, NULL, BAD_CAST elname, BAD_CAST text->text); |
| 144 | } |
| 145 | |
| 146 | if (text_node == NULL) return NULL; |
| 147 | |
| 148 | if (text->lang) |
| 149 | xmlNodeSetLang(text_node, BAD_CAST text->lang); |
| 150 | if (text->traits.can_override && text->traits.overrides) |
| 151 | xmlNewProp(text_node, BAD_CAST "override", BAD_CAST "true"); |
| 152 | |
| 153 | |
| 154 | return text_node; |
| 155 | } |
| 156 | |
| 157 | bool oscap_text_export(struct oscap_text *text, xmlTextWriter *writer, const char *elname) |
| 158 | { |
| 159 | if (text == NULL || writer == NULL) return false; |
| 160 | |
| 161 | if (elname) xmlTextWriterStartElement(writer, BAD_CAST elname); |
| 162 | |
| 163 | if (text->lang) |
| 164 | xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang", BAD_CAST text->lang); |
| 165 | if (text->traits.can_override && text->traits.overrides) |
| 166 | xmlTextWriterWriteAttribute(writer, BAD_CAST "override", BAD_CAST "true"); |
| 167 | |
| 168 | if (text->traits.html || text->traits.can_substitute) |
| 169 | xmlTextWriterWriteRaw(writer, BAD_CAST text->text); |
| 170 | else xmlTextWriterWriteString(writer, BAD_CAST text->text); |
| 171 | |
| 172 | if (elname) xmlTextWriterEndElement(writer); |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | char *_xhtml_to_plaintext(const char *xhtml_in) |
| 178 | { |
| 179 | char *out = NULL; |
| 180 | char *str = oscap_sprintf("<x xmlns='http://www.w3.org/1999/xhtml'>%s</x>", xhtml_in); |
| 181 | xmlDoc *doc = xmlParseMemory(str, strlen(str)); |
| 182 | if (doc == NULL) { |
| 183 | free(str); |
| 184 | return NULL; |
| 185 | } |
| 186 | xmlNode *root = xmlDocGetRootElement(doc); |
| 187 | if (root == NULL) { |
| 188 | free(str); |
| 189 | return out; |
| 190 | } |
| 191 | |
| 192 | // TODO: better HTML -> plaintext conversion |
| 193 | // (perhaps use xml_iterate) |
| 194 | out = (char*) xmlNodeGetContent(root); |
| 195 | xmlFreeDoc(doc); |
| 196 | free(str); |
| 197 | return out; |
| 198 | } |
| 199 | |
| 200 | char *oscap_text_get_plaintext(const struct oscap_text *text) |
| 201 | { |
| 202 | if (text == NULL) return NULL; |
| 203 | |
| 204 | if (!text->traits.html) return oscap_strdup(text->text); |
| 205 | |
| 206 | return _xhtml_to_plaintext(text->text); |
| 207 | } |
| 208 | |
| 209 | bool oscap_textlist_export(struct oscap_text_iterator *texts, xmlTextWriter *writer, const char *elname) |
| 210 | { |
| 211 | if (texts == NULL || writer == NULL || elname == NULL) return false; |
| 212 | |
| 213 | OSCAP_FOR(oscap_text, text, texts) |
| 214 | oscap_text_export(text, writer, elname); |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | struct oscap_text *oscap_textlist_get_preferred_text(struct oscap_text_iterator *texts, const char *preferred_lang) |
| 220 | { |
| 221 | if (preferred_lang == NULL) |
| 222 | preferred_lang = OSCAP_LANG_DEFAULT; |
| 223 | |
| 224 | oscap_text_iterator_reset(texts); |
| 225 | |
| 226 | while (oscap_text_iterator_has_more(texts)) { |
| 227 | struct oscap_text *text = oscap_text_iterator_next(texts); |
| 228 | const char *lang = oscap_text_get_lang(text); |
| 229 | if (lang && strcmp(lang, preferred_lang) == 0) |
| 230 | return text; |
| 231 | } |
| 232 | |
| 233 | // not found as exact match in preferred language, try to find implicit match |
| 234 | oscap_text_iterator_reset(texts); |
| 235 | while (oscap_text_iterator_has_more(texts)) { |
| 236 | struct oscap_text *text = oscap_text_iterator_next(texts); |
| 237 | const char *lang = oscap_text_get_lang(text); |
| 238 | if (lang == NULL) |
| 239 | return text; |
| 240 | } |
| 241 | |
| 242 | // fallback to any language |
| 243 | oscap_text_iterator_reset(texts); |
| 244 | while (oscap_text_iterator_has_more(texts)) { |
| 245 | struct oscap_text *text = oscap_text_iterator_next(texts); |
| 246 | return text; |
| 247 | } |
| 248 | |
| 249 | // nothing found |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | char *oscap_textlist_get_preferred_plaintext(struct oscap_text_iterator *texts, const char *preferred_lang) |
| 254 | { |
| 255 | struct oscap_text *text = oscap_textlist_get_preferred_text(texts, preferred_lang); |
| 256 | return (text != NULL) ? oscap_text_get_plaintext(text) : NULL; |
| 257 | } |