Browse Source

Basic GUI to validate XML

Matt Clarkson 11 years ago
parent
commit
9fc4f7ff63

+ 32 - 1
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.cpp

@@ -37,6 +37,7 @@
 #include <ctkCmdLineModuleFrontendFactoryQtWebKit.h>
 #include <ctkCmdLineModuleBackendLocalProcess.h>
 #include <ctkCmdLineModuleBackendFunctionPointer.h>
+#include <ctkCmdLineModuleBackendXMLChecker.h>
 #include <ctkException.h>
 #include <ctkCmdLineModuleXmlException.h>
 
@@ -48,6 +49,7 @@
 #include <QFutureSynchronizer>
 #include <QCloseEvent>
 #include <QFileDialog>
+#include <QMessageBox>
 
 //-----------------------------------------------------------------------------
 ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
@@ -80,9 +82,13 @@ ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
 
   // Backends
   ctkCmdLineModuleBackendFunctionPointer* backendFunctionPointer = new ctkCmdLineModuleBackendFunctionPointer;
+  moduleBackends.push_back(backendFunctionPointer);
+
+  xmlCheckerBackEnd = new ctkCmdLineModuleBackendXMLChecker;
+  moduleBackends.push_back(xmlCheckerBackEnd);
 
   moduleBackends.push_back(new ctkCmdLineModuleBackendLocalProcess);
