[Libreoffice-commits] core.git: desktop/source

Henry Castro hcastro at collabora.com
Wed Jun 20 19:33:41 UTC 2018


 desktop/source/lib/init.cxx |   81 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 78 insertions(+), 3 deletions(-)

New commits:
commit 87674a28893520eb8bb528c7e774a7ed926976cb
Author: Henry Castro <hcastro at collabora.com>
Date:   Mon Jun 18 08:28:40 2018 -0400

    lokit: add jsonToUnoAny
    
    Change-Id: I79c2fe22fe7f3a8daa121ecaa529b6bca3216bf3
    Reviewed-on: https://gerrit.libreoffice.org/56032
    Reviewed-by: Jan Holesovsky <kendy at collabora.com>
    Tested-by: Jan Holesovsky <kendy at collabora.com>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index e07fe4babb2a..a9cc9b8bb90f 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -48,6 +48,9 @@
 #include <com/sun/star/lang/Locale.hpp>
 #include <com/sun/star/lang/XComponent.hpp>
 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
+#include <com/sun/star/reflection/theCoreReflection.hpp>
+#include <com/sun/star/reflection/XIdlClass.hpp>
+#include <com/sun/star/reflection/XIdlReflection.hpp>
 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
 #include <com/sun/star/ucb/XContentProvider.hpp>
 #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
@@ -230,19 +233,79 @@ static OUString getAbsoluteURL(const char* pURL)
     return OUString();
 }
 
+static uno::Any jsonToUnoAny(const boost::property_tree::ptree& aTree)
+{
+    uno::Any aAny;
+    uno::Any aValue;
+    sal_Int32 nFields;
+    uno::TypeClass aTypeClass;
+    uno::Reference< reflection::XIdlField > aField;
+    boost::property_tree::ptree aNodeNull, aNodeValue, aNodeField;
+    const std::string& rType = aTree.get<std::string>("type", "");
+    const std::string& rValue = aTree.get<std::string>("value", "");
+    uno::Sequence< uno::Reference< reflection::XIdlField > > aFields;
+    uno::Reference< reflection:: XIdlClass > xIdlClass =
+        css::reflection::theCoreReflection::get(comphelper::getProcessComponentContext())->forName(OUString::fromUtf8(rType.c_str()));
+    if (xIdlClass.is())
+    {
+        aTypeClass = xIdlClass->getTypeClass();
+        xIdlClass->createObject(aAny);
+        aFields = xIdlClass->getFields();
+        nFields = aFields.getLength();
+        aNodeValue = aTree.get_child("value", aNodeNull);
+        if (nFields > 0 && aNodeValue != aNodeNull)
+        {
+            for (sal_Int32 itField = 0; itField < nFields; ++itField)
+            {
+                aField = aFields[itField];
+                aNodeField = aNodeValue.get_child(aField->getName().toUtf8().getStr(), aNodeNull);
+                if (aNodeField != aNodeNull)
+                {
+                    aValue = jsonToUnoAny(aNodeField);
+                    aField->set(aAny, aValue);
+                }
+            }
+        }
+        else if (!rValue.empty())
+        {
+            if (aTypeClass == uno::TypeClass_VOID)
+                aAny.clear();
+            else if (aTypeClass == uno::TypeClass_BYTE)
+                aAny <<= static_cast<sal_Int8>(OString(rValue.c_str()).toInt32());
+            else if (aTypeClass == uno::TypeClass_BOOLEAN)
+                aAny <<= OString(rValue.c_str()).toBoolean();
+            else if (aTypeClass == uno::TypeClass_SHORT)
+                aAny <<= static_cast<sal_Int16>(OString(rValue.c_str()).toInt32());
+            else if (aTypeClass == uno::TypeClass_UNSIGNED_SHORT)
+                aAny <<= static_cast<sal_uInt16>(OString(rValue.c_str()).toUInt32());
+            else if (aTypeClass == uno::TypeClass_LONG)
+                aAny <<= OString(rValue.c_str()).toInt32();
+            else if (aTypeClass == uno::TypeClass_UNSIGNED_LONG)
+                aAny <<= static_cast<sal_uInt32>(OString(rValue.c_str()).toInt32());
+            else if (aTypeClass == uno::TypeClass_FLOAT)
+                aAny <<= OString(rValue.c_str()).toFloat();
+            else if (aTypeClass == uno::TypeClass_DOUBLE)
+                aAny <<= OString(rValue.c_str()).toDouble();
+            else if (aTypeClass == uno::TypeClass_STRING)
+                aAny <<= OUString::fromUtf8(rValue.c_str());
+        }
+    }
+    return aAny;
+}
+
 static std::vector<beans::PropertyValue> jsonToPropertyValuesVector(const char* pJSON)
 {
     std::vector<beans::PropertyValue> aArguments;
     if (pJSON && pJSON[0] != '\0')
     {
-        boost::property_tree::ptree aTree;
+        boost::property_tree::ptree aTree, aNodeNull, aNodeValue;
         std::stringstream aStream(pJSON);
         boost::property_tree::read_json(aStream, aTree);
 
         for (const auto& rPair : aTree)
         {
-            const std::string& rType = rPair.second.get<std::string>("type");
-            const std::string& rValue = rPair.second.get<std::string>("value");
+            const std::string& rType = rPair.second.get<std::string>("type", "");
+            const std::string& rValue = rPair.second.get<std::string>("value", "");
 
             beans::PropertyValue aValue;
             aValue.Name = OUString::fromUtf8(rPair.first.c_str());
@@ -256,6 +319,18 @@ static std::vector<beans::PropertyValue> jsonToPropertyValuesVector(const char*
                 aValue.Value <<= OString(rValue.c_str()).toInt32();
             else if (rType == "unsigned short")
                 aValue.Value <<= static_cast<sal_uInt16>(OString(rValue.c_str()).toUInt32());
+            else if (rType == "[]any")
+            {
+                aNodeValue = rPair.second.get_child("value", aNodeNull);
+                if (aNodeValue != aNodeNull && aNodeValue.size() > 0)
+                {
+                    sal_Int32 itSeq = 0;
+                    uno::Sequence< uno::Any > aSeq(aNodeValue.size());
+                    for (const auto& rSeqPair : aNodeValue)
+                        aSeq[itSeq++] = jsonToUnoAny(rSeqPair.second);
+                    aValue.Value <<= aSeq;
+                }
+            }
             else
                 SAL_WARN("desktop.lib", "jsonToPropertyValuesVector: unhandled type '"<<rType<<"'");
             aArguments.push_back(aValue);


More information about the Libreoffice-commits mailing list