⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 book.cpp

📁 个人电子图书管理系统.提供电子书的书名和其他信息.检查电子书信息的合法性(E-1) .为这本电子书生成 id.使用分类id
💻 CPP
字号:
// 静态模型
#include "Book.h"
#include <fstream>
namespace Library
{
    using std::string;

    
    /// <summary>使用三条简要信息构造Book</summary>
    /// <param name="name">[IN]图书的名称</param>
    /// <param name="author">[IN]图书的作者。</param>
    /// <param name="brief">[IN]图书的摘要,简单的介绍信息</param>
    /// <exception>重新抛出基类构造函数的所有异常</exception>
    /// <remarks></remarks>
    Book::Book(const string& name,
        const string& author,
        const string& brief)
        try:BookObject()
    {
        this->name = name;
        this->author = author;
        this->brief = brief;
        this->urls.push_back("");
    }catch(...){
        throw; // 重新抛出异常
    };

    
    /// <summary>Book的默认构造函数</summary>
    /// <exception>重新抛出基类构造函数的所有异常</exception>
    /// <remarks>Book的id之外的属性均没有赋值,为空值</remarks>
    Book::Book()
        try:BookObject()
    {
        this->urls.push_back("");
    }catch(...){
        throw; // 重新抛出异常
    };

    
    /// <summary>从一个XML节点创建Book</summary>
    /// <param name="bookElem">[IN]一个XML节点的指针, 这个节点应当拥有一定的结构(&lt;Book&gt;&lt;/Book&gt;)。</param>
    /// <returns> 成功返回:0    失败返回: 其它值</returns>
    /// <exception cref="KException">没有找到图书的id.缺乏必须的信息.</exception>
    /// <remarks>bookElem指针为空时, 构造函数正常执行,但是Book的所有属性均为空值.
    /// 如果bookElem(&lt;Book&gt;&lt;/Book&gt;)没有结构, 构造函数正常执行,但是Book的所有属性均为空值.
    /// 如果bookElem(&lt;Book&gt;&lt;/Book&gt;)有结构可以不提供构造一个Book的所有信息,但是必须提供Book::id信息,否则函数执行失败, 抛出异常.</remarks>
    Book::Book(TiXmlElement* bookElem)
        :BookObject(0)
    {
        if(NULL == bookElem)
        {
            return;
        }
        if(string(bookElem->Value()) != "Book")
        {
            return;
        }
        //TODO: if given attribute doesn't exist, then ?
        //DONE: if given attribute doesn't exist, then return NULL and set to ""
        const char*   pAttu = NULL;
        pAttu = bookElem->Attribute("id");
        if(NULL != pAttu)
        {
            this->id = pAttu;
        }
        else
        {
            throw KException("没有找到图书的id.缺乏必须的信息.");
        }

        pAttu = bookElem->Attribute("name");
        if(NULL != pAttu)
            this->name = pAttu;
        pAttu = bookElem->Attribute("author");
        if(NULL != pAttu)
            this->author = pAttu;
        pAttu = bookElem->Attribute("brief");
        if(NULL != pAttu)
            this->brief = pAttu;


        TiXmlNode* item = NULL;
        TiXmlNode* text = NULL;

        item = bookElem->FirstChild("image");
        if(NULL != item)
        {
            text = item->FirstChild();
            if(NULL != text)
                this->image = text->ToText()->Value();
        }

        this->urls.clear();
        item = bookElem->FirstChild("urls");
        if(NULL != item)
        {
            TiXmlNode* urlItem = NULL;
            for(urlItem = item->FirstChild("url");
                urlItem != NULL;
                urlItem = urlItem->NextSibling())
            {
                text = urlItem->FirstChild();
                if(NULL != text)
                {
                    this->urls.push_back( std::string(text->ToText()->Value()) );
                }
            }
        }

        item = bookElem->FirstChild("publisher");
        if(NULL != item)
        {
            text = item->FirstChild();
            if(NULL != text)
                this->publisher = text->ToText()->Value();
        }

        item = bookElem->FirstChild("edition");
        if(NULL != item)
        {
            text = item->FirstChild();
            if(NULL != text)
                this->edition = text->ToText()->Value();
        }

        item = bookElem->FirstChild("desc");
        if(NULL != item)
        {
            text = item->FirstChild();
            if(NULL != text)
                this->descriprtion = text->ToText()->Value();
        }


    }

    
    /// <summary>使用一个uuid创建Book</summary>
    /// <param name="uuid">[IN]用字符串表示的一个全球唯一标识符</param>
    /// <remarks></remarks>
    Book::Book(string uuid)
        :BookObject(uuid)
    {}

    
    /// <summary>重载Book的赋值运算符</summary>
    /// <param name="r">[IN]右操作数</param>
    /// <returns>Book&amp;:左操作数的引用</returns>
    /// <remarks></remarks>
    Book&  Book::operator=(const Book& r)
    {
        if(this == &r)
        {
            return *this;
        }

        // 复制属性
        this->id    = r.id;
        this->name  = r.name;
        this->author= r.author;
        this->brief = r.brief;
        this->image = r.image;
        this->urls.clear();
        this->urls  = r.urls;
        this->publisher = r.publisher;
        this->edition   = r.edition;
        this->descriprtion = r.descriprtion;
        return *this;
    }

    
    /// <summary>从一个XML节点加载Book的信息</summary>
    /// <param name="bookElem">[IN]一个XML节点的指针, 这个节点应当拥有一定的结构(&lt;Book&gt;&lt;/Book&gt;)。</param>
    /// <returns> 成功返回:0    失败返回: 其它值
    /// </returns>
    /// <exception cref="KException">没有找到图书的id.缺乏必须的信息.</exception>
    /// <remarks>bookElem指针为空时, 函数返回 1 (失败),但是Book的所有属性均没有变化。
    /// 如果bookElem(&lt;Book&gt;&lt;/Book&gt;)没有结构, 函数 1 (失败),但是Book的所有属性均没有变化.
    /// 如果bookElem(&lt;Book&gt;&lt;/Book&gt;)有结构可以不提供构造一个Book的所有信息,但是必须提供Book::id信息,否则函数执行失败, 抛出异常.
    /// </remarks>
    int Book::LoadFromXml(TiXmlElement* bookElem)
    {
        if(NULL == bookElem)
        {
            return 1;
        }
        if(string(bookElem->Value()) != "Book")
        {
            return 1;
        }
        const char*   pAttu = NULL;
        pAttu = bookElem->Attribute("id");
        if(NULL != pAttu)
        {
            this->id = pAttu;
        }
        else
        {
            throw KException("没有找到图书的id.缺乏必须的信息.");
        }
        pAttu = bookElem->Attribute("name");
        if(NULL != pAttu)
            this->name = pAttu;
        pAttu = bookElem->Attribute("author");
        if(NULL != pAttu)
            this->author = pAttu;
        pAttu = bookElem->Attribute("brief");
        if(NULL != pAttu)
            this->brief = pAttu;


        TiXmlNode* item = NULL;
        TiXmlNode* text = NULL;

        item = bookElem->FirstChild("image");
        if(NULL != item)
        {
            text = item->FirstChild();
            if(NULL != text)
                this->image = text->ToText()->Value();
        }

        item = bookElem->FirstChild("urls");
        if(NULL != item)
        {
            this->urls.clear();
            TiXmlNode* urlItem = NULL;
            for(urlItem = item->FirstChild("url");
                urlItem != NULL;
                urlItem = urlItem->NextSibling())
            {
                text = urlItem->FirstChild();
                if(NULL != text)
                {
                    this->urls.push_back( std::string(text->ToText()->Value()) );
                }
            }
        }

        item = bookElem->FirstChild("publisher");
        if(NULL != item)
        {
            text = item->FirstChild();
            if(NULL != text)
                this->publisher = text->ToText()->Value();
        }

        item = bookElem->FirstChild("edition");
        if(NULL != item)
        {
            text = item->FirstChild();
            if(NULL != text)
                this->edition = text->ToText()->Value();
        }

        item = bookElem->FirstChild("desc");
        if(NULL != item)
        {
            text = item->FirstChild();
            if(NULL != text)
                this->descriprtion = text->ToText()->Value();
        }

        return 0;
    }

    
    /// <summary>把Book的信息保存到一个XML节点</summary>
    /// <param name="bookElem">[IN]一个XML节点的指针,将清空bookElem的所有内容, 然后根据信息的内容,重新构造节点的结构.不能为空</param>
    /// <returns> 成功返回:0    失败返回: 其它值
    /// </returns>
    /// <remarks>bookElem不能为NULL</remarks>
    int Book::SaveToXml(TiXmlElement* bookElem) const 
    {
        if(NULL == bookElem)
            return 1;
        // 清空
        bookElem->Clear();

        // 节点名
        bookElem->SetValue("Book");

        // 设置attribute
        bookElem->SetAttribute("id", this->id);
        bookElem->SetAttribute("name", this->name);
        bookElem->SetAttribute("author", this->author);
        bookElem->SetAttribute("brief", this->brief);

        // 添加元素
        TiXmlElement item("");
        TiXmlText text("");

        item.SetValue("image");
        text.SetValue(this->image);
        item.InsertEndChild(text);
        bookElem->InsertEndChild(item);
        item.Clear();

        item.SetValue("urls");
        TiXmlElement urlItem("bookurl");
        item.InsertEndChild(urlItem);
        for(int i = 0; i < this->urls.size(); i ++)
        {
            TiXmlElement urlItem("url");
            text.SetValue(this->urls[i]);
            urlItem.InsertEndChild(text);
            item.InsertEndChild(urlItem);
        }
        bookElem->InsertEndChild(item);
        item.Clear();

        item.SetValue("publisher");
        text.SetValue(this->publisher);
        item.InsertEndChild(text);
        bookElem->InsertEndChild(item);
        item.Clear();

        item.SetValue("edition");
        text.SetValue(this->edition);
        item.InsertEndChild(text);
        bookElem->InsertEndChild(item);
        item.Clear();

        item.SetValue("desc");
        text.SetCDATA(true);
        text.SetValue(this->descriprtion);
        item.InsertEndChild(text);
        bookElem->InsertEndChild(item);
        item.Clear();
        return 0;
    }

    
    /// <summary>将Book的信息已XML文件的方式保存到一个本地文件,以便显示</summary>
    /// <param name="fileName">[IN]目的文件名</param>
    /// <returns> 成功返回:0    失败返回: 其它值</returns>
    /// <exception>这个函数不抛出异常</exception>
    /// <remarks></remarks>
    int Book::SaveToFile(const std::string &fileName, const std::string& xslt) const
    {//TODO:添加预处理, 以表明编码方式和样式表
        TiXmlElement bookElem("");
        if(0 != this->SaveToXml(&bookElem))
            return 1;

        TiXmlDeclaration xmlDec("1.0", "gb2312", "no");
        TiXmlStyleSheet  xmlStyle("text/xsl",xslt);

        // XML DOC
        TiXmlDocument xmlDoc;
        xmlDoc.InsertEndChild(xmlDec);
        xmlDoc.InsertEndChild(xmlStyle);
        xmlDoc.InsertEndChild(bookElem);
        //std::ofstream fout(fileName.c_str());
        //fout<< bookElem ;
        xmlDoc.SaveFile(fileName);
        return 0;
    }

    
    /// <summary>设置书名</summary>
    /// <param name="name">[IN]书的名字</param>
    /// <returns>void</returns>
    /// <remarks></remarks>
    void Book::SetName(const string& name)
    {
        this->name = name;
    }

    
    /// <summary>设置书的作者</summary>
    /// <param name="author">[IN]书的作者</param>
    /// <returns>void</returns>
    /// <remarks></remarks>
    void Book::SetAuthor(const string& author)
    {
        this->author = author;
    }

    
    /// <summary>设置书的摘要</summary>
    /// <param name="brief">[IN]书的摘要,简单的介绍信息</param>
    /// <remarks></remarks>
    void Book::SetBrief(const string& brief)
    {
        this->brief = brief;
    }

    
    /// <summary>设置书的图片, 比如封面</summary>
    /// <param name="image">[IN]图片的绝对地址,可以为本地路径,也可以为Web的URL</param>
    /// <remarks></remarks>
    void Book::SetImage(const string& image)
    {
        this->image = image;
    }

    
    /// <summary>添加书的绝对地址, KingBook通过这个地址通知操作系统打开这个路径.</summary>
    /// <param name="url">[IN]书的绝对地址,可以为本地路径,也可以为Web的URL</param>
    /// <remarks></remarks>
    void Book::AddURL(const string& url)
    {
        if(this->urls[0].size() == 0)
        {
            this->urls[0] = url;
        }
        else
        {
            this->urls.push_back(url);
        }
        
    }
    
