Ejemplo n.º 1
0
void FileItem::fromXML(const xmlpp::Element &root) throw(xmlpp::exception, invalid_argument, overflow_error)
{
    using namespace xmlpp;
    
    Node::NodeList list = root.get_children("file");
    string name = "";
    if (list.size()==0)
    {
        throw xmlpp::exception("Missing file name");
    }
    else
    {
        Element *tmp = dynamic_cast<Element*>(list.front());
        Attribute *attr = tmp->get_attribute("name");
        if (attr!=NULL)
        {
            name = attr->get_value();
        }
    }
    setFileName(name);
}
string XmlReader::getTextFromElement(const Element* elementNode, const string& childName, bool required) const	{
    string text;
    const Node::NodeList children = elementNode->get_children(childName);
    if (children.size() == 1)	{
        const Element *elementChild = castToElement(children.front());
        for (const Node *child : elementChild->get_children())	{
            const TextNode* textNode = dynamic_cast<const TextNode*>(child);
            if (textNode != nullptr)	{
                if (!textNode->is_white_space())
                    text += textNode->get_content();
            } else {
                throw InvalidDatasetFile(caller(), "Invalid cast to 'TextNode*' type!", (child != nullptr ? child->get_line() : -1));
            }
        }
    } else if (children.size() > 1)	{
        throw InvalidDatasetFile(caller(), "Only from one child the text can be retrieved!", elementNode->get_line());
    } else if (children.empty() && required)	{
        string msg = "Cannot find the '"+string(childName)+"' element!\n";
        throw InvalidDatasetFile(caller(), msg+"Invalid input xml file!", elementNode->get_line());
    }

    return text;
}
Ejemplo n.º 3
0
void Scenario::fromFile(const std::string &fileName, bool checkFiles) throw(xmlpp::exception, invalid_argument)
{
    using namespace xmlpp;
    using namespace Poco;
    string xmlFile, fileType;
    bool isArchive = false;

    clear();
    if (pDetector)
    {
       fileType = pDetector->typeOfFile(fileName);
    }
    if (fileType == "text/xml" || fileType == "application/xml")
    {
        xmlFile = fileName;
    }
    else if (!pDetector || fileType == "application/zip")
    {
        // attempt to unzip
        FileInputStream input(fileName.c_str());
        if (input.good())
        {
            // creating new temporary directory and extracting into it
            sTempDir = TemporaryFile::tempName();
            Zip::Decompress dec(input, sTempDir);
            TemporaryFile::registerForDeletion(sTempDir);
            // valid extraction
            isArchive = false;
            try
            {
                dec.decompressAllFiles();
                isArchive = true;
            }
            catch (Poco::Exception)
            {
                if (!pDetector)
                {
                    xmlFile = fileName;
                }
                else
                {
                    throw xmlpp::exception("Bad file format");
                }
            }
            if (isArchive)
            {
                Zip::Decompress::ZipMapping mapping = dec.mapping();
                try
                {
                    xmlFile = mapping.at("scenario.xml").makeAbsolute(sTempDir).toString();
                }
                catch(out_of_range &e)
                {
                    throw xmlpp::exception("No scenario in file " + fileName);
                }
            }
        }
        else
        {
            throw xmlpp::exception("Unable to open the file " + fileName);
        }
    }
    else
    {
        throw xmlpp::exception("Unrecognized file format");
    }
    ioConfig = IOConfig::detect(xmlFile, isArchive);
    DomParser parser(xmlFile);
    Document *document = parser.get_document();
    Element *root = document->get_root_node();
    if (root->get_name() != ioConfig.rootName())
    {
        throw xmlpp::exception("Bad document content type: " + ioConfig.rootName() + " expected");
    }
    // getting the user interface
    try
    {
        Attribute *attr = root->get_attribute("interface");
        if (attr)
        {
            uiInterface = stringToInterface(attr->get_value());
        }
        else
        {
            uiInterface = uiFull;
        }
    }
    catch (invalid_argument)
    {
        throw xmlpp::exception("Bad user interface");
    }
    // now loading the different parts of the game
    Node::NodeList node;
    if (ioConfig.hasMetadata())
    {
        node = root->get_children("metadata");
        if (!node.empty())
        {
            mMetadata.fromXML(*dynamic_cast<Element*>(node.front()));
        }
    }
    node = root->get_children(ioConfig.plotName());
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"" + ioConfig.plotName() + "\" section");
    }
    else
    {
        tPlot.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()), checkFiles);
    }
    node = root->get_children("notes");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"notes\" section");
    }
    else
    {
        Element *elem = dynamic_cast<Element*>(node.front());
        string mainText = "";
        if (elem->has_child_text())
        {
            mainText = elem->get_child_text()->get_content();
        }
        nMain = Note("", mainText);
    }
    node = root->get_children(ioConfig.propertiesName());
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"" + ioConfig.propertiesName() + "\" section");
    }
    else
    {
        lProperties.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("characters");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"characters\" section");
    }
    else
    {
        lCharacters.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("history");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"history\" section");
    }
    else
    {
        tHistory.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()), checkFiles);
    }
    node = root->get_children("music");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"music\" section");
    }
    else
    {
        tMusic.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()), checkFiles);
    }
    node = root->get_children("effects");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"effects\" section");
    }
    else
    {
        tEffects.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()), checkFiles);
    }
}
Ejemplo n.º 4
0
void Scenario::fromFile(const std::string &fileName) throw(xmlpp::exception, invalid_argument, overflow_error)
{
    using namespace xmlpp;

    clear();
    DomParser parser(fileName);
    Document *document = parser.get_document();
    Element *root = document->get_root_node();
    if (root->get_name()!="game")
    {
        throw xmlpp::exception("Bad document content type: game expected");
    }
    // getting the user interface
    try
    {
        Attribute *attr = root->get_attribute("interface");
        if (attr != NULL)
        {
            uiInterface = stringToInterface(attr->get_value());
        }
        else
        {
            uiInterface = uiFull;
        }
    }
    catch (invalid_argument)
    {
        throw xmlpp::exception("Bad user interface");
    }
    // now loading the different parts of the game
    Node::NodeList node = root->get_children("scenario");
    if (!node.empty())
    {
        tScenario.fromXML(*dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("notes");
    if (!node.empty())
    {
        Element *elem = dynamic_cast<Element*>(node.front());
        if (elem->has_child_text())
        {
            sNotes = elem->get_child_text()->get_content();
        }
        else
        {
            sNotes = "";
        }
    }
    node = root->get_children("skills");
    if (!node.empty())
    {
        lSkills.fromXML(*dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("characters");
    if (!node.empty())
    {
        lCharacters.fromXML(*dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("history");
    if (!node.empty())
    {
        tHistory.fromXML(*dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("music");
    if (!node.empty())
    {
        tMusic.fromXML(*dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("effects");
    if (!node.empty())
    {
        tEffects.fromXML(*dynamic_cast<Element*>(node.front()),true);
    }
}