protected virtual void AddPagePropertiesToAudioNode(TreeNode audioWrapperNode, XmlNode pageTargetNode) { TextMedia textMedia = audioWrapperNode.Presentation.MediaFactory.CreateTextMedia(); textMedia.Text = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(pageTargetNode, true, "text", pageTargetNode.NamespaceURI).InnerText; ChannelsProperty cProp = audioWrapperNode.Presentation.PropertyFactory.CreateChannelsProperty(); cProp.SetMedia(audioWrapperNode.Presentation.ChannelsManager.GetOrCreateTextChannel(), textMedia); audioWrapperNode.AddProperty(cProp); System.Xml.XmlAttributeCollection pageAttributes = pageTargetNode.Attributes; if (pageAttributes != null) { XmlProperty xmlProp = audioWrapperNode.GetXmlProperty(); xmlProp.SetQName("pagenum", ""); string nsUri = audioWrapperNode.GetXmlNamespaceUri(); foreach (System.Xml.XmlAttribute attr in pageAttributes) { string uri = ""; if (!string.IsNullOrEmpty(attr.NamespaceURI)) { if (attr.NamespaceURI != nsUri) { uri = attr.NamespaceURI; } } xmlProp.SetAttribute(attr.Name, uri, attr.Value); } } }
protected virtual TreeNode AddAnchorNode(TreeNode navPointTreeNode, XmlNode textPeerNode, string fullSmilPath) { TreeNode anchorNode = navPointTreeNode.Presentation.TreeNodeFactory.Create(); navPointTreeNode.AppendChild(anchorNode); XmlNode xmlNodeAttr = textPeerNode.Attributes.GetNamedItem("href"); string strReferedID = xmlNodeAttr.Value; XmlNode seqParent = textPeerNode.ParentNode; while (seqParent != null) { if (seqParent.LocalName == "seq" && seqParent.Attributes.GetNamedItem("customTest") != null) { break; } seqParent = seqParent.ParentNode; } string strClass = seqParent.Attributes.GetNamedItem("class").Value; if (strClass != null) { XmlProperty prop = anchorNode.GetOrCreateXmlProperty(); prop.SetQName(strClass, ""); prop.SetAttribute(xmlNodeAttr.Name, "", strReferedID); } return(anchorNode); }
protected void UpdateAttributesInXmlProperty(XmlProperty xmlProp, string attributeLocalName, string attributeValue) { XmlAttribute attr = xmlProp.GetAttribute(attributeLocalName); if (attr == null) { xmlProp.SetAttribute(attributeLocalName, "", attributeValue); } else { attr.Value = attributeValue; } }
private void parseContentDocument(XmlNode xmlNode, TreeNode parentTreeNode, string filePath) { XmlNodeType xmlType = xmlNode.NodeType; switch (xmlType) { case XmlNodeType.Attribute: { System.Diagnostics.Debug.Fail("Calling this method with an XmlAttribute should never happen !!"); break; } case XmlNodeType.Document: { //XmlNodeList listOfBodies = ((XmlDocument)xmlNode).GetElementsByTagName("body"); //if (listOfBodies.Count == 0) //{ // listOfBodies = ((XmlDocument)xmlNode).GetElementsByTagName("book"); //} XmlNode bodyElement = getFirstChildElementsWithName(xmlNode, true, "body", null); if (bodyElement == null) { bodyElement = getFirstChildElementsWithName(xmlNode, true, "book", null); } if (bodyElement != null) { Presentation presentation = m_Project.Presentations.Get(0); presentation.PropertyFactory.DefaultXmlNamespaceUri = bodyElement.NamespaceURI; // preserve internal DTD if it exists in dtbook string strInternalDTD = ExtractInternalDTD(((XmlDocument)xmlNode).DocumentType); if (strInternalDTD != null) { File.WriteAllText( Path.Combine(presentation.DataProviderManager.DataFileDirectoryFullPath, "DTBookLocalDTD.dtd"), strInternalDTD); } parseContentDocument(bodyElement, parentTreeNode, filePath); } //parseContentDocument(((XmlDocument)xmlNode).DocumentElement, parentTreeNode); break; } case XmlNodeType.Element: { Presentation presentation = m_Project.Presentations.Get(0); TreeNode treeNode = presentation.TreeNodeFactory.Create(); if (parentTreeNode == null) { presentation.RootNode = treeNode; parentTreeNode = presentation.RootNode; } else { parentTreeNode.AppendChild(treeNode); } XmlProperty xmlProp = presentation.PropertyFactory.CreateXmlProperty(); treeNode.AddProperty(xmlProp); xmlProp.LocalName = xmlNode.LocalName; if (xmlNode.ParentNode != null && xmlNode.ParentNode.NodeType == XmlNodeType.Document) { presentation.PropertyFactory.DefaultXmlNamespaceUri = xmlNode.NamespaceURI; } if (xmlNode.NamespaceURI != presentation.PropertyFactory.DefaultXmlNamespaceUri) { xmlProp.NamespaceUri = xmlNode.NamespaceURI; } string updatedSRC = null; if (xmlNode.LocalName == "img") { XmlNode getSRC = xmlNode.Attributes.GetNamedItem("src"); if (getSRC != null) { string relativePath = xmlNode.Attributes.GetNamedItem("src").Value; if (!relativePath.StartsWith("http://")) { string parentPath = Directory.GetParent(filePath).FullName; string imgSourceFullpath = Path.Combine(parentPath, relativePath); string datafilePath = presentation.DataProviderManager.DataFileDirectoryFullPath; string imgDestFullpath = Path.Combine(datafilePath, Path.GetFileName(imgSourceFullpath)); if (!File.Exists(imgDestFullpath)) { //File.Delete(imgDestFullpath); File.Copy(imgSourceFullpath, imgDestFullpath); } updatedSRC = presentation.RootUri.MakeRelativeUri(new Uri(imgDestFullpath, UriKind.Absolute)) .ToString(); //string dirPath = Path.GetDirectoryName(presentation.RootUri.LocalPath); //updatedSRC = presentation.DataProviderManager.DataFileDirectory + Path.DirectorySeparatorChar + Path.GetFileName(imgDestFullpath); ChannelsProperty chProp = presentation.PropertyFactory.CreateChannelsProperty(); treeNode.AddProperty(chProp); ExternalImageMedia externalImage = presentation.MediaFactory.CreateExternalImageMedia(); externalImage.Src = updatedSRC; chProp.SetMedia(m_ImageChannel, externalImage); } } } XmlAttributeCollection attributeCol = xmlNode.Attributes; if (attributeCol != null) { for (int i = 0; i < attributeCol.Count; i++) { XmlNode attr = attributeCol.Item(i); if (attr.LocalName != "smilref" && attr.Name != "xmlns:xsi" && attr.Name != "xml:space") { if (updatedSRC != null && attr.LocalName == "src") { xmlProp.SetAttribute(attr.LocalName, "", updatedSRC); } else { if (attr.LocalName == "xmlns") { if (attr.Value != presentation.PropertyFactory.DefaultXmlNamespaceUri) { xmlProp.SetAttribute(attr.LocalName, "", attr.Value); } } else if (string.IsNullOrEmpty(attr.NamespaceURI) || attr.NamespaceURI == presentation.PropertyFactory.DefaultXmlNamespaceUri) { xmlProp.SetAttribute(attr.LocalName, "", attr.Value); } else { xmlProp.SetAttribute(attr.Name, attr.NamespaceURI, attr.Value); } } } } } foreach (XmlNode childXmlNode in xmlNode.ChildNodes) { parseContentDocument(childXmlNode, treeNode, filePath); } break; } case XmlNodeType.Text: { Presentation presentation = m_Project.Presentations.Get(0); string text = trimXmlText(xmlNode.Value); TextMedia textMedia = presentation.MediaFactory.CreateTextMedia(); textMedia.Text = text; ChannelsProperty cProp = presentation.PropertyFactory.CreateChannelsProperty(); cProp.SetMedia(m_textChannel, textMedia); int counter = 0; foreach (XmlNode childXmlNode in xmlNode.ParentNode.ChildNodes) { XmlNodeType childXmlType = childXmlNode.NodeType; if (childXmlType == XmlNodeType.Text || childXmlType == XmlNodeType.Element) { counter++; } } if (counter == 1) { parentTreeNode.AddProperty(cProp); } else { TreeNode txtWrapperNode = presentation.TreeNodeFactory.Create(); txtWrapperNode.AddProperty(cProp); parentTreeNode.AppendChild(txtWrapperNode); } break; } default: { return; } } }
private void parseDTBookXmlDocAndPopulateDataModel(XmlNode xmlNode, core.TreeNode parentTreeNode) { XmlNodeType xmlType = xmlNode.NodeType; switch (xmlType) { case XmlNodeType.Attribute: { System.Diagnostics.Debug.Fail("Calling this method with an XmlAttribute should never happen !!"); break; } case XmlNodeType.Document: { parseDTBookXmlDocAndPopulateDataModel(((XmlDocument)xmlNode).DocumentElement, parentTreeNode); break; } case XmlNodeType.Element: { Presentation presentation = m_Project.GetPresentation(0); core.TreeNode treeNode = presentation.TreeNodeFactory.Create(); if (parentTreeNode == null) { presentation.RootNode = treeNode; parentTreeNode = presentation.RootNode; } else { parentTreeNode.AppendChild(treeNode); } XmlProperty xmlProp = presentation.PropertyFactory.CreateXmlProperty(); treeNode.AddProperty(xmlProp); xmlProp.LocalName = xmlNode.Name; if (xmlNode.ParentNode != null && xmlNode.ParentNode.NodeType == XmlNodeType.Document) { presentation.PropertyFactory.DefaultXmlNamespaceUri = xmlNode.NamespaceURI; } if (xmlNode.NamespaceURI != presentation.PropertyFactory.DefaultXmlNamespaceUri) { xmlProp.NamespaceUri = xmlNode.NamespaceURI; } XmlAttributeCollection attributeCol = xmlNode.Attributes; if (attributeCol != null) { for (int i = 0; i < attributeCol.Count; i++) { XmlNode attr = attributeCol.Item(i); if (attr.Name != "smilref") { xmlProp.SetAttribute(attr.Name, "", attr.Value); } } if (xmlNode.Name == "meta") { XmlNode attrName = attributeCol.GetNamedItem("name"); XmlNode attrContent = attributeCol.GetNamedItem("content"); if (attrName != null && attrContent != null && !String.IsNullOrEmpty(attrName.Value) && !String.IsNullOrEmpty(attrContent.Value)) { Metadata md = presentation.MetadataFactory.CreateMetadata(); md.Name = attrName.Value; md.Content = attrContent.Value; presentation.AddMetadata(md); } } } foreach (XmlNode childXmlNode in xmlNode.ChildNodes) { parseDTBookXmlDocAndPopulateDataModel(childXmlNode, treeNode); } break; } case XmlNodeType.Text: { Presentation presentation = m_Project.GetPresentation(0); string text = xmlNode.Value; TextMedia textMedia = presentation.MediaFactory.CreateTextMedia(); textMedia.Text = text; ChannelsProperty cProp = presentation.PropertyFactory.CreateChannelsProperty(); cProp.SetMedia(m_textChannel, textMedia); int counter = 0; foreach (XmlNode childXmlNode in xmlNode.ParentNode.ChildNodes) { XmlNodeType childXmlType = childXmlNode.NodeType; if (childXmlType == XmlNodeType.Text || childXmlType == XmlNodeType.Element) { counter++; } } if (counter == 1) { parentTreeNode.AddProperty(cProp); } else { core.TreeNode txtWrapperNode = presentation.TreeNodeFactory.Create(); txtWrapperNode.AddProperty(cProp); parentTreeNode.AppendChild(txtWrapperNode); } break; } default: { return; } } }
private void parseHeadLinks(string rootFilePath, Project project, XmlDocument contentDoc) { XmlNode headXmlNode = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(contentDoc.DocumentElement, true, "head", null); if (headXmlNode == null) { return; } Presentation presentation = project.Presentations.Get(0); List <string> externalFileRelativePaths = new List <string>(); foreach (ExternalFiles.ExternalFileData extData in presentation.ExternalFilesDataManager.ManagedObjects.ContentsAs_Enumerable) { if (!string.IsNullOrEmpty(extData.OriginalRelativePath)) { string fullPath = Path.Combine(Path.GetDirectoryName(rootFilePath), extData.OriginalRelativePath); string relPath = FileDataProvider.NormaliseFullFilePath(fullPath).Replace('/', '\\'); if (!externalFileRelativePaths.Contains(relPath)) { externalFileRelativePaths.Add(relPath); } } } List <XmlNode> externalFilesLinks = new List <XmlNode>(); externalFilesLinks.AddRange(XmlDocumentHelper.GetChildrenElementsOrSelfWithName(headXmlNode, true, "link", headXmlNode.NamespaceURI, false)); externalFilesLinks.AddRange(XmlDocumentHelper.GetChildrenElementsOrSelfWithName(headXmlNode, true, "script", headXmlNode.NamespaceURI, false)); externalFilesLinks.AddRange(XmlDocumentHelper.GetChildrenElementsOrSelfWithName(headXmlNode, true, "style", headXmlNode.NamespaceURI, false)); externalFilesLinks.AddRange(XmlDocumentHelper.GetChildrenElementsOrSelfWithName(headXmlNode, true, "title", headXmlNode.NamespaceURI, false)); foreach (XmlNode linkNode in externalFilesLinks) { TreeNode treeNode = presentation.TreeNodeFactory.Create(); presentation.HeadNode.AppendChild(treeNode); XmlProperty xmlProp = presentation.PropertyFactory.CreateXmlProperty(); treeNode.AddProperty(xmlProp); xmlProp.SetQName(linkNode.LocalName, headXmlNode.NamespaceURI == linkNode.NamespaceURI ? "" : linkNode.NamespaceURI); //Console.WriteLine("XmlProperty: " + xmlProp.LocalName); foreach (System.Xml.XmlAttribute xAttr in linkNode.Attributes) { if ( //xAttr.LocalName.Equals(XmlReaderWriterHelper.NS_PREFIX_XMLNS, StringComparison.OrdinalIgnoreCase) //|| xAttr.LocalName.Equals("xsi", StringComparison.OrdinalIgnoreCase) xAttr.NamespaceURI.Equals(XmlReaderWriterHelper.NS_URL_XMLNS, StringComparison.OrdinalIgnoreCase) || xAttr.LocalName.Equals("space", StringComparison.OrdinalIgnoreCase) && xAttr.NamespaceURI.Equals(XmlReaderWriterHelper.NS_URL_XML, StringComparison.OrdinalIgnoreCase) ) { continue; } xmlProp.SetAttribute(xAttr.Name, linkNode.NamespaceURI == xAttr.NamespaceURI ? "" : xAttr.NamespaceURI, xAttr.Value); if ((xAttr.Name.Equals("href", StringComparison.OrdinalIgnoreCase) || xAttr.Name.Equals("src", StringComparison.OrdinalIgnoreCase)) && !string.IsNullOrEmpty(xAttr.Value) && !FileDataProvider.isHTTPFile(xAttr.Value)) { string urlDecoded = FileDataProvider.UriDecode(xAttr.Value); string fullPath = Path.Combine(Path.GetDirectoryName(rootFilePath), urlDecoded); string pathFromAttr = FileDataProvider.NormaliseFullFilePath(fullPath).Replace('/', '\\'); if (!externalFileRelativePaths.Contains(pathFromAttr)) { if (File.Exists(pathFromAttr)) { ExternalFiles.ExternalFileData efd = presentation.ExternalFilesDataFactory.Create <ExternalFiles.GenericExternalFileData>(); try { efd.InitializeWithData(pathFromAttr, urlDecoded, true, null); externalFileRelativePaths.Add(pathFromAttr); addOPF_GlobalAssetPath(pathFromAttr); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); #if DEBUG Debugger.Break(); #endif } } #if DEBUG else { Debugger.Break(); } #endif } } } string innerText = linkNode.InnerText; // includes CDATA sections! (merges "//" javascript comment markers too) if (!string.IsNullOrEmpty(innerText)) { urakawa.media.TextMedia textMedia = presentation.MediaFactory.CreateTextMedia(); textMedia.Text = innerText; ChannelsProperty cProp = presentation.PropertyFactory.CreateChannelsProperty(); cProp.SetMedia(presentation.ChannelsManager.GetOrCreateTextChannel(), textMedia); treeNode.AddProperty(cProp); //Console.WriteLine("Link inner text: " + textMedia.Text); } } }