From 33aa8c4cb3f9709ec5e3da9f964d990c7449f45d Mon Sep 17 00:00:00 2001
From: Russell Taylor <taylorrj@ornl.gov>
Date: Fri, 16 Mar 2012 15:40:43 -0400
Subject: [PATCH] Re #4801. Split up long FacilityInfo constructor.

---
 .../Kernel/inc/MantidKernel/FacilityInfo.h    |  28 +++--
 .../Framework/Kernel/src/FacilityInfo.cpp     | 112 +++++++++++-------
 2 files changed, 84 insertions(+), 56 deletions(-)

diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/FacilityInfo.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/FacilityInfo.h
index a4d7f79d2c8..4b9e2c577c3 100644
--- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/FacilityInfo.h
+++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/FacilityInfo.h
@@ -29,10 +29,7 @@ namespace Kernel
 
 /** A class that holds information about a facility.
 
-    @author Roman Tolchenov, Tessella plc
-    @date 20/07/2010
-
-    Copyright &copy; 2007-2010 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
+    Copyright &copy; 2007-2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
 
     This file is part of Mantid.
 
@@ -57,10 +54,9 @@ class MANTID_KERNEL_DLL FacilityInfo
 public:
   explicit FacilityInfo(const Poco::XML::Element* elem);
 
-  const std::string getDelimiter() const;
-
   /// Return the name of the facility
   const std::string & name() const { return m_name; }
+
   /// Returns default zero padding for this facility
   int zeroPadding() const { return m_zeroPadding; }
   /// Returns the default delimiter between instrument name and run number
@@ -69,10 +65,17 @@ public:
   const std::vector<std::string> extensions() const { return m_extensions; }
   /// Returns the preferred file extension
   const std::string & preferredExtension()const { return m_extensions.front(); }
+
   /// Return the soap endpoint name
   const std::string & getSoapEndPoint() const { return m_soapEndPoint; }
+  /// Returns the catalog name
+  const std::string & catalogName()const {return m_catalogName;}
   /// Return the archive search interface names
   const std::vector<std::string> & archiveSearch() const { return m_archiveSearch; }
+
+  /// Returns the name of the default live listener
+  const std::string & liveListener() const { return m_liveListener; }
+
   /// Returns a list of instruments of this facility
   const std::vector<InstrumentInfo> & instruments() const { return m_instruments; }
   /// Returns a list of instruments of given technique
@@ -80,12 +83,15 @@ public:
   /// Returns instruments with given name
   const InstrumentInfo & instrument(const std::string& iName = "") const;
 
-  /// Returns the catalog name
-  const std::string & catalogName()const {return m_catalogName;}
-  /// Returns the name of the default live listener
-  const std::string & liveListener() const { return m_liveListener; }
-
 private:
+  void fillZeroPadding(const Poco::XML::Element* elem);
+  void fillDelimiter(const Poco::XML::Element* elem);
+  void fillExtensions(const Poco::XML::Element* elem);
+  void fillSoapEndPoint(const Poco::XML::Element* elem);
+  void fillArchiveNames(const Poco::XML::Element* elem);
+  void fillCatalogName(const Poco::XML::Element* elem);
+  void fillInstruments(const Poco::XML::Element* elem);
+
   /// Add new extension
   void addExtension(const std::string& ext);
 
diff --git a/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp b/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
index 5fc875023d8..c045a386741 100644
--- a/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
+++ b/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
@@ -33,15 +33,37 @@ FacilityInfo::FacilityInfo(const Poco::XML::Element* elem) :
     g_log.error("Facility name is not defined");
     throw std::runtime_error("Facility name is not defined");
   }
+
+  // Fill the various fields from the XML
+  fillZeroPadding(elem);
+  fillDelimiter(elem);
+  fillExtensions(elem);
+  fillSoapEndPoint(elem);
+  fillArchiveNames(elem);
+  fillCatalogName(elem);
+  fillInstruments(elem);
+}
+
+/// Called from constructor to fill zero padding field
+void FacilityInfo::fillZeroPadding(const Poco::XML::Element* elem)
+{
   std::string paddingStr = elem->getAttribute("zeropadding");
   if ( paddingStr.empty() || !Mantid::Kernel::Strings::convert(paddingStr,m_zeroPadding) )
   {
     m_zeroPadding = 0;
   }
+}
 
+/// Called from constructor to fill default delimiter
+void FacilityInfo::fillDelimiter(const Poco::XML::Element* elem)
+{
   // The string to separate the instrument name and the run number.
   m_delimiter = elem->getAttribute("delimiter");
+}
 
+/// Called from constructor to fill file extensions
+void FacilityInfo::fillExtensions(const Poco::XML::Element* elem)
+{
   std::string extsStr = elem->getAttribute("FileExtensions");
   if (extsStr.empty())
   {
@@ -54,27 +76,48 @@ FacilityInfo::FacilityInfo(const Poco::XML::Element* elem) :
   {
     addExtension(*it);
   }
+}
 
-  Poco::XML::NodeList* pNL_soapEndPoint = elem->getElementsByTagName("soapEndPoint");
-  if(!pNL_soapEndPoint)
+/**
+  * Add new extension. Adds both a lowercase and uppercase version
+  * @param ext :: File extension, including the dot, e.g. ".nxs" or ".raw"
+  */
+void FacilityInfo::addExtension(const std::string& ext)
+{
+  std::string casedExt(ext);
+  std::transform(ext.begin(), ext.end(), casedExt.begin(), tolower);
+  std::vector<std::string>::iterator it = std::find(m_extensions.begin(),m_extensions.end(),casedExt);
+  if (it == m_extensions.end())
   {
-    throw std::runtime_error("Facilities.xml file  must have soapEndPoint information");
+    m_extensions.push_back(casedExt);
+    std::transform(ext.begin(), ext.end(), casedExt.begin(), toupper);
+    m_extensions.push_back(casedExt);
   }
+}
+
+/// Called from constructor to fill ICAT soap end point
+void FacilityInfo::fillSoapEndPoint(const Poco::XML::Element* elem)
+{
+  Poco::XML::NodeList* pNL_soapEndPoint = elem->getElementsByTagName("soapEndPoint");
+
   if (pNL_soapEndPoint->length() > 1)
   {
+    pNL_soapEndPoint->release();
     g_log.error("Facility must have only one soapEndPoint tag");
     throw std::runtime_error("Facility must have only one csoapEndPoint tag");
   }
-  else if (pNL_soapEndPoint->length() == 1)
+
+  Poco::XML::Element* endpoint = dynamic_cast<Poco::XML::Element*>(pNL_soapEndPoint->item(0));
+  if(!endpoint->getAttribute("url").empty())
   {
-    Poco::XML::Element* elem = dynamic_cast<Poco::XML::Element*>(pNL_soapEndPoint->item(0));
-    if(!elem->getAttribute("url").empty())
-    {
-      m_soapEndPoint= elem->getAttribute("url");
-    }
+    m_soapEndPoint= endpoint->getAttribute("url");
   }
   pNL_soapEndPoint->release();
+}
 
