changeset 2180:71b8bec8ca91

improvements
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Nov 2016 17:00:39 +0100
parents de32f3b4ff09
children 761dfdabe437
files Plugins/Samples/Common/FullOrthancDataset.cpp Plugins/Samples/Common/FullOrthancDataset.h Plugins/Samples/Common/IOrthancConnection.cpp Plugins/Samples/Common/IOrthancConnection.h Plugins/Samples/Common/SimplifiedOrthancDataset.cpp Plugins/Samples/Common/SimplifiedOrthancDataset.h
diffstat 6 files changed, 127 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Samples/Common/FullOrthancDataset.cpp	Fri Nov 25 16:11:44 2016 +0100
+++ b/Plugins/Samples/Common/FullOrthancDataset.cpp	Fri Nov 25 17:00:39 2016 +0100
@@ -34,22 +34,8 @@
 
 #include "OrthancPluginCppWrapper.h"
 
-#include <json/reader.h>
-
 namespace OrthancPlugins
 {
-  void FullOrthancDataset::Parse(const std::string& source)
-  {
-    Json::Reader reader;
-
-    if (!reader.parse(source, root_) ||
-        root_.type() != Json::objectValue)
-    {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
-    }
-  }
-
-
   static const Json::Value* AccessTag(const Json::Value& dataset,
                                       const DicomTag& tag) 
   {
@@ -152,12 +138,27 @@
   }
 
 
+  void FullOrthancDataset::CheckRoot() const
+  {
+    if (root_.type() != Json::objectValue)
+    {
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+    }
+  }
+
+
   FullOrthancDataset::FullOrthancDataset(IOrthancConnection& orthanc,
                                          const std::string& uri)
   {
-    std::string content;
-    orthanc.RestApiGet(content, uri);
-    Parse(content);
+    IOrthancConnection::RestApiGet(root_, orthanc, uri);
+    CheckRoot();
+  }
+
+
+  FullOrthancDataset::FullOrthancDataset(const std::string& content)
+  {
+    IOrthancConnection::ParseJson(root_, content);
+    CheckRoot();
   }
 
 
--- a/Plugins/Samples/Common/FullOrthancDataset.h	Fri Nov 25 16:11:44 2016 +0100
+++ b/Plugins/Samples/Common/FullOrthancDataset.h	Fri Nov 25 17:00:39 2016 +0100
@@ -44,18 +44,15 @@
   private:
     Json::Value   root_;
 
-    void Parse(const std::string& source);
+    const Json::Value* LookupPath(const DicomPath& path) const;
 
-    const Json::Value* LookupPath(const DicomPath& path) const;
+    void CheckRoot() const;
 
   public:
     FullOrthancDataset(IOrthancConnection& orthanc,
                        const std::string& uri);
 
-    FullOrthancDataset(const std::string& content)
-    {
-      Parse(content);
-    }
+    FullOrthancDataset(const std::string& content);
 
     virtual bool GetStringValue(std::string& result,
                                 const DicomPath& path) const;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Samples/Common/IOrthancConnection.cpp	Fri Nov 25 17:00:39 2016 +0100
@@ -0,0 +1,72 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "IOrthancConnection.h"
+
+#include "OrthancPluginCppWrapper.h"
+
+#include <json/reader.h>
+
+namespace OrthancPlugins
+{
+  void IOrthancConnection::ParseJson(Json::Value& result,
+                                     const std::string& content)
+  {
+    Json::Reader reader;
+    
+    if (!reader.parse(content, result))
+    {
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+    }
+  }
+
+
+  void IOrthancConnection::RestApiGet(Json::Value& result,
+                                      IOrthancConnection& orthanc,
+                                      const std::string& uri)
+  {
+    std::string content;
+    orthanc.RestApiGet(content, uri);
+    ParseJson(result, content);
+  }
+
+
+  void IOrthancConnection::RestApiGet(Json::Value& result,
+                                      IOrthancConnection& orthanc,
+                                      const std::string& uri,
+                                      const std::string& body)
+  {
+    std::string content;
+    orthanc.RestApiPost(content, uri, body);
+    ParseJson(result, content);
+  }
+}
--- a/Plugins/Samples/Common/IOrthancConnection.h	Fri Nov 25 16:11:44 2016 +0100
+++ b/Plugins/Samples/Common/IOrthancConnection.h	Fri Nov 25 17:00:39 2016 +0100
@@ -36,6 +36,7 @@
 
 #include <boost/noncopyable.hpp>
 #include <string>
+#include <json/value.h>
 
 namespace OrthancPlugins
 {
@@ -52,5 +53,17 @@
     virtual void RestApiPost(std::string& result,
                              const std::string& uri,
                              const std::string& body) = 0;
+
+    static void ParseJson(Json::Value& result,
+                          const std::string& content);
+
+    static void RestApiGet(Json::Value& result,
+                           IOrthancConnection& orthanc,
+                           const std::string& uri);
+
+    static void RestApiGet(Json::Value& result,
+                           IOrthancConnection& orthanc,
+                           const std::string& uri,
+                           const std::string& body);
   };
 }