    /// <summary>设置书的(首要)绝对地址, KingBook通过这个地址通知操作系统打开这个路径.</summary>
    /// <param name="url">[IN]书的绝对地址,可以为本地路径,也可以为Web的URL</param>
    /// <remarks></remarks>
    void Book::SetURL(const string& url)
    {
        this->urls[0] = url;
    }
    
    /// <summary>设置书的版本</summary>
    /// <param name="edition">[IN]字符串表示的书的版本</param>
    /// <remarks></remarks>
    void Book::SetEdition(const string& edition)
    {
        this->edition = edition;
    }

    
    /// <summary>设置书的详细信息</summary>
    /// <param name="description">[IN]书的详细描述,字数较多</param>
    /// <remarks>
    /// \todo 替换换行符.
    /// \scetion LR 关于换行符
    /// 在字符串里的换行符为"\n",在网页里显示时, 经过XSL转换后,将不能正确显示为换行
    /// 因此将字符串里的"\n",替换为<br/>
    /// 新问题出现,在CDATA里<br/>被原样输出
    /// <b>通过修改样式表解决了问题</b>
    /// </remarks>
    void Book::SetDescription(const string& desc)
    {
        //std::string br("<br/>");
        //std::string tmp(desc);
        //std::string ln("\n");
        //size_t pos = tmp.find(ln);
        //while(string::npos != pos)
        //{
        //    tmp.replace(pos, ln.size(), br);
        //    pos = tmp.find(ln, pos);
        //}
        this->descriprtion = desc;
    }

    
    /// <summary>设置出版者</summary>
    /// <param name="publisher">[IN]出版商名字</param>
    /// <remarks></remarks>
    void Book::SetPublisher(const string& publisher)
    {
        this->publisher = publisher;
    }
} // Library

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -