コード例 #1
0
ファイル: FB2File.cs プロジェクト: BigDaddy1337/DocsReader
        private void LoadDescriptionSection(XDocument fileDocument, bool loadBinaryItems = true)
        {
            if (fileDocument == null)
            {
                throw new ArgumentNullException("fileDocument");
            }
            if (fileDocument.Root == null)
            {
                throw new NullReferenceException("LoadDescriptionSection: Root is null");
            }
            XElement xTextDescription = fileDocument.Root.Element(_fileNameSpace + Fb2TextDescriptionElementName);
            // attempt to load some bad FB2 with wrong namespace
            XNamespace namespaceUsed = _fileNameSpace;
            if (xTextDescription == null)
            {
                namespaceUsed = "";
                xTextDescription = fileDocument.Root.Element(Fb2TextDescriptionElementName);
            }
            if (xTextDescription != null)
            {
                // Load Title info 
                XElement xTitleInfo = xTextDescription.Element(namespaceUsed + TitleInfoElementName);
                if (xTitleInfo != null)
                {
                    _titleInfo.Namespace = namespaceUsed;
                    try
                    {
                        _titleInfo.Load(xTitleInfo);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error reading title info : {0}", ex.Message);
                    }

                }

                // Load Src Title info 
                XElement xSrcTitleInfo = xTextDescription.Element(namespaceUsed + SrcTitleInfoElementName);
                if (xSrcTitleInfo != null)
                {
                    _srcTitleInfo.Namespace = _fileNameSpace;
                    try
                    {
                        _srcTitleInfo.Load(xSrcTitleInfo);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error reading source title info : {0}", ex.Message);
                    }
                }

                // Load document info
                XElement xDocumentInfo = xTextDescription.Element(namespaceUsed + DocumentInfoElementName);
                if (xDocumentInfo != null)
                {
                    _documentInfo.Namespace = _fileNameSpace;
                    try
                    {
                        _documentInfo.Load(xDocumentInfo);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error reading document info : {0}", ex.Message);
                    }
                }

                // Load publish info 
                XElement xPublishInfo = xTextDescription.Element(namespaceUsed + ItemPublishInfo.PublishInfoElementName);
                if (xPublishInfo != null)
                {
                    _publishInfo.Namespace = _fileNameSpace;
                    try
                    {
                        _publishInfo.Load(xPublishInfo);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error reading publishing info : {0}", ex.Message);
                    }
                }

                XElement xCustomInfo = xTextDescription.Element(namespaceUsed + ItemCustomInfo.CustomInfoElementName);
                if (xCustomInfo != null)
                {
                    var custElement = new ItemCustomInfo {Namespace = _fileNameSpace};
                    try
                    {
                        custElement.Load(xCustomInfo);
                        _customInfo.Add(custElement);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error reading custom info : {0}", ex.Message);
                    }
                }

                IEnumerable<XElement> xInstructions = xTextDescription.Elements(xTextDescription.Name.Namespace + "output");
                int outputCount = 0;
                _output.Clear();
                foreach (var xInstruction in xInstructions)
                {
                    // only two elements allowed by scheme
                    if (outputCount > 1)
                    {
                        break;
                    }
                    var outp = new ShareInstructionType { Namespace = namespaceUsed };
                    try
                    {
                        outp.Load(xInstruction);
                        _output.Add(outp);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error reading output instructions : {0}", ex);
                    }
                    finally
                    {
                        outputCount++;
                    }
                }

                if (loadBinaryItems && _titleInfo.Cover != null)
                {

                    foreach (InlineImageItem coverImag in _titleInfo.Cover.CoverpageImages)
                    {
                        if (string.IsNullOrEmpty(coverImag.HRef))
                        {
                            continue;
                        }
                        string coverref = coverImag.HRef.Substring(0, 1) == "#" ? coverImag.HRef.Substring(1) : coverImag.HRef;
                        IEnumerable<XElement> xBinaryes =
                            fileDocument.Root.Elements(_fileNameSpace + Fb2BinaryElementName).Where(
                                cov => ((cov.Attribute("id") != null) && (cov.Attribute("id").Value == coverref)));
                        foreach (var binarye in xBinaryes)
                        {
                            var item = new BinaryItem();
                            try
                            {
                                item.Load(binarye);
                            }
                            catch (Exception)
                            {

                                continue;
                            }
                            // add just unique IDs to fix some invalid FB2s 
                            if (!_binaryObjects.ContainsKey(item.Id))
                            {
                                _binaryObjects.Add(item.Id, item);
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void Load(XElement xElement)
        {
            if (xElement == null)
            {
                throw new ArgumentNullException("xElement");
            }

            parts.Clear();
            IEnumerable<XElement> xParts = xElement.Elements(fileNameSpace + "part");
            foreach (var xPart in xParts)
            {
                ShareInstructionType part = new ShareInstructionType {Namespace = fileNameSpace};
                try
                {
                    part.Load(xPart);
                    parts.Add(part);
                }
                catch (Exception)
                {
                    continue;
                }
            }

            Name = null;
            XAttribute xName = xElement.Attribute("name");
            if (xName == null)
            {
                throw new Exception("name is required attribute - absent");
            }
            Name = xName.Value;

            Create = GenerationInstructionEnum.Unknown;
            XAttribute xCreate = xElement.Attribute("create");
            if (xCreate != null)
            {
                switch (xCreate.Value)
                {
                    case "require":
                        Create = GenerationInstructionEnum.Require;
                        break;
                    case "allow":
                        Create = GenerationInstructionEnum.Allow;
                        break;
                    case "deny":
                        Create = GenerationInstructionEnum.Deny;
                        break;
                    default:
                        Debug.Fail(string.Format("Invalid instruction type : {0}", xCreate.Value));
                        break;
                }
            }

            Price = null;
            XAttribute xPrice = xElement.Attribute("price");
            if ((xPrice != null) && !string.IsNullOrEmpty(xPrice.Value))
            {
                float val;
                if (float.TryParse(xPrice.Value, out val))
                {
                    Price = val;
                }
            }
        }
コード例 #3
0
        public void Load(XElement xElement)
        {
            if (xElement == null)
            {
                throw new ArgumentNullException(nameof(xElement));
            }

            _parts.Clear();
            IEnumerable <XElement> xParts = xElement.Elements(_fileNameSpace + "part");

            foreach (var xPart in xParts)
            {
                ShareInstructionType part = new ShareInstructionType {
                    Namespace = _fileNameSpace
                };
                try
                {
                    part.Load(xPart);
                    _parts.Add(part);
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            Name = null;
            XAttribute xName = xElement.Attribute("name");

            if (xName == null)
            {
                throw new Exception("name is required attribute - absent");
            }
            Name = xName.Value;


            Create = GenerationInstructionEnum.Unknown;
            XAttribute xCreate = xElement.Attribute("create");

            if (xCreate != null)
            {
                switch (xCreate.Value)
                {
                case "require":
                    Create = GenerationInstructionEnum.Require;
                    break;

                case "allow":
                    Create = GenerationInstructionEnum.Allow;
                    break;

                case "deny":
                    Create = GenerationInstructionEnum.Deny;
                    break;

                default:
                    Debug.WriteLine($"Invalid instruction type : {xCreate.Value}");
                    break;
                }
            }

            Price = null;
            XAttribute xPrice = xElement.Attribute("price");

            if (xPrice != null && !string.IsNullOrEmpty(xPrice.Value))
            {
                if (float.TryParse(xPrice.Value, out var val))
                {
                    Price = val;
                }
            }
        }