text: //----------------------------------------------------------------------------
  //
  // TSDuck - The MPEG Transport Stream Toolkit
  // Copyright (c) 2005-2021, Thierry Lelegard
  // All rights reserved.
  //
  // Redistribution and use in source and binary forms, with or without
  // modification, are permitted provided that the following conditions are met:
  //
  // 1. Redistributions of source code must retain the above copyright notice,
  //    this list of conditions and the following disclaimer.
  // 2. 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.
  //
  // 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 "tsCAIdentifierDescriptor.h"
  #include "tsDescriptor.h"
  #include "tsNames.h"
  #include "tsTablesDisplay.h"
  #include "tsPSIRepository.h"
  #include "tsPSIBuffer.h"
  #include "tsDuckContext.h"
  #include "tsxmlElement.h"
  
  #define MY_XML_NAME u"CA_identifier_descriptor"
  #define MY_CLASS ts::CAIdentifierDescriptor
  #define MY_DID ts::DID_CA_ID
  #define MY_STD ts::Standards::DVB
  
  TS_REGISTER_DESCRIPTOR(MY_CLASS, ts::EDID::Standard(MY_DID), MY_XML_NAME, MY_CLASS::DisplayDescriptor);
  
  
  //----------------------------------------------------------------------------
  // Constructors
  //----------------------------------------------------------------------------
  
  ts::CAIdentifierDescriptor::CAIdentifierDescriptor() :
      AbstractDescriptor(MY_DID, MY_XML_NAME, MY_STD, 0),
      casids()
  {
  }
  
  void ts::CAIdentifierDescriptor::clearContent()
  {
      casids.clear();
  }
  
  ts::CAIdentifierDescriptor::CAIdentifierDescriptor(DuckContext& duck, const Descriptor& desc) :
      CAIdentifierDescriptor()
  {
      deserialize(duck, desc);
  }
  
  ts::CAIdentifierDescriptor::CAIdentifierDescriptor(std::initializer_list<uint16_t> ids) :
      AbstractDescriptor(MY_DID, MY_XML_NAME, MY_STD, 0),
      casids(ids)
  {
  }
  
  
  //----------------------------------------------------------------------------
  // Exactly identical descriptors shall not be dumplicated.
  //----------------------------------------------------------------------------
  
  ts::DescriptorDuplication ts::CAIdentifierDescriptor::duplicationMode() const
  {
      return DescriptorDuplication::ADD_OTHER;
  }
  
  
  //----------------------------------------------------------------------------
  // Serialization
  //----------------------------------------------------------------------------
  
  void ts::CAIdentifierDescriptor::serializePayload(PSIBuffer& buf) const
  {
      for (size_t n = 0; n < casids.size(); ++n) {
          buf.putUInt16(casids[n]);
      }
  }
  
  
  //----------------------------------------------------------------------------
  // Deserialization
  //----------------------------------------------------------------------------
  
  void ts::CAIdentifierDescriptor::deserializePayload(PSIBuffer& buf)
  {
      while (buf.canRead()) {
          casids.push_back(buf.getUInt16());
      }
  }
  
  
  //----------------------------------------------------------------------------
  // Static method to display a descriptor.
  //----------------------------------------------------------------------------
  
  void ts::CAIdentifierDescriptor::DisplayDescriptor(TablesDisplay& disp, PSIBuffer& buf, const UString& margin, DID did, TID tid, PDS pds)
  {
      while (buf.canReadBytes(2)) {
          disp << margin << "CA System Id: " << names::CASId(disp.duck(), buf.getUInt16(), NamesFlags::FIRST) << std::endl;
      }
  }
  
  
  //----------------------------------------------------------------------------
  // XML serialization
  //----------------------------------------------------------------------------
  
  void ts::CAIdentifierDescriptor::buildXML(DuckContext& duck, xml::Element* root) const
  {
      for (size_t i = 0; i < casids.size(); ++i) {
          root->addElement(u"CA_system_id")->setIntAttribute(u"value", casids[i], true);
      }
  }
  
  
  //----------------------------------------------------------------------------
  // XML deserialization
  //----------------------------------------------------------------------------
  
  bool ts::CAIdentifierDescriptor::analyzeXML(DuckContext& duck, const xml::Element* element)
  {
      xml::ElementVector children;
      bool ok = element->getChildren(children, u"CA_system_id", 0, (MAX_DESCRIPTOR_SIZE - 2) / 2);
      for (size_t i = 0; ok && i < children.size(); ++i) {
          uint16_t id = 0;
          ok = children[i]->getIntAttribute(id, u"value", true, 0, 0x0000, 0xFFFF);
          casids.push_back(id);
      }
      return ok;
  }
  