Esempio n. 1
0
    /**
     * Writes the daventure data into the given file.
     *
     * @param folderName
     *            Folder where to write the data
     * @param adventureData
     *            Adventure data to write in the file
     * @param valid
     *            True if the adventure is valid (can be executed in the
     *            engine), false otherwise
     * @return True if the operation was succesfully completed, false otherwise
     */

    public static bool writeData(string folderName, AdventureDataControl adventureData, bool valid)
    {
        bool dataSaved = false;

        // Create the necessary elements for building the DOM
        doc = new XmlDocument();

        // Delete the previous XML files in the root of the project dir
        //DirectoryInfo projectFolder = new DirectoryInfo(folderName);
        //if (projectFolder.Exists)
        //{
        //    foreach (FileInfo file in projectFolder.GetFiles())
        //    {
        //        file.Delete();
        //    }
        //    foreach (DirectoryInfo dir in projectFolder.GetDirectories())
        //    {
        //        dir.Delete(true);
        //    }
        //}

        // Add the special asset files
        AssetsController.addSpecialAssets();

        /** ******* START WRITING THE DESCRIPTOR ********* */
        // Pick the main node for the descriptor
        XmlDeclaration  declaration    = doc.CreateXmlDeclaration("1.0", "UTF-8", "no");
        XmlDocumentType typeDescriptor = doc.CreateDocumentType("game-descriptor", "SYSTEM", "descriptor.dtd", null);

        doc.AppendChild(declaration);
        doc.AppendChild(typeDescriptor);
        XmlNode mainNode = DescriptorDOMWriter.buildDOM(adventureData, valid);

        indentDOM(mainNode, 0);
        doc.ImportNode(mainNode, true);
        doc.AppendChild(mainNode);

        // Create the necessary elements for export the DOM into a XML file
        //transformer = tFactory.newTransformer();
        //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "descriptor.dtd");

        // Create the output buffer, write the DOM and close it
        //fout = new FileOutputStream(folderName + "/descriptor.xml");
        //writeFile = new OutputStreamWriter(fout, "UTF-8");
        //transformer.transform(new DOMSource(doc), new StreamResult(writeFile));
        //writeFile.close();
        //fout.close();
        doc.Save(folderName + "/descriptor.xml");
        /** ******** END WRITING THE DESCRIPTOR ********** */

        /** ******* START WRITING THE CHAPTERS ********* */
        // Write every chapter
        XmlDocumentType typeChapter;

        int chapterIndex = 1;

        foreach (Chapter chapter in adventureData.getChapters())
        {
            doc         = new XmlDocument();
            declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "no");
            typeChapter = doc.CreateDocumentType("eAdventure", "SYSTEM", "eadventure.dtd", null);
            doc.AppendChild(declaration);
            doc.AppendChild(typeChapter);
            // Pick the main node of the chapter
            mainNode = ChapterDOMWriter.buildDOM(chapter, folderName, doc);
            /** ******* START WRITING THE ADAPTATION DATA ***** */
            foreach (AdaptationProfile profile in chapter.getAdaptationProfiles())
            {
                mainNode.AppendChild(Writer.writeAdaptationData(profile, true, doc));
            }
            /** ******* END WRITING THE ADAPTATION DATA ***** */

            /** ******* START WRITING THE ASSESSMENT DATA ***** */
            foreach (AssessmentProfile profile in chapter.getAssessmentProfiles())
            {
                mainNode.AppendChild(Writer.writeAssessmentData(profile, true, doc));
            }
            /** ******* END WRITING THE ASSESSMENT DATA ***** */

            indentDOM(mainNode, 0);
            //TODO: testing
            //doc = new XmlDocument();
            doc.ImportNode(mainNode, true);
            doc.AppendChild(mainNode);

            // Create the necessary elements for export the DOM into a XML file
            //transformer = tFactory.newTransformer();
            //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "eadventure.dtd");

            // Create the output buffer, write the DOM and close it
            //fout = new FileOutputStream(folderName + "/chapter" + chapterIndex++ + ".xml");
            //writeFile = new OutputStreamWriter(fout, "UTF-8");
            //transformer.transform(new DOMSource(doc), new StreamResult(writeFile));
            //writeFile.close();
            //fout.close();

            doc.Save(folderName + "/chapter" + chapterIndex++ + ".xml");
        }
        /** ******** END WRITING THE CHAPTERS ********** */

        // Update the zip files
        //File.umount( );
        dataSaved = true;
        return(dataSaved);
    }