// ContentIdList.cxx #include "dataset_id/ContentIdList.h" #include #include #include "dataset_util/XmlElement.h" #include "dataset_util/DtdRegistry.h" using std::string; using std::pair; using std::insert_iterator; using std::includes; using std::ostream; using std::endl; //********************************************************************** // Local definitions. //********************************************************************** namespace { // Register the DTD. DtdRegistry::Status ISTAT = DtdRegistry::register_dtd("dataset"); } // end unnamed namespace //********************************************************************** // Static member functions. //********************************************************************** // DTD. const Text& ContentIdList::dtd() { static Text txt; if ( txt.size() == 0 ) { txt.append(""); } return txt; } //********************************************************************** // Member functions. //********************************************************************** // Default constructor. ContentIdList::ContentIdList() { } //********************************************************************** // XML constructor. ContentIdList:: ContentIdList(const XmlElement& ele, bool try_children) { const XmlElement* pele = 0; if ( ele.name() == xml_name() ) { pele = &ele; } else if ( try_children ) { const XmlElement::ElementList& children = ele.children(xml_name()); if ( children.size() == 1 ) { pele = children.front(); } } if ( pele != 0 ) { assert( pele->name() == xml_name() ); // The children are the content ID's. for ( XmlElement::ElementList::const_iterator iele=pele->children().begin(); iele!=pele->children().end(); ++iele ) { const XmlElement& xcid = **iele; assert( xcid.name() == ContentId::xml_name() ); if ( xcid.name() == ContentId::xml_name() ) { ContentId cid = ContentId(xcid); if ( ! cid.is_valid() ) { std::cerr << "ContentId from the following XML is invalid:" << std::endl; std::cerr << xcid << endl; cid = ContentId("UNKNOWN", ""); //assert( false ); } pair istat = insert(cid); if ( ! istat.second && !(cid== ContentId("UNKNOWN", "")) ) { std::cerr << "ContentId " << cid << " is a duplicate in: " << std::endl; std::cerr << *this << std::endl; std::cerr << "XML:" << std::endl; std::cerr << *pele << std::endl; //assert( false ); } } } //assert( pele->children().size() == size() ); } } //********************************************************************** // Keep only elements that are in the provided list. // The size of the new list is returned. ContentIdList::size_type ContentIdList::select(const ContentIdList& cids) { ContentIdList save; assert( save.size() == 0 ); insert_iterator isav(save, save.begin()); set_intersection(begin(), end(), cids.begin(), cids.end(), isav); *this = save; return size(); } //********************************************************************** // Contains an ID. bool ContentIdList::contains(ContentId cid) const { ContentIdList::const_iterator icid = find(cid); return icid != end(); } //********************************************************************** // Contains an list. bool ContentIdList::contains(const ContentIdList& cids) const { return includes(begin(), end(), cids.begin(), cids.end()); } //********************************************************************** // Write to xml. const XmlElement* ContentIdList::xml() const { XmlElement* pele = new XmlElement(xml_name()); for ( const_iterator icid=begin(); icid!=end(); ++icid ) { pele->add_child(icid->xml()); } assert( size() == pele->children().size() ); return pele; } //********************************************************************** // Output stream. ostream& ContentIdList::ostr(ostream& lhs, string indent) const { lhs << indent << "Content ID list has " << size() << " entr"; if ( size() == 1 ) { lhs << "y"; } else { lhs << "ies"; } if ( size() > 0 ) { lhs << ":"; } else { lhs << "."; } for ( ContentIdList::const_iterator icid=begin(); icid!=end(); ++icid ) { lhs << endl; lhs << indent << " " << *icid; } return lhs; } //********************************************************************** // Free functions. //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const ContentIdList& rhs) { return rhs.ostr(lhs, ""); } //**********************************************************************