Пример #1
0
        public void Write(ePUB docPUB)
        {
            try
            {
                //write ePUB is nothing

                //write mime-type
                using (StreamWriter outfile = new StreamWriter(Path.Combine(docPUB.ExpandFolder, ePUBHelper.Files.Mime)))
                {
                    outfile.Write(ePUBHelper.XmlMediaTypes.ePUB);
                }

                //write META-INF//container.xml
                WriteContainer(docPUB);

                // write content.OPF file
                WritePackage(docPUB);

                // parse the table of content
                //ParseTOC(docPUB);
            }
            catch (Exception err)
            {
                LogHelper.Manage("ePUBManager:ParseFolder", err);
            }
        }
Пример #2
0
        public void Compile(ePUB docPUB)
        {
            //write
            Write(docPUB);

            //zip it
            Compress(docPUB);
        }
Пример #3
0
        private void ParseTOC(ePUB docPUB, Stream content)
        {
            try
            {
                //reading
                XmlDocument doc = GetDocumentWithNoValidation(content);

                XmlNode root = doc.DocumentElement;                 //<ncx>

                // resolve <ncx>
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                ResolveNamespaces(nsmgr, root);

                ePUBTableNCX toc = docPUB.Container.Package.TableOfContent;
                toc.Title = root.SelectSingleNode("//ROOT:docTitle/ROOT:text", nsmgr).InnerText;

                XmlNodeList listNode;
                listNode = root.SelectNodes("child::ROOT:navMap/ROOT:navPoint", nsmgr);

                List <ePUBNavPoint> navMapList = new List <ePUBNavPoint>();
                foreach (XmlNode navPoint in listNode)
                {
                    ePUBNavPoint item = new ePUBNavPoint()
                    {
                        Id        = TryGetAttributeString(navPoint, "id"),
                        PlayOrder = TryGetAttributeInt(navPoint, "playOrder"),
                        Label     = navPoint.SelectSingleNode("child::ROOT:navLabel/ROOT:text", nsmgr).InnerText,
                        Content   = TryGetAttributeString(navPoint.SelectSingleNode("child::ROOT:content", nsmgr), "src")
                    };
                    string[] parts = item.Content.Split('#');
                    item.PageSource = parts[0];
                    if (parts.Count() == 2)
                    {
                        item.TargetId = parts[2];
                    }
                    item.XamlId = docPUB.Container.Package.Manifest.Items.Single(p => p.hRef == item.PageSource).XamlId;

                    //is there any bookmark?
                    if (item.Content.IndexOf('#') != -1)
                    {
                        item.Content = item.Content.Split('#')[1];
                    }

                    navMapList.Add(item);

                    item.Items = ParseNavRecursif(docPUB, navPoint, nsmgr);
                }
                toc.Items = navMapList;
            }
            catch (Exception err)
            {
                LogHelper.Manage("ePUBManager:ParseTOC", err);
            }
        }
Пример #4
0
 private void ParseTOC(ePUB docPUB)
 {
     try
     {
         using (FileStream fs = File.Open(docPUB.GetTOCFile(), FileMode.Open, FileAccess.Read))
         {
             ParseTOC(docPUB, fs);
         }
     }
     catch (Exception err)
     {
         LogHelper.Manage("ePUBManager:ParseTOC", err);
     }
 }
Пример #5
0
 private void ParsePackage(ePUB docPUB)
 {
     try
     {
         using (FileStream fs = File.Open(Path.Combine(docPUB.ExpandFolder, docPUB.Container.Package.SysRelativFilePath), FileMode.Open, FileAccess.Read))
         {
             ParsePackage(docPUB, fs);
         }
     }
     catch (Exception err)
     {
         LogHelper.Manage("ePUBManager:ParsePackage", err);
     }
 }
Пример #6
0
        public ePUB ParseFile(string filePath)
        {
            SevenZipExtractor temp = null;

            try
            {
                ePUB docPUB = new ePUB(filePath);

                temp = ZipHelper.Instance.GetExtractor(filePath);

                // find container.xml and parse it
                ArchiveFileInfo fil = temp.ArchiveFileData.Where(p => !p.IsDirectory && p.FileName == ePUBHelper.Files.ContainerRelativFile).First();
                using (MemoryStream stream = new MemoryStream())
                {
                    temp.ExtractFile(fil.FileName, stream);
                    ParseContainer(docPUB, stream);
                }

                // find OPF package file and parse it
                fil = temp.ArchiveFileData.Where(p => !p.IsDirectory && p.FileName == docPUB.Container.Package.SysRelativFilePath).First();
                using (MemoryStream stream = new MemoryStream())
                {
                    temp.ExtractFile(fil.FileName, stream);
                    ParsePackage(docPUB, stream);
                }

                // find the toc file and parse it
                fil = temp.ArchiveFileData.Where(p => !p.IsDirectory && p.FileName == docPUB.GetTOCFile()).First();
                using (MemoryStream stream = new MemoryStream())
                {
                    temp.ExtractFile(fil.FileName, stream);
                    ParseTOC(docPUB, stream);
                }

                return(docPUB);
            }
            catch (Exception err)
            {
                LogHelper.Manage("ePUBManager:ParseFile", err);
                return(null);
            }
            finally
            {
                ZipHelper.Instance.ReleaseExtractor(temp);
            }
        }
