Exemplo n.º 1
0
        public static string PrepareAuthenticatedUri(string uri)
        {
            string uristring = null;
            string timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
            string nonce     = GenerateNonce();
            string query     = String.Format("authid={0}&time={1}&nonce={2}", AUTH_ID, timestamp, nonce);

            if (uri.Contains("?"))
            {
                uristring = String.Format("{0}&{1}", uri, query);
            }
            else
            {
                uristring = String.Format("{0}?{1}", uri, query);
            }

            string hash         = GenerateHash(uristring);
            string generatedUri = String.Format("{0}&sign={1}", uristring, FileDataProvider.UriEncode(hash));

            return(generatedUri);
        }
        private void generateImageDescriptionInDTBook(TreeNode n, XmlNode currentXmlNode, string exportImageName, XmlDocument DTBookDocument)
        {
            AlternateContentProperty altProp = n.GetAlternateContentProperty();

            if (currentXmlNode.LocalName == null ||
                !currentXmlNode.LocalName.Equals("img", StringComparison.OrdinalIgnoreCase) ||
                altProp == null || altProp.IsEmpty)
            {
                return;
            }

            m_Map_AltProperty_TO_Description.Add(altProp, new Description());

            PCMFormatInfo     audioFormat = m_Presentation.MediaDataManager.DefaultPCMFormat;
            AudioLibPCMFormat pcmFormat   = audioFormat.Data;

            pcmFormat.SampleRate       = (ushort)m_sampleRate;
            pcmFormat.NumberOfChannels = (ushort)(m_audioStereo ? 2 : 1);

            Dictionary <string, List <string> > map_DiagramElementName_TO_TextualDescriptions = new Dictionary <string, List <string> >();

            string imageDescriptionDirectoryPath = GetAndCreateImageDescriptionDirectoryPath(true, exportImageName, m_OutputDirectory);
            string descriptionFile = CreateImageDescription(m_SkipACM, pcmFormat, m_encodeAudioFiles, m_BitRate_Encoding,
                                                            imageDescriptionDirectoryPath, exportImageName,
                                                            altProp,
                                                            map_DiagramElementName_TO_TextualDescriptions,
                                                            m_Map_AltProperty_TO_Description,
                                                            m_Map_AltContentAudio_TO_RelativeExportedFilePath);

            if (m_includeImageDescriptions && !String.IsNullOrEmpty(descriptionFile))
            {
                //short term way for executing image description code: will be updated in later phase of implementation
                XmlNode prodNoteNode = DTBookDocument.CreateElement("prodnote", currentXmlNode.NamespaceURI);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, prodNoteNode, "render", "optional");
                string id_Prodnote = GetNextID(ID_DTBPrefix);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, prodNoteNode, "id", id_Prodnote);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, prodNoteNode, "imgref", currentXmlNode.Attributes.GetNamedItem("id").Value);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, prodNoteNode, "class", DiagramContentModelHelper.EPUB_DescribedAt);

                currentXmlNode.ParentNode.AppendChild(prodNoteNode);
                if (!m_Image_ProdNoteMap.ContainsKey(n))
                {
                    m_Image_ProdNoteMap.Add(n, new List <XmlNode>());
                }
                m_Image_ProdNoteMap[n].Add(prodNoteNode);
                XmlNode anchorNode = DTBookDocument.CreateElement("a", currentXmlNode.NamespaceURI);

                XmlNode pAnchor = DTBookDocument.CreateElement(
                    DiagramContentModelHelper.P, currentXmlNode.NamespaceURI
                    );
                pAnchor.AppendChild(anchorNode);
                prodNoteNode.AppendChild(pAnchor);
                string descriptionFileUrl = descriptionFile.Replace('\\', '/');

                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, anchorNode, "href", FileDataProvider.UriEncode(descriptionFileUrl));
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, anchorNode, "external", "true");
                anchorNode.AppendChild(DTBookDocument.CreateTextNode("Image description (DIAGRAM XML)"));

                if (map_DiagramElementName_TO_TextualDescriptions.Count > 0)
                {
                    foreach (string diagramDescriptionElementName in map_DiagramElementName_TO_TextualDescriptions.Keys)
                    {
                        //System.Windows.Forms.MessageBox.Show(s + " : " + imageDescriptions[s]);

                        XmlNode prodNoteDesc = DTBookDocument.CreateElement("prodnote", currentXmlNode.NamespaceURI);
                        XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, prodNoteDesc, "render", "optional");
                        string id_ProdnoteDesc = GetNextID(ID_DTBPrefix);
                        XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, prodNoteDesc, "id", id_ProdnoteDesc);
                        XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, prodNoteDesc, "imgref", currentXmlNode.Attributes.GetNamedItem("id").Value);
                        XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, prodNoteDesc, "class", diagramDescriptionElementName);
                        currentXmlNode.ParentNode.AppendChild(prodNoteDesc);
                        m_Image_ProdNoteMap[n].Add(prodNoteDesc);

                        foreach (string txt in map_DiagramElementName_TO_TextualDescriptions[diagramDescriptionElementName])
                        {
                            string descText = txt;

                            bool xmlParseFail = descText.StartsWith(DIAGRAM_XML_PARSE_FAIL);

                            bool descriptionTextContainsMarkup = !xmlParseFail && descText.IndexOf('<') >= 0; // descText.Contains("<");
                            if (descriptionTextContainsMarkup)
                            {
                                try
                                {
                                    prodNoteDesc.InnerXml = descText;
                                }
                                catch (Exception ex)
                                {
#if DEBUG
                                    Debugger.Break();
#endif
                                    Console.WriteLine(@"Cannot set DIAGRAM XML: " + descText);

                                    XmlNode wrapperNode = DTBookDocument.CreateElement(DiagramContentModelHelper.CODE,
                                                                                       currentXmlNode.NamespaceURI);
                                    prodNoteDesc.AppendChild(wrapperNode);
                                    wrapperNode.AppendChild(DTBookDocument.CreateTextNode(descText));
                                }
                            }
                            else if (xmlParseFail)
                            {
                                //descText = descText.Replace(DIAGRAM_XML_PARSE_FAIL, "");
                                descText = descText.Substring(DIAGRAM_XML_PARSE_FAIL.Length);

                                XmlNode wrapperNode = DTBookDocument.CreateElement(DiagramContentModelHelper.CODE, currentXmlNode.NamespaceURI);
                                prodNoteDesc.AppendChild(wrapperNode);
                                wrapperNode.AppendChild(DTBookDocument.CreateTextNode(descText));
                            }
                            else
                            {
                                string normalizedText = descText.Replace("\r\n", "\n");

                                string[] parasText = normalizedText.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                                //string[] parasText = System.Text.RegularExpressions.Regex.Split(normalizedText, "\n");

                                for (int i = 0; i < parasText.Length; i++)
                                {
                                    string paraText = parasText[i].Trim();
                                    if (string.IsNullOrEmpty(paraText))
                                    {
                                        continue;
                                    }

                                    XmlNode paragraph = DTBookDocument.CreateElement(
                                        //DiagramContentModelHelper.NS_PREFIX_ZAI,
                                        DiagramContentModelHelper.P
                                        , currentXmlNode.NamespaceURI
                                        //, DiagramContentModelHelper.NS_URL_ZAI
                                        );

                                    paragraph.InnerText = paraText;

                                    prodNoteDesc.AppendChild(paragraph);
                                }
                            }
                        }
                    }
                }

                /*
                 * if ( EXPORT_IMAGE_DESCRIPTION_IN_DTBOOK )
                 * {//1
                 * // to do copy the diagram nodes that descend directly from body
                 *  if (m_AltProperrty_DiagramDocument.ContainsKey(n.GetAlternateContentProperty()))
                 *  {//2
                 *
                 *      XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, DTBookDocument.GetElementsByTagName("dtbook")[0],
                 * XmlReaderWriterHelper.NS_PREFIX_XMLNS+":" + DiagramContentModelHelper.NS_PREFIX_DIAGRAM,
                 * DiagramContentModelHelper.NS_URL_DIAGRAM);
                 *      XmlDocument descriptionDocument = m_AltProperrty_DiagramDocument[n.GetAlternateContentProperty()];
                 *      XmlNodeList diagramNodesList = descriptionDocument.GetElementsByTagName("d:body")[0].ChildNodes;
                 *      foreach (XmlNode xn in diagramNodesList)
                 *      {//3
                 *          XmlNode newNode = DTBookDocument.ImportNode(xn, true);
                 *          prodNoteNode.AppendChild(newNode);
                 *          for (int i = 0; i < newNode.Attributes.Count; i++)
                 *          {//4
                 *              XmlAttribute attr = newNode.Attributes[i];
                 *              if (attr.Name == DiagramContentModelHelper.NS_PREFIX_XML + ":id")
                 *              {//-4
                 *                  XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, newNode, "id", attr.Value);
                 *                  newNode.Attributes.Remove(attr);
                 *              }//-3
                 *          }//-2
                 *
                 *      }//-1
                 *
                 *  }
                 *
                 * //XmlNode newNode = DTBookDocument.ImportNode(M_DescriptionDocument.GetElementsByTagName("d:description")[0], true);
                 *  //prodNoteNode.AppendChild(newNode);
                 * }
                 */
            }
            //}
            //catch (System.Exception ex)
            //{
            //System.Windows.Forms.MessageBox.Show(ex.ToString());
            //}
            //}

            //}
        }
        protected XmlNode AddFilenameToManifest(XmlDocument opfDocument, XmlNode manifestNode, string filename, string strID, string mediaType)
        {
            XmlNode itemNode = opfDocument.CreateElement(null, "item", manifestNode.NamespaceURI);

            manifestNode.AppendChild(itemNode);
            XmlDocumentHelper.CreateAppendXmlAttribute(opfDocument, itemNode, "href", FileDataProvider.UriEncode(filename));
            XmlDocumentHelper.CreateAppendXmlAttribute(opfDocument, itemNode, "id", strID);
            XmlDocumentHelper.CreateAppendXmlAttribute(opfDocument, itemNode, "media-type", mediaType);

            return(itemNode);
        }
