[Ktutorial-commits] SF.net SVN: ktutorial:[192] trunk/ktutorial/ktutorial-editor/src/ serialization
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-25 23:45:25
|
Revision: 192
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=192&view=rev
Author: danxuliu
Date: 2010-03-25 23:45:18 +0000 (Thu, 25 Mar 2010)
Log Message:
-----------
Add TutorialWriter class to serialize a Tutorial in XML. A W3C Schema for XML serialized tutorials is also included.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt
Added Paths:
-----------
trunk/ktutorial/ktutorial-editor/src/serialization/Tutorial.xsd
trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.cpp
trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.h
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt 2010-03-24 21:08:50 UTC (rev 191)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt 2010-03-25 23:45:18 UTC (rev 192)
@@ -3,6 +3,7 @@
set(ktutorial_editor_serialization_SRCS
JavascriptExporter.cpp
Serialization.cpp
+ TutorialWriter.cpp
)
kde4_add_library(ktutorial_editor_serialization ${ktutorial_editor_serialization_SRCS})
Added: trunk/ktutorial/ktutorial-editor/src/serialization/Tutorial.xsd
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/Tutorial.xsd (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/Tutorial.xsd 2010-03-25 23:45:18 UTC (rev 192)
@@ -0,0 +1,99 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
+ <xsd:element name="tutorial" type="TutorialType"/>
+
+ <xsd:complexType name="TutorialType">
+ <xsd:sequence>
+ <xsd:element name="description" type="xsd:string" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="license" type="xsd:string" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="setup" type="xsd:string" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="tearDown" type="xsd:string" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="step" type="StepType" minOccurs="0" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="optional"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="StepType">
+ <xsd:sequence>
+ <xsd:element name="text" type="xsd:string" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="setup" type="xsd:string" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="tearDown" type="xsd:string" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="reaction" type="ReactionType" minOccurs="0" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="id" type="xsd:string" use="optional"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="ReactionType">
+ <xsd:sequence>
+ <xsd:element name="option" type="OptionType" minOccurs="0" maxOccurs="1"/>
+ <xsd:group ref="waitFor" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="customCode" type="xsd:string" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="nextStep" type="NextStepType" minOccurs="0" maxOccurs="1"/>
+ </xsd:sequence>
+ <xsd:attribute name="triggerType" type="TriggerTypeType" use="optional"/>
+ <xsd:attribute name="responseType" type="ResponseTypeType" use="optional"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="OptionType">
+ <xsd:attribute name="name" type="xsd:string" use="optional"/>
+ </xsd:complexType>
+
+ <xsd:group name="waitFor">
+ <xsd:choice>
+ <xsd:element name="waitForComposed" type="WaitForComposedType"/>
+ <xsd:element name="waitForEvent" type="WaitForEventType"/>
+ <xsd:element name="waitForNot" type="WaitForNotType"/>
+ <xsd:element name="waitForSignal" type="WaitForSignalType"/>
+ </xsd:choice>
+ </xsd:group>
+
+ <xsd:complexType name="NextStepType">
+ <xsd:attribute name="id" type="xsd:string" use="optional"/>
+ </xsd:complexType>
+
+ <xsd:simpleType name="TriggerTypeType">
+ <xsd:restriction base="xsd:string">
+ <xsd:enumeration value="ConditionMet"/>
+ <xsd:enumeration value="OptionSelected"/>
+ </xsd:restriction>
+ </xsd:simpleType>
+
+ <xsd:simpleType name="ResponseTypeType">
+ <xsd:restriction base="xsd:string">
+ <xsd:enumeration value="CustomCode"/>
+ <xsd:enumeration value="NextStep"/>
+ </xsd:restriction>
+ </xsd:simpleType>
+
+ <xsd:complexType name="WaitForComposedType">
+ <xsd:sequence>
+ <xsd:group ref="waitFor" minOccurs="0" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="compositionType" type="CompositionTypeType" use="optional"/>
+ </xsd:complexType>
+
+ <xsd:simpleType name="CompositionTypeType">
+ <xsd:restriction base="xsd:string">
+ <xsd:enumeration value="And"/>
+ <xsd:enumeration value="Or"/>
+ </xsd:restriction>
+ </xsd:simpleType>
+
+ <xsd:complexType name="WaitForEventType">
+ <xsd:attribute name="receiverName" type="xsd:string" use="optional"/>
+ <xsd:attribute name="eventName" type="xsd:string" use="optional"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="WaitForNotType">
+ <xsd:sequence>
+ <xsd:group ref="waitFor" minOccurs="0" maxOccurs="1"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:complexType name="WaitForSignalType">
+ <xsd:attribute name="emitterName" type="xsd:string" use="optional"/>
+ <xsd:attribute name="signalName" type="xsd:string" use="optional"/>
+ </xsd:complexType>
+
+</xsd:schema>
Added: trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.cpp 2010-03-25 23:45:18 UTC (rev 192)
@@ -0,0 +1,206 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * 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. *
+ * *
+ * 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 "TutorialWriter.h"
+
+#include "../Reaction.h"
+#include "../Step.h"
+#include "../Tutorial.h"
+#include "../WaitForComposed.h"
+#include "../WaitForEvent.h"
+#include "../WaitForNot.h"
+#include "../WaitForSignal.h"
+
+//public:
+
+TutorialWriter::TutorialWriter(): mXmlWriter(0) {
+}
+
+QString TutorialWriter::writeTutorial(const Tutorial* tutorial) {
+ QString serializedTutorial;
+
+ mXmlWriter = new QXmlStreamWriter(&serializedTutorial);
+ mXmlWriter->setAutoFormatting(true);
+ mXmlWriter->setAutoFormattingIndent(4);
+
+ mXmlWriter->writeStartDocument();
+ mXmlWriter->writeStartElement("tutorial");
+
+ if (!tutorial->name().isEmpty()) {
+ mXmlWriter->writeAttribute("name", tutorial->name());
+ }
+ if (!tutorial->description().isEmpty()) {
+ mXmlWriter->writeTextElement("description", tutorial->description());
+ }
+ if (!tutorial->licenseText().isEmpty()) {
+ mXmlWriter->writeTextElement("license", tutorial->licenseText());
+ }
+ if (!tutorial->customSetupCode().isEmpty()) {
+ mXmlWriter->writeTextElement("setup", tutorial->customSetupCode());
+ }
+ if (!tutorial->customTearDownCode().isEmpty()) {
+ mXmlWriter->writeTextElement("tearDown",
+ tutorial->customTearDownCode());
+ }
+ foreach (const Step* step, tutorial->steps()) {
+ write(step);
+ }
+
+ mXmlWriter->writeEndElement();
+ mXmlWriter->writeEndDocument();
+
+ delete mXmlWriter;
+ mXmlWriter = 0;
+
+ //QXm lStreamWriter doesn't write the encoding information when a string
+ //is used instead of a QIODevice
+ serializedTutorial.replace("<?xml version=\"1.0\"?>\n",
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
+ return serializedTutorial;
+}
+
+//private:
+
+void TutorialWriter::write(const Step* step) {
+ mXmlWriter->writeStartElement("step");
+
+ if (!step->id().isEmpty()) {
+ mXmlWriter->writeAttribute("id", step->id());
+ }
+ if (!step->text().isEmpty()) {
+ mXmlWriter->writeTextElement("text", step->text());
+ }
+ if (!step->customSetupCode().isEmpty()) {
+ mXmlWriter->writeTextElement("setup", step->customSetupCode());
+ }
+ if (!step->customTearDownCode().isEmpty()) {
+ mXmlWriter->writeTextElement("tearDown", step->customTearDownCode());
+ }
+ foreach (const Reaction* reaction, step->reactions()) {
+ write(reaction);
+ }
+
+ mXmlWriter->writeEndElement();
+}
+
+void TutorialWriter::write(const Reaction* reaction) {
+ mXmlWriter->writeStartElement("reaction");
+
+ QString triggerTypeValue;
+ if (reaction->triggerType() == Reaction::ConditionMet) {
+ triggerTypeValue = "ConditionMet";
+ } else {
+ triggerTypeValue = "OptionSelected";
+ }
+ mXmlWriter->writeAttribute("triggerType", triggerTypeValue);
+
+ QString responseTypeValue;
+ if (reaction->responseType() == Reaction::CustomCode) {
+ responseTypeValue = "CustomCode";
+ } else {
+ responseTypeValue = "NextStep";
+ }
+ mXmlWriter->writeAttribute("responseType", responseTypeValue);
+
+ if (!reaction->optionName().isEmpty()) {
+ mXmlWriter->writeEmptyElement("option");
+ mXmlWriter->writeAttribute("name", reaction->optionName());
+ }
+ if (reaction->waitFor()) {
+ write(reaction->waitFor());
+ }
+ if (!reaction->customCode().isEmpty()) {
+ mXmlWriter->writeTextElement("customCode", reaction->customCode());
+ }
+ if (!reaction->nextStepId().isEmpty()) {
+ mXmlWriter->writeEmptyElement("nextStep");
+ mXmlWriter->writeAttribute("id", reaction->nextStepId());
+ }
+
+ mXmlWriter->writeEndElement();
+}
+
+void TutorialWriter::write(const WaitFor* waitFor) {
+ if (qobject_cast<const WaitForComposed*>(waitFor)) {
+ write(static_cast<const WaitForComposed*>(waitFor));
+ return;
+ }
+ if (qobject_cast<const WaitForEvent*>(waitFor)) {
+ write(static_cast<const WaitForEvent*>(waitFor));
+ return;
+ }
+ if (qobject_cast<const WaitForNot*>(waitFor)) {
+ write(static_cast<const WaitForNot*>(waitFor));
+ return;
+ }
+ if (qobject_cast<const WaitForSignal*>(waitFor)) {
+ write(static_cast<const WaitForSignal*>(waitFor));
+ return;
+ }
+}
+
+void TutorialWriter::write(const WaitForComposed* waitForComposed) {
+ mXmlWriter->writeStartElement("waitForComposed");
+
+ QString compositionTypeValue;
+ if (waitForComposed->compositionType() == WaitForComposed::And) {
+ compositionTypeValue = "And";
+ } else {
+ compositionTypeValue = "Or";
+ }
+ mXmlWriter->writeAttribute("compositionType", compositionTypeValue);
+
+ foreach (const WaitFor* waitFor, waitForComposed->waitFors()) {
+ write(waitFor);
+ }
+
+ mXmlWriter->writeEndElement();
+}
+
+void TutorialWriter::write(const WaitForEvent* waitForEvent) {
+ mXmlWriter->writeEmptyElement("waitForEvent");
+
+ if (!waitForEvent->receiverName().isEmpty()) {
+ mXmlWriter->writeAttribute("receiverName",
+ waitForEvent->receiverName());
+ }
+ if (!waitForEvent->eventName().isEmpty()) {
+ mXmlWriter->writeAttribute("eventName", waitForEvent->eventName());
+ }
+}
+
+void TutorialWriter::write(const WaitForNot* waitForNot) {
+ mXmlWriter->writeStartElement("waitForNot");
+
+ if (waitForNot->negatedWaitFor()) {
+ write(waitForNot->negatedWaitFor());
+ }
+
+ mXmlWriter->writeEndElement();
+}
+
+void TutorialWriter::write(const WaitForSignal* waitForSignal) {
+ mXmlWriter->writeEmptyElement("waitForSignal");
+
+ if (!waitForSignal->emitterName().isEmpty()) {
+ mXmlWriter->writeAttribute("emitterName", waitForSignal->emitterName());
+ }
+ if (!waitForSignal->signalName().isEmpty()) {
+ mXmlWriter->writeAttribute("signalName", waitForSignal->signalName());
+ }
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.h 2010-03-25 23:45:18 UTC (rev 192)
@@ -0,0 +1,126 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * 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. *
+ * *
+ * 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/>. *
+ ***************************************************************************/
+
+#ifndef TUTORIALWRITER_H
+#define TUTORIALWRITER_H
+
+#include <QXmlStreamWriter>
+
+class Reaction;
+class Step;
+class Tutorial;
+class WaitFor;
+class WaitForComposed;
+class WaitForEvent;
+class WaitForNot;
+class WaitForSignal;
+
+/**
+ * Serializes a Tutorial in XML.
+ * Each data class has its own XML element (Tutorial, Step...). Objects that
+ * contain objects from other classes (for example, a Tutorial contains Steps)
+ * follow a hierarchical XML structure (the Step XML element is child of
+ * Tutorial XML element).
+ *
+ * Most object attributes are written as XML text elements (like the custom
+ * code in a Tutorial). Some of them, which by their very own nature are small
+ * strings, are written as XML attributes instead (like the id of a Step).
+ *
+ * When an object doesn't have some attribute set, be it a string or another
+ * object, their XML attribute or XML element isn't written at all.
+ *
+ * For further details, a W3C XML Schema is provided in Tutorial.xsd file
+ * (ktutorial/ktutorial-editor/src/serialization/Tutorial.xsd).
+ */
+class TutorialWriter {
+public:
+
+ /**
+ * Creates a new TutorialWriter.
+ */
+ TutorialWriter();
+
+ /**
+ * Returns the XML serialization of the given tutorial.
+ *
+ * @param tutorial The tutorial to get its XML serialization.
+ * @return The XML serialization of the given tutorial.
+ */
+ QString writeTutorial(const Tutorial* tutorial);
+
+private:
+
+ /**
+ * The XML writer to use.
+ */
+ QXmlStreamWriter* mXmlWriter;
+
+ /**
+ * Writes the XML serialization of the given step.
+ *
+ * @param step The Step to get its XML serialization.
+ */
+ void write(const Step* step);
+
+ /**
+ * Writes the XML serialization of the given reaction.
+ *
+ * @param reaction The Reaction to get its XML serialization.
+ */
+ void write(const Reaction* reaction);
+
+ /**
+ * Writes the XML serialization of the given WaitFor.
+ * It calls the appropriate write(const WaitForXXX*) method, depending on
+ * the WaitFor subclass.
+ *
+ * @param waitFor The WaitFor to get its XML serialization.
+ */
+ void write(const WaitFor* waitFor);
+
+ /**
+ * Writes the XML serialization of the given WaitForComposed.
+ *
+ * @param waitForComposed The WaitForComposed to get its XML serialization.
+ */
+ void write(const WaitForComposed* waitForComposed);
+
+ /**
+ * Writes the XML serialization of the given WaitForEvent.
+ *
+ * @param waitForEvent The WaitForEvent to get its XML serialization.
+ */
+ void write(const WaitForEvent* waitForEvent);
+
+ /**
+ * Writes the XML serialization of the given WaitForNot.
+ *
+ * @param waitForNot The WaitForNot to get its XML serialization.
+ */
+ void write(const WaitForNot* waitForNot);
+
+ /**
+ * Writes the XML serialization of the given WaitForComposed.
+ *
+ * @param waitForSignal The WaitForSignal to get its XML serialization.
+ */
+ void write(const WaitForSignal* waitForSignal);
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/serialization/TutorialWriter.h
___________________________________________________________________
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|