Пример #7
0
        private void ParseContainer(ePUB docPUB, Stream content)
        {
            try
            {
                XmlDocument doc = GetDocumentWithNoValidation(content);

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                ResolveNamespaces(nsmgr, doc.DocumentElement);

                XmlNode rootfileNode = doc.SelectSingleNode("//ROOT:rootfiles/ROOT:rootfile", nsmgr);

                docPUB.Container         = new ePUBContainer();
                docPUB.Container.Package = new ePUBPackage(TryGetAttributeString(rootfileNode, ePUBHelper.XmlAttributes.container_full_path));
            }
            catch (Exception err)
            {
                LogHelper.Manage("ePUBManager:ParseContainer", err);
            }
        }
Пример #8
0
        public void WriteContainer(ePUB docPUB)
        {
            string path = Path.Combine(docPUB.ExpandFolder, ePUBHelper.Files.ContainerFolder);

            //check or create metainf
            DirectoryHelper.Check(path);

            //write container.xml
            using (StreamWriter outfile = new StreamWriter(Path.Combine(path, ePUBHelper.Files.ContainerFile)))
            {
                outfile.WriteLine("<?xml version=\"1.0\"?>");
                outfile.WriteLine("<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">");
                outfile.WriteLine("<rootfiles>");
                outfile.WriteLine(string.Format("<rootfile full-path=\"{0}/{1}\"",
                                                docPUB.Container.Package.RelativPackageFolder, docPUB.Container.Package.PackageFileName));
                outfile.WriteLine("media-type=\"application/oebps-package+xml\" />");
                outfile.WriteLine("</rootfiles>");
                outfile.WriteLine("</container>");
            }
        }
Пример #9
0
        public ePUB ParseExtracted(string filePath, string extractFolder)
        {
            try
            {
                ePUB docPUB = new ePUB(filePath, extractFolder);

                //read META-INF container.xml
                ParseContainer(docPUB);

                // read content.OPF file
                ParsePackage(docPUB);

                // parse the table of content
                ParseTOC(docPUB);

                return(docPUB);
            }
            catch (Exception err)
            {
                LogHelper.Manage("ePUBManager:ParseExtracted", err);
                return(null);
            }
        }
Пример #10
0
        private List <ePUBNavPoint> ParseNavRecursif(ePUB docPUB, XmlNode node, XmlNamespaceManager nsmgr)
        {
            try
            {
                XmlNodeList listNode = node.SelectNodes("child::ROOT:navPoint", nsmgr);

                List <ePUBNavPoint> navMapList = new List <ePUBNavPoint>();
                foreach (XmlNode navPoint in listNode)
                {
                    ePUBNavPoint item = new ePUBNavPoint()
                    {
                        Id        = TryGetAttributeString(navPoint, "id"),
                        PlayOrder = TryGetAttributeInt(navPoint, "playOrder"),
                        Label     = navPoint.SelectSingleNode("child::ROOT:navLabel/ROOT:text", nsmgr).InnerText,
                        Content   = TryGetAttributeString(navPoint.SelectSingleNode("child::ROOT:content", nsmgr), "src")
                    };

                    //is there any bookmark?
                    if (item.Content.IndexOf('#') != -1)
                    {
                        item.Content = item.Content.Split('#')[1];
                    }

                    navMapList.Add(item);

                    item.Items = ParseNavRecursif(docPUB, navPoint, nsmgr);
                }

                return(navMapList);
            }
            catch (Exception err)
            {
                LogHelper.Manage("ePUBManager:ParseNavRecursif", err);
                return(null);
            }
        }
Пример #11
0
 public void WritePackage(ePUB docPUB)
 {
 }
Пример #12
0
 public void Compress(ePUB docPUB)
 {
 }
Пример #13
0
 public bool Check(ePUB docPUB)
 {
     return(true);
 }
Пример #14
0
        private void ParsePackage(ePUB docPUB, Stream content)
        {
            try
            {
                //reading the OPF
                XmlDocument doc = GetDocumentWithNoValidation(content);

                XmlNode root = doc.DocumentElement;                 //<package>

                // resolve <package>
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                ResolveNamespaces(nsmgr, root);

                // resolve <metadata>
                ResolveNamespaces(nsmgr, root.SelectSingleNode("//ROOT:metadata", nsmgr));

                //shortcut
                ePUBPackage pack = docPUB.Container.Package;

                pack.Title      = root.SelectSingleNode("//ROOT:metadata/dc:title", nsmgr).InnerText;
                pack.Identifier = root.SelectSingleNode("//ROOT:metadata/dc:identifier", nsmgr).InnerText;

                ////try get the cover id
                //XmlNode xmlNod = root.SelectSingleNode("//ROOT:metadata/ROOT:meta[@name='cover']", nsmgr);
                //pack.CoverId = TryGetAttributeString(xmlNod, "content");

                pack.Metadata = new ePUBMetadata();

                //read the meta list
                XmlNodeList listNode = root.SelectNodes("//ROOT:metadata/ROOT:meta", nsmgr);
                foreach (XmlNode node in listNode)
                {
                    pack.Metadata.Meta.Add(new ePUBMetaItem(node.Attributes["name"].Value, node.Attributes["content"].Value));
                }
                listNode = root.SelectNodes("//ROOT:metadata/dc:*", nsmgr);
                foreach (XmlNode node in listNode)
                {
                    ePUBMetaDcItem item = new ePUBMetaDcItem(node.LocalName, node.InnerText);

                    foreach (XmlAttribute attrib in node.Attributes)
                    {
                        if (attrib.Prefix == "opf")
                        {
                            item.SetOpfAttribute(attrib.LocalName, attrib.Value);
                        }
                        else
                        {
                            item.SetAttribute(attrib.LocalName, attrib.Value);
                        }
                    }
                    pack.Metadata.MetaDC.Add(item);
                }

                //read manifest items
                listNode = root.SelectNodes("//ROOT:manifest/ROOT:item", nsmgr);

                foreach (XmlNode node in listNode)
                {
                    pack.Manifest.Items.Add(new ePUBManifestItem()
                    {
                        Id          = TryGetAttributeString(node, "id"),
                        hRef        = TryGetAttributeString(node, "href"),
                        hRefForPath = TryGetAttributeString(node, "href").Replace("/", "\\"),
                        XamlId      = "XamlBody" + pack.Manifest.Items.Count(),
                        MediaType   = TryGetAttributeString(node, "media-type")
                    });
                }

                //read <spine>
                XmlNode xmlNode = root.SelectSingleNode("//ROOT:spine", nsmgr);
                pack.Spine.TableOfContentId = TryGetAttributeString(xmlNode, "toc");

                listNode = root.SelectNodes("//ROOT:spine/ROOT:itemref", nsmgr);
                foreach (XmlNode node in listNode)
                {
                    ePUBSpineItem item = new ePUBSpineItem(TryGetAttributeString(node, "idref"), TryGetAttributeString(node, "linear"));
                    pack.Spine.Items.Add(item);
                }

                //read <guide>
                listNode = root.SelectNodes("//ROOT:guide/ROOT:reference", nsmgr);
                foreach (XmlNode node in listNode)
                {
                    ePUBGuideItem item = new ePUBGuideItem()
                    {
                        hRef  = TryGetAttributeString(node, "href"),
                        Type  = TryGetAttributeString(node, "type"),
                        Title = TryGetAttributeString(node, "title")
                    };
                    pack.Guide.Items.Add(item);
                }

                ////if no cover search for an image
                //if (string.IsNullOrEmpty(pack.CoverId) || pack.ManifestItems.Count(p => p.Id == pack.CoverId) <= 0)
                //{
                //    IEnumerable<ManifestItem> img = pack.ManifestItems.Where(p => p.MediaType == "image/jpeg");
                //    if (img.Count() > 1)
                //    {
                //        foreach (ManifestItem item in img)
                //        {
                //            if (item.Id.Contains("cover") || item.Reference.Contains("cover"))
                //            {
                //                pack.CoverId = item.Id;
                //                break;
                //            }
                //        }
                //    }
                //    else
                //    {
                //        foreach (ManifestItem item in img)
                //        {
                //            pack.CoverId = item.Id;
                //            break;
                //        }
                //    }

                //}
            }
            catch (Exception err)
            {
                LogHelper.Manage("ePUBManager:ParsePackage", err);
            }
        }