Ver código fonte

Implemented getting a ctkCmdLineModuleDescription object.

Sascha Zelzer 12 anos atrás
pai
commit
7a8050ed85

+ 3 - 1
Libs/CommandLineModules/Core/CMakeLists.txt

@@ -30,9 +30,11 @@ set(KIT_SRCS
   ctkCmdLineModuleProcessRunner.cpp
   ctkCmdLineModuleProcessRunner_p.h
   ctkCmdLineModuleReference.cpp
-  ctkCmdLineModuleReferencePrivate.h
+  ctkCmdLineModuleReferencePrivate.cpp
+  ctkCmdLineModuleXmlException.cpp
   ctkCmdLineModuleXmlMsgHandler_p.h
   ctkCmdLineModuleXmlMsgHandler.cpp
+  ctkCmdLineModuleXmlParser_p.h
   ctkCmdLineModuleXmlParser.cpp
   ctkCmdLineModuleXmlValidator.cpp
   ctkCmdLineModuleXslTransform.cpp

+ 1 - 1
Libs/CommandLineModules/Core/ctkCmdLineModuleReference.cpp

@@ -48,7 +48,7 @@ ctkCmdLineModuleReference::operator bool()
 
 ctkCmdLineModuleDescription ctkCmdLineModuleReference::description() const
 {
-  return d->Description;
+  return d->description();
 }
 
 QByteArray ctkCmdLineModuleReference::rawXmlDescription() const

+ 39 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleReferencePrivate.cpp

@@ -0,0 +1,39 @@
+/*=============================================================================
+  
+  Library: CTK
+  
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+    
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+  
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  
+=============================================================================*/
+
+#include "ctkCmdLineModuleReferencePrivate.h"
+#include "ctkCmdLineModuleXmlParser_p.h"
+
+#include <QBuffer>
+
+ctkCmdLineModuleDescription ctkCmdLineModuleReferencePrivate::description() const
+{
+  // lazy creation
+  if (!Description.d)
+  {
+    QByteArray xml(RawXmlDescription);
+    QBuffer xmlInput(&xml);
+    ctkCmdLineModuleXmlParser parser(&xmlInput, &Description);
+    parser.doParse();
+  }
+  return Description;
+}
+

+ 5 - 1
Libs/CommandLineModules/Core/ctkCmdLineModuleReferencePrivate.h

@@ -29,10 +29,14 @@
 
 struct ctkCmdLineModuleReferencePrivate : public QSharedData
 {
+  ctkCmdLineModuleDescription description() const;
+
   QString Location;
   QByteArray RawXmlDescription;
 
-  ctkCmdLineModuleDescription Description;
+private:
+
+  mutable ctkCmdLineModuleDescription Description;
 };
 
 #endif // CTKCMDLINEMODULEREFERENCEPRIVATE_H

+ 24 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleXmlException.cpp

@@ -0,0 +1,24 @@
+/*=============================================================================
+  
+  Library: CTK
+  
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+    
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+  
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  
+=============================================================================*/
+
+#include "ctkCmdLineModuleXmlException.h"
+
+CTK_IMPLEMENT_EXCEPTION(ctkCmdLineModuleXmlException, ctkException, "Command Line Module XML exception")

+ 31 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleXmlException.h

@@ -0,0 +1,31 @@
+/*=============================================================================
+  
+  Library: CTK
+  
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+    
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+  
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  
+=============================================================================*/
+
+#ifndef CTKCMDLINEMODULEXMLEXCEPTION_H
+#define CTKCMDLINEMODULEXMLEXCEPTION_H
+
+#include <ctkException.h>
+
+#include "ctkCommandLineModulesCoreExport.h"
+
+CTK_DECLARE_EXCEPTION(CTK_CMDLINEMODULECORE_EXPORT, ctkCmdLineModuleXmlException, ctkException)
+
+#endif // CTKCMDLINEMODULEXMLEXCEPTION_H

+ 4 - 121
Libs/CommandLineModules/Core/ctkCmdLineModuleXmlParser.cpp

@@ -26,38 +26,18 @@ limitations under the License.
 #include <QXmlStreamReader>
 
 // CTK includes
+#include "ctkCmdLineModuleXmlParser_p.h"
 #include "ctkCmdLineModuleDescription.h"
 #include "ctkCmdLineModuleDescriptionPrivate.h"
 #include "ctkCmdLineModuleParameterGroup.h"
 #include "ctkCmdLineModuleParameterGroupPrivate.h"
 #include "ctkCmdLineModuleParameterParsers_p.h"
 
-#include "ctkException.h"
+#include "ctkCmdLineModuleXmlException.h"
 
 // STD includes
 #include <stdexcept>
 
-class ctkCmdLineModuleXmlParser
-{
-public:
-
-  ctkCmdLineModuleXmlParser(QIODevice* device, ctkCmdLineModuleDescription* md);
-  ~ctkCmdLineModuleXmlParser();
-
-  void validate();
-  void doParse();
-
-  void handleExecutableElement();
-  void handleParametersElement();
-  ctkCmdLineModuleParameter handleParameterElement();
-
-private:
-
-  QIODevice* const _device;
-  ctkCmdLineModuleDescription* _md;
-  QXmlStreamReader _xmlReader;
-  QHash<QString, ctkCmdLineModuleParameterParser*> _paramParsers;
-};
 
 namespace {
 
@@ -99,89 +79,6 @@ ctkCmdLineModuleXmlParser::~ctkCmdLineModuleXmlParser()
 }
 
 // ----------------------------------------------------------------------------
-void ctkCmdLineModuleXmlParser::validate()
-{
-  class _MessageHandler : public QAbstractMessageHandler
-  {
-  public:
-
-    QString statusMessage() const { return m_description; }
-    int line() const { return m_sourceLocation.line(); }
-    int column() const { return m_sourceLocation.column(); }
-
-  protected:
-    virtual void handleMessage(QtMsgType type, const QString& description,
-                               const QUrl& identifier, const QSourceLocation& sourceLocation)
-    {
-      Q_UNUSED(identifier)
-
-      m_messageType = type;
-      m_sourceLocation = sourceLocation;
-
-      QXmlStreamReader reader(description);
-      m_description.clear();
-      m_description.reserve(description.size());
-      while(!reader.atEnd())
-      {
-        reader.readNext();
-
-        switch(reader.tokenType())
-        {
-        case QXmlStreamReader::Characters:
-        {
-          m_description.append(reader.text().toString());
-          continue;
-        }
-        case QXmlStreamReader::StartElement:
-          /* Fallthrough, */
-        case QXmlStreamReader::EndElement:
-          /* Fallthrough, */
-        case QXmlStreamReader::StartDocument:
-          /* Fallthrough, */
-        case QXmlStreamReader::EndDocument:
-          continue;
-        default:
-          Q_ASSERT_X(false, Q_FUNC_INFO,
-                     "Unexpected node.");
-        }
-      }
-    }
-
-  private:
-    QtMsgType m_messageType;
-    QString m_description;
-    QSourceLocation m_sourceLocation;
-  };
-  _MessageHandler errorHandler;
-
-  QXmlSchema schema;
-  schema.setMessageHandler(&errorHandler);
-  schema.load(QUrl::fromLocalFile(":/ctkCmdLineModuleDescription.xsd"));
-
-  bool res = schema.isValid();
-  if (!res)
-  {
-    QString msg("Invalid Schema at line %1, column %2: %3");
-    msg = msg.arg(errorHandler.line()).arg(errorHandler.column()).arg(errorHandler.statusMessage());
-    throw std::runtime_error(msg.toStdString());
-  }
-
-  QXmlSchemaValidator validator(schema);
-  _device->open(QIODevice::ReadOnly);
-  res = validator.validate(_device);
-  _device->close();
-
-  if (!res)
-  {
-    QString msg("Error validating XML description, at line %1, column %2: %3");
-    throw std::runtime_error(msg.arg(errorHandler.line())
-                             .arg(errorHandler.column())
-                             .arg(errorHandler.statusMessage()).toStdString());
-  }
-
-}
-
-// ----------------------------------------------------------------------------
 void ctkCmdLineModuleXmlParser::doParse()
 {
   _xmlReader.clear();
@@ -194,8 +91,8 @@ void ctkCmdLineModuleXmlParser::doParse()
   if (_xmlReader.hasError())
   {
     QString msg("Error parsing module description at line %1, column %2: %3");
-    throw std::runtime_error(msg.arg(_xmlReader.lineNumber()).arg(_xmlReader.columnNumber())
-                             .arg(_xmlReader.errorString()).toStdString());
+    throw ctkCmdLineModuleXmlException(msg.arg(_xmlReader.lineNumber()).arg(_xmlReader.columnNumber())
+                                       .arg(_xmlReader.errorString()));
   }
 }
 
@@ -306,17 +203,3 @@ ctkCmdLineModuleParameter ctkCmdLineModuleXmlParser::handleParameterElement()
     return moduleParam;
   }
 }
