text: //----------------------------------------------------------------------------
  //
  // TSDuck - The MPEG Transport Stream Toolkit
  // Copyright (c) 2005-2020, 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 "tsSSUEventNameDescriptor.h"
  #include "tsDescriptor.h"
  #include "tsTablesDisplay.h"
  #include "tsPSIRepository.h"
  #include "tsDuckContext.h"
  #include "tsxmlElement.h"
  TSDUCK_SOURCE;
  
  #define MY_XML_NAME u"SSU_event_name_descriptor"
  #define MY_CLASS ts::SSUEventNameDescriptor
  #define MY_DID ts::DID_UNT_SSU_EVENT_NAME
  #define MY_TID ts::TID_UNT
  #define MY_STD ts::STD_DVB
  
  TS_REGISTER_DESCRIPTOR(MY_CLASS, ts::EDID::TableSpecific(MY_DID, MY_TID), MY_XML_NAME, MY_CLASS::DisplayDescriptor);
  
  
  //----------------------------------------------------------------------------
  // Constructors
  //----------------------------------------------------------------------------
  
  ts::SSUEventNameDescriptor::SSUEventNameDescriptor() :
      AbstractDescriptor(MY_DID, MY_XML_NAME, MY_STD, 0),
      ISO_639_language_code(),
      name(),
      text()
  {
      _is_valid = true;
  }
  
  ts::SSUEventNameDescriptor::SSUEventNameDescriptor(DuckContext& duck, const Descriptor& desc) :
      SSUEventNameDescriptor()
  {
      deserialize(duck, desc);
  }
  
  
  //----------------------------------------------------------------------------
  // Serialization
  //----------------------------------------------------------------------------
  
  void ts::SSUEventNameDescriptor::serialize(DuckContext& duck, Descriptor& desc) const
  {
      ByteBlockPtr bbp(serializeStart());
      if (!SerializeLanguageCode(*bbp, ISO_639_language_code)) {
          desc.invalidate();
          return;
      }
      bbp->append(duck.encodedWithByteLength(name));
      bbp->append(duck.encodedWithByteLength(text));
      serializeEnd(desc, bbp);
  }
  
  
  //----------------------------------------------------------------------------
  // Deserialization
  //----------------------------------------------------------------------------
  
  void ts::SSUEventNameDescriptor::deserialize(DuckContext& duck, const Descriptor& desc)
  {
      const uint8_t* data = desc.payload();
      size_t size = desc.payloadSize();
  
      _is_valid = desc.isValid() && desc.tag() == _tag && size >= 5;
  
      if (_is_valid) {
          ISO_639_language_code = DeserializeLanguageCode(data);
          data += 3; size -= 3;
          duck.decodeWithByteLength(name, data, size);
          duck.decodeWithByteLength(text, data, size);
      }
      else {
          ISO_639_language_code.clear();
          name.clear();
          text.clear();
      }
  }
  
  
  //----------------------------------------------------------------------------
  // Static method to display a descriptor.
  //----------------------------------------------------------------------------
  
  void ts::SSUEventNameDescriptor::DisplayDescriptor(TablesDisplay& display, DID did, const uint8_t* data, size_t size, int indent, TID tid, PDS pds)
  {
      DuckContext& duck(display.duck());
      std::ostream& strm(duck.out());
      const std::string margin(indent, ' ');
  
      if (size >= 4) {
          const UString lang(DeserializeLanguageCode(data));
          data += 3; size -= 3;
          const UString name(duck.decodedWithByteLength(data, size));
          const UString text(duck.decodedWithByteLength(data, size));
          strm << margin << "Language: " << lang << std::endl
               << margin << "Event name: \"" << name << "\"" << std::endl
               << margin << "Event text: \"" << text << "\"" << std::endl;
      }
      display.displayExtraData(data, size, indent);
  }
  
  
  //----------------------------------------------------------------------------
  // XML serialization
  //----------------------------------------------------------------------------
  
  void ts::SSUEventNameDescriptor::buildXML(DuckContext& duck, xml::Element* root) const
  {
      root->setAttribute(u"ISO_639_language_code", ISO_639_language_code);
      root->addElement(u"name")->addText(name);
      root->addElement(u"text")->addText(text);
  }
  
  
  //----------------------------------------------------------------------------
  // XML deserialization
  //----------------------------------------------------------------------------
  
  void ts::SSUEventNameDescriptor::fromXML(DuckContext& duck, const xml::Element* element)
  {
      _is_valid =
          checkXMLName(element) &&
          element->getAttribute(ISO_639_language_code, u"ISO_639_language_code", true, u"", 3, 3) &&
          element->getTextChild(name, u"name") &&
          element->getTextChild(text, u"text");
  }
  