refactored reason string and remediation instructions framework
[strongswan.git] / src / libimcv / imv / imv_remediation_string.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imv_remediation_string.h"
17
18 #include <utils/debug.h>
19
20 typedef struct private_imv_remediation_string_t private_imv_remediation_string_t;
21
22 /**
23 * Private data of an imv_remediation_string_t object.
24 */
25 struct private_imv_remediation_string_t {
26
27 /**
28 * Public members of imv_remediation_string_t
29 */
30 imv_remediation_string_t public;
31
32 /**
33 * XML or plaintext encoding
34 */
35 bool as_xml;
36
37 /**
38 * Preferred language
39 */
40 char *lang;
41
42 /**
43 * Contains the concatenated remediation instructions
44 */
45 chunk_t instructions;
46
47 };
48
49 METHOD(imv_remediation_string_t, add_instruction, void,
50 private_imv_remediation_string_t *this, imv_lang_string_t title[],
51 imv_lang_string_t description[], imv_lang_string_t itemsheader[],
52 linked_list_t *items)
53 {
54 char xml_format[] = " <instruction>\n"
55 " <title>%s</title>\n"
56 " <description>%s</description>\n"
57 "%s%s"
58 " </instruction>\n";
59 char *instruction, *format, *s_title, *s_description, *s_itemsheader;
60 size_t len;
61
62 s_title = imv_lang_string_select_string(title, this->lang);
63 s_description = imv_lang_string_select_string(description, this->lang);
64 s_itemsheader = imv_lang_string_select_string(itemsheader, this->lang);
65 len = strlen(s_title) + strlen(s_description);
66
67 if (this->as_xml)
68 {
69 format = xml_format;
70 len += strlen(xml_format) - 8;
71 }
72 else
73 {
74 format = this->instructions.len ? "\n%s\n%s%s%s" : "%s\n%s%s%s";
75 len += 2;
76 }
77 instruction = malloc(len + 1);
78 sprintf(instruction, format, s_title, s_description, "", "");
79 this->instructions = chunk_cat("mm", this->instructions,
80 chunk_create(instruction, strlen(instruction)));
81 }
82
83 METHOD(imv_remediation_string_t, get_encoding, chunk_t,
84 private_imv_remediation_string_t *this)
85 {
86 char xml_header[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
87 "<remediationinstructions>\n";
88 char xml_trailer[] = "</remediationinstructions>";
89
90 if (!this->instructions.len)
91 {
92 return chunk_empty;
93 }
94 if (this->as_xml)
95 {
96 this->instructions = chunk_cat("cmc",
97 chunk_create(xml_header, strlen(xml_header)),
98 this->instructions,
99 chunk_create(xml_trailer, strlen(xml_trailer))
100 );
101 }
102 return this->instructions;
103 }
104
105 METHOD(imv_remediation_string_t, destroy, void,
106 private_imv_remediation_string_t *this)
107 {
108 free(this->instructions.ptr);
109 free(this);
110 }
111
112 /**
113 * Described in header.
114 */
115 imv_remediation_string_t *imv_remediation_string_create(bool as_xml, char *lang)
116 {
117 private_imv_remediation_string_t *this;
118
119 INIT(this,
120 .public = {
121 .add_instruction = _add_instruction,
122 .get_encoding = _get_encoding,
123 .destroy = _destroy,
124 },
125 .as_xml = as_xml,
126 .lang = lang,
127 );
128
129 return &this->public;
130 }
131