Exemplo n.º 4
0
        private XmlNode CreateNavPointWithoutContentNode(XmlDocument ncxDocument, TreeNode urakawaNode, TreeNode currentHeadingTreeNode, TreeNode n, Dictionary <TreeNode, XmlNode> treeNode_NavNodeMap)
        {
            XmlNode navMapNode = ncxDocument.GetElementsByTagName("navMap")[0];

            ExternalAudioMedia externalAudio = GetExternalAudioMedia(n);

            // first create navPoints
            XmlNode navPointNode = ncxDocument.CreateElement(null, "navPoint", navMapNode.NamespaceURI);

            if (currentHeadingTreeNode != null)
            {
                XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, navPointNode, "class", currentHeadingTreeNode.GetXmlProperty().LocalName);
            }
            XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, navPointNode, "id", GetNextID(ID_NcxPrefix));
            XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, navPointNode, "playOrder", "");

            TreeNode parentNode = GetParentLevelNode(urakawaNode);

            if (parentNode == null)
            {
                navMapNode.AppendChild(navPointNode);
            }
            else
            {
                XmlNode obj;
                treeNode_NavNodeMap.TryGetValue(parentNode, out obj);

                if (obj != null)                   //treeNode_NavNodeMap.ContainsKey(parentNode))
                {
                    obj.AppendChild(navPointNode); //treeNode_NavNodeMap[parentNode]
                }
                else // surch up for node
                {
                    int counter = 0;
                    while (parentNode != null && counter <= 6)
                    {
                        parentNode = GetParentLevelNode(parentNode);

                        if (parentNode != null
                            //&& treeNode_NavNodeMap.ContainsKey(parentNode)
                            )
                        {
                            treeNode_NavNodeMap.TryGetValue(parentNode, out obj);
                            if (obj != null)
                            {
                                obj.AppendChild(navPointNode); //treeNode_NavNodeMap[parentNode]
                                break;
                            }
                        }
                        counter++;
                    }

                    if (parentNode == null || counter > 7)
                    {
                        navMapNode.AppendChild(navPointNode);
                    }
                }
            }


            treeNode_NavNodeMap.Add(urakawaNode, navPointNode);

            // create navLabel
            XmlNode navLabel = ncxDocument.CreateElement(null, "navLabel", navPointNode.NamespaceURI);

            navPointNode.AppendChild(navLabel);

            // create text node
            XmlNode txtNode = ncxDocument.CreateElement(null, "text", navMapNode.NamespaceURI);

            navLabel.AppendChild(txtNode);
            if (currentHeadingTreeNode != null)
            {
                txtNode.AppendChild(
                    ncxDocument.CreateTextNode(prepareNcxLabelText(n)));
            }

            if (externalAudio != null)
            {
                // create audio node
                XmlNode audioNode = ncxDocument.CreateElement(null, "audio", navMapNode.NamespaceURI);
                navLabel.AppendChild(audioNode);
                XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, audioNode, "clipBegin",
                                                           FormatTimeString(externalAudio.ClipBegin));
                XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, audioNode, "clipEnd",
                                                           FormatTimeString(externalAudio.ClipEnd));

                string extAudioSrc = AdjustAudioFileName(externalAudio, urakawaNode);

                XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, audioNode, "src",
                                                           FileDataProvider.UriEncode(Path.GetFileName(extAudioSrc)));
            }
            return(navPointNode);
        }
