Ejemplo n.º 1
0
 internal override bool IsInVersion(FileFormatVersions version) => version.AtLeast(FileFormatVersions.Office2007);
Ejemplo n.º 2
0
 public void AtLeastExceptions(FileFormatVersions version)
 {
     Assert.Throws <ArgumentOutOfRangeException>(nameof(version), () => version.AtLeast(FileFormatVersions.Office2007));
     Assert.Throws <ArgumentOutOfRangeException>("minimum", () => FileFormatVersions.Office2007.AtLeast(version));
 }
Ejemplo n.º 3
0
 public void CheckAtLeast(FileFormatVersions version, FileFormatVersions minimum, bool expected)
 {
     Assert.Equal(expected, version.AtLeast(minimum));
 }
Ejemplo n.º 4
0
        /// <inheritdoc />
        internal override bool IsInVersion(FileFormatVersions fileFormat)
        {
            Debug.Assert(HasValue);

            return(fileFormat.AtLeast(EnumInfoLookup <T> .GetVersion(Value)));
        }
Ejemplo n.º 5
0
 internal override bool IsInVersion(FileFormatVersions version)
 {
     return(version.AtLeast(FileFormatVersions.Office2013));
 }
Ejemplo n.º 6
0
        private static IEnumerable <OpenXmlElement> ValidatingTraverse(OpenXmlElement inElement, MCContext mcContext, FileFormatVersions version)
        {
            var stack = new Stack <OpenXmlElement>();

            stack.Push(inElement);

            while (stack.Count > 0)
            {
                var element = stack.Pop();

                if (element is null)
                {
                    mcContext.PopMCAttributes2();
                    continue;
                }

                // 1. Skip elements in ProcessContent.
                // 2. Select correct Choice / Fallback
                // Need bookkeep MC context
                // Need to collect MC context from ancestor

                // bookkeep MC context,
                // MC Spec: Compatibility-rule attributes shall affect the element to which they 1 are attached, including the element’s other attributes and contents.
                mcContext.PushMCAttributes2(element.MCAttributes, element.LookupNamespace);
                stack.Push(null);

                if (element.IsStrongTypedElement())
                {
                    // only call validate on elements that defined in the format.
                    if (version.AtLeast(element.InitialVersion))
                    {
                        yield return(element);
                    }
                    else if (mcContext.IsProcessContent(element))
                    {
                        // do not validate this element.
                    }

                    foreach (var child in element.ChildElements)
                    {
                        stack.Push(child);
                    }
                }
                else if (element.IsUnknown())
                {
                    // TODO: Issue: all types are weak types now, need to change the Framework to load strong typed elements!!!
                    if (mcContext.IsProcessContent(element))
                    {
                        // do validating on children elements.
                        foreach (var child in element.ChildElements)
                        {
                            stack.Push(child);
                        }
                    }
                }
                else if (element.IsAlternateContent())
                {
                    yield return(element);

                    var selectedContent = mcContext.GetContentFromACBlock((AlternateContent)element, version);

                    if (selectedContent != null)
                    {
                        foreach (var child in selectedContent.ChildElements)
                        {
                            stack.Push(child);
                        }
                    }
                }
                else if (element.IsMiscNode())
                {
                    // non-element node
                    // just skip
                }
                else
                {
                    Debug.Assert(element is AlternateContentChoice || element is AlternateContentFallback);
                    Debug.Assert(element.Parent != null && element.Parent is AlternateContent);

                    // should not be here, otherwise, wrong case ( the parent is not AlternateContent).
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Validates the package (do not validate the xml content in each part).
        /// </summary>
        /// <param name="container">The Open XML container to validate</param>
        /// <param name="version">Version to validate against</param>
        /// <param name="processedParts">Parts already processed.</param>
        private IEnumerable<OpenXmlPackageValidationEventArgs> ValidateInternal(OpenXmlPartContainer container, FileFormatVersions version, Dictionary<OpenXmlPart, bool> processedParts)
        {
            foreach (var result in ValidateDataPartReferenceRelationships(container, version))
            {
                yield return result;
            }

            // count all parts of same type
            var partOccurs = new Dictionary<string, int>(StringComparer.Ordinal);

            foreach (var part in container.ChildrenRelationshipParts.Values)
            {
                if (partOccurs.TryGetValue(part.RelationshipType, out int occurs))
                {
                    partOccurs[part.RelationshipType] = occurs + 1;
                }
                else
                {
                    partOccurs.Add(part.RelationshipType, 1);
                }

                if (!(container is ExtendedPart) &&
                    !container.GetPartMetadata().PartConstraints.ContainsRelationship(part.RelationshipType) &&
                    part.IsInVersion(version))
                {
                    yield return new OpenXmlPackageValidationEventArgs(container)
                    {
                        MessageId = "PartIsNotAllowed",
                        PartClassName = part.RelationshipType,
                        Part = container.ThisOpenXmlPart,
                        SubPart = part,
                    };
                }

                // if the part is not defined in this version, then should not report error, just treat it as ExtendedPart.
            }

            foreach (var constraintRulePair in container.GetPartMetadata().PartConstraints)
            {
                var relatinshipType = constraintRulePair.Key;
                var constraintRule = constraintRulePair.Value;

                // validate the required parts
                if (constraintRule.MinOccursIsNonZero

                    // only check rules apply to the specified version.
                    && version.AtLeast(constraintRule.FileFormat))
                {
                    // must have one
                    if (container.GetSubPart(relatinshipType) is null)
                    {
                        yield return new OpenXmlPackageValidationEventArgs(container)
                        {
                            MessageId = "RequiredPartDoNotExist",
                            PartClassName = constraintRule.PartClassName,
                            Part = container.ThisOpenXmlPart,
                        };
                    }
                }

                // check for parts MaxOccursGreatThanOne=false, but do have multiple instance
                if (!constraintRule.MaxOccursGreatThanOne

                    // only check rules apply to the specified version.
                    && version.AtLeast(constraintRule.FileFormat))
                {
                    if (partOccurs.TryGetValue(relatinshipType, out int occurs))
                    {
                        if (occurs > 1)
                        {
                            yield return new OpenXmlPackageValidationEventArgs(container)
                            {
                                MessageId = "OnlyOnePartAllowed",
                                PartClassName = constraintRule.PartClassName,
                                Part = container.ThisOpenXmlPart,
#if DEBUG
                                SubPart = container.GetSubPart(relatinshipType),
#endif
                            };
                        }
                    }
                }
            }

            foreach (var part in container.ChildrenRelationshipParts.Values)
            {
                if (!processedParts.ContainsKey(part))
                {
                    if (!(part is ExtendedPart))
                    {
                        if (container.GetPartMetadata().PartConstraints.TryGetValue(part.RelationshipType, out var rule))
                        {
                            if (version.AtLeast(rule.FileFormat))
                            {
                                // validate content type
                                if (rule.PartContentType is not null && part.ContentType != rule.PartContentType)
                                {
                                    var message = SR.Format(ExceptionMessages.InvalidContentTypePart, rule.PartContentType);

                                    yield return new OpenXmlPackageValidationEventArgs(container)
                                    {
                                        Message = message,
                                        MessageId = "InvalidContentTypePart",
                                        SubPart = part,
                                        Part = container.ThisOpenXmlPart,
                                    };
                                }
                            }
                            else
                            {
                                // if the part is not defined in this version, then should not report error, just treat it as ExtendedPart.
                            }
                        }
                    }
#if DEBUG
                    else
                    {
                        // check the relationship type
                        if (part.RelationshipType.StartsWith(@"http://schemas.openxmlformats.org", StringComparison.OrdinalIgnoreCase))
                        {
                            yield return new OpenXmlPackageValidationEventArgs(container)
                            {
                                MessageId = "ExtendedPartIsOpenXmlPart",
                                SubPart = part,
                                Part = container.ThisOpenXmlPart,
                            };
                        }
                    }
#endif
                    processedParts.Add(part, true);

                    foreach (var result in ValidateInternal(part, version, processedParts))
                    {
                        yield return result;
                    }
                }
            }
        }
Ejemplo n.º 8
0
 public virtual ParticleConstraint Build(FileFormatVersions version)
 => version.AtLeast(Version) ? this : null;