소스 검색

Added option to format xml output from xsl transformations.

Sascha Zelzer 12 년 전
부모
커밋
7e1ec6e317

+ 6 - 54
Libs/CommandLineModules/Core/Testing/Cpp/ctkCmdLineModuleXslTransformTest.cpp

@@ -156,30 +156,12 @@ QString integerWidgetSpinBox =
   "                                <property name=\"toolTip\">\n"
   "                                    <string>Integer description</string>\n"
   "                                </property>\n"
-  "                                <property name=\"parameter:cppType\">\n"
-  "                                    <string>int</string>\n"
-  "                                </property>\n"
   "                                <property name=\"parameter:valueProperty\">\n"
   "                                    <string>value</string>\n"
   "                                </property>\n"
   "                                <property name=\"value\">\n"
   "                                    <number>1</number>\n"
   "                                </property>\n"
-  "                                <property name=\"parameter:flag\">\n"
-  "                                    <string>-i</string>\n"
-  "                                </property>\n"
-  "                                <property name=\"parameter:longflag\">\n"
-  "                                    <string>--integer</string>\n"
-  "                                </property>\n"
-  "                                <property name=\"parameter:description\">\n"
-  "                                    <string>Integer description</string>\n"
-  "                                </property>\n"
-  "                                <property name=\"parameter:label\">\n"
-  "                                    <string>Integer</string>\n"
-  "                                </property>\n"
-  "                                <property name=\"parameter:default\">\n"
-  "                                    <string>1</string>\n"
-  "                                </property>\n"
   "                            </widget>\n"
   ;
 QString integerWidgetSpinBoxFooter =
@@ -201,6 +183,8 @@ void ctkCmdLineModuleXslTransformTester::testTransform()
   output.open(QBuffer::ReadWrite);
   transformer.setOutput(&output);
 
+  transformer.setFormatXmlOutput(true);
+
   QFETCH(bool, expectedSuccess);
   bool success = transformer.transform();
   if (!success)
@@ -277,6 +261,8 @@ void ctkCmdLineModuleXslTransformTester::testBindVariable()
   output.open(QBuffer::ReadWrite);
   transformer.setOutput(&output);
 
+  transformer.setFormatXmlOutput(true);
+
   QFETCH(QString, variableName);
   QFETCH(QString, variableValue);
   transformer.bindVariable(variableName, variableValue);
@@ -333,6 +319,8 @@ void ctkCmdLineModuleXslTransformTester::testXslExtraTransformation()
   output.open(QBuffer::ReadWrite);
   transformer.setOutput(&output);
 
+  transformer.setFormatXmlOutput(true);
+
   QFETCH(QString, extra);
   QByteArray extraTransformationArray(extra.toUtf8());
   QBuffer extraTransformationBuffer(&extraTransformationArray);
@@ -397,30 +385,12 @@ void ctkCmdLineModuleXslTransformTester::testXslExtraTransformation_data()
     "                                        <property name=\"toolTip\">\n"
     "                                            <string>Integer description</string>\n"
     "                                        </property>\n"
-    "                                        <property name=\"parameter:cppType\">\n"
-    "                                            <string>int</string>\n"
-    "                                        </property>\n"
     "                                        <property name=\"parameter:valueProperty\">\n"
     "                                            <string>value</string>\n"
     "                                        </property>\n"
     "                                        <property name=\"value\">\n"
     "                                            <number>1</number>\n"
     "                                        </property>\n"
-    "                                        <property name=\"parameter:flag\">\n"
-    "                                            <string>-i</string>\n"
-    "                                        </property>\n"
-    "                                        <property name=\"parameter:longflag\">\n"
-    "                                            <string>--integer</string>\n"
-    "                                        </property>\n"
-    "                                        <property name=\"parameter:description\">\n"
-    "                                            <string>Integer description</string>\n"
-    "                                        </property>\n"
-    "                                        <property name=\"parameter:label\">\n"
-    "                                            <string>Integer</string>\n"
-    "                                        </property>\n"
-    "                                        <property name=\"parameter:default\">\n"
-    "                                            <string>1</string>\n"
-    "                                        </property>\n"
     "                                    </widget>\n"
     "                                </item>\n"
     "                                <item>\n"
@@ -434,30 +404,12 @@ void ctkCmdLineModuleXslTransformTester::testXslExtraTransformation_data()
     "                                        <property name=\"toolTip\">\n"
     "                                            <string>Integer description</string>\n"
     "                                        </property>\n"
-    "                                        <property name=\"parameter:cppType\">\n"
-    "                                            <string>int</string>\n"
-    "                                        </property>\n"
     "                                        <property name=\"parameter:valueProperty\">\n"
     "                                            <string>value</string>\n"
     "                                        </property>\n"
     "                                        <property name=\"value\">\n"
     "                                            <number>1</number>\n"
     "                                        </property>\n"