Exemplo n.º 5
0
        private XmlNode CreateDocTitle(XmlDocument ncxDocument, XmlNode ncxRootNode, TreeNode n, TreeNode levelNode)
        {
            XmlNode docNode = ncxDocument.CreateElement(null,
                                                        "docTitle",
                                                        ncxRootNode.NamespaceURI);

            XmlNode navMapNode = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(ncxDocument, true, "navMap", null);

            ncxRootNode.InsertBefore(docNode, navMapNode);

            XmlNode docTxtNode = ncxDocument.CreateElement(null, "text", docNode.NamespaceURI);

            docNode.AppendChild(docTxtNode);
            docTxtNode.AppendChild(
                ncxDocument.CreateTextNode(prepareNcxLabelText(n)));


            ExternalAudioMedia externalAudio = GetExternalAudioMedia(n);

            if (externalAudio != null)
            {
                // create audio node
                XmlNode docAudioNode = ncxDocument.CreateElement(null, "audio", docNode.NamespaceURI);
                docNode.AppendChild(docAudioNode);
                XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, docAudioNode, "clipBegin", FormatTimeString(externalAudio.ClipBegin));
                XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, docAudioNode, "clipEnd", FormatTimeString(externalAudio.ClipEnd));

                string extAudioSrc = AdjustAudioFileName(externalAudio, levelNode);

                XmlDocumentHelper.CreateAppendXmlAttribute(ncxDocument, docAudioNode, "src", FileDataProvider.UriEncode(Path.GetFileName(extAudioSrc)));
            }
            return(docNode);
        }