--- a/Plugins/Samples/Common/SimplifiedOrthancDataset.cpp	Fri Nov 25 16:11:44 2016 +0100
+++ b/Plugins/Samples/Common/SimplifiedOrthancDataset.cpp	Fri Nov 25 17:00:39 2016 +0100
@@ -34,22 +34,8 @@
 
 #include "OrthancPluginCppWrapper.h"
 
-#include <json/reader.h>
-
 namespace OrthancPlugins
 {
-  void SimplifiedOrthancDataset::Parse(const std::string& source)
-  {
-    Json::Reader reader;
-
-    if (!reader.parse(source, root_) ||
-        root_.type() != Json::objectValue)
-    {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
-    }
-  }
-
-
   const Json::Value* SimplifiedOrthancDataset::LookupPath(const DicomPath& path) const
   {
     const Json::Value* content = &root_;
@@ -101,12 +87,27 @@
   }
 
 
+  void SimplifiedOrthancDataset::CheckRoot() const
+  {
+    if (root_.type() != Json::objectValue)
+    {
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+    }
+  }
+
+
   SimplifiedOrthancDataset::SimplifiedOrthancDataset(IOrthancConnection& orthanc,
                                                      const std::string& uri)
   {
-    std::string content;
-    orthanc.RestApiGet(content, uri);
-    Parse(content);
+    IOrthancConnection::RestApiGet(root_, orthanc, uri);
+    CheckRoot();
+  }
+
+
+  SimplifiedOrthancDataset::SimplifiedOrthancDataset(const std::string& content)
+  {
+    IOrthancConnection::ParseJson(root_, content);
+    CheckRoot();
   }
 
 
--- a/Plugins/Samples/Common/SimplifiedOrthancDataset.h	Fri Nov 25 16:11:44 2016 +0100
+++ b/Plugins/Samples/Common/SimplifiedOrthancDataset.h	Fri Nov 25 17:00:39 2016 +0100
@@ -35,8 +35,6 @@
 #include "IOrthancConnection.h"
 #include "IDicomDataset.h"
 
-#include <json/value.h>
-
 namespace OrthancPlugins
 {
   class SimplifiedOrthancDataset : public IDicomDataset
@@ -44,18 +42,15 @@
   private:
     Json::Value   root_;
 
-    void Parse(const std::string& source);
+    const Json::Value* LookupPath(const DicomPath& path) const;
 
-    const Json::Value* LookupPath(const DicomPath& path) const;
+    void CheckRoot() const;
 
   public:
     SimplifiedOrthancDataset(IOrthancConnection& orthanc,
                              const std::string& uri);
 
-    SimplifiedOrthancDataset(const std::string& content)
-    {
-      Parse(content);
-    }
+    SimplifiedOrthancDataset(const std::string& content);
 
     virtual bool GetStringValue(std::string& result,
                                 const DicomPath& path) const;