-    "                                        <property name=\"parameter:flag\">\n"
-    "                                            <string>-i</string>\n"
-    "                                        </property>\n"
-    "                                        <property name=\"parameter:longflag\">\n"
-    "                                            <string>--integer</string>\n"
-    "                                        </property>\n"
-    "                                        <property name=\"parameter:description\">\n"
-    "                                            <string>Integer description</string>\n"
-    "                                        </property>\n"
-    "                                        <property name=\"parameter:label\">\n"
-    "                                            <string>Integer</string>\n"
-    "                                        </property>\n"
-    "                                        <property name=\"parameter:default\">\n"
-    "                                            <string>1</string>\n"
-    "                                        </property>\n"
     "                                    </widget>\n"
     "                                </item>\n"
     "                            </layout>\n"

+ 31 - 2
Libs/CommandLineModules/Core/ctkCmdLineModuleXslTransform.cpp

@@ -25,6 +25,7 @@
 #include <QFile>
 #include <QXmlSchema>
 #include <QXmlSchemaValidator>
+#include <QXmlFormatter>
 
 // CTK includes
 #include "ctkCmdLineModuleXslTransform.h"
@@ -33,6 +34,7 @@
 ctkCmdLineModuleXslTransform::ctkCmdLineModuleXslTransform(QIODevice *input, QIODevice *output)
   : ctkCmdLineModuleXmlValidator(input)
   , Validate(false)
+  , Format(false)
   , OutputSchema(0)
   , Transformation(0)
   , Output(output)
@@ -75,6 +77,16 @@ void ctkCmdLineModuleXslTransform::setOutputSchema(QIODevice *output)
   this->OutputSchema = output;
 }
 
+bool ctkCmdLineModuleXslTransform::formatXmlOutput() const
+{
+  return this->Format;
+}
+
+void ctkCmdLineModuleXslTransform::setFormatXmlOutput(bool format)
+{
+  this->Format = format;
+}
+
 bool ctkCmdLineModuleXslTransform::transform()
 {
   this->ErrorStr.clear();
@@ -131,7 +143,17 @@ bool ctkCmdLineModuleXslTransform::transform()
     closeOutput = true;
   }
 
-  if (!this->XslTransform.evaluateTo(this->Output))
+  QScopedPointer<QXmlSerializer> xmlSerializer;
+  if (Format)
+  {
+    xmlSerializer.reset(new QXmlFormatter(this->XslTransform, this->Output));
+  }
+  else
+  {
+    xmlSerializer.reset(new QXmlSerializer(this->XslTransform, this->Output));
+  }
+
+  if (!this->XslTransform.evaluateTo(xmlSerializer.data()))
   {
     QString msg("Error transforming XML input, at line %1, column %2: %3");
     this->ErrorStr = msg.arg(this->MsgHandler->line()).arg(this->MsgHandler->column())
@@ -169,7 +191,14 @@ void ctkCmdLineModuleXslTransform::bindVariable(const QString& name, const QVari
   this->XslTransform.bindVariable(name, value);
 }
 
-void ctkCmdLineModuleXslTransform::setXslExtraTransformations(QList<QIODevice*> transformations)
+void ctkCmdLineModuleXslTransform::setXslExtraTransformation(QIODevice* transformation)
+{
+  QList<QIODevice*> transformations;
+  transformations<<transformation;
+  this->setXslExtraTransformations(transformations);
+}
+
+void ctkCmdLineModuleXslTransform::setXslExtraTransformations(const QList<QIODevice *>& transformations)
 {
   this->ExtraTransformations = transformations;
 }

+ 19 - 8
Libs/CommandLineModules/Core/ctkCmdLineModuleXslTransform.h

@@ -51,6 +51,22 @@ public:
   void setOutputSchema(QIODevice* output);
 
   /**
+   * @brief Returns \code true if the XSL output will be formatted.
+   * @return \code true if the ouptut will be formatted, \code false otherwise.
+   */
+  bool formatXmlOutput() const;
+
+  /**
+   * @brief Formats the XSL output to be human-readable.
+   *
+   * It is assumed that the XSL output is valid XML. The output will be
+   * formatted with an indentation depth of four spaces. Note that setting
+   * \e format to \code true increases compuational overhead and memory
+   * requirements and is usually only done for testing or debugging purposes.
+   */
+  void setFormatXmlOutput(bool format);
+
+  /**
    * @brief Transforms an XML input via a XSL transformation.
    *
    * This method assumes that the input set via setInput() or supplied
@@ -70,8 +86,8 @@ public:
    *
    * @return
    */
-  inline void setXslExtraTransformation(QIODevice* transformation);
-  void setXslExtraTransformations(QList<QIODevice*> transformations);
+  void setXslExtraTransformation(QIODevice* transformation);
+  void setXslExtraTransformations(const QList<QIODevice*>& transformations);
 
   /**
    *  @brief Binds the variable name to the value so that $name can be used
@@ -103,6 +119,7 @@ private:
   bool validateOutput();
 
   bool Validate;
+  bool Format;
 
   QIODevice* OutputSchema;
   QIODevice* Transformation;
@@ -115,11 +132,5 @@ private:
   QString ErrorStr;
 };
 
-void ctkCmdLineModuleXslTransform::setXslExtraTransformation(QIODevice* transformation)
-{
-  QList<QIODevice*> transformations;
-  transformations<<transformation;
-  this->setXslExtraTransformations(transformations);
-}
 
 #endif // CTKCMDLINEMODULEXSLTRANSFORM_H