imf bugs
[asdcplib.git] / src / ST2052_TextParser.cpp
1 /*
2 Copyright (c) 2013-2014, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    ST2052_TimedText.cpp
28     \version $Id$       
29     \brief   AS-DCP library, PCM essence reader and writer implementation
30 */
31
32
33 #include "AS_02_internal.h"
34 #include "KM_xml.h"
35
36 using namespace Kumu;
37 using namespace ASDCP;
38
39 using Kumu::DefaultLogSink;
40
41 // TODO: 
42 const char* c_dcst_namespace_name = "http://www.smpte-ra.org/schemas/428-7/2007/DCST";
43
44 //------------------------------------------------------------------------------------------
45
46 typedef std::map<Kumu::UUID, ASDCP::TimedText::MIMEType_t> ResourceTypeMap_t;
47
48 class AS_02::TimedText::ST2052_TextParser::h__TextParser
49 {
50   XMLElement  m_Root;
51   ResourceTypeMap_t m_ResourceTypes;
52   Result_t OpenRead();
53
54   ASDCP_NO_COPY_CONSTRUCT(h__TextParser);
55
56 public:
57   std::string m_Filename;
58   std::string m_XMLDoc;
59   TimedTextDescriptor  m_TDesc;
60   ASDCP::mem_ptr<ASDCP::TimedText::LocalFilenameResolver> m_DefaultResolver;
61
62   h__TextParser() : m_Root("**ParserRoot**")
63   {
64     memset(&m_TDesc.AssetID, 0, UUIDlen);
65   }
66
67   ~h__TextParser() {}
68
69   ASDCP::TimedText::IResourceResolver* GetDefaultResolver()
70   {
71     if ( m_DefaultResolver.empty() )
72       {
73         ASDCP::TimedText::LocalFilenameResolver *resolver = new ASDCP::TimedText::LocalFilenameResolver;
74         resolver->OpenRead(PathDirname(m_Filename));
75         m_DefaultResolver = resolver;
76       }
77     
78     return m_DefaultResolver;
79   }
80
81   Result_t OpenRead(const std::string& filename);
82   Result_t OpenRead(const std::string& xml_doc, const std::string& filename);
83   Result_t ReadAncillaryResource(const UUID& uuid, ASDCP::TimedText::FrameBuffer& FrameBuf,
84                                  const ASDCP::TimedText::IResourceResolver& Resolver) const;
85 };
86
87 //
88 Result_t
89 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead(const std::string& filename)
90 {
91   Result_t result = ReadFileIntoString(filename, m_XMLDoc);
92
93   if ( KM_SUCCESS(result) )
94     {
95       m_Filename = filename;
96       result = OpenRead();
97     }
98
99   return result;
100 }
101
102 //
103 Result_t
104 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead(const std::string& xml_doc, const std::string& filename)
105 {
106   m_XMLDoc = xml_doc;
107   m_Filename = filename;
108   return OpenRead();
109 }
110
111 //
112 Result_t
113 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead()
114 {
115   if ( ! m_Root.ParseString(m_XMLDoc.c_str()) )
116     return RESULT_FORMAT;
117
118   m_TDesc.EncodingName = "UTF-8"; // the XML parser demands UTF-8
119   m_TDesc.ResourceList.clear();
120   m_TDesc.ContainerDuration = 0;
121   const XMLNamespace* ns = m_Root.Namespace();
122
123   if ( ns == 0 )
124     {
125       DefaultLogSink(). Warn("Document has no namespace name, assuming %s\n", c_dcst_namespace_name);
126       m_TDesc.NamespaceName = c_dcst_namespace_name;
127     }
128   else
129     {
130       m_TDesc.NamespaceName = ns->Name();
131     }
132
133   return RESULT_OK;
134 }
135
136
137 //------------------------------------------------------------------------------------------
138
139 AS_02::TimedText::ST2052_TextParser::ST2052_TextParser()
140 {
141 }
142
143 AS_02::TimedText::ST2052_TextParser::~ST2052_TextParser()
144 {
145 }
146
147 // Opens the stream for reading, parses enough data to provide a complete
148 // set of stream metadata for the MXFWriter below.
149 ASDCP::Result_t
150 AS_02::TimedText::ST2052_TextParser::OpenRead(const std::string& filename) const
151 {
152   const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = new h__TextParser;
153
154   Result_t result = m_Parser->OpenRead(filename);
155
156   if ( ASDCP_FAILURE(result) )
157     const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = 0;
158
159   return result;
160 }
161
162 // Parses an XML document to provide a complete set of stream metadata for the MXFWriter below.
163 Result_t
164 AS_02::TimedText::ST2052_TextParser::OpenRead(const std::string& xml_doc, const std::string& filename) const
165 {
166   const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = new h__TextParser;
167
168   Result_t result = m_Parser->OpenRead(xml_doc, filename);
169
170   if ( ASDCP_FAILURE(result) )
171     const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = 0;
172
173   return result;
174 }
175
176 //
177 ASDCP::Result_t
178 AS_02::TimedText::ST2052_TextParser::FillTimedTextDescriptor(TimedTextDescriptor& TDesc) const
179 {
180   if ( m_Parser.empty() )
181     return RESULT_INIT;
182
183   TDesc = m_Parser->m_TDesc;
184   return RESULT_OK;
185 }
186
187 // Reads the complete Timed Text Resource into the given string.
188 ASDCP::Result_t
189 AS_02::TimedText::ST2052_TextParser::ReadTimedTextResource(std::string& s) const
190 {
191   if ( m_Parser.empty() )
192     return RESULT_INIT;
193
194   s = m_Parser->m_XMLDoc;
195   return RESULT_OK;
196 }
197
198 //
199 ASDCP::Result_t
200 AS_02::TimedText::ST2052_TextParser::ReadAncillaryResource(const Kumu::UUID& uuid, ASDCP::TimedText::FrameBuffer& FrameBuf,
201                                                            const ASDCP::TimedText::IResourceResolver* Resolver) const
202 {
203   return RESULT_NOTIMPL;
204 }
205
206
207 //
208 // end ST2052_TextParser.cpp
209 //