public LookupData(Type type) { Element = type.FullName; ElementLookup GetLookup() { if (typeof(OpenXmlPartRootElement) == type) { return(ElementLookup.Parts); } else if (type.GetConstructor(Cached.Array <Type>()) != null) { var instance = (OpenXmlElement)Activator.CreateInstance(type); return(instance.Metadata.Children); } else { return(ElementLookup.Empty); } } Children = GetLookup().Elements.Select(t => new ChildData { Name = t.Name, Namespace = t.Namespace, }); }
private PartHolder <ICollection <string> > GetReferencedAttributes(ValidationContext context) { var part = GetReferencedPart(context, _partPath); if (part?.RootElement is null) { return(new PartHolder <ICollection <string> >(Cached.Array <string>(), part)); } var result = context.State.Get(new { part.Uri, _partPath, _element, _attribute }, () => { var referencedAttributes = new HashSet <string>(StringComparer.Ordinal); foreach (var element in part.RootElement.Descendants(context.FileFormat, TraversalOptions.SelectAlternateContent)) { if (element.GetType() == _element) { var attribute = element.ParsedState.Attributes[_attribute]; //Attributes whose value is empty string or null don't need to be cached. if (attribute.HasValue && !string.IsNullOrEmpty(attribute.Value.InnerText)) { referencedAttributes.Add(attribute.Value.InnerText); } } } return(referencedAttributes); }); return(new PartHolder <ICollection <string> >(result, part)); }
public LookupData(Type type) { Element = type.FullName; ElementFactoryCollection GetLookup() { if (typeof(OpenXmlPartRootElement) == type) { return(FeatureCollection.Default.GetRequired <IRootElementFactory>().Collection); } else if (type.GetConstructor(Cached.Array <Type>()) is not null) { var instance = (OpenXmlElement)Activator.CreateInstance(type); return(instance.Metadata.Children); } else { return(ElementFactoryCollection.Empty); } } Children = GetLookup().Elements.Select(t => new ChildData { Name = t.QName.Name, Namespace = t.QName.Namespace.Uri, }); }
private static byte[] FromChunkedBase64String(string?chunkedBase64String) { if (chunkedBase64String is null) { return(Cached.Array <byte>()); } char[] base64CharArray = chunkedBase64String.Where(c => c != '\r' && c != '\n').ToArray(); return(Convert.FromBase64CharArray(base64CharArray, 0, base64CharArray.Length)); }
public AttributeCollection(ReadOnlyArray <AttributeMetadata> tags) { _attributes = tags; if (tags.Length == 0) { _data = Cached.Array <OpenXmlSimpleType>(); } else { _data = new OpenXmlSimpleType[tags.Length]; } }
public void ValidateExpectedParticles() { var exclude = new HashSet <Type> { typeof(OpenXmlUnknownElement), typeof(OpenXmlMiscNode), }; var elements = typeof(OpenXmlElement).GetTypeInfo().Assembly.GetTypes() .Where(t => !t.GetTypeInfo().IsAbstract&& typeof(OpenXmlElement).IsAssignableFrom(t)) .Where(t => !exclude.Contains(t)); var constraints = new Dictionary <Type, VersionCollection <ParticleConstraint> >(); foreach (var version in FileFormatVersionExtensions.AllVersions) { foreach (var type in elements) { var constructor = type.GetConstructor(Cached.Array <Type>()); if (constructor is not null) { var element = (OpenXmlElement)Activator.CreateInstance(type); if (version.AtLeast(element.InitialVersion)) { var constraint = element.Metadata.Particle.Particle?.Build(version); if (constraint is not null) { if (constraints.TryGetValue(type, out var current)) { current.Add(version, constraint); } else { constraints.Add(type, new VersionCollection <ParticleConstraint> { { version, constraint } }); } } } } } } AssertEqual(constraints); }
public static IEnumerable <OpenXmlElement> Descendants(this OpenXmlElement element, FileFormatVersions version = FileFormatVersions.Office2007, TraversalOptions options = TraversalOptions.None) { if (element is null) { return(Cached.Array <OpenXmlElement>()); } if ((options & TraversalOptions.SelectAlternateContent) == TraversalOptions.SelectAlternateContent) { return(ValidatingTraverse(element, new MCContext(false), version)); } else { return(element.Descendants()); } }
/// <summary> /// Load the particle constraint from the specified data in binary database. /// </summary> /// <param name="particleIndex">The index of the particle constraint data in the binary database.</param> /// <returns>The particle constraint in ParticleConstraint.</returns> private ParticleConstraint BuildParticleConstraint(ushort particleIndex) { Debug.Assert(particleIndex >= 0); Debug.Assert(particleIndex < Particles.Length); SdbParticleConstraint sdbParticleConstraint = Particles[particleIndex]; var particleConstraint = ParticleConstraint.CreateParticleConstraint(sdbParticleConstraint.ParticleType); particleConstraint.ParticleType = sdbParticleConstraint.ParticleType; particleConstraint.MaxOccurs = sdbParticleConstraint.MaxOccurs; particleConstraint.MinOccurs = sdbParticleConstraint.MinOccurs; particleConstraint.ElementId = sdbParticleConstraint.ElementTypeId; if (sdbParticleConstraint.ChildrenCount > 0) { Debug.Assert(sdbParticleConstraint.ParticleType == ParticleType.All || sdbParticleConstraint.ParticleType == ParticleType.Choice || sdbParticleConstraint.ParticleType == ParticleType.Group || sdbParticleConstraint.ParticleType == ParticleType.Sequence); particleConstraint.ChildrenParticles = new ParticleConstraint[sdbParticleConstraint.ChildrenCount]; for (ushort i = 0; i < sdbParticleConstraint.ChildrenCount; i++) { ushort childIndex = ParticleIndexes[(ushort)(sdbParticleConstraint.ChildrenStartIndex + i)].ParticleIndex; particleConstraint.ChildrenParticles[i] = BuildParticleConstraint(childIndex); } } else if (sdbParticleConstraint.ParticleType == ParticleType.All || sdbParticleConstraint.ParticleType == ParticleType.Choice || sdbParticleConstraint.ParticleType == ParticleType.Group || sdbParticleConstraint.ParticleType == ParticleType.Sequence) { particleConstraint.ChildrenParticles = Cached.Array <ParticleConstraint>(); } return(particleConstraint); }
protected IValidator[] GetValidators() => _validators?.ToArray() ?? Cached.Array <IValidator>();
public void ExpectedKind() { var elements = typeof(OpenXmlElement).GetTypeInfo().Assembly.GetTypes() .Where(t => !t.GetTypeInfo().IsAbstract&& typeof(OpenXmlCompositeElement).IsAssignableFrom(t) && t.GetConstructor(Cached.Array <Type>()) != null) .Select(t => { var element = (OpenXmlCompositeElement)Activator.CreateInstance(t); return(new ParticleInfo { Name = t.FullName, Type = element.OpenXmlCompositeType, }); }) .Where(p => p.Type != OpenXmlCompositeType.Other) .OrderBy(o => o.Name); #if DEBUG var tmp = Path.GetTempFileName(); _output.WriteLine($"Wrote to temp path {tmp}"); File.WriteAllText(tmp, JsonConvert.SerializeObject(elements, Formatting.Indented, new StringEnumConverter())); #endif var expected = GetData <IEnumerable <ParticleInfo> >("CompositeParticleTypes"); CollectionAssert(expected, elements); }
protected IOpenXmlSimpleTypeValidator[] GetValidators() => _validators?.ToArray() ?? Cached.Array <IOpenXmlSimpleTypeValidator>();