-  moduleBackends.push_back(backendFunctionPointer);
+
   for(int i = 0; i < moduleBackends.size(); ++i)
   {
     moduleManager.registerBackend(moduleBackends[i]);
@@ -369,6 +375,31 @@ void ctkCLModuleExplorerMainWindow::currentModuleFinished()
 //-----------------------------------------------------------------------------
 void ctkCLModuleExplorerMainWindow::checkXMLPressed()
 {
+  xmlCheckerBackEnd->setXML(ui->m_XMLToValidate->toPlainText());
+  QUrl url(QString("xmlchecker://should call ctkCmdLineModuleBackendXMLChecker"));
+
+  qDebug() << "ctkCLModuleExplorerMainWindow::checkXMLPressed validating:\n" << ui->m_XMLToValidate->toPlainText();
+
+  ctkCmdLineModuleManager::ValidationMode previousMode = moduleManager.validationMode();
+
+  try
+  {
+    ctkCmdLineModuleReference ref = moduleManager.moduleReference(url);
+    if (ref)
+    {
+      moduleManager.unregisterModule(ref);
+    }
+    moduleManager.setValidationMode(ctkCmdLineModuleManager::STRICT_VALIDATION);
+    moduleManager.registerModule(url);
+    moduleManager.setValidationMode(previousMode);
+
+  } catch (ctkException& except)
+  {
+    moduleManager.setValidationMode(previousMode);
+    QWidget* widget = QApplication::activeModalWidget();
+    if (widget == NULL) widget = QApplication::activeWindow();
+    QMessageBox::critical(widget, QObject::tr("Failed while checking XML:"), except.message());
+  }
 }
 
 

+ 2 - 1
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.h

@@ -35,6 +35,7 @@ class ctkCmdLineModuleExplorerTabList;
 class ctkCmdLineModuleReference;
 class ctkCmdLineModuleResult;
 class ctkSettingsDialog;
+class ctkCmdLineModuleBackendXMLChecker;
 
 namespace Ui {
 class ctkCmdLineModuleExplorerMainWindow;
@@ -98,7 +99,7 @@ private:
 
   ctkSettings settings;
   ctkSettingsDialog* settingsDialog;
-
+  ctkCmdLineModuleBackendXMLChecker* xmlCheckerBackEnd;
 };
 
 #endif // CTKCLIPLUGINEXPLORERMAINWINDOW_H

+ 1 - 1
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.ui

@@ -351,7 +351,7 @@
           <number>0</number>
          </property>
          <item>
-          <widget class="QPlainTextEdit" name="plainTextEdit"/>
+          <widget class="QPlainTextEdit" name="m_XMLToValidate"/>
          </item>
         </layout>
        </widget>

+ 29 - 4
Libs/CommandLineModules/Backend/XMLChecker/ctkCmdLineModuleBackendXMLChecker.cpp

@@ -32,14 +32,25 @@
 struct ctkCmdLineModuleBackendXMLCheckerPrivate
 {
   QString m_HardCodedXML;
+  QDateTime m_LastModified;
 };
 
 
 //----------------------------------------------------------------------------
+ctkCmdLineModuleBackendXMLChecker::ctkCmdLineModuleBackendXMLChecker()
+  : d(new ctkCmdLineModuleBackendXMLCheckerPrivate)
+{
+  d->m_HardCodedXML = QString("");
+  d->m_LastModified = QDateTime::currentDateTime();
+}
+
+
+//----------------------------------------------------------------------------
 ctkCmdLineModuleBackendXMLChecker::ctkCmdLineModuleBackendXMLChecker(const QString &xmlToValidate)
   : d(new ctkCmdLineModuleBackendXMLCheckerPrivate)
 {
   d->m_HardCodedXML = xmlToValidate;
+  d->m_LastModified = QDateTime::currentDateTime();
 }
 
 
@@ -50,6 +61,21 @@ ctkCmdLineModuleBackendXMLChecker::~ctkCmdLineModuleBackendXMLChecker()
 
 
 //----------------------------------------------------------------------------
+void ctkCmdLineModuleBackendXMLChecker::setXML(const QString& xml)
+{
+  d->m_HardCodedXML = xml;
+  d->m_LastModified = QDateTime::currentDateTime();
+}
+
+
+//----------------------------------------------------------------------------
+QString ctkCmdLineModuleBackendXMLChecker::xml() const
+{
+  return d->m_HardCodedXML;
+}
+
+
+//----------------------------------------------------------------------------
 QString ctkCmdLineModuleBackendXMLChecker::name() const
 {
   return "XML Checker";
@@ -59,14 +85,14 @@ QString ctkCmdLineModuleBackendXMLChecker::name() const
 //----------------------------------------------------------------------------
 QString ctkCmdLineModuleBackendXMLChecker::description() const
 {
-  return "Fakes a backend process, returning a hard coded piece of XML, provided at construction time.";
+  return "Fakes a backend process, returning a static piece of XML.";
 }
 
 
 //----------------------------------------------------------------------------
 QList<QString> ctkCmdLineModuleBackendXMLChecker::schemes() const
 {
-  static QList<QString> supportedSchemes = QList<QString>() << "xml checker";
+  static QList<QString> supportedSchemes = QList<QString>() << "xmlchecker";
   return supportedSchemes;
 }
 
@@ -74,8 +100,7 @@ QList<QString> ctkCmdLineModuleBackendXMLChecker::schemes() const
 //----------------------------------------------------------------------------
 qint64 ctkCmdLineModuleBackendXMLChecker::timeStamp(const QUrl & /*location*/) const
 {
-  QDateTime now = QDateTime::currentDateTime();
-  return ctk::msecsTo(QDateTime::fromTime_t(0), now);
+  return ctk::msecsTo(QDateTime::fromTime_t(0), d->m_LastModified);
 }
 
 

+ 4 - 0
Libs/CommandLineModules/Backend/XMLChecker/ctkCmdLineModuleBackendXMLChecker.h

@@ -42,6 +42,7 @@ class CTK_CMDLINEMODULEBACKENDXMLCHECKER_EXPORT ctkCmdLineModuleBackendXMLChecke
 
 public:
 
+  ctkCmdLineModuleBackendXMLChecker();
   ctkCmdLineModuleBackendXMLChecker(const QString &xmlToValidate);
   ~ctkCmdLineModuleBackendXMLChecker();
 
@@ -78,6 +79,9 @@ public:
    */
   virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend *frontend);
 
+  void setXML(const QString& xml);
+  QString xml() const;
+
 private:
 
   QScopedPointer<ctkCmdLineModuleBackendXMLCheckerPrivate> d;