+/// Called from constructor to fill archive interface names
+void FacilityInfo::fillArchiveNames(const Poco::XML::Element* elem)
+{
   Poco::XML::NodeList* pNL_archives = elem->getElementsByTagName("archive");
   if (pNL_archives->length() > 1)
   {
@@ -96,29 +139,34 @@ FacilityInfo::FacilityInfo(const Poco::XML::Element* elem) :
     pNL_interfaces->release();
   }
   pNL_archives->release();
+}
 
+/// Called from constructor to fill catalog name
+void FacilityInfo::fillCatalogName(const Poco::XML::Element* elem)
+{
   Poco::XML::NodeList* pNL_catalogs = elem->getElementsByTagName("catalog");
-  if(!pNL_catalogs)
-  {
-    throw std::runtime_error("Facilities.xml file  must have catalog information");
-  }
+
   if (pNL_catalogs->length() > 1)
   {
     g_log.error("Facility must have only one catalog tag");
     throw std::runtime_error("Facility must have only one catalog tag");
   }
-  else if (pNL_catalogs->length() == 1)
+
+  Poco::XML::Element* catalog = dynamic_cast<Poco::XML::Element*>(pNL_catalogs->item(0));
+  if(!catalog->getAttribute("name").empty())
   {
-    Poco::XML::Element* elem = dynamic_cast<Poco::XML::Element*>(pNL_catalogs->item(0));
-    if(!elem->getAttribute("name").empty())
-    {
-      m_catalogName= elem->getAttribute("name");
-    }
+    m_catalogName= catalog->getAttribute("name");
   }
+
   pNL_catalogs->release();
+}
 
+/// Called from constructor to fill instrument list
+void FacilityInfo::fillInstruments(const Poco::XML::Element* elem)
+{
   Poco::XML::NodeList* pNL_instrument = elem->getElementsByTagName("instrument");
   unsigned long n = pNL_instrument->length();
+  m_instruments.reserve(n);
 
   for (unsigned long i = 0; i < n; ++i)
   {
@@ -142,23 +190,6 @@ FacilityInfo::FacilityInfo(const Poco::XML::Element* elem) :
   }
 }
 
-/**
-  * Add new extension. Adds both a lowercase and uppercase version
-  * @param ext :: File extension, including the dot, e.g. ".nxs" or ".raw"
-  */
-void FacilityInfo::addExtension(const std::string& ext)
-{
-  std::string casedExt(ext); 
-  std::transform(ext.begin(), ext.end(), casedExt.begin(), tolower);
-  std::vector<std::string>::iterator it = std::find(m_extensions.begin(),m_extensions.end(),casedExt);
-  if (it == m_extensions.end())
-  {
-    m_extensions.push_back(casedExt);
-    std::transform(ext.begin(), ext.end(), casedExt.begin(), toupper);
-    m_extensions.push_back(casedExt);
-  }
-}
-
 /**
   * Returns instrument with given name
   * @param  iName Instrument name
@@ -225,14 +256,5 @@ std::vector<InstrumentInfo> FacilityInfo::instruments(const std::string& tech)co
   return out;
 }
 
-/**
- * Returns the delimiter between the instrument name and the run number.
- * @return the delimiter as a string
- */
-const std::string FacilityInfo::getDelimiter() const
-{
-  return m_delimiter;
-}
-
 } // namespace Kernel
 } // namespace Mantid
-- 
GitLab