123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- /* ----------------------------------------------------------------- */
- /* The HMM-Based Singing Voice Synthesis System "Sinsy" */
- /* developed by Sinsy Working Group */
- /* http://sinsy.sourceforge.net/ */
- /* ----------------------------------------------------------------- */
- /* */
- /* Copyright (c) 2009-2015 Nagoya Institute of Technology */
- /* Department of Computer Science */
- /* */
- /* All rights reserved. */
- /* */
- /* Redistribution and use in source and binary forms, with or */
- /* without modification, are permitted provided that the following */
- /* conditions are met: */
- /* */
- /* - Redistributions of source code must retain the above copyright */
- /* notice, this list of conditions and the following disclaimer. */
- /* - Redistributions in binary form must reproduce the above */
- /* copyright notice, this list of conditions and the following */
- /* disclaimer in the documentation and/or other materials provided */
- /* with the distribution. */
- /* - Neither the name of the Sinsy working group nor the names of */
- /* its contributors may be used to endorse or promote products */
- /* derived from this software without specific prior written */
- /* permission. */
- /* */
- /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
- /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
- /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
- /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
- /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
- /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
- /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
- /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
- /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
- /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
- /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
- /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
- /* POSSIBILITY OF SUCH DAMAGE. */
- /* ----------------------------------------------------------------- */
- #include <algorithm>
- #include "XmlData.h"
- #include "Deleter.h"
- #include "util_log.h"
- #include "util_string.h"
- namespace sinsy
- {
- namespace
- {
- class StringOutputter
- {
- public:
- //! constructor
- StringOutputter(std::ostream& s) : stream(s) {}
- //! copy constructor
- StringOutputter(const StringOutputter& obj) : stream(obj.stream) {}
- //! destructor
- virtual ~StringOutputter() {}
- //! ...
- void operator()(const IStringWritable* obj) {
- stream << *obj;
- }
- private:
- //! assignment operator (donot use)
- StringOutputter& operator=(const StringOutputter&);
- //! stream
- std::ostream& stream;
- };
- class AttributeOutputter
- {
- public:
- //! constructor
- AttributeOutputter(std::ostream& s) : stream(s) {}
- //! copy constructor
- AttributeOutputter(const AttributeOutputter& obj) : stream(obj.stream) {}
- //! destructor
- virtual ~AttributeOutputter() {}
- //! ...
- void operator()(const std::pair<std::string, std::string>& obj) {
- stream << " " << obj.first << "=\"" << obj.second << "\"";
- }
- private:
- //! assignment operator (donot use)
- AttributeOutputter& operator=(const AttributeOutputter&);
- //! stream
- std::ostream& stream;
- };
- }; // namespace
- /*!
- constructor
- @param t tag
- @param d data
- */
- XmlData::XmlData(const std::string& t, const std::string& d) : tag(t), data(d)
- {
- toLower(tag);
- }
- /*!
- copy constructor
- */
- XmlData::XmlData(const XmlData& obj) : tag(obj.tag), data(obj.data), attributes(obj.attributes)
- {
- try {
- // copy children
- children.reserve(obj.children.size());
- const Children::const_iterator itrEnd(obj.childEnd());
- for (Children::const_iterator itr(obj.childBegin()); itrEnd != itr; ++itr) {
- this->addChild(new XmlData(**itr));
- }
- } catch (const std::exception&) {
- // delete children
- std::for_each(children.begin(), children.end(), Deleter<XmlData>());
- throw;
- }
- }
- /*!
- destructor
- */
- XmlData::~XmlData()
- {
- std::for_each(children.begin(), children.end(), Deleter<XmlData>());
- }
- /*!
- get tag
- */
- const std::string& XmlData::getTag() const
- {
- return tag;
- }
- /*!
- get data
- */
- const std::string& XmlData::getData() const
- {
- return data;
- }
- /*!
- set data
- */
- void XmlData::setData(const std::string& str)
- {
- data = str;
- }
- /*!
- add child
- */
- XmlData::Children::iterator XmlData::addChild(XmlData* child)
- {
- if (NULL == child) {
- std::runtime_error("XmlData::addChild() NULL pointer");
- }
- children.push_back(child);
- return children.end() - 1;
- }
- //! erase child
- XmlData::Children::iterator XmlData::eraseChild(const Children::iterator& itr)
- {
- delete *itr;
- return children.erase(itr);
- }
- /*!
- add attribute
- */
- bool XmlData::addAttribute(const std::string& k, const std::string& v)
- {
- std::string key(k);
- std::string value(v);
- toLower(key);
- toLower(value);
- if (!attributes.insert(std::make_pair(key, value)).second) {
- ERR_MSG("Failed to insert attribute (" << key << ", " << value << ")");
- return false;
- }
- return true;
- }
- /*!
- get attribute
- */
- const std::string& XmlData::getAttribute(const std::string& k) const
- {
- Attributes::const_iterator itr = attributes.find(k);
- if (attributes.end() == itr) {
- return NULL_STR;
- }
- return itr->second;
- }
- /*!
- to string stream
- */
- std::ostream& XmlData::toStringStream(std::ostream& stream) const
- {
- stream << "<" << tag;
- std::for_each(attributes.begin(), attributes.end(), AttributeOutputter(stream));
- if (data.empty() && children.empty()) {
- stream << " />\n";
- return stream;
- }
- stream << ">" << data;
- if (!children.empty()) {
- stream << "\n";
- std::for_each(children.begin(), children.end(), StringOutputter(stream));
- }
- stream << "</" << tag << ">\n";
- return stream;
- }
- /*!
- get begin const iterator of children
- */
- XmlData::Children::const_iterator XmlData::childBegin() const
- {
- return children.begin();
- }
- /*!
- get end const iterator of children
- */
- XmlData::Children::const_iterator XmlData::childEnd() const
- {
- return children.end();
- }
- /*!
- get begin iterator of children
- */
- XmlData::Children::iterator XmlData::childBegin()
- {
- return children.begin();
- }
- /*!
- get end iterator of children
- */
- XmlData::Children::iterator XmlData::childEnd()
- {
- return children.end();
- }
- }; // namespace sinsy
|