Exemplo n.º 6
0
        protected virtual void CreateDTBookDocument()
        {
            // check if there is preserved internal DTD
            //string[] dtbFilesList = Directory.GetFiles(m_Presentation.DataProviderManager.DataFileDirectoryFullPath, daisy.import.Daisy3_Import.INTERNAL_DTD_NAME, SearchOption.AllDirectories);
            //string strInternalDTD = null;
            //if (dtbFilesList.Length > 0)
            //{
            //    strInternalDTD = File.ReadAllText(dtbFilesList[0]);
            //    if (strInternalDTD.Trim() == "") strInternalDTD = null;
            //}



            List <ExternalFileData> list_ExternalStyleSheets = new List <ExternalFileData>();
            string strInternalDTD = null;

            foreach (ExternalFiles.ExternalFileData efd in m_Presentation.ExternalFilesDataManager.ManagedObjects.ContentsAs_Enumerable)
            {
                if (RequestCancellation)
                {
                    return;
                }

                if (efd is ExternalFiles.DTDExternalFileData &&
                    efd.OriginalRelativePath == daisy.import.Daisy3_Import.INTERNAL_DTD_NAME &&
                    !efd.IsPreservedForOutputFile &&
                    string.IsNullOrEmpty(strInternalDTD))
                {
                    StreamReader sr = new StreamReader(efd.OpenInputStream(), Encoding.UTF8);
                    strInternalDTD = sr.ReadToEnd();
                }
                else if (efd is ExternalFiles.CSSExternalFileData ||
                         efd is XSLTExternalFileData
                         //&& !efd.OriginalRelativePath.StartsWith(SupportedMetadata_Z39862005.MATHML_XSLT_METADATA)
                         )
                {
                    list_ExternalStyleSheets.Add(efd);
                }
            }

            if (RequestCancellation)
            {
                return;
            }

            //m_ProgressPercentage = 0;
            reportProgress(-1, UrakawaSDK_daisy_Lang.CreatingXMLFile);
            XmlDocument DTBookDocument = XmlDocumentHelper.CreateStub_DTBDocument(m_Presentation.Language, strInternalDTD, list_ExternalStyleSheets);

            string mathML_XSLT = null;

            foreach (ExternalFileData efd in list_ExternalStyleSheets)
            {
                string filename = efd.OriginalRelativePath;
                if (filename.StartsWith(SupportedMetadata_Z39862005.MATHML_XSLT_METADATA))
                {
                    filename = filename.Substring(SupportedMetadata_Z39862005.MATHML_XSLT_METADATA.Length);

                    mathML_XSLT = filename;
                }

                filename = FileDataProvider.EliminateForbiddenFileNameCharacters(filename);

                if (efd.IsPreservedForOutputFile &&
                    !m_FilesList_ExternalFiles.Contains(filename))
                {
                    string filePath = Path.Combine(m_OutputDirectory, filename);
                    efd.DataProvider.ExportDataStreamToFile(filePath, true);
                    m_FilesList_ExternalFiles.Add(filename);
                }
            }

            string mathPrefix = m_Presentation.RootNode.GetXmlNamespacePrefix(DiagramContentModelHelper.NS_URL_MATHML);


            if (!string.IsNullOrEmpty(mathPrefix) && string.IsNullOrEmpty(mathML_XSLT))
            {
                string appDir       = System.AppDomain.CurrentDomain.BaseDirectory;
                string xsltFileName = FileDataProvider.EliminateForbiddenFileNameCharacters(SupportedMetadata_Z39862005._builtInMathMLXSLT);
                string xsltFullPath = Path.Combine(appDir, xsltFileName);

                if (File.Exists(xsltFullPath))
                {
                    string destFile = Path.Combine(m_OutputDirectory, xsltFileName);
                    File.Copy(xsltFullPath, destFile, true);
                    try
                    {
                        File.SetAttributes(destFile, FileAttributes.Normal);
                    }
                    catch
                    {
                    }
                    m_FilesList_ExternalFiles.Add(xsltFileName);
                }
            }

            m_ListOfLevels = new List <TreeNode>();
            Dictionary <string, string> old_New_IDMap             = new Dictionary <string, string>();
            List <XmlAttribute>         referencingAttributesList = new List <XmlAttribute>();



            // add metadata
            XmlNode headNode = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(DTBookDocument, true, "head", null); //DTBookDocument.GetElementsByTagName("head")[0]

            Metadata mdId = AddMetadata_DtbUid(false, DTBookDocument, headNode);

            AddMetadata_Generator(DTBookDocument, headNode);

            bool hasMathML_z39_86_extension_version = false;
            bool hasMathML_DTBook_XSLTFallback      = false;

            // todo: filter-out unecessary metadata for DTBOOK (e.g. dtb:multimediatype)
            foreach (Metadata m in m_Presentation.Metadatas.ContentsAs_Enumerable)
            {
                if (mdId == m)
                {
                    continue;
                }

                XmlNode metaNode = DTBookDocument.CreateElement(null, "meta", headNode.NamespaceURI);
                headNode.AppendChild(metaNode);

                string name = m.NameContentAttribute.Name;

                if (name == SupportedMetadata_Z39862005._z39_86_extension_version)
                {
                    hasMathML_z39_86_extension_version = true;
                }
                else if (name == SupportedMetadata_Z39862005.MATHML_XSLT_METADATA)
                {
                    hasMathML_DTBook_XSLTFallback = true;
                }

                string prefix;
                string localName;
                urakawa.property.xml.XmlProperty.SplitLocalName(name, out prefix, out localName);

                if (!string.IsNullOrEmpty(prefix))
                {
                    // split the metadata name and make first alphabet upper, required for daisy 3.0

                    localName = localName.Substring(0, 1).ToUpper() + localName.Remove(0, 1);

                    name = prefix + ":" + localName;
                }

                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, "name", name);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, "content", m.NameContentAttribute.Value);

                // add metadata optional attributes if any
                foreach (MetadataAttribute ma in m.OtherAttributes.ContentsAs_Enumerable)
                {
                    if (ma.Name == "id" || ma.Name == Metadata.PrimaryIdentifierMark)
                    {
                        continue;
                    }

                    XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, ma.Name, ma.Value);
                }
            }

            if (!string.IsNullOrEmpty(mathML_XSLT))
            {
                DebugFix.Assert(hasMathML_z39_86_extension_version);
                DebugFix.Assert(hasMathML_DTBook_XSLTFallback);
            }

            if (!string.IsNullOrEmpty(mathPrefix) && !hasMathML_z39_86_extension_version)
            {
                XmlNode metaNode = DTBookDocument.CreateElement(null, "meta", headNode.NamespaceURI);
                headNode.AppendChild(metaNode);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, "name", SupportedMetadata_Z39862005._z39_86_extension_version);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, "content", "1.0");
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, "scheme", DiagramContentModelHelper.NS_URL_MATHML);
            }

            if (!string.IsNullOrEmpty(mathPrefix) && !hasMathML_DTBook_XSLTFallback)
            {
                XmlNode metaNode = DTBookDocument.CreateElement(null, "meta", headNode.NamespaceURI);
                headNode.AppendChild(metaNode);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, "name", SupportedMetadata_Z39862005.MATHML_XSLT_METADATA);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, "content", string.IsNullOrEmpty(mathML_XSLT) ? SupportedMetadata_Z39862005._builtInMathMLXSLT : mathML_XSLT);
                XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, metaNode, "scheme", DiagramContentModelHelper.NS_URL_MATHML);
            }

            // add elements to book body
            m_TreeNode_XmlNodeMap = new Dictionary <TreeNode, XmlNode>();



            XmlNode bookNode = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(DTBookDocument, true, "book", null); //DTBookDocument.GetElementsByTagName("book")[0];

            if (bookNode == null)
            {
                bookNode = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(DTBookDocument, true, "body", null);
            }


            XmlNode docRootNode = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(DTBookDocument, true, "dtbook", null);

            if (docRootNode == null)
            {
                docRootNode = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(DTBookDocument, true, "html", null);
            }

            if (false && !string.IsNullOrEmpty(mathML_XSLT)) // namespace prefix attribute automatically added for each m:math element because of MathML DTD
            {
                XmlDocumentHelper.CreateAppendXmlAttribute(
                    DTBookDocument,
                    bookNode,
                    XmlReaderWriterHelper.NS_PREFIX_XMLNS + ":" + "dtbook",
                    bookNode.NamespaceURI,
                    XmlReaderWriterHelper.NS_URL_XMLNS
                    );
            }

            m_ListOfLevels.Add(m_Presentation.RootNode);

            m_TreeNode_XmlNodeMap.Add(m_Presentation.RootNode, bookNode);
            XmlNode currentXmlNode         = null;
            bool    isHeadingNodeAvailable = true;

            m_Presentation.RootNode.AcceptDepthFirst(
                delegate(TreeNode n)
            {
                if (RequestCancellation)
                {
                    return(false);
                }
                // add to list of levels if xml property has level string
                //QualifiedName qName = n.GetXmlElementQName();
                //if (qName != null &&
                //    (qName.LocalName.StartsWith("level") || qName.LocalName == "doctitle" || qName.LocalName == "docauthor"))
                //{
                //    m_ListOfLevels.Add(n);
                //}

                if (doesTreeNodeTriggerNewSmil(n))
                {
                    if (!isHeadingNodeAvailable && m_ListOfLevels.Count > 1)
                    {
                        m_ListOfLevels.RemoveAt(m_ListOfLevels.Count - 1);
                        //System.Windows.Forms.MessageBox.Show ( "removing :" + m_ListOfLevels.Count.ToString () );
                    }
                    m_ListOfLevels.Add(n);
                    isHeadingNodeAvailable = false;
                    reportProgress(-1, UrakawaSDK_daisy_Lang.CreatingXMLFile);
                }

                if (IsHeadingNode(n))
                {
                    isHeadingNodeAvailable = true;
                }
                if (n.HasXmlProperty && (n.GetXmlElementLocalName() == "note" || n.GetXmlElementLocalName() == "annotation"))
                {
                    m_NotesNodeList.Add(n);
                }

                property.xml.XmlProperty xmlProp = n.GetXmlProperty();

                if (xmlProp == null)
                {
                    string txtx = n.GetTextMedia() != null ? n.GetTextMedia().Text : null;
                    if (txtx != null)
                    {
                        XmlNode textNode = DTBookDocument.CreateTextNode(txtx);


                        ExternalAudioMedia extAudio = GetExternalAudioMedia(n);

                        if (extAudio == null)
                        {
                            m_TreeNode_XmlNodeMap[n.Parent].AppendChild(textNode);
                            m_TreeNode_XmlNodeMap.Add(n, textNode);
                        }
                        else
                        {
                            Debug.Fail("TreeNode without XmlProperty but with TextMedia cannot have Audio attached to it ! (reason: at authoring time, an XmlProperty should have been added when audio was recorded for the pure-text TreeNode) => " + txtx);

                            //XmlNode textParent = DTBookDocument.CreateElement(null, "sent", bookNode.NamespaceURI);
                            //textParent.AppendChild(textNode);

                            //m_TreeNode_XmlNodeMap[n.Parent].AppendChild(textParent);
                            //m_TreeNode_XmlNodeMap.Add(n, textParent);
                        }
                    }

                    return(true);
                }

                // create sml node in dtbook document

                // code removed because XmlProperty stores proper namespaces, useful for inline MathML, SVG, whatever...
                //string name = xmlProp.LocalName;
                //string prefix = name.Contains(":") ? name.Split(':')[0] : null;
                //string elementName = name.Contains(":") ? name.Split(':')[1] : name;
                //currentXmlNode = DTBookDocument.CreateElement(prefix, elementName, bookNode.NamespaceURI);

                bool notBookRoot = !"book".Equals(xmlProp.LocalName, StringComparison.OrdinalIgnoreCase) &&
                                   !"body".Equals(xmlProp.LocalName, StringComparison.OrdinalIgnoreCase);


                bool forceXmlNamespacePrefix = false;

                if (        //xmlProp.LocalName.Equals("math", StringComparison.OrdinalIgnoreCase)
                    xmlProp.GetNamespaceUri() == DiagramContentModelHelper.NS_URL_MATHML &&
                    bookNode.LocalName.Equals("book", StringComparison.OrdinalIgnoreCase)
                    )
                {
                    // Hack, because some MathML in DAISY is produced
                    // with redundant (and DTD-invalid) xmlns="http://MATHML"
                    forceXmlNamespacePrefix = true;
                }

                if (!notBookRoot)
                {
                    currentXmlNode = bookNode;
                }
                else
                {
                    XmlNode xmlNodeParent = m_TreeNode_XmlNodeMap[n.Parent];

                    string nsUri = n.GetXmlNamespaceUri();

                    if (string.IsNullOrEmpty(nsUri))
                    {
                        nsUri = xmlNodeParent.NamespaceURI;
                    }

                    DebugFix.Assert(!string.IsNullOrEmpty(nsUri));

                    if (string.IsNullOrEmpty(nsUri))
                    {
                        nsUri = bookNode.NamespaceURI;
                    }

                    if (string.IsNullOrEmpty(nsUri))
                    {
                        nsUri = DTBookDocument.DocumentElement.NamespaceURI;
                    }

                    if (string.IsNullOrEmpty(nsUri))
                    {
                        nsUri = DTBookDocument.NamespaceURI;
                    }

                    string prefix = (forceXmlNamespacePrefix || n.NeedsXmlNamespacePrefix()) ? n.GetXmlNamespacePrefix(nsUri) : null;

                    currentXmlNode = DTBookDocument.CreateElement(
                        prefix,
                        xmlProp.LocalName,
                        nsUri);

                    xmlNodeParent.AppendChild(currentXmlNode);

                    m_TreeNode_XmlNodeMap.Add(n, currentXmlNode);
                }

                // add attributes
                for (int i = 0; i < xmlProp.Attributes.Count; i++)
                {
                    property.xml.XmlAttribute xmlAttr = xmlProp.Attributes.Get(i);

                    string prefix            = xmlAttr.Prefix;
                    string nameWithoutPrefix = xmlAttr.PrefixedLocalName != null ? xmlAttr.PrefixedLocalName : xmlAttr.LocalName;

                    if (!string.IsNullOrEmpty(prefix))
                    {
                        string nsUriPrefix = xmlProp.GetNamespaceUri(prefix);

                        if (string.IsNullOrEmpty(nsUriPrefix))
                        {
#if DEBUG
                            Debugger.Break();
#endif //DEBUG
                            nsUriPrefix = currentXmlNode.GetNamespaceOfPrefix(prefix);
                        }

                        if (!string.IsNullOrEmpty(nsUriPrefix) &&
                            string.IsNullOrEmpty(DTBookDocument.DocumentElement.GetNamespaceOfPrefix(prefix)) &&
                            string.IsNullOrEmpty(bookNode.GetNamespaceOfPrefix(prefix)))
                        {
                            XmlDocumentHelper.CreateAppendXmlAttribute(
                                DTBookDocument,
                                docRootNode,         //bookNode,
                                XmlReaderWriterHelper.NS_PREFIX_XMLNS + ":" + prefix,
                                nsUriPrefix,
                                XmlReaderWriterHelper.NS_URL_XMLNS
                                );
                        }
                    }

                    //todo: check ID attribute, normalize with fresh new list of IDs
                    // (warning: be careful maintaining ID REFS, such as idref attributes for annotation/annoref and prodnote/noteref
                    // (be careful because idref contain URIs with hash character),
                    // and also the special imgref and headers attributes which contain space-separated list of IDs, not URIs)

                    if (nameWithoutPrefix == "id")         //  xmlAttr.LocalName == "id" || xmlAttr.LocalName == XmlReaderWriterHelper.XmlId)
                    {
                        string id_New = GetNextID(ID_DTBPrefix);
                        XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument,
                                                                   currentXmlNode,
                                                                   "id", id_New);

                        if (!old_New_IDMap.ContainsKey(xmlAttr.Value))
                        {
                            old_New_IDMap.Add(xmlAttr.Value, id_New);
                        }
                        else
                        {
                            System.Diagnostics.Debug.Fail("Duplicate ID found in original DTBook document", "Original DTBook document has duplicate ID: " + xmlProp.Attributes.Get(i).Value);
                        }
                    }
                    else
                    {
                        XmlNode xmlNd = null;
                        if (currentXmlNode == bookNode
                            &&
                            (prefix == XmlReaderWriterHelper.NS_PREFIX_XMLNS ||
                             nameWithoutPrefix == "lang"))
                        {
                            // Hack: to make sure DTD validation passes.
                            xmlNd = docRootNode;
                        }
                        else
                        {
                            xmlNd = currentXmlNode;
                        }

                        if (forceXmlNamespacePrefix &&
                            nameWithoutPrefix == XmlReaderWriterHelper.NS_PREFIX_XMLNS &&
                            xmlAttr.Value == DiagramContentModelHelper.NS_URL_MATHML
                            )
                        {
                            bool debug = true;         // skip xmlns="http://MATH"
                        }
                        else
                        {
                            XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument,
                                                                       xmlNd,
                                                                       xmlAttr.LocalName,
                                                                       xmlAttr.Value,
                                                                       xmlAttr.GetNamespaceUri());
                        }
                    }
                }         // for loop ends

                if (!notBookRoot)
                {
                    return(true);
                }


                if (xmlProp.LocalName == "imggroup")
                {
                    m_TempImageId = new List <string>(1);
                }
                if (m_TempImageId != null &&      // !string.IsNullOrEmpty(m_TempImageId)
                    (xmlProp.LocalName == "caption" || xmlProp.LocalName == "prodnote"))
                {
                    string imgIds = "";
                    foreach (string imgId in m_TempImageId)
                    {
                        imgIds += imgId + " ";
                    }
                    imgIds = imgIds.Trim();
                    if (!string.IsNullOrEmpty(imgIds))
                    {
                        XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, currentXmlNode, "imgref", imgIds);
                    }
                }

                XmlAttributeCollection currentXmlNodeAttrs = currentXmlNode.Attributes;

                // add id attribute in case it do not exists and it is required
                if (IsIDRequired(currentXmlNode.LocalName) && currentXmlNodeAttrs != null)
                {
                    XmlNode idAttr = currentXmlNodeAttrs.GetNamedItem("id");
                    if (idAttr == null)
                    {
                        string id = GetNextID(ID_DTBPrefix);
                        XmlDocumentHelper.CreateAppendXmlAttribute(DTBookDocument, currentXmlNode, "id", id);
                    }
                    else if (xmlProp.LocalName != null &&
                             xmlProp.LocalName.Equals("img", StringComparison.OrdinalIgnoreCase) &&
                             m_TempImageId != null)
                    {
                        string id = idAttr.Value;
                        m_TempImageId.Add(id);
                    }
                }

                // add text from text property

                string txt = n.GetTextMedia() != null ? n.GetTextMedia().Text : null;
                if (txt != null)
                {
                    XmlNode textNode = DTBookDocument.CreateTextNode(txt);
                    currentXmlNode.AppendChild(textNode);

                    DebugFix.Assert(n.Children.Count == 0);
                }

                // if current xmlnode is referencing node, add its referencing attribute to referencingAttributesList
                AddReferencingNodeToReferencedAttributesList(currentXmlNode, referencingAttributesList);

                if (currentXmlNodeAttrs != null && currentXmlNode.LocalName != null)
                {
                    bool isHTML = @"body".Equals(m_Presentation.RootNode.GetXmlElementLocalName(), StringComparison.OrdinalIgnoreCase);         //n.Presentation
                    // TODO: special treatment of subfolders in file paths (restore full hierarchy, including OPF, XHTMLs, etc., not just referenced media assets)

                    if (currentXmlNode.LocalName.Equals("img", StringComparison.OrdinalIgnoreCase))
                    {
                        XmlAttribute imgSrcAttribute =
                            (XmlAttribute)currentXmlNodeAttrs.GetNamedItem("src");
                        if (imgSrcAttribute != null &&
                            n.GetImageMedia() != null &&
                            n.GetImageMedia() is media.data.image.ManagedImageMedia)
                        {
                            media.data.image.ManagedImageMedia managedImage =
                                (media.data.image.ManagedImageMedia)n.GetImageMedia();

                            //if (FileDataProvider.isHTTPFile(managedImage.ImageMediaData.OriginalRelativePath))
                            //exportImageName = Path.GetFileName(managedImage.ImageMediaData.OriginalRelativePath);

                            string exportImageName =
                                //Path.GetFileName
                                FileDataProvider.EliminateForbiddenFileNameCharacters
                                    (managedImage.ImageMediaData.OriginalRelativePath)
                            ;

                            string destPath = Path.Combine(m_OutputDirectory, exportImageName);

                            if (!File.Exists(destPath))
                            {
                                if (RequestCancellation)
                                {
                                    return(false);
                                }
                                managedImage.ImageMediaData.DataProvider.ExportDataStreamToFile(destPath, false);
                            }

                            imgSrcAttribute.Value = FileDataProvider.UriEncode(exportImageName);

                            if (!m_FilesList_Image.Contains(exportImageName))
                            {
                                m_FilesList_Image.Add(exportImageName);
                            }

                            generateImageDescriptionInDTBook(n, currentXmlNode, exportImageName, DTBookDocument);
                        }
                    }
                    else if (currentXmlNode.LocalName.Equals(DiagramContentModelHelper.Math, StringComparison.OrdinalIgnoreCase))
                    {
                        XmlAttribute imgSrcAttribute =
                            (XmlAttribute)currentXmlNodeAttrs.GetNamedItem("altimg");
                        if (imgSrcAttribute != null &&
                            n.GetImageMedia() != null &&
                            n.GetImageMedia() is media.data.image.ManagedImageMedia)
                        {
                            media.data.image.ManagedImageMedia managedImage =
                                (media.data.image.ManagedImageMedia)n.GetImageMedia();

                            //if (FileDataProvider.isHTTPFile(managedImage.ImageMediaData.OriginalRelativePath))
                            //exportImageName = Path.GetFileName(managedImage.ImageMediaData.OriginalRelativePath);

                            string exportImageName =
                                //Path.GetFileName
                                FileDataProvider.EliminateForbiddenFileNameCharacters
                                    (managedImage.ImageMediaData.OriginalRelativePath)
                            ;

                            string destPath = Path.Combine(m_OutputDirectory, exportImageName);

                            if (!File.Exists(destPath))
                            {
                                if (RequestCancellation)
                                {
                                    return(false);
                                }
                                managedImage.ImageMediaData.DataProvider.ExportDataStreamToFile(destPath, false);
                            }

                            imgSrcAttribute.Value = FileDataProvider.UriEncode(exportImageName);

                            if (!m_FilesList_Image.Contains(exportImageName))
                            {
                                m_FilesList_Image.Add(exportImageName);
                            }
                        }
                    }

#if SUPPORT_AUDIO_VIDEO
                    else if (currentXmlNode.LocalName.Equals("video", StringComparison.OrdinalIgnoreCase) ||
                             (
                                 currentXmlNode.LocalName.Equals("source", StringComparison.OrdinalIgnoreCase) &&
                                 currentXmlNode.ParentNode != null &&
                                 currentXmlNode.ParentNode.LocalName.Equals("video", StringComparison.OrdinalIgnoreCase)
                             )
                             )
                    {
                        XmlAttribute videoSrcAttribute =
                            (XmlAttribute)currentXmlNodeAttrs.GetNamedItem("src");
                        if (videoSrcAttribute != null &&
                            n.GetVideoMedia() != null &&
                            n.GetVideoMedia() is ManagedVideoMedia)
                        {
                            ManagedVideoMedia managedVideo = (ManagedVideoMedia)n.GetVideoMedia();

                            //if (FileDataProvider.isHTTPFile(managedVideo.VideoMediaData.OriginalRelativePath))
                            //exportVideoName = Path.GetFileName(managedVideo.VideoMediaData.OriginalRelativePath);

                            string exportVideoName =
                                FileDataProvider.EliminateForbiddenFileNameCharacters(
                                    managedVideo.VideoMediaData.OriginalRelativePath);

                            string destPath = Path.Combine(m_OutputDirectory, exportVideoName);



                            if (!File.Exists(destPath))
                            {
                                if (RequestCancellation)
                                {
                                    return(false);
                                }
                                managedVideo.VideoMediaData.DataProvider.ExportDataStreamToFile(destPath, false);
                            }

                            videoSrcAttribute.Value = FileDataProvider.UriEncode(exportVideoName);

                            if (!m_FilesList_Video.Contains(exportVideoName))
                            {
                                m_FilesList_Video.Add(exportVideoName);
                            }
                        }
                    }
                    else if (currentXmlNode.LocalName.Equals("audio", StringComparison.OrdinalIgnoreCase) ||
                             (
                                 currentXmlNode.LocalName.Equals("source", StringComparison.OrdinalIgnoreCase) &&
                                 currentXmlNode.ParentNode != null &&
                                 currentXmlNode.ParentNode.LocalName.Equals("audio", StringComparison.OrdinalIgnoreCase)
                             )
                             )
                    {
                        XmlAttribute audioSrcAttribute =
                            (XmlAttribute)currentXmlNodeAttrs.GetNamedItem("src");
                        ManagedAudioMedia managedAudio = n.GetManagedAudioMedia();
                        if (audioSrcAttribute != null &&
                            managedAudio != null)
                        {
                            //if (FileDataProvider.isHTTPFile(managedAudio.AudioMediaData.OriginalRelativePath))
                            //exportAudioName = Path.GetFileName(managedAudio.AudioMediaData.OriginalRelativePath);

                            string exportAudioName =
                                FileDataProvider.EliminateForbiddenFileNameCharacters(
                                    managedAudio.AudioMediaData.OriginalRelativePath);

                            string destPath = Path.Combine(m_OutputDirectory, exportAudioName);



                            if (!File.Exists(destPath))
                            {
                                if (RequestCancellation)
                                {
                                    return(false);
                                }
                                managedAudio.AudioMediaData.DataProvider.ExportDataStreamToFile(destPath, false);
                            }

                            audioSrcAttribute.Value = FileDataProvider.UriEncode(exportAudioName);

                            if (!m_FilesList_Audio.Contains(exportAudioName))
                            {
                                m_FilesList_Audio.Add(exportAudioName);
                            }
                        }
                    }
#endif
                }


                return(true);
            },
                delegate(urakawa.core.TreeNode n)
            {
                property.xml.XmlProperty xmlProp = n.GetXmlProperty();
                //QualifiedName qName = n.GetXmlElementQName();
                if (xmlProp != null && xmlProp.LocalName == "imggroup")
                {
                    m_TempImageId = null;
                }
            });

            if (RequestCancellation)
            {
                return;
            }
            // set references to new ids
            foreach (XmlAttribute attr in referencingAttributesList)
            {
                string strIDToFind = attr.Value;
                if (strIDToFind.IndexOf('#') >= 0) //strIDToFind.Contains("#")
                {
                    strIDToFind = strIDToFind.Split('#')[1];
                }

                string str;
                old_New_IDMap.TryGetValue(strIDToFind, out str);

                if (!string.IsNullOrEmpty(str)) //old_New_IDMap.ContainsKey(strIDToFind))
                {
                    string id_New = str;        // old_New_IDMap[strIDToFind];

                    attr.Value = "#" + id_New;
                }
            }

            m_DTBDocument = DTBookDocument;
            //CommonFunctions.WriteXmlDocumentToFile(DTBookDocument,
            //  Path.Combine(m_OutputDirectory, m_Filename_Content));
        }
        public void CreateSmilNodesForImageDescription(TreeNode levelNodeDescendant, XmlDocument smilDocument, XmlNode smilBodySeq, Time durationOfCurrentSmil, AlternateContentProperty altProperty, string smilFileName)
        {
            //try
            //{
            int counter = 0;

            foreach (string diagramDescriptionElementName in m_Map_AltProperty_TO_Description[altProperty].Map_DiagramElementName_TO_AltContent.Keys)
            {
                AlternateContent altContent = m_Map_AltProperty_TO_Description[altProperty].Map_DiagramElementName_TO_AltContent[diagramDescriptionElementName];
                if (altContent.Text == null)
                {
                    continue;
                }
                counter++;
                if (m_Image_ProdNoteMap[levelNodeDescendant].Count <= counter)
                {
                    break;
                }
                XmlNode seqNode = smilDocument.CreateElement("seq", smilBodySeq.NamespaceURI);
                smilBodySeq.AppendChild(seqNode);
                XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, seqNode, "class", "prodnote");
                string strSeqID = GetNextID(ID_SmilPrefix);
                //System.Windows.Forms.MessageBox.Show(counter.ToString ()  + " : " + m_Image_ProdNoteMap[n].Count.ToString());
                string dtbookID = m_Image_ProdNoteMap[levelNodeDescendant][counter].Attributes.GetNamedItem("id").Value;
                string par_id   = GetNextID(ID_SmilPrefix);
                XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, seqNode, "id", strSeqID);
                XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, seqNode, "class", "prodnote");
                XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, seqNode, "customTest", "prodnote");
                XmlDocumentHelper.CreateAppendXmlAttribute(m_DTBDocument, m_Image_ProdNoteMap[levelNodeDescendant][counter], "smilref",
                                                           FileDataProvider.UriEncode(smilFileName + "#" + strSeqID));

                XmlNode parNode = smilDocument.CreateElement(null, "par", smilBodySeq.NamespaceURI);
                seqNode.AppendChild(parNode);
                XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, parNode, "id", par_id);
                XmlNode SmilTextNode = smilDocument.CreateElement(null, "text", smilBodySeq.NamespaceURI);
                XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, SmilTextNode, "id", GetNextID(ID_SmilPrefix));
                XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, SmilTextNode, "src",
                                                           FileDataProvider.UriEncode(m_Filename_Content + "#" + dtbookID));
                parNode.AppendChild(SmilTextNode);


                if (altContent.Audio != null)
                {
                    media.data.audio.ManagedAudioMedia managedAudio = altContent.Audio;
                    string srcPath = m_Map_AltContentAudio_TO_RelativeExportedFilePath[altContent];

                    DebugFix.Assert(!string.IsNullOrEmpty(srcPath));

                    XmlNode audioNode = smilDocument.CreateElement(null, "audio", smilBodySeq.NamespaceURI);
                    XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, audioNode, "clipBegin",
                                                               "00:00:00");
                    XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, audioNode, "clipEnd",
                                                               FormatTimeString(managedAudio.Duration));
                    XmlDocumentHelper.CreateAppendXmlAttribute(smilDocument, audioNode, "src",
                                                               FileDataProvider.UriEncode(srcPath));
                    parNode.AppendChild(audioNode);

                    if (!m_FilesList_SmilAudio.Contains(srcPath))
                    {
                        m_FilesList_SmilAudio.Add(srcPath);
                    }

                    // add to duration
                    durationOfCurrentSmil.Add(managedAudio.Duration);
                }
            }
            //}
            //catch (System.Exception ex)
            //{
            //    System.Windows.Forms.MessageBox.Show(ex.ToString());
            //}
        }