Ejemplo n.º 1
0
        public Fooder Get(PackageRelationship relationship, FooderType fooderType, FooderDesignation fooderDesignation)
        {
            var packagePartUri = PackagePartHelper.EnsureCorrectUri(relationship.TargetUri);

            if (fooders.ContainsKey(packagePartUri))
            {
                var cachedFooder = fooders[packagePartUri];

                return(cachedFooder.FooderType == fooderType
                                        ? cachedFooder
                                        : throw new Exception($"Requested fooder is not of type {fooderType}"));
            }

            var packagePart = package.GetPart(relationship);

            var document = PackagePartHelper.ReadPackagePart(packagePart);

            var fooder = fooderType switch
            {
                FooderType.Header => (Fooder) new Header(packagePart, document, relationship, fooderDesignation, wordDocument),
                FooderType.Footer => new Footer(packagePart, document, relationship, fooderDesignation, wordDocument),
                _ => throw new ArgumentOutOfRangeException(nameof(fooderType))
            };

            fooders[packagePartUri] = fooder;
            return(fooder);
        }
Ejemplo n.º 2
0
        public Fooder Create(FooderType fooderType, FooderDesignation fooderDesignation)
        {
            var(part, relationship) = fooderType switch
            {
                FooderType.Header => package.AddHeaderPart(),
                FooderType.Footer => package.AddFooterPart(),
                _ => throw new ArgumentOutOfRangeException(nameof(fooderType))
            };

            var document = PackagePartHelper.ReadPackagePart(part);

            Fooder fooder = fooderType switch
            {
                FooderType.Header => new Header(part, document, relationship, fooderDesignation, wordDocument),
                FooderType.Footer => new Footer(part, document, relationship, fooderDesignation, wordDocument),
                _ => throw new ArgumentOutOfRangeException(nameof(fooderType))
            };

            var packagePartUri = PackagePartHelper.EnsureCorrectUri(relationship.TargetUri);

            fooders[packagePartUri] = fooder;

            EnsureDefaultStyles(fooderType);

            return(fooder);
        }
Ejemplo n.º 3
0
 protected Fooder(FooderType fooderType, PackagePart packagePart, XDocument xDocument, PackageRelationship relationship, FooderDesignation designation,
                  WordDocument document)
     : base(xDocument.Root ?? throw new Exception("Header or footer root must not be null"), packagePart, document)
 {
     this.xDocument = xDocument;
     FooderType     = fooderType;
     Relationship   = relationship;
     Designation    = designation;
 }
Ejemplo n.º 4
0
        private PackageRelationship GetFooderRelationship(FooderType fooderType, string relationshipId)
        {
            var relationshipType = fooderType == FooderType.Header ? RelationshipTypes.Header : RelationshipTypes.Footer;
            var relationship     = PackagePart.GetRelationship(relationshipId);

            if (relationship.RelationshipType != relationshipType)
            {
                throw new Exception($"Relationship {relationshipId} type does not match {fooderType}");
            }

            return(relationship);
        }
Ejemplo n.º 5
0
        private T?FindFooderOfType <T>(FooderType fooderType, FooderDesignation fooderDesignation) where T : Fooder
        {
            var keyword          = fooderType == FooderType.Header ? "header" : "footer";
            var referenceElement = FindFooderReferenceElement(keyword, fooderDesignation);

            if (referenceElement == null)
            {
                return(null);
            }

            var relationshipId = GetRelationshipId(keyword, fooderDesignation, referenceElement);
            var relationship   = GetFooderRelationship(fooderType, relationshipId);
            var fooder         = document.FooderCollection.Get(relationship, fooderType, fooderDesignation);

            return(fooder as T);
        }
Ejemplo n.º 6
0
        private void EnsureDefaultStyles(FooderType fooderType)
        {
            switch (fooderType)
            {
            case FooderType.Header:
                wordDocument.Styles.EnsureStyle("Header", StyleType.Paragraph);
                wordDocument.Styles.EnsureStyle("HeaderChar", StyleType.Character);
                break;

            case FooderType.Footer:
                wordDocument.Styles.EnsureStyle("Footer", StyleType.Paragraph);
                wordDocument.Styles.EnsureStyle("FooterChar", StyleType.Character);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(fooderType));
            }
        }
Ejemplo n.º 7
0
        private T CreateFooder <T>(FooderType fooderType, FooderDesignation fooderDesignation) where T : Fooder
        {
            var referenceElementName = fooderType == FooderType.Header ? "headerReference" : "footerReference";

            var typeAttributeValue = fooderDesignation switch
            {
                FooderDesignation.Default => "default",
                FooderDesignation.First => "first",
                FooderDesignation.Even => "even",
                _ => throw new ArgumentOutOfRangeException(nameof(fooderDesignation), $"Unsupported header or footer type {fooderDesignation}")
            };

            var fooder         = document.FooderCollection.Create(fooderType, fooderDesignation);
            var relationshipId = fooder.Relationship.Id;

            Xml.Add(new XElement(Namespaces.w + referenceElementName, new XAttribute(Namespaces.w + "type", typeAttributeValue),
                                 new XAttribute(Namespaces.r + "id", relationshipId)));

            return((T)fooder);
        }