-
-// ----------------------------------------------------------------------------
-ctkCmdLineModuleDescription ctkCmdLineModuleDescription::parse(QIODevice* device)
-{
-  ctkCmdLineModuleDescription moduleDescription;
-  ctkCmdLineModuleXmlParser parser(device, &moduleDescription);
-
-  // Verify the xml is correct
-  parser.validate();
-
-  parser.doParse();
-
-  return moduleDescription;
-}

+ 56 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleXmlParser_p.h

@@ -0,0 +1,56 @@
+/*=============================================================================
+  
+  Library: CTK
+  
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+    
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+  
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  
+=============================================================================*/
+
+#ifndef CTKCMDLINEMODULEXMLPARSER_P_H
+#define CTKCMDLINEMODULEXMLPARSER_P_H
+
+#include <QXmlStreamReader>
+#include <QHash>
+
+class ctkCmdLineModuleDescription;
+class ctkCmdLineModuleParameter;
+class ctkCmdLineModuleParameterParser;
+
+class QIODevice;
+
+class ctkCmdLineModuleXmlParser
+{
+
+public:
+
+  ctkCmdLineModuleXmlParser(QIODevice* device, ctkCmdLineModuleDescription* md);
+  ~ctkCmdLineModuleXmlParser();
+
+  void doParse();
+
+private:
+
+  void handleExecutableElement();
+  void handleParametersElement();
+  ctkCmdLineModuleParameter handleParameterElement();
+
+  QIODevice* const _device;
+  ctkCmdLineModuleDescription* _md;
+  QXmlStreamReader _xmlReader;
+  QHash<QString, ctkCmdLineModuleParameterParser*> _paramParsers;
+};
+
+#endif // CTKCMDLINEMODULEXMLPARSER_P_H