GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 70.6% 396 / 0 / 561
Functions: 75.9% 44 / 0 / 58
Branches: 57.9% 490 / 0 / 847

src/utilities/bcl/BCLXML.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "BCLXML.hpp"
7
8 #include "../units/Quantity.hpp"
9 #include "../core/Checksum.hpp"
10 #include "../core/FilesystemHelpers.hpp"
11 #include "../time/DateTime.hpp"
12 #include "../xml/XMLValidator.hpp"
13
14 #include <json/value.h>
15 #include <pugixml.hpp>
16 #include <json/json.h>
17
18 #include <algorithm>
19 #include <sstream>
20
21 namespace openstudio {
22
23 34 VersionString BCLXML::currentSchemaVersion() {
24 34 return {3, 1};
25 }
26
27 482 BCLXML::BCLXML(const BCLXMLType& bclXMLType)
28 482 : m_bclXMLType(bclXMLType),
29
2/2
✓ Branch 1 taken 482 times.
✓ Branch 4 taken 482 times.
482 m_uid{removeBraces(openstudio::createUUID())},
30
2/2
✓ Branch 1 taken 482 times.
✓ Branch 4 taken 482 times.
482 m_versionId{removeBraces(openstudio::createUUID())},
31
2/2
✓ Branch 6 taken 482 times.
✓ Branch 9 taken 482 times.
964 m_versionModified{DateTime::nowUTC().toXsdDateTime()} {}
32
33
1/1
✓ Branch 2 taken 478 times.
478 BCLXML::BCLXML(const openstudio::path& xmlPath) : m_path(openstudio::filesystem::system_complete(xmlPath)) {
34
3/3
✓ Branch 1 taken 478 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 473 times.
478 if (!openstudio::filesystem::exists(xmlPath)) {
35
19/19
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✓ Branch 10 taken 5 times.
✓ Branch 13 taken 5 times.
✓ Branch 17 taken 5 times.
✓ Branch 20 taken 5 times.
✓ Branch 23 taken 5 times.
✓ Branch 29 taken 5 times.
✓ Branch 32 taken 5 times.
✓ Branch 35 taken 5 times.
✓ Branch 38 taken 5 times.
✓ Branch 41 taken 5 times.
✓ Branch 44 taken 5 times.
✓ Branch 47 taken 5 times.
✓ Branch 50 taken 5 times.
✓ Branch 53 taken 5 times.
✓ Branch 58 taken 5 times.
✓ Branch 61 taken 5 times.
5 LOG_AND_THROW("'" << openstudio::toString(xmlPath) << "' does not exist");
36
2/3
✓ Branch 1 taken 473 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 473 times.
473 } else if (!openstudio::filesystem::is_regular_file(xmlPath)) {
37 ✗ LOG_AND_THROW("'" << openstudio::toString(xmlPath) << "' cannot be opened for reading BCL XML data");
38 }
39
40
1/1
✓ Branch 1 taken 473 times.
473 pugi::xml_document bclXML;
41
42
1/1
✓ Branch 1 taken 473 times.
473 openstudio::filesystem::ifstream file(m_path);
43
1/2
✓ Branch 1 taken 473 times.
✗ Branch 2 not taken.
473 if (file.is_open()) {
44
1/1
✓ Branch 1 taken 473 times.
473 auto result = bclXML.load(file);
45
2/3
✓ Branch 1 taken 473 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 473 times.
473 if (!result) {
46 ✗ LOG_AND_THROW("'" << openstudio::toString(xmlPath) << "' could not be read as XML data");
47 }
48
49
1/1
✓ Branch 1 taken 473 times.
473 file.close();
50 } else {
51 ✗ file.close();
52 ✗ LOG_AND_THROW("'" << openstudio::toString(xmlPath) << "' could not be opened");
53 }
54
55
1/1
✓ Branch 1 taken 473 times.
473 auto element = bclXML.child("component");
56
2/3
✓ Branch 1 taken 473 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 473 times.
473 if (element) {
57 ✗ m_bclXMLType = BCLXMLType::ComponentXML;
58 } else {
59
1/1
✓ Branch 1 taken 473 times.
473 element = bclXML.child("measure");
60
2/3
✓ Branch 1 taken 473 times.
✓ Branch 3 taken 473 times.
✗ Branch 4 not taken.
473 if (element) {
61
1/1
✓ Branch 1 taken 473 times.
473 m_bclXMLType = BCLXMLType::MeasureXML;
62 } else {
63 ✗ LOG_AND_THROW("'" << openstudio::toString(xmlPath) << "' is not a correct BCL XML");
64 }
65 }
66
67 // Avoids gettings lots of 'Could not parse '' as a version string'
68 1393 auto parseVersionIfExistsAndNotEmpty = [](const pugi::xml_node& vElement) -> boost::optional<VersionString> {
69
3/3
✓ Branch 1 taken 1393 times.
✓ Branch 3 taken 461 times.
✓ Branch 4 taken 932 times.
1393 if (!vElement) {
70 461 return boost::none;
71 }
72
3/3
✓ Branch 1 taken 932 times.
✓ Branch 4 taken 932 times.
✓ Branch 7 taken 932 times.
932 const std::string vstr = vElement.text().as_string();
73
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 932 times.
932 if (vstr.empty()) {
74 ✗ return boost::none;
75 }
76 932 boost::optional<VersionString> result;
77
78 try {
79
2/2
✓ Branch 1 taken 932 times.
✓ Branch 4 taken 932 times.
932 result = VersionString(vstr);
80 ✗ } catch (const std::exception&) {
81 ✗ }
82 932 return result;
83 932 };
84
85 // get schema version to see if we need to upgrade anything
86 // added in schema version 3
87
6/6
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
✓ Branch 13 taken 473 times.
✓ Branch 16 taken 473 times.
1419 const VersionString startingVersion = parseVersionIfExistsAndNotEmpty(element.child("schema_version")).get_value_or(VersionString("2.0"));
88
89 // validate the gbxml prior to reverse translation
90
1/1
✓ Branch 1 taken 473 times.
473 auto bclXMLValidator = XMLValidator::bclXMLValidator(m_bclXMLType, startingVersion);
91
3/3
✓ Branch 1 taken 473 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 460 times.
473 if (!bclXMLValidator.validate(xmlPath)) {
92
6/6
✓ Branch 1 taken 13 times.
✓ Branch 4 taken 13 times.
✓ Branch 7 taken 13 times.
✓ Branch 10 taken 13 times.
✓ Branch 13 taken 13 times.
✓ Branch 16 taken 13 times.
13 LOG(Warn, "Failed to validate measure.xml at " << xmlPath);
93 }
94
95
5/5
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
✓ Branch 13 taken 473 times.
473 m_name = decodeString(element.child("name").text().as_string());
96
97
4/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
473 m_uid = element.child("uid").text().as_string();
98
99
4/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
473 m_versionId = element.child("version_id").text().as_string();
100
101
4/8
✓ Branch 1 taken 473 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 473 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 473 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 473 times.
473 if (m_name.empty() || m_uid.empty() || m_versionId.empty()) {
102 ✗ LOG_AND_THROW("'" << openstudio::toString(xmlPath) << "' is not a correct BCL XML");
103 }
104
105 // added in schema version 3
106 // error is only present if something went wrong, so we check if it exists first
107 // to avoid always initializing m_error to a string (even if empty)
108 // note: decodeString(element.child("error").text().as_string()) would always return a string even if 'error' key didn't exist
109
1/1
✓ Branch 1 taken 473 times.
473 const pugi::xml_node errorElement = element.child("error");
110
2/3
✓ Branch 1 taken 473 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 473 times.
473 if (errorElement) {
111 ✗ m_error = errorElement.text().as_string();
112 }
113
114
4/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 6 taken 472 times.
✓ Branch 7 taken 1 time.
473 if (auto subelement = element.child("version_modified")) {
115
3/3
✓ Branch 1 taken 472 times.
✓ Branch 4 taken 472 times.
✓ Branch 7 taken 472 times.
472 m_versionModified = subelement.text().as_string();
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 472 times.
472 if (m_versionModified.empty()) {
117 // fromXsdDateTime forwards to fromISO8601 and handles both formats
118 ✗ if (!DateTime::fromXsdDateTime(m_versionModified)) {
119 // not an allowable date time
120 ✗ m_versionModified = "";
121 }
122 }
123 }
124
125
2/3
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✗ Branch 5 not taken.
473 if (m_bclXMLType == BCLXMLType::MeasureXML) {
126 // added in schema version 3
127
5/5
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
✓ Branch 13 taken 473 times.
473 m_className = decodeString(element.child("class_name").text().as_string());
128 }
129
130 // added in schema version 3
131
5/5
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
✓ Branch 13 taken 473 times.
473 m_displayName = decodeString(element.child("display_name").text().as_string());
132
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 459 times.
473 if (m_displayName.empty()) {
133 // use name
134
1/1
✓ Branch 1 taken 14 times.
14 m_displayName = m_name;
135 }
136
137
5/5
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
✓ Branch 13 taken 473 times.
473 m_description = decodeString(element.child("description").text().as_string());
138
139
2/3
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✗ Branch 5 not taken.
473 if (m_bclXMLType == BCLXMLType::MeasureXML) {
140
5/5
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
✓ Branch 13 taken 473 times.
473 m_modelerDescription = decodeString(element.child("modeler_description").text().as_string());
141
142
4/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 6 taken 472 times.
✓ Branch 7 taken 1 time.
473 if (auto subelement = element.child("arguments")) {
143
6/6
✓ Branch 1 taken 472 times.
✓ Branch 6 taken 293 times.
✓ Branch 9 taken 293 times.
✓ Branch 12 taken 765 times.
✓ Branch 14 taken 293 times.
✓ Branch 15 taken 472 times.
765 for (auto& arg : subelement.children("argument")) {
144 try {
145
1/1
✓ Branch 1 taken 293 times.
293 m_arguments.emplace_back(arg);
146 ✗ } catch (const std::exception&) {
147 ✗ LOG(Error, "Bad argument in BCL XML");
148 ✗ }
149 }
150 }
151
152
4/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 6 taken 472 times.
✓ Branch 7 taken 1 time.
473 if (auto subelement = element.child("outputs")) {
153
6/6
✓ Branch 1 taken 472 times.
✓ Branch 6 taken 141 times.
✓ Branch 9 taken 141 times.
✓ Branch 12 taken 613 times.
✓ Branch 14 taken 141 times.
✓ Branch 15 taken 472 times.
613 for (auto& outputElement : subelement.children("output")) {
154
3/4
✓ Branch 1 taken 141 times.
✓ Branch 4 taken 141 times.
✓ Branch 6 taken 141 times.
✗ Branch 7 not taken.
141 if (outputElement.first_child() != nullptr) {
155 try {
156
1/1
✓ Branch 1 taken 141 times.
141 m_outputs.emplace_back(outputElement);
157 ✗ } catch (const std::exception&) {
158 ✗ LOG(Error, "Bad output in BCL XML");
159 ✗ }
160 }
161 }
162 }
163 }
164
165
3/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 6 taken 473 times.
✗ Branch 7 not taken.
473 if (auto subelement = element.child("files")) {
166
6/6
✓ Branch 1 taken 473 times.
✓ Branch 6 taken 772 times.
✓ Branch 9 taken 772 times.
✓ Branch 12 taken 1245 times.
✓ Branch 14 taken 772 times.
✓ Branch 15 taken 473 times.
1245 for (auto& fileElement : subelement.children("file")) {
167
3/4
✓ Branch 1 taken 772 times.
✓ Branch 4 taken 772 times.
✓ Branch 6 taken 772 times.
✗ Branch 7 not taken.
772 if (fileElement.first_child() != nullptr) {
168
169 772 std::string softwareProgram;
170 772 std::string softwareProgramVersion;
171 772 boost::optional<VersionString> minCompatibleVersion;
172 772 boost::optional<VersionString> maxCompatibleVersion;
173
1/1
✓ Branch 1 taken 772 times.
772 auto versionElement = fileElement.child("version");
174
3/3
✓ Branch 1 taken 772 times.
✓ Branch 3 taken 460 times.
✓ Branch 4 taken 312 times.
772 if (versionElement) {
175
1/1
✓ Branch 1 taken 460 times.
460 auto softwareProgramElement = versionElement.child("software_program");
176
3/3
✓ Branch 1 taken 460 times.
✓ Branch 4 taken 460 times.
✓ Branch 7 taken 460 times.
460 softwareProgram = softwareProgramElement.text().as_string();
177
1/1
✓ Branch 1 taken 460 times.
460 auto softwareProgramVersionElement = versionElement.child("identifier");
178
3/3
✓ Branch 1 taken 460 times.
✓ Branch 4 taken 460 times.
✓ Branch 7 taken 460 times.
460 softwareProgramVersion = softwareProgramVersionElement.text().as_string();
179
180 // added in schema version 3
181
1/1
✓ Branch 1 taken 460 times.
460 auto minCompatibleVersionElement = versionElement.child("min_compatible");
182
183
3/3
✓ Branch 1 taken 460 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 449 times.
460 if (!minCompatibleVersionElement) {
184
1/1
✓ Branch 1 taken 11 times.
11 minCompatibleVersion = parseVersionIfExistsAndNotEmpty(softwareProgramVersionElement);
185 } else {
186
1/1
✓ Branch 1 taken 449 times.
449 minCompatibleVersion = parseVersionIfExistsAndNotEmpty(minCompatibleVersionElement);
187 }
188
189 // added in schema version 3
190
1/1
✓ Branch 1 taken 460 times.
460 auto maxCompatibleVersionElement = versionElement.child("max_compatible");
191
1/1
✓ Branch 1 taken 460 times.
460 maxCompatibleVersion = parseVersionIfExistsAndNotEmpty(maxCompatibleVersionElement);
192 }
193
4/4
✓ Branch 1 taken 772 times.
✓ Branch 4 taken 772 times.
✓ Branch 7 taken 772 times.
✓ Branch 10 taken 772 times.
772 const std::string fileName = fileElement.child("filename").text().as_string();
194 //std::string fileType = fileElement.firstChildElement("filetype").firstChild().nodeValue().toStdString();
195
4/4
✓ Branch 1 taken 772 times.
✓ Branch 4 taken 772 times.
✓ Branch 7 taken 772 times.
✓ Branch 10 taken 772 times.
772 const std::string usageType = fileElement.child("usage_type").text().as_string();
196
4/4
✓ Branch 1 taken 772 times.
✓ Branch 4 taken 772 times.
✓ Branch 7 taken 772 times.
✓ Branch 10 taken 772 times.
772 const std::string checkSum = fileElement.child("checksum").text().as_string();
197
198 772 openstudio::path relativePath;
199
200
3/3
✓ Branch 1 taken 772 times.
✓ Branch 3 taken 473 times.
✓ Branch 4 taken 299 times.
772 if (usageType == "script") {
201
1/1
✓ Branch 1 taken 473 times.
473 relativePath = toPath(fileName);
202
3/3
✓ Branch 1 taken 299 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 288 times.
299 } else if (usageType == "doc") {
203
2/2
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
22 relativePath = toPath("docs") / toPath(fileName);
204
3/3
✓ Branch 1 taken 288 times.
✓ Branch 3 taken 178 times.
✓ Branch 4 taken 110 times.
288 } else if (usageType == "test") {
205
2/2
✓ Branch 1 taken 178 times.
✓ Branch 4 taken 178 times.
356 relativePath = toPath("tests") / toPath(fileName);
206
3/3
✓ Branch 1 taken 110 times.
✓ Branch 3 taken 86 times.
✓ Branch 4 taken 24 times.
110 } else if (usageType == "resource") {
207
2/2
✓ Branch 1 taken 86 times.
✓ Branch 4 taken 86 times.
172 relativePath = toPath("resources") / toPath(fileName);
208 } else {
209
1/1
✓ Branch 1 taken 24 times.
24 relativePath = toPath(fileName);
210 }
211
212
2/2
✓ Branch 1 taken 772 times.
✓ Branch 4 taken 772 times.
772 BCLFileReference fileref(m_path.parent_path(), relativePath);
213
1/1
✓ Branch 1 taken 772 times.
772 fileref.setSoftwareProgram(softwareProgram);
214
1/1
✓ Branch 1 taken 772 times.
772 fileref.setSoftwareProgramVersion(softwareProgramVersion);
215
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 312 times.
772 if (minCompatibleVersion) {
216
2/2
✓ Branch 1 taken 460 times.
✓ Branch 4 taken 460 times.
460 fileref.setMinCompatibleVersion(*minCompatibleVersion);
217 }
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 772 times.
772 if (maxCompatibleVersion) {
219 ✗ fileref.setMaxCompatibleVersion(*maxCompatibleVersion);
220 }
221
1/1
✓ Branch 1 taken 772 times.
772 fileref.setUsageType(usageType);
222
1/1
✓ Branch 1 taken 772 times.
772 fileref.setChecksum(checkSum);
223
224
1/1
✓ Branch 1 taken 772 times.
772 m_files.push_back(fileref);
225
226 772 } else {
227 ✗ break;
228 }
229 }
230 }
231
232
3/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 6 taken 473 times.
✗ Branch 7 not taken.
473 if (auto subelement = element.child("attributes")) {
233
6/6
✓ Branch 1 taken 473 times.
✓ Branch 6 taken 1320 times.
✓ Branch 9 taken 1320 times.
✓ Branch 12 taken 1793 times.
✓ Branch 14 taken 1320 times.
✓ Branch 15 taken 473 times.
1793 for (auto& attributeElement : subelement.children("attribute")) {
234
3/4
✓ Branch 1 taken 1320 times.
✓ Branch 4 taken 1320 times.
✓ Branch 6 taken 1320 times.
✗ Branch 7 not taken.
1320 if (attributeElement.first_child() != nullptr) {
235
4/4
✓ Branch 1 taken 1320 times.
✓ Branch 4 taken 1320 times.
✓ Branch 7 taken 1320 times.
✓ Branch 10 taken 1320 times.
1320 const std::string name = attributeElement.child("name").text().as_string();
236
4/4
✓ Branch 1 taken 1320 times.
✓ Branch 4 taken 1320 times.
✓ Branch 7 taken 1320 times.
✓ Branch 10 taken 1320 times.
1320 const std::string value = attributeElement.child("value").text().as_string();
237
4/4
✓ Branch 1 taken 1320 times.
✓ Branch 4 taken 1320 times.
✓ Branch 7 taken 1320 times.
✓ Branch 10 taken 1320 times.
1320 const std::string datatype = attributeElement.child("datatype").text().as_string();
238
239 // Units are optional
240
4/4
✓ Branch 1 taken 1320 times.
✓ Branch 4 taken 1320 times.
✓ Branch 7 taken 1320 times.
✓ Branch 10 taken 1320 times.
1320 const std::string units = attributeElement.child("units").text().as_string();
241
242
3/3
✓ Branch 1 taken 1320 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 1316 times.
1320 if (datatype == "float") {
243
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (units.empty()) {
244
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 m_attributes.emplace_back(name, boost::lexical_cast<double>(value));
245 } else {
246 ✗ m_attributes.emplace_back(name, boost::lexical_cast<double>(value), units);
247 }
248
2/3
✓ Branch 1 taken 1316 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1316 times.
1316 } else if (datatype == "int") {
249 ✗ if (units.empty()) {
250 ✗ m_attributes.emplace_back(name, boost::lexical_cast<int>(value));
251 } else {
252 ✗ m_attributes.emplace_back(name, boost::lexical_cast<int>(value), units);
253 }
254
3/3
✓ Branch 1 taken 1316 times.
✓ Branch 3 taken 475 times.
✓ Branch 4 taken 841 times.
1316 } else if (datatype == "boolean") {
255
1/1
✓ Branch 1 taken 475 times.
475 const bool temp = (value == "true");
256
1/2
✓ Branch 1 taken 475 times.
✗ Branch 2 not taken.
475 if (units.empty()) {
257
1/1
✓ Branch 1 taken 475 times.
475 m_attributes.emplace_back(name, temp);
258 } else {
259 ✗ m_attributes.emplace_back(name, temp, units);
260 }
261 } else {
262 // Assume string
263
1/2
✓ Branch 1 taken 841 times.
✗ Branch 2 not taken.
841 if (units.empty()) {
264
1/1
✓ Branch 1 taken 841 times.
841 m_attributes.emplace_back(name, value);
265 } else {
266 ✗ m_attributes.emplace_back(name, value, units);
267 }
268 }
269 1320 } else {
270 ✗ break;
271 }
272 }
273 }
274
275
3/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 6 taken 473 times.
✗ Branch 7 not taken.
473 if (auto subelement = element.child("tags")) {
276
6/6
✓ Branch 1 taken 473 times.
✓ Branch 6 taken 461 times.
✓ Branch 9 taken 461 times.
✓ Branch 12 taken 934 times.
✓ Branch 14 taken 461 times.
✓ Branch 15 taken 473 times.
934 for (auto& tagElement : subelement.children("tag")) {
277
1/1
✓ Branch 1 taken 461 times.
461 auto text = tagElement.text();
278
2/3
✓ Branch 1 taken 461 times.
✓ Branch 3 taken 461 times.
✗ Branch 4 not taken.
461 if (!text.empty()) {
279
2/2
✓ Branch 1 taken 461 times.
✓ Branch 4 taken 461 times.
461 m_tags.emplace_back(text.as_string());
280 }
281 }
282 }
283
284 // added in schema version 3
285
4/4
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 473 times.
✓ Branch 7 taken 473 times.
✓ Branch 10 taken 473 times.
473 m_xmlChecksum = element.child("xml_checksum").text().as_string();
286 558 }
287
288 469 boost::optional<BCLXML> BCLXML::load(const openstudio::path& xmlPath) {
289 469 boost::optional<BCLXML> result;
290 try {
291
2/2
✓ Branch 1 taken 464 times.
✓ Branch 4 taken 464 times.
469 result = BCLXML(xmlPath);
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 } catch (const std::exception&) {
293 5 }
294 469 return result;
295 ✗ }
296
297 173 std::string BCLXML::escapeString(const std::string& txt) {
298 // seems that we don't need to do this anymore
299 173 return txt;
300
301 // http://stackoverflow.com/questions/2083754/why-shouldnt-apos-be-used-to-escape-single-quotes
302 // This code was dead already, with the return above.
303 // Commented out, but should likely be removed
304 // QString result = toQString(txt);//.replace("'", "#x27;");
305 // return result.toStdString();
306 }
307
308 2365 std::string BCLXML::decodeString(const std::string& txt) {
309 // seems that we don't need to do this anymore
310 // only thing that can't be in the text node on disk are '<' (should be '&lt;') and '&' (should be '&amp;')
311 2365 return txt;
312
313 // This code was dead already, with the return above.
314 // Commented out, but should likely be removed
315 // QString result = toQString(txt);//.replace("#x27;", "'");
316 // return result.toStdString();
317 }
318
319 61 std::string BCLXML::uid() const {
320 61 return m_uid;
321 }
322
323 989 std::string BCLXML::versionId() const {
324 989 return m_versionId;
325 }
326
327 63 boost::optional<DateTime> BCLXML::versionModified() const {
328 63 boost::optional<DateTime> result;
329
1/2
✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
63 if (!m_versionModified.empty()) {
330
2/2
✓ Branch 1 taken 63 times.
✓ Branch 4 taken 63 times.
63 result = DateTime::fromXsdDateTime(m_versionModified);
331 }
332 63 return result;
333 ✗ }
334
335 513 std::string BCLXML::xmlChecksum() const {
336 513 return m_xmlChecksum;
337 }
338
339 63 std::string BCLXML::name() const {
340 63 return m_name;
341 }
342
343 61 std::string BCLXML::displayName() const {
344 61 return m_displayName;
345 }
346
347 224 std::string BCLXML::className() const {
348 224 return m_className;
349 }
350
351 2 std::string BCLXML::description() const {
352 2 return m_description;
353 }
354
355 2 std::string BCLXML::modelerDescription() const {
356 2 return m_modelerDescription;
357 }
358
359 ✗ std::vector<BCLMeasureArgument> BCLXML::arguments() const {
360 ✗ return m_arguments;
361 }
362
363 ✗ std::vector<BCLMeasureOutput> BCLXML::outputs() const {
364 ✗ return m_outputs;
365 }
366
367 23 std::vector<BCLFileReference> BCLXML::files() const {
368 23 return m_files;
369 }
370
371 ✗ std::vector<BCLFileReference> BCLXML::files(const std::string& filetype) const {
372 ✗ std::vector<BCLFileReference> matches;
373 ✗ for (const BCLFileReference& file : m_files) {
374 ✗ if (file.fileType() == filetype) {
375 ✗ matches.push_back(file);
376 }
377 }
378 ✗ return matches;
379 ✗ }
380
381 10 std::vector<Attribute> BCLXML::attributes() const {
382 10 return m_attributes;
383 }
384
385 1581 std::vector<Attribute> BCLXML::getAttributes(const std::string& name) const {
386 1581 std::vector<Attribute> result;
387
2/2
✓ Branch 5 taken 4432 times.
✓ Branch 6 taken 1581 times.
6013 for (const Attribute& attribute : m_attributes) {
388
3/3
✓ Branch 1 taken 4432 times.
✓ Branch 5 taken 1312 times.
✓ Branch 6 taken 3120 times.
4432 if (attribute.name() == name) {
389
1/1
✓ Branch 1 taken 1312 times.
1312 result.push_back(attribute);
390 }
391 }
392 1581 return result;
393 ✗ }
394
395 60 std::vector<std::string> BCLXML::tags() const {
396 60 return m_tags;
397 }
398
399 1 openstudio::path BCLXML::path() const {
400 1 return m_path;
401 }
402
403 1 openstudio::path BCLXML::directory() const {
404 1 return m_path.parent_path();
405 }
406
407 ✗ boost::optional<std::string> BCLXML::error() const {
408 ✗ return m_error;
409 }
410
411 14 void BCLXML::resetXMLChecksum() {
412 14 incrementVersionId();
413 14 m_xmlChecksum = "00000000";
414 14 }
415
416 ✗ void BCLXML::setError(const std::string& error) {
417 ✗ incrementVersionId();
418 ✗ m_error = escapeString(error);
419 ✗ }
420
421 ✗ void BCLXML::resetError() {
422 ✗ incrementVersionId();
423 ✗ m_error.reset();
424 ✗ }
425
426 16 void BCLXML::setName(const std::string& name) {
427 16 incrementVersionId();
428
1/1
✓ Branch 1 taken 16 times.
16 m_name = escapeString(name);
429 16 }
430
431 14 void BCLXML::setDisplayName(const std::string& displayName) {
432 14 incrementVersionId();
433
1/1
✓ Branch 1 taken 14 times.
14 m_displayName = escapeString(displayName);
434 14 }
435
436 14 void BCLXML::setClassName(const std::string& className) {
437 14 incrementVersionId();
438
1/1
✓ Branch 1 taken 14 times.
14 m_className = escapeString(className);
439 14 }
440
441 15 void BCLXML::setDescription(const std::string& description) {
442 15 incrementVersionId();
443
1/1
✓ Branch 1 taken 15 times.
15 m_description = escapeString(description);
444 15 }
445
446 15 void BCLXML::setModelerDescription(const std::string& modelerDescription) {
447 15 incrementVersionId();
448
1/1
✓ Branch 1 taken 15 times.
15 m_modelerDescription = escapeString(modelerDescription);
449 15 }
450
451 14 void BCLXML::setArguments(const std::vector<BCLMeasureArgument>& arguments) {
452 14 incrementVersionId();
453 14 m_arguments = arguments;
454 14 }
455
456 14 void BCLXML::setOutputs(const std::vector<BCLMeasureOutput>& outputs) {
457 14 incrementVersionId();
458 14 m_outputs = outputs;
459 14 }
460
461 107 void BCLXML::addFile(const BCLFileReference& file) {
462
2/2
✓ Branch 1 taken 107 times.
✓ Branch 4 taken 107 times.
107 removeFile(file.path());
463
464 107 incrementVersionId();
465 107 m_files.push_back(file);
466 107 }
467
468 971 bool BCLXML::hasFile(const openstudio::path& path) const {
469 971 bool result = false;
470
471
1/1
✓ Branch 1 taken 971 times.
971 const openstudio::path test = openstudio::filesystem::system_complete(path);
472
473
2/2
✓ Branch 5 taken 1509 times.
✓ Branch 6 taken 479 times.
1988 for (const BCLFileReference& file : m_files) {
474
3/3
✓ Branch 1 taken 1509 times.
✓ Branch 4 taken 492 times.
✓ Branch 5 taken 1017 times.
3018 if (file.path() == test) {
475 492 result = true;
476 492 break;
477 }
478 }
479
480 971 return result;
481 971 }
482
483 113 bool BCLXML::removeFile(const openstudio::path& path) {
484 113 bool result = false;
485
486
1/1
✓ Branch 1 taken 113 times.
113 const openstudio::path test = openstudio::filesystem::system_complete(path);
487
488 113 std::vector<BCLFileReference> newFiles;
489
2/2
✓ Branch 5 taken 402 times.
✓ Branch 6 taken 113 times.
515 for (const BCLFileReference& file : m_files) {
490
3/3
✓ Branch 1 taken 402 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 396 times.
804 if (file.path() == test) {
491 6 result = true;
492 } else {
493
1/1
✓ Branch 1 taken 396 times.
396 newFiles.push_back(file);
494 }
495 }
496
497
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 107 times.
113 if (result) {
498
1/1
✓ Branch 1 taken 6 times.
6 incrementVersionId();
499
1/1
✓ Branch 1 taken 6 times.
6 m_files = newFiles;
500 }
501
502 113 return result;
503 113 }
504
505 ✗ void BCLXML::clearFiles() {
506 ✗ incrementVersionId();
507 ✗ m_files.clear();
508 ✗ }
509
510 37 void BCLXML::addAttribute(const Attribute& attribute) {
511 37 incrementVersionId();
512 37 m_attributes.push_back(attribute);
513 37 }
514
515 32 bool BCLXML::removeAttributes(const std::string& name) {
516 32 bool result = false;
517
518 32 std::vector<Attribute> newAttributes;
519
2/2
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 32 times.
54 for (const Attribute& attribute : m_attributes) {
520
3/3
✓ Branch 1 taken 22 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 17 times.
22 if (attribute.name() == name) {
521 5 result = true;
522 } else {
523
1/1
✓ Branch 1 taken 17 times.
17 newAttributes.push_back(attribute);
524 }
525 }
526
527
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 28 times.
32 if (result) {
528
1/1
✓ Branch 1 taken 4 times.
4 incrementVersionId();
529
1/1
✓ Branch 1 taken 4 times.
4 m_attributes = newAttributes;
530 }
531
532 32 return result;
533 32 }
534
535 ✗ void BCLXML::clearAttributes() {
536 ✗ incrementVersionId();
537 ✗ m_attributes.clear();
538 ✗ }
539
540 16 void BCLXML::addTag(const std::string& tagName) {
541 16 removeTag(tagName);
542
543 16 incrementVersionId();
544 16 m_tags.push_back(tagName);
545 16 }
546
547 16 bool BCLXML::removeTag(const std::string& tagName) {
548
1/1
✓ Branch 3 taken 16 times.
16 auto it = std::find(m_tags.begin(), m_tags.end(), tagName);
549
2/2
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 15 times.
16 if (it != m_tags.end()) {
550
1/1
✓ Branch 1 taken 1 time.
1 incrementVersionId();
551
1/1
✓ Branch 2 taken 1 time.
1 m_tags.erase(it);
552 1 return true;
553 }
554
555 15 return false;
556 }
557
558 ✗ void BCLXML::clearTags() {
559 ✗ incrementVersionId();
560 ✗ m_tags.clear();
561 ✗ }
562
563 33 pugi::xml_document BCLXML::toXML() const {
564
565
1/1
✓ Branch 1 taken 33 times.
33 pugi::xml_document doc;
566
567 //doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
568
569
1/1
✓ Branch 1 taken 33 times.
33 pugi::xml_node docElement;
570
2/3
✓ Branch 1 taken 33 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 33 times.
33 if (m_bclXMLType == BCLXMLType::ComponentXML) {
571 ✗ docElement = doc.append_child("component");
572
2/3
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
33 } else if (m_bclXMLType == BCLXMLType::MeasureXML) {
573
1/1
✓ Branch 1 taken 33 times.
33 docElement = doc.append_child("measure");
574 } else {
575 ✗ LOG_AND_THROW("Unknown BCLXMLType '" << m_bclXMLType.valueName() << "'.");
576 }
577
578
1/1
✓ Branch 1 taken 33 times.
33 auto element = docElement.append_child("schema_version");
579
1/1
✓ Branch 1 taken 33 times.
33 auto text = element.text();
580
2/2
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
33 const std::string schemaVersion = BCLXML::currentSchemaVersion().str();
581
1/1
✓ Branch 2 taken 33 times.
33 text.set(schemaVersion.c_str());
582
583
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
66 if (m_error) {
584 ✗ element = docElement.append_child("error");
585 ✗ text = element.text();
586 ✗ text.set(escapeString(*m_error).c_str());
587 }
588
589
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("name");
590
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
591
2/2
✓ Branch 1 taken 33 times.
✓ Branch 5 taken 33 times.
33 text.set(escapeString(m_name).c_str());
592
593
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("uid");
594
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
595
1/1
✓ Branch 2 taken 33 times.
33 text.set(m_uid.c_str());
596
597
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("version_id");
598
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
599
1/1
✓ Branch 2 taken 33 times.
33 text.set(m_versionId.c_str());
600
601
1/2
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
33 if (!m_versionModified.empty()) {
602
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("version_modified");
603
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
604
1/1
✓ Branch 2 taken 33 times.
33 text.set(m_versionModified.c_str());
605 }
606
607
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("xml_checksum");
608
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
609
1/1
✓ Branch 2 taken 33 times.
33 text.set(m_xmlChecksum.c_str());
610
611
2/3
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
33 if (m_bclXMLType == BCLXMLType::MeasureXML) {
612
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("class_name");
613
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
614
1/1
✓ Branch 2 taken 33 times.
33 text.set(m_className.c_str());
615 }
616
617
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("display_name");
618
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
619
1/1
✓ Branch 2 taken 33 times.
33 text.set(m_displayName.c_str());
620
621
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("description");
622
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
623
2/2
✓ Branch 1 taken 33 times.
✓ Branch 5 taken 33 times.
33 text.set(escapeString(m_description).c_str());
624
625
2/3
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
33 if (m_bclXMLType == BCLXMLType::MeasureXML) {
626
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("modeler_description");
627
1/1
✓ Branch 1 taken 33 times.
33 text = element.text();
628
2/2
✓ Branch 1 taken 33 times.
✓ Branch 5 taken 33 times.
33 text.set(escapeString(m_modelerDescription).c_str());
629
630
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("arguments");
631
2/2
✓ Branch 5 taken 15 times.
✓ Branch 6 taken 33 times.
48 for (const BCLMeasureArgument& argument : m_arguments) {
632
1/1
✓ Branch 1 taken 15 times.
15 auto argumentElement = element.append_child("argument");
633
1/1
✓ Branch 1 taken 15 times.
15 argument.writeValues(argumentElement);
634 }
635
636
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("outputs");
637
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
33 for (const BCLMeasureOutput& output : m_outputs) {
638 ✗ auto outputElement = element.append_child("output");
639 ✗ output.writeValues(outputElement);
640 }
641 }
642
643 // TODO: write provenances
644
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("provenances");
645
646 // write tags
647 // provenances isn't written so element above is not used... so ignore it here
648 // cppcheck-suppress redundantAssignment
649
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("tags");
650
2/2
✓ Branch 5 taken 21 times.
✓ Branch 6 taken 33 times.
54 for (const std::string& tag : m_tags) {
651
1/1
✓ Branch 1 taken 21 times.
21 auto tagElement = element.append_child("tag");
652
1/1
✓ Branch 1 taken 21 times.
21 text = tagElement.text();
653
1/1
✓ Branch 2 taken 21 times.
21 text.set(tag.c_str());
654 }
655
656 // write attributes
657
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("attributes");
658
2/2
✓ Branch 5 taken 61 times.
✓ Branch 6 taken 33 times.
94 for (const Attribute& attribute : m_attributes) {
659
660 61 std::string value;
661 61 std::string dataType;
662
4/9
✓ Branch 1 taken 61 times.
✓ Branch 4 taken 61 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 7 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 54 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
61 switch (attribute.valueType().value()) {
663 ✗ case AttributeValueType::Boolean:
664 ✗ if (attribute.valueAsBoolean()) {
665 ✗ value = "true";
666 } else {
667 ✗ value = "false";
668 }
669 ✗ dataType = "boolean";
670 ✗ break;
671 7 case AttributeValueType::Double:
672
1/1
✓ Branch 1 taken 7 times.
7 value = attribute.toString();
673
1/1
✓ Branch 1 taken 7 times.
7 dataType = "float";
674 7 break;
675 ✗ case AttributeValueType::Integer:
676 ✗ value = attribute.toString();
677 ✗ dataType = "integer";
678 ✗ break;
679 ✗ case AttributeValueType::Unsigned:
680 ✗ value = attribute.toString();
681 ✗ dataType = "unsigned";
682 ✗ break;
683 54 case AttributeValueType::String:
684
1/1
✓ Branch 1 taken 54 times.
54 value = attribute.toString();
685
1/1
✓ Branch 1 taken 54 times.
54 dataType = "string";
686 54 break;
687 ✗ case AttributeValueType::AttributeVector:
688 // can't handle this yet
689 ✗ continue;
690 break;
691 ✗ default:
692 // can't handle this yet
693 ✗ continue;
694 }
695
696
1/1
✓ Branch 1 taken 61 times.
61 auto attributeElement = element.append_child("attribute");
697
698
1/1
✓ Branch 1 taken 61 times.
61 auto subelement = attributeElement.append_child("name");
699
1/1
✓ Branch 1 taken 61 times.
61 text = subelement.text();
700
2/2
✓ Branch 1 taken 61 times.
✓ Branch 5 taken 61 times.
61 text.set(attribute.name().c_str());
701
702
1/1
✓ Branch 1 taken 61 times.
61 subelement = attributeElement.append_child("value");
703
1/1
✓ Branch 1 taken 61 times.
61 text = subelement.text();
704
1/1
✓ Branch 2 taken 61 times.
61 text.set(value.c_str());
705
706
1/1
✓ Branch 1 taken 61 times.
61 subelement = attributeElement.append_child("datatype");
707
1/1
✓ Branch 1 taken 61 times.
61 text = subelement.text();
708
1/1
✓ Branch 2 taken 61 times.
61 text.set(dataType.c_str());
709
710
1/1
✓ Branch 1 taken 61 times.
122 boost::optional<std::string> units = attribute.units();
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (units) {
712 ✗ subelement = attributeElement.append_child("units");
713 ✗ text = subelement.text();
714 ✗ text.set((*units).c_str());
715 }
716 61 }
717
718 // write files
719
1/1
✓ Branch 1 taken 33 times.
33 element = docElement.append_child("files");
720
721 // Fix for #4748 - sort files
722
1/1
✓ Branch 1 taken 33 times.
33 auto sortedFiles = m_files;
723
1/1
✓ Branch 3 taken 33 times.
33 std::sort(sortedFiles.begin(), sortedFiles.end());
724
2/2
✓ Branch 5 taken 143 times.
✓ Branch 6 taken 33 times.
176 for (const BCLFileReference& file : sortedFiles) {
725
1/1
✓ Branch 1 taken 143 times.
143 auto subelement = element.append_child("file");
726
1/1
✓ Branch 1 taken 143 times.
143 file.writeValues(subelement);
727 }
728
729 66 return doc;
730 33 }
731
732 ✗ Json::Value BCLXML::toJSON() const {
733
734 ✗ Json::Value root;
735
736 ✗ root["measure_dir"] = m_path.parent_path().generic_string();
737 ✗ if (m_bclXMLType == BCLXMLType::ComponentXML) {
738 ✗ root["type"] = "component";
739 ✗ } else if (m_bclXMLType == BCLXMLType::MeasureXML) {
740 ✗ root["type"] = "measure";
741 } else {
742 ✗ LOG_AND_THROW("Unknown BCLXMLType '" << m_bclXMLType.valueName() << "'.");
743 }
744
745 ✗ root["schema_version"] = BCLXML::currentSchemaVersion().str();
746
747 ✗ if (m_error) {
748 ✗ root["error"] = escapeString(*m_error);
749 }
750 ✗ root["name"] = escapeString(m_name);
751
752 ✗ root["uid"] = m_uid;
753 ✗ root["version_id"] = m_versionId;
754 ✗ if (boost::optional<DateTime> versionMod_ = versionModified()) {
755 ✗ root["version_modified"] = versionMod_->toISO8601();
756 ✗ }
757 ✗ root["xml_checksum"] = m_xmlChecksum;
758
759 ✗ if (m_bclXMLType == BCLXMLType::MeasureXML) {
760 ✗ root["class_name"] = m_className;
761 }
762
763 ✗ root["display_name"] = m_displayName;
764 ✗ root["description"] = m_description;
765 ✗ if (m_bclXMLType == BCLXMLType::MeasureXML) {
766
767 ✗ root["modeler_description"] = m_modelerDescription;
768
769 {
770 ✗ auto& arguments = root["arguments"];
771 ✗ for (const BCLMeasureArgument& argument : m_arguments) {
772 ✗ arguments.append(argument.toJSON());
773 }
774 }
775
776 {
777 ✗ auto& outputs = root["outputs"];
778 ✗ for (const BCLMeasureOutput& output : m_outputs) {
779 ✗ outputs.append(output.toJSON());
780 }
781 }
782 }
783
784 // TODO: write provenances
785 // root["provenances"] = Json::Value(Json::arrayValue);
786
787 // write tags
788 // provenances isn't written so element above is not used... so ignore it here
789 // cppcheck-suppress redundantAssignment
790 ✗ auto& tags = root["tags"];
791 ✗ for (const std::string& tag : m_tags) {
792 ✗ tags.append(tag);
793 }
794
795 // TODO: why isn't this using Attribute::toXml (which appears unused anywhere and could be repurposed to work exactly as intended)?!
796
797 // write attributes
798 ✗ auto& attributes = root["attributes"];
799 ✗ for (const Attribute& attribute : m_attributes) {
800 ✗ attributes.append(attribute.toJSON(true));
801 }
802
803 // write files
804 {
805 ✗ auto& files = root["files"];
806 // Fix for #4748 - sort files
807 ✗ auto sortedFiles = m_files;
808 ✗ std::sort(sortedFiles.begin(), sortedFiles.end());
809 ✗ for (const BCLFileReference& file : sortedFiles) {
810 ✗ files.append(file.toJSON());
811 }
812 ✗ }
813
814 ✗ return root;
815 ✗ }
816
817 ✗ std::string BCLXML::toJSONString() const {
818 ✗ return toJSON().toStyledString();
819 }
820
821 34 bool BCLXML::save() const {
822
823
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 33 times.
34 if (m_path.empty()) {
824
5/5
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
1 LOG(Warn, "Cannot save, since it has no known path. Use BCLXML::save(const openstudio::path& xmlPath) instead.");
825 1 return false;
826 }
827
828
1/1
✓ Branch 1 taken 33 times.
33 const pugi::xml_document doc = toXML();
829
2/3
✓ Branch 1 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
33 if (!doc) {
830 ✗ return false;
831 }
832
833 // write to disk
834
1/1
✓ Branch 1 taken 33 times.
33 openstudio::filesystem::ofstream file(m_path);
835
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (!file.is_open()) {
836 ✗ return false;
837 }
838
839
1/1
✓ Branch 1 taken 33 times.
33 doc.save(file, " ");
840
1/1
✓ Branch 1 taken 33 times.
33 file.close();
841 33 return true;
842 33 }
843
844 27 bool BCLXML::saveAs(const openstudio::path& xmlPath) {
845 27 incrementVersionId();
846
1/1
✓ Branch 1 taken 27 times.
27 m_path = openstudio::filesystem::system_complete(xmlPath);
847 27 return save();
848 }
849
850 ✗ std::string BCLXML::toString() const {
851
852 ✗ std::string result;
853
854 ✗ const pugi::xml_document doc = toXML();
855 ✗ if (!doc) {
856 ✗ return result;
857 }
858
859 ✗ std::stringstream ss;
860 ✗ doc.save(ss, " ");
861 ✗ result = ss.str();
862
863 ✗ return result;
864 ✗ }
865
866 ✗ std::ostream& operator<<(std::ostream& os, const BCLXML& bclXML) {
867 ✗ const pugi::xml_document doc = bclXML.toXML();
868 ✗ if (doc) {
869 ✗ doc.save(os, " ");
870 }
871
872 ✗ return os;
873 ✗ }
874
875 ✗ void BCLXML::changeUID() {
876 ✗ m_uid = removeBraces(openstudio::createUUID());
877 // DLM: should this call incrementVersionId() ?
878 ✗ }
879
880 323 void BCLXML::incrementVersionId() {
881
2/2
✓ Branch 1 taken 323 times.
✓ Branch 4 taken 323 times.
323 m_versionId = removeBraces(openstudio::createUUID());
882
2/2
✓ Branch 1 taken 323 times.
✓ Branch 4 taken 323 times.
323 m_versionModified = DateTime::nowUTC().toXsdDateTime();
883 323 }
884
885 9 bool BCLXML::checkForUpdatesXML() {
886
1/1
✓ Branch 1 taken 9 times.
9 std::string newChecksum = computeXMLChecksum();
887
888
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
9 if (m_xmlChecksum.empty()) {
889 // we are unsure if this is a real change or update from version 2
890 // set this here and return false
891
1/1
✓ Branch 1 taken 4 times.
4 m_xmlChecksum = newChecksum;
892 4 return false;
893 }
894
895
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if (m_xmlChecksum != newChecksum) {
896
1/1
✓ Branch 1 taken 5 times.
5 incrementVersionId();
897
1/1
✓ Branch 1 taken 5 times.
5 m_xmlChecksum = newChecksum;
898 5 return true;
899 }
900
901 ✗ return false;
902 9 }
903
904 16 void printAttributeForChecksum(std::ostream& os, const Attribute& attribute, const std::string& tabs) {
905
3/4
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 16 times.
16 if (attribute.valueType() == AttributeValueType::AttributeVector) {
906 ✗ for (const Attribute& child : attribute.valueAsAttributeVector()) {
907 ✗ printAttributeForChecksum(os, child, tabs + " ");
908 ✗ }
909 } else {
910
3/3
✓ Branch 3 taken 16 times.
✓ Branch 6 taken 16 times.
✓ Branch 9 taken 16 times.
16 os << tabs << "Name: " << attribute.name() << '\n';
911
2/3
✓ Branch 1 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 16 times.
32 if (attribute.displayName()) {
912 ✗ os << tabs << "Display Name: " << attribute.displayName().get() << '\n';
913 }
914
4/4
✓ Branch 3 taken 16 times.
✓ Branch 6 taken 16 times.
✓ Branch 9 taken 16 times.
✓ Branch 12 taken 16 times.
16 os << tabs << "Value Type: " << attribute.valueType().valueName() << '\n';
915
3/3
✓ Branch 3 taken 16 times.
✓ Branch 6 taken 16 times.
✓ Branch 9 taken 16 times.
16 os << tabs << "Value: " << attribute.toString() << '\n';
916
2/3
✓ Branch 1 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 16 times.
32 if (attribute.units()) {
917 ✗ os << tabs << "Units: " << attribute.units().get() << '\n';
918 }
919 16 os << '\n';
920 }
921 16 }
922
923 9 std::string BCLXML::computeXMLChecksum() const {
924 // DLM: CHANGING THE IMPLEMENTATION OF THIS FUNCTION WILL CAUSE
925 // CHECKSUMS TO BE COMPUTED DIFFERENTLY
926 // WE WANT TO AVOID FIGHTING WHERE DIFFERENT VERSIONS OF OPENSTUDIO
927 // COMPUTE THE CHECKSUM IN DIFFERENT WAYS
928
1/1
✓ Branch 1 taken 9 times.
9 std::stringstream ss;
929
930 // will be picked up when Ruby file changes
931 //ss << "Name: " << m_name << '\n';
932
933 // will be picked up when Ruby file changes
934 //ss << "Display Name: " << m_displayName << '\n';
935
936 // will be picked up when Ruby file changes
937 //ss << "Class Name: " << m_className << '\n';
938
939 // not managed manually
940 //ss << m_uid;
941 //ss << m_versionId;
942 //ss << m_xmlChecksum;
943
944 // will be picked up when Ruby file changes
945 //ss << "Description: " << m_description << '\n';
946
947 // will be picked up when Ruby file changes
948 //ss << "Modeler Description: " << m_modelerDescription << '\n';
949
950 // will be picked up when Ruby file changes
951 //ss << "Arguments: " << '\n';
952 //for (const BCLMeasureArgument& argument : m_arguments){
953 // ss << argument << '\n';
954 //}
955
956 // will be picked up when checkForUpdatesFiles
957 //ss << "Files: " << '\n';
958 //for (const BCLFileReference& file : m_files){
959 // ss << file << '\n';
960 //}
961
962 // attributes are edited in the xml
963
2/2
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
9 ss << "Attributes: " << '\n';
964
2/2
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 9 times.
25 for (const Attribute& attribute : m_attributes) {
965 //ss << attribute; // can't use this because attributes uuid are regenerated on each load
966
967 // DLM: in the end just create a new method that won't change
968
2/2
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
32 printAttributeForChecksum(ss, attribute, " ");
969 }
970
971 // tags are edited in the xml
972
2/2
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
9 ss << "Tags: " << '\n';
973
2/2
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 9 times.
14 for (const std::string& tag : m_tags) {
974
3/3
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
5 ss << " " << tag << '\n';
975 }
976
977 //std::cout << "Checksum computed on:" << '\n';
978 //std::cout << ss.str() << '\n';
979
980
1/1
✓ Branch 1 taken 9 times.
18 return checksum(ss);
981 9 }
982
983 } // namespace openstudio
984