version bump
[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 const char* c_tt_namespace_name = "http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt";
42
43 //------------------------------------------------------------------------------------------
44
45 typedef std::map<Kumu::UUID, ASDCP::TimedText::MIMEType_t> ResourceTypeMap_t;
46
47 class AS_02::TimedText::ST2052_TextParser::h__TextParser
48 {
49   XMLElement  m_Root;
50   ResourceTypeMap_t m_ResourceTypes;
51   Result_t OpenRead();
52
53   ASDCP_NO_COPY_CONSTRUCT(h__TextParser);
54
55 public:
56   std::string m_Filename;
57   std::string m_XMLDoc;
58   TimedTextDescriptor  m_TDesc;
59   ASDCP::mem_ptr<ASDCP::TimedText::LocalFilenameResolver> m_DefaultResolver;
60
61   h__TextParser() : m_Root("**ParserRoot**")
62   {
63     memset(&m_TDesc.AssetID, 0, UUIDlen);
64   }
65
66   ~h__TextParser() {}
67
68   ASDCP::TimedText::IResourceResolver* GetDefaultResolver()
69   {
70     if ( m_DefaultResolver.empty() )
71       {
72         ASDCP::TimedText::LocalFilenameResolver *resolver = new ASDCP::TimedText::LocalFilenameResolver;
73         resolver->OpenRead(PathDirname(m_Filename));
74         m_DefaultResolver = resolver;
75       }
76     
77     return m_DefaultResolver;
78   }
79
80   Result_t OpenRead(const std::string& filename);
81   Result_t OpenRead(const std::string& xml_doc, const std::string& filename);
82   Result_t ReadAncillaryResource(const UUID& uuid, ASDCP::TimedText::FrameBuffer& FrameBuf,
83                                  const ASDCP::TimedText::IResourceResolver& Resolver) const;
84 };
85
86 //
87 Result_t
88 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead(const std::string& filename)
89 {
90   Result_t result = ReadFileIntoString(filename, m_XMLDoc);
91
92   if ( KM_SUCCESS(result) )
93     {
94       m_Filename = filename;
95       result = OpenRead();
96     }
97
98   return result;
99 }
100
101 //
102 Result_t
103 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead(const std::string& xml_doc, const std::string& filename)
104 {
105   m_XMLDoc = xml_doc;
106   m_Filename = filename;
107   return OpenRead();
108 }
109
110 //
111 Result_t
112 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead()
113 {
114   if ( ! m_Root.ParseString(m_XMLDoc.c_str()) )
115     return RESULT_FORMAT;
116
117   m_TDesc.EncodingName = "UTF-8"; // the XML parser demands UTF-8
118   m_TDesc.ResourceList.clear();
119   m_TDesc.ContainerDuration = 0;
120   const XMLNamespace* ns = m_Root.Namespace();
121
122   if ( ns == 0 )
123     {
124       DefaultLogSink(). Warn("Document has no namespace name, assuming %s\n", c_tt_namespace_name);
125       m_TDesc.NamespaceName = c_tt_namespace_name;
126     }
127   else
128     {
129       m_TDesc.NamespaceName = ns->Name();
130     }
131
132   return RESULT_OK;
133 }
134
135
136 //------------------------------------------------------------------------------------------
137
138 AS_02::TimedText::ST2052_TextParser::ST2052_TextParser()
139 {
140 }
141
142 AS_02::TimedText::ST2052_TextParser::~ST2052_TextParser()
143 {
144 }
145
146 // Opens the stream for reading, parses enough data to provide a complete
147 // set of stream metadata for the MXFWriter below.
148 ASDCP::Result_t
149 AS_02::TimedText::ST2052_TextParser::OpenRead(const std::string& filename) const
150 {
151   const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = new h__TextParser;
152
153   Result_t result = m_Parser->OpenRead(filename);
154
155   if ( ASDCP_FAILURE(result) )
156     const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = 0;
157
158   return result;
159 }
160
161 // Parses an XML document to provide a complete set of stream metadata for the MXFWriter below.
162 Result_t
163 AS_02::TimedText::ST2052_TextParser::OpenRead(const std::string& xml_doc, const std::string& filename) const
164 {
165   const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = new h__TextParser;
166
167   Result_t result = m_Parser->OpenRead(xml_doc, filename);
168
169   if ( ASDCP_FAILURE(result) )
170     const_cast<AS_02::TimedText::ST2052_TextParser*>(this)->m_Parser = 0;
171
172   return result;
173 }
174
175 //
176 ASDCP::Result_t
177 AS_02::TimedText::ST2052_TextParser::FillTimedTextDescriptor(TimedTextDescriptor& TDesc) const
178 {
179   if ( m_Parser.empty() )
180     return RESULT_INIT;
181
182   TDesc = m_Parser->m_TDesc;
183   return RESULT_OK;
184 }
185
186 // Reads the complete Timed Text Resource into the given string.
187 ASDCP::Result_t
188 AS_02::TimedText::ST2052_TextParser::ReadTimedTextResource(std::string& s) const
189 {
190   if ( m_Parser.empty() )
191     return RESULT_INIT;
192
193   s = m_Parser->m_XMLDoc;
194   return RESULT_OK;
195 }
196
197 //
198 ASDCP::Result_t
199 AS_02::TimedText::ST2052_TextParser::ReadAncillaryResource(const Kumu::UUID& uuid, ASDCP::TimedText::FrameBuffer& FrameBuf,
200                                                            const ASDCP::TimedText::IResourceResolver* Resolver) const
201 {
202   return RESULT_NOTIMPL;
203 }
204
205
206 //
207 // end ST2052_TextParser.cpp
208 //