/// <summary> /// Get RootElement of the current part. /// </summary> /// <param name="part">Current Part</param> /// <returns>Return Root element of the pass-in part</returns> public static OpenXmlPartRootElement RootElement(this OpenXmlPart part) { if (null == part) { throw new ArgumentNullException(nameof(part)); } if (part is CustomXmlPart) { XmlDocument xmlDoc = new XmlDocument(); using (var stream = part.GetStream()) xmlDoc.Load(stream); if (part.IsBibliographyPart()) { return(new Sources(xmlDoc.DocumentElement.OuterXml)); } else if (part.IsInkPart()) { return(new DocumentFormat.OpenXml.InkML.Ink(xmlDoc.DocumentElement.OuterXml)); } } var flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy; var properties = part.GetType().GetProperties(flag) .Where(p => p.PropertyType.IsSubclassOf(typeof(OpenXmlPartRootElement))); return(null == properties.FirstOrDefault() ? null : properties.First().GetValue(part, null) as OpenXmlPartRootElement); }
private static string GetPartNameAndUri(OpenXmlPart part) { Debug.Assert(part != null); string partClassName = part.GetType().Name; // Example: WordprocessingCommentsPart{/word/comments.xml} return(SR.Format("{0}{1}{2}{3}", partClassName, '{', part.Uri, '}')); }
public void ValidateValid(OpenXmlPart part) { var availability = part.GetType().GetCustomAttribute <OfficeAvailabilityAttribute>().OfficeVersion; var versions = Enum.GetValues(typeof(FileFormatVersions)) .Cast <FileFormatVersions>() .Where(v => v != FileFormatVersions.None); foreach (var version in versions) { Assert.Equal(version.AtLeast(availability), part.IsInVersion(version)); } }
/// <summary> /// Get RootElement of the current part. /// </summary> /// <param name="part">Current Part</param> /// <returns>Return Root element of the pass-in part</returns> public static OpenXmlPartRootElement RootElement(this OpenXmlPart part) { if (null == part) { throw new ArgumentNullException(nameof(part)); } if (part is CustomXmlPart) { using (var stream = part.GetStream()) { var xmlDoc = new XmlDocument(); xmlDoc.Load(stream); if (part.IsBibliographyPart()) { return(new Sources(xmlDoc.DocumentElement.OuterXml)); } else if (part.IsInkPart()) { return(new InkML.Ink(xmlDoc.DocumentElement.OuterXml)); } } } var flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy; var property = part.GetType().GetProperties(flag) .Where(p => p.PropertyType.IsSubclassOf(typeof(OpenXmlPartRootElement))) .FirstOrDefault(); if (property == null) { return(null); } try { return(property.GetValue(part, null) as OpenXmlPartRootElement); } catch (TargetInvocationException e) { throw e.InnerException; } }
/// <summary> /// Check if current part is reflectable (with xml content). /// </summary> /// <param name="part">Current part</param> /// <returns>return true if it has non-null root element, otherwise rturn false.</returns> public static bool IsReflectable(this OpenXmlPart part) { if (null == part) { throw new ArgumentNullException(nameof(part)); } if (part.IsBibliographyPart() || part.IsAdditionalCharacteristicsPart() || part.IsInkPart()) { return(true); } var flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy; var property = part.GetType().GetProperties(flag) .Where(p => p.PropertyType.IsSubclassOf(typeof(OpenXmlPartRootElement))) .FirstOrDefault(); return(null != property); }
private static ConstraintData GetConstraintData(OpenXmlPart part) => _cachedConstraintData.Value[GetName(part.GetType())];
private static string GetPartNameAndUri(OpenXmlPart part) { Debug.Assert(part != null); string partClassName = part.GetType().Name; // Example: WordprocessingCommentsPart{/word/comments.xml} return string.Format(CultureInfo.CurrentUICulture, "{0}{1}{2}{3}", partClassName, '{', part.Uri, '}'); }
private static ConstraintData GetConstraintData(OpenXmlPart part) => _cachedConstraintData.Value[part.GetType().FullName];
/// <summary> /// Converts an <see cref="OpenXmlPart"/> into a CodeDom object that can be used /// to build code in a given .NET language to build the referenced <see cref="OpenXmlPart"/>. /// </summary> /// <param name="part"> /// The <see cref="OpenXmlPart"/> object to generate source code for. /// </param> /// <param name="settings"> /// The <see cref="ISerializeSettings"/> to use during the code generation /// process. /// </param> /// <returns> /// A new <see cref="CodeCompileUnit"/> containing the instructions to build /// the referenced <see cref="OpenXmlPart"/>. /// </returns> public static CodeCompileUnit GenerateSourceCode(this OpenXmlPart part, ISerializeSettings settings) { CodeMethodReferenceExpression methodRef; OpenXmlPartBluePrint mainBluePrint; var result = new CodeCompileUnit(); var eType = part.GetType(); var partTypeName = eType.Name; var partTypeFullName = eType.FullName; var varName = eType.Name.ToCamelCase(); var partTypeCounts = new Dictionary <string, int>(); var namespaces = new SortedSet <string>(); var mainNamespace = new CodeNamespace(settings.NamespaceName); var bluePrints = new OpenXmlPartBluePrintCollection(); // Assign the appropriate variable name if (partTypeCounts.ContainsKey(partTypeFullName)) { varName = String.Concat(varName, partTypeCounts[partTypeFullName]++); } else { partTypeCounts.Add(partTypeFullName, 1); } // Generate a new blue print for the current part to help create the main // method reference then add it to the blue print collection mainBluePrint = new OpenXmlPartBluePrint(part, varName); bluePrints.Add(mainBluePrint); methodRef = new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), mainBluePrint.MethodName); // Build the entry method var entryMethod = new CodeMemberMethod() { Name = $"Create{partTypeName}", ReturnType = new CodeTypeReference(), Attributes = MemberAttributes.Public | MemberAttributes.Final }; entryMethod.Parameters.Add( new CodeParameterDeclarationExpression(partTypeName, methodParamName) { Direction = FieldDirection.Ref }); // Add all of the child part references here if (part.Parts != null) { var rootPartPair = new KeyValuePair <string, Type>(methodParamName, eType); foreach (var pair in part.Parts) { entryMethod.Statements.AddRange(BuildEntryMethodCodeStatements( pair, settings, partTypeCounts, namespaces, bluePrints, rootPartPair)); } } entryMethod.Statements.Add(new CodeMethodInvokeExpression(methodRef, new CodeArgumentReferenceExpression(methodParamName))); // Setup the main class next var mainClass = new CodeTypeDeclaration($"{eType.Name}BuilderClass") { IsClass = true, Attributes = MemberAttributes.Public }; mainClass.Members.Add(entryMethod); mainClass.Members.AddRange(BuildHelperMethods(bluePrints, settings, namespaces)); // Setup the imports var codeNameSpaces = new List <CodeNamespaceImport>(namespaces.Count); foreach (var ns in namespaces) { codeNameSpaces.Add(ns.GetCodeNamespaceImport(settings.NamespaceAliasOptions)); } codeNameSpaces.Sort(new CodeNamespaceImportComparer()); mainNamespace.Imports.AddRange(codeNameSpaces.ToArray()); mainNamespace.Types.Add(mainClass); // Finish up result.Namespaces.Add(mainNamespace); return(result); }
private static void CopyPartRecursive(OpenXmlPart destinationParent, OpenXmlPart part, string id, IDictionary <OpenXmlPart, OpenXmlPart> mapping) { if (part is IFixedContentTypePart) { OpenXmlPart parent = part.GetParentParts().Select(p => mapping.TryGetValue(p, out OpenXmlPart mappedParent) ? mappedParent : null).FirstOrDefault(p => p != null); if (mapping.TryGetValue(part, out OpenXmlPart existingPart)) { destinationParent.CreateRelationshipToPart(existingPart); } else { MethodInfo method = typeof(OpenXmlPart).GetTypeInfo().GetMethod(nameof(OpenXmlPart.AddNewPart), new Type[] { typeof(string) }).MakeGenericMethod(part.GetType()); OpenXmlPart newPart = method.Invoke(parent, new object[] { id }) as OpenXmlPart; mapping.Add(part, newPart); using (Stream data = part.GetStream()) { newPart.FeedData(data); } foreach (IdPartPair relationship in part.Parts) { CopyPartRecursive(newPart, relationship.OpenXmlPart, relationship.RelationshipId, mapping); } } } }