Simplify a bit
[LibreOffice.git] / comphelper / source / misc / sequenceashashmap.cxx
blob647ccb74974d964874857d6b47f296764ae8317b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <boost/property_tree/json_parser.hpp>
24 #include <com/sun/star/beans/NamedValue.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/reflection/XIdlField.hpp>
28 #include <com/sun/star/reflection/theCoreReflection.hpp>
29 #include <comphelper/sequenceashashmap.hxx>
30 #include <comphelper/processfactory.hxx>
31 #include <comphelper/propertysequence.hxx>
32 #include <sal/log.hxx>
33 #include <o3tl/string_view.hxx>
34 #include <comphelper/sequence.hxx>
36 using namespace com::sun::star;
38 namespace
40 uno::Any jsonToUnoAny(const boost::property_tree::ptree& aTree)
42 uno::Any aAny;
43 uno::Any aValue;
44 sal_Int32 nFields;
45 uno::Reference<reflection::XIdlField> aField;
46 boost::property_tree::ptree aNodeNull, aNodeValue, aNodeField;
47 const std::string& rType = aTree.get<std::string>("type", "");
48 const std::string& rValue = aTree.get<std::string>("value", "");
49 uno::Sequence<uno::Reference<reflection::XIdlField>> aFields;
50 uno::Reference<reflection::XIdlClass> xIdlClass
51 = css::reflection::theCoreReflection::get(comphelper::getProcessComponentContext())
52 ->forName(OUString::fromUtf8(rType));
53 if (xIdlClass.is())
55 uno::TypeClass aTypeClass = xIdlClass->getTypeClass();
56 xIdlClass->createObject(aAny);
57 aFields = xIdlClass->getFields();
58 nFields = aFields.getLength();
59 aNodeValue = aTree.get_child("value", aNodeNull);
60 if (nFields > 0 && aNodeValue != aNodeNull)
62 for (sal_Int32 itField = 0; itField < nFields; ++itField)
64 aField = aFields[itField];
65 aNodeField = aNodeValue.get_child(aField->getName().toUtf8().getStr(), aNodeNull);
66 if (aNodeField != aNodeNull)
68 aValue = jsonToUnoAny(aNodeField);
69 aField->set(aAny, aValue);
73 else if (!rValue.empty())
75 if (aTypeClass == uno::TypeClass_VOID)
76 aAny.clear();
77 else if (aTypeClass == uno::TypeClass_BYTE)
78 aAny <<= static_cast<sal_Int8>(o3tl::toInt32(rValue));
79 else if (aTypeClass == uno::TypeClass_BOOLEAN)
80 aAny <<= rtl_str_toBoolean(rValue.c_str());
81 else if (aTypeClass == uno::TypeClass_SHORT)
82 aAny <<= static_cast<sal_Int16>(o3tl::toInt32(rValue));
83 else if (aTypeClass == uno::TypeClass_UNSIGNED_SHORT)
84 aAny <<= static_cast<sal_uInt16>(o3tl::toUInt32(rValue));
85 else if (aTypeClass == uno::TypeClass_LONG)
86 aAny <<= o3tl::toInt32(rValue);
87 else if (aTypeClass == uno::TypeClass_UNSIGNED_LONG)
88 aAny <<= static_cast<sal_uInt32>(o3tl::toInt32(rValue));
89 else if (aTypeClass == uno::TypeClass_FLOAT)
90 aAny <<= rtl_str_toFloat(rValue.c_str());
91 else if (aTypeClass == uno::TypeClass_DOUBLE)
92 aAny <<= o3tl::toDouble(rValue);
93 else if (aTypeClass == uno::TypeClass_STRING)
94 aAny <<= OUString::fromUtf8(rValue);
97 return aAny;
101 namespace comphelper{
103 SequenceAsHashMap::SequenceAsHashMap()
107 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Any& aSource)
109 (*this) << aSource;
113 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::uno::Any >& lSource)
115 (*this) << lSource;
118 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
120 (*this) << lSource;
123 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::NamedValue >& lSource)
125 (*this) << lSource;
128 void SequenceAsHashMap::operator<<(const css::uno::Any& aSource)
130 // An empty Any reset this instance!
131 if (!aSource.hasValue())
133 clear();
134 return;
137 css::uno::Sequence< css::beans::NamedValue > lN;
138 if (aSource >>= lN)
140 (*this) << lN;
141 return;
144 css::uno::Sequence< css::beans::PropertyValue > lP;
145 if (aSource >>= lP)
147 (*this) << lP;
148 return;
151 throw css::lang::IllegalArgumentException(
152 "Any contains wrong type.", css::uno::Reference<css::uno::XInterface>(),
153 -1);
157 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::uno::Any >& lSource)
159 sal_Int32 c = lSource.getLength();
160 sal_Int32 i = 0;
162 m_aMap.reserve(c);
163 for (i=0; i<c; ++i)
165 css::beans::PropertyValue lP;
166 if (lSource[i] >>= lP)
168 if (
169 (lP.Name.isEmpty()) ||
170 (!lP.Value.hasValue())
172 throw css::lang::IllegalArgumentException(
173 "PropertyValue struct contains no useful information.",
174 css::uno::Reference<css::uno::XInterface>(), -1);
175 (*this)[lP.Name] = lP.Value;
176 continue;
179 css::beans::NamedValue lN;
180 if (lSource[i] >>= lN)
182 if (
183 (lN.Name.isEmpty()) ||
184 (!lN.Value.hasValue())
186 throw css::lang::IllegalArgumentException(
187 "NamedValue struct contains no useful information.",
188 css::uno::Reference<css::uno::XInterface>(), -1);
189 (*this)[lN.Name] = lN.Value;
190 continue;
193 // ignore VOID Any ... but reject wrong filled ones!
194 if (lSource[i].hasValue())
195 throw css::lang::IllegalArgumentException(
196 "Any contains wrong type.",
197 css::uno::Reference<css::uno::XInterface>(), -1);
201 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
203 clear();
205 m_aMap.reserve(lSource.getLength());
206 for (auto& rSource : lSource)
207 (*this)[rSource.Name] = rSource.Value;
210 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::NamedValue >& lSource)
212 clear();
214 m_aMap.reserve(lSource.getLength());
215 for (auto& rSource : lSource)
216 (*this)[rSource.Name] = rSource.Value;
219 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::PropertyValue >& lDestination) const
221 sal_Int32 c = static_cast<sal_Int32>(size());
222 lDestination.realloc(c);
223 css::beans::PropertyValue* pDestination = lDestination.getArray();
225 sal_Int32 i = 0;
226 for (const_iterator pThis = begin();
227 pThis != end() ;
228 ++pThis )
230 pDestination[i].Name = pThis->first.maString;
231 pDestination[i].Value = pThis->second;
232 ++i;
236 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::NamedValue >& lDestination) const
238 sal_Int32 c = static_cast<sal_Int32>(size());
239 lDestination.realloc(c);
240 css::beans::NamedValue* pDestination = lDestination.getArray();
242 sal_Int32 i = 0;
243 for (const_iterator pThis = begin();
244 pThis != end() ;
245 ++pThis )
247 pDestination[i].Name = pThis->first.maString;
248 pDestination[i].Value = pThis->second;
249 ++i;
253 css::uno::Any SequenceAsHashMap::getAsConstAny(bool bAsPropertyValueList) const
255 css::uno::Any aDestination;
256 if (bAsPropertyValueList)
257 aDestination <<= getAsConstPropertyValueList();
258 else
259 aDestination <<= getAsConstNamedValueList();
260 return aDestination;
263 css::uno::Sequence< css::beans::NamedValue > SequenceAsHashMap::getAsConstNamedValueList() const
265 css::uno::Sequence< css::beans::NamedValue > lReturn;
266 (*this) >> lReturn;
267 return lReturn;
270 css::uno::Sequence< css::beans::PropertyValue > SequenceAsHashMap::getAsConstPropertyValueList() const
272 css::uno::Sequence< css::beans::PropertyValue > lReturn;
273 (*this) >> lReturn;
274 return lReturn;
277 bool SequenceAsHashMap::match(const SequenceAsHashMap& rCheck) const
279 for (auto const& elem : rCheck)
281 const OUString& sCheckName = elem.first.maString;
282 const css::uno::Any& aCheckValue = elem.second;
283 const_iterator pFound = find(sCheckName);
285 if (pFound == end())
286 return false;
288 const css::uno::Any& aFoundValue = pFound->second;
289 if (aFoundValue != aCheckValue)
290 return false;
293 return true;
296 void SequenceAsHashMap::update(const SequenceAsHashMap& rUpdate)
298 m_aMap.reserve(std::max(size(), rUpdate.size()));
299 for (auto const& elem : rUpdate.m_aMap)
301 m_aMap[elem.first] = elem.second;
305 std::vector<css::beans::PropertyValue> JsonToPropertyValues(const OString& rJson)
307 std::vector<beans::PropertyValue> aArguments;
308 boost::property_tree::ptree aTree, aNodeNull, aNodeValue;
309 std::stringstream aStream((std::string(rJson)));
310 boost::property_tree::read_json(aStream, aTree);
312 for (const auto& rPair : aTree)
314 const std::string& rType = rPair.second.get<std::string>("type", "");
315 const std::string& rValue = rPair.second.get<std::string>("value", "");
317 beans::PropertyValue aValue;
318 aValue.Name = OUString::fromUtf8(rPair.first);
319 if (rType == "string")
320 aValue.Value <<= OUString::fromUtf8(rValue);
321 else if (rType == "boolean")
322 aValue.Value <<= rtl_str_toBoolean(rValue.c_str());
323 else if (rType == "float")
324 aValue.Value <<= rtl_str_toFloat(rValue.c_str());
325 else if (rType == "long")
326 aValue.Value <<= o3tl::toInt32(rValue);
327 else if (rType == "short")
328 aValue.Value <<= sal_Int16(o3tl::toInt32(rValue));
329 else if (rType == "unsigned short")
330 aValue.Value <<= sal_uInt16(o3tl::toUInt32(rValue));
331 else if (rType == "int64")
332 aValue.Value <<= o3tl::toInt64(rValue);
333 else if (rType == "int32")
334 aValue.Value <<= o3tl::toInt32(rValue);
335 else if (rType == "int16")
336 aValue.Value <<= sal_Int16(o3tl::toInt32(rValue));
337 else if (rType == "uint64")
338 aValue.Value <<= rtl_str_toUInt64(rValue.c_str(), 10);
339 else if (rType == "uint32")
340 aValue.Value <<= o3tl::toUInt32(rValue);
341 else if (rType == "uint16")
342 aValue.Value <<= sal_uInt16(o3tl::toUInt32(rValue));
343 else if (rType == "[]byte")
345 aNodeValue = rPair.second.get_child("value", aNodeNull);
346 if (aNodeValue != aNodeNull && aNodeValue.size() == 0)
348 uno::Sequence<sal_Int8> aSeqByte(reinterpret_cast<const sal_Int8*>(rValue.c_str()),
349 rValue.size());
350 aValue.Value <<= aSeqByte;
353 else if (rType == "[]any")
355 aNodeValue = rPair.second.get_child("value", aNodeNull);
356 if (aNodeValue != aNodeNull && !aNodeValue.empty())
358 uno::Sequence<uno::Any> aSeq(aNodeValue.size());
359 std::transform(aNodeValue.begin(), aNodeValue.end(), aSeq.getArray(),
360 [](const auto& rSeqPair) { return jsonToUnoAny(rSeqPair.second); });
361 aValue.Value <<= aSeq;
364 else if (rType == "[]com.sun.star.beans.PropertyValue")
366 aNodeValue = rPair.second.get_child("value", aNodeNull);
367 std::stringstream s;
368 boost::property_tree::write_json(s, aNodeValue);
369 std::vector<beans::PropertyValue> aPropertyValues = JsonToPropertyValues(OString(s.str()));
370 aValue.Value <<= comphelper::containerToSequence(aPropertyValues);
372 else if (rType == "[][]com.sun.star.beans.PropertyValue")
374 aNodeValue = rPair.second.get_child("value", aNodeNull);
375 std::vector<uno::Sequence<beans::PropertyValue>> aSeqs;
376 for (const auto& rItem : aNodeValue)
378 std::stringstream s;
379 boost::property_tree::write_json(s, rItem.second);
380 std::vector<beans::PropertyValue> aPropertyValues = JsonToPropertyValues(OString(s.str()));
381 aSeqs.push_back(comphelper::containerToSequence(aPropertyValues));
383 aValue.Value <<= comphelper::containerToSequence(aSeqs);
385 else
386 SAL_WARN("comphelper", "JsonToPropertyValues: unhandled type '" << rType << "'");
387 aArguments.push_back(aValue);
389 return aArguments;
392 } // namespace comphelper
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */