/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Xml; using System.Xml.Serialization; using DotCMIS.CMISWebServicesReference; using DotCMIS.Data; using DotCMIS.Data.Extensions; using DotCMIS.Data.Impl; using DotCMIS.Enums; using DotCMIS.Exceptions; namespace DotCMIS.Binding { internal class Converter { private const string CMISNamespaceURI = "http://docs.oasis-open.org/ns/cmis/core/200908/"; private delegate void ProcessAnyElement(XmlElement element); /// /// Walks through CMIS Any elements. /// private static void ProcessAnyElements(XmlElement[] any, ProcessAnyElement proc) { try { if (any != null) { foreach (XmlElement element in any) { if (element.NamespaceURI.Equals(CMISNamespaceURI) && element.FirstChild != null) { proc(element); } } } } catch (Exception e) { throw new CmisRuntimeException(e.Message, e); } } /// /// Deserializes a string. /// private static string DeserializeString(XmlElement element) { return element.FirstChild.Value; } /// /// Deserializes an integer. /// private static long? DeserializeInteger(XmlElement element) { string s = DeserializeString(element); return s == null ? (long?)null : Int64.Parse(s); } /// /// Deserializes a decimal. /// private static decimal? DeserializeDecimal(XmlElement element) { string s = DeserializeString(element); return s == null ? (decimal?)null : Decimal.Parse(s); } /// /// Deserializes a boolean. /// private static Boolean DeserializeBoolean(XmlElement element) { return "true".Equals(DeserializeString(element), StringComparison.OrdinalIgnoreCase); } /// /// Deserializes an enum. /// private static T DeserializeEnum(XmlElement element) { return CmisValue.GetCmisEnum(DeserializeString(element)); } /// /// Deserializes an elemenet. /// private static T DeserializeElement(XmlElement element) { XmlRootAttribute xmlRoot = new XmlRootAttribute(element.LocalName); xmlRoot.Namespace = CMISNamespaceURI; XmlSerializer s = new XmlSerializer(typeof(T), xmlRoot); return (T)s.Deserialize(new XmlNodeReader(element)); } /// /// Converts a repository info. /// public static IRepositoryInfo Convert(cmisRepositoryInfoType repositotyInfo) { if (repositotyInfo == null) { return null; } RepositoryInfo result = new RepositoryInfo(); result.Id = repositotyInfo.repositoryId; result.Name = repositotyInfo.repositoryName; result.Description = repositotyInfo.repositoryDescription; result.VendorName = repositotyInfo.vendorName; result.ProductName = repositotyInfo.productName; result.ProductVersion = repositotyInfo.productVersion; result.RootFolderId = repositotyInfo.rootFolderId; result.Capabilities = Convert(repositotyInfo.capabilities); result.AclCapabilities = Convert(repositotyInfo.aclCapability); result.LatestChangeLogToken = repositotyInfo.latestChangeLogToken; result.CmisVersionSupported = repositotyInfo.cmisVersionSupported; result.ThinClientUri = repositotyInfo.thinClientURI; result.ChangesIncomplete = (repositotyInfo.changesIncompleteSpecified ? (bool?)repositotyInfo.changesIncomplete : null); result.ChangesOnType = new List(); if (repositotyInfo.changesOnType != null) { foreach (enumBaseObjectTypeIds baseType in repositotyInfo.changesOnType) { result.ChangesOnType.Add((BaseTypeId?)CmisValue.SerializerToCmisEnum(baseType)); } } result.PrincipalIdAnonymous = repositotyInfo.principalAnonymous; result.PrincipalIdAnyone = repositotyInfo.principalAnyone; ConvertExtension(repositotyInfo, result); return result; } /// /// Converts repository capabilities. /// public static IRepositoryCapabilities Convert(cmisRepositoryCapabilitiesType capabilities) { if (capabilities == null) { return null; } RepositoryCapabilities result = new RepositoryCapabilities(); result.ContentStreamUpdatesCapability = (CapabilityContentStreamUpdates)CmisValue.SerializerToCmisEnum(capabilities.capabilityContentStreamUpdatability); result.ChangesCapability = (CapabilityChanges)CmisValue.SerializerToCmisEnum(capabilities.capabilityChanges); result.RenditionsCapability = (CapabilityRenditions)CmisValue.SerializerToCmisEnum(capabilities.capabilityRenditions); result.IsGetDescendantsSupported = capabilities.capabilityGetDescendants; result.IsGetFolderTreeSupported = capabilities.capabilityGetFolderTree; result.IsMultifilingSupported = capabilities.capabilityMultifiling; result.IsUnfilingSupported = capabilities.capabilityUnfiling; result.IsVersionSpecificFilingSupported = capabilities.capabilityVersionSpecificFiling; result.IsPwcSearchableSupported = capabilities.capabilityPWCSearchable; result.IsPwcUpdatableSupported = capabilities.capabilityPWCUpdatable; result.IsAllVersionsSearchableSupported = capabilities.capabilityAllVersionsSearchable; result.QueryCapability = (CapabilityQuery)CmisValue.SerializerToCmisEnum(capabilities.capabilityQuery); result.JoinCapability = (CapabilityJoin)CmisValue.SerializerToCmisEnum(capabilities.capabilityJoin); result.AclCapability = (CapabilityAcl)CmisValue.SerializerToCmisEnum(capabilities.capabilityACL); ConvertExtension(capabilities, result); return result; } /// /// Converts ACL capabilities. /// public static IAclCapabilities Convert(cmisACLCapabilityType capabilities) { if (capabilities == null) { return null; } AclCapabilities result = new AclCapabilities(); result.SupportedPermissions = (SupportedPermissions)CmisValue.SerializerToCmisEnum(capabilities.supportedPermissions); result.AclPropagation = (AclPropagation)CmisValue.SerializerToCmisEnum(capabilities.propagation); result.Permissions = new List(); if (capabilities.permissions != null) { foreach (cmisPermissionDefinition permission in capabilities.permissions) { PermissionDefinition permDef = new PermissionDefinition(); permDef.Id = permission.permission; permDef.Description = permission.description; result.Permissions.Add(permDef); ConvertExtension(permission, permDef); } } result.PermissionMapping = new Dictionary(); if (capabilities.mapping != null) { foreach (cmisPermissionMapping mapping in capabilities.mapping) { PermissionMapping permMap = new PermissionMapping(); permMap.Key = GetXmlEnumAttributeValue(mapping.key); permMap.Permissions = new List(); if (mapping.permission != null) { foreach (string s in mapping.permission) { permMap.Permissions.Add(s); } } result.PermissionMapping[permMap.Key] = permMap; ConvertExtension(mapping, permMap); } } ConvertExtension(capabilities, result); return result; } public static ITypeDefinition Convert(cmisTypeDefinitionType typeDef) { if (typeDef == null) { return null; } AbstractTypeDefinition result = null; if (typeDef is cmisTypeDocumentDefinitionType) { DocumentTypeDefinition docType = new DocumentTypeDefinition(); cmisTypeDocumentDefinitionType docTypeDef = (cmisTypeDocumentDefinitionType)typeDef; docType.IsVersionable = docTypeDef.versionable; docType.ContentStreamAllowed = (ContentStreamAllowed)CmisValue.SerializerToCmisEnum(docTypeDef.contentStreamAllowed); ProcessAnyElements(docTypeDef.Any, (element) => { if (element.LocalName.Equals("versionable")) { docType.IsVersionable = DeserializeBoolean(element); } else if (element.LocalName.Equals("contentStreamAllowed")) { docType.ContentStreamAllowed = DeserializeEnum(element); } }); result = docType; } else if (typeDef is cmisTypeFolderDefinitionType) { result = new FolderTypeDefinition(); } else if (typeDef is cmisTypePolicyDefinitionType) { result = new PolicyTypeDefinition(); } else if (typeDef is cmisTypeRelationshipDefinitionType) { RelationshipTypeDefinition relType = new RelationshipTypeDefinition(); cmisTypeRelationshipDefinitionType relTypeDef = (cmisTypeRelationshipDefinitionType)typeDef; if (relTypeDef.allowedSourceTypes != null) { relType.AllowedSourceTypeIds = new List(); foreach (string id in relTypeDef.allowedSourceTypes) { relType.AllowedSourceTypeIds.Add(id); } } if (relTypeDef.allowedTargetTypes != null) { relType.AllowedTargetTypeIds = new List(); foreach (string id in relTypeDef.allowedTargetTypes) { relType.AllowedTargetTypeIds.Add(id); } } ProcessAnyElements(relTypeDef.Any, (element) => { if (element.LocalName.Equals("allowedSourceTypes")) { if (relType.AllowedSourceTypeIds == null) { relType.AllowedSourceTypeIds = new List(); } relType.AllowedSourceTypeIds.Add(element.FirstChild.Value); } else if (element.LocalName.Equals("allowedTargetTypes")) { if (relType.AllowedTargetTypeIds == null) { relType.AllowedTargetTypeIds = new List(); } relType.AllowedTargetTypeIds.Add(element.FirstChild.Value); } }); result = relType; } result.Id = typeDef.id; result.LocalName = typeDef.localName; result.LocalNamespace = typeDef.localNamespace; result.DisplayName = typeDef.displayName; result.QueryName = typeDef.queryName; result.Description = typeDef.description; result.BaseTypeId = (BaseTypeId)CmisValue.SerializerToCmisEnum(typeDef.baseId); result.ParentTypeId = typeDef.parentId; result.IsCreatable = typeDef.creatable; result.IsFileable = typeDef.fileable; result.IsQueryable = typeDef.queryable; result.IsFulltextIndexed = typeDef.fulltextIndexed; result.IsIncludedInSupertypeQuery = typeDef.includedInSupertypeQuery; result.IsControllablePolicy = typeDef.controllablePolicy; result.IsControllableAcl = typeDef.controllableACL; if (typeDef.Items != null) { foreach (cmisPropertyDefinitionType propertyDefinition in typeDef.Items) { result.AddPropertyDefinition(Convert(propertyDefinition)); } } ConvertExtension(typeDef, result); return result; } public static IPropertyDefinition Convert(cmisPropertyDefinitionType propDef) { if (propDef == null) { return null; } PropertyDefinition result = null; if (propDef is cmisPropertyBooleanDefinitionType) { PropertyBooleanDefinition pd = new PropertyBooleanDefinition(); cmisPropertyBooleanDefinitionType cpd = (cmisPropertyBooleanDefinitionType)propDef; if (cpd.defaultValue != null && cpd.defaultValue.value != null) { pd.DefaultValue = new List(); foreach (bool value in cpd.defaultValue.value) { pd.DefaultValue.Add(value); } } if (cpd.choice != null) { pd.Choices = new List>(); foreach (cmisChoiceBoolean c in cpd.choice) { pd.Choices.Add(ConvertChoice(c)); } } ProcessAnyElements(cpd.Any, (element) => { if (element.LocalName.Equals("defaultValue")) { cmisPropertyBoolean prop = DeserializeElement(element); if (prop != null && prop.value != null) { pd.DefaultValue = new List(); foreach (bool value in prop.value) { pd.DefaultValue.Add(value); } } } else if (element.LocalName.Equals("choice")) { cmisChoiceBoolean choice = DeserializeElement(element); if (choice != null) { if (pd.Choices == null) { pd.Choices = new List>(); } pd.Choices.Add(ConvertChoice(choice)); } } }); result = pd; } else if (propDef is cmisPropertyDateTimeDefinitionType) { PropertyDateTimeDefinition pd = new PropertyDateTimeDefinition(); cmisPropertyDateTimeDefinitionType cpd = (cmisPropertyDateTimeDefinitionType)propDef; if (cpd.defaultValue != null && cpd.defaultValue.value != null) { pd.DefaultValue = new List(); foreach (DateTime value in cpd.defaultValue.value) { pd.DefaultValue.Add(value); } } if (cpd.choice != null) { pd.Choices = new List>(); foreach (cmisChoiceDateTime c in cpd.choice) { pd.Choices.Add(ConvertChoice(c)); } } if (cpd.resolutionSpecified) { pd.DateTimeResolution = (DateTimeResolution)CmisValue.SerializerToCmisEnum(cpd.resolution); } ProcessAnyElements(cpd.Any, (element) => { if (element.LocalName.Equals("defaultValue")) { cmisChoiceDateTime prop = DeserializeElement(element); if (prop != null && prop.value != null) { pd.DefaultValue = new List(); foreach (DateTime value in prop.value) { pd.DefaultValue.Add(value); } } } else if (element.LocalName.Equals("choice")) { cmisChoiceDateTime choice = DeserializeElement(element); if (choice != null) { if (pd.Choices == null) { pd.Choices = new List>(); } pd.Choices.Add(ConvertChoice(choice)); } } else if (element.LocalName.Equals("maxLength")) { pd.DateTimeResolution = DeserializeEnum(element); } }); result = pd; } else if (propDef is cmisPropertyDecimalDefinitionType) { PropertyDecimalDefinition pd = new PropertyDecimalDefinition(); cmisPropertyDecimalDefinitionType cpd = (cmisPropertyDecimalDefinitionType)propDef; if (cpd.defaultValue != null && cpd.defaultValue.value != null) { pd.DefaultValue = new List(); foreach (decimal value in cpd.defaultValue.value) { pd.DefaultValue.Add(value); } } if (cpd.choice != null) { pd.Choices = new List>(); foreach (cmisChoiceDecimal c in cpd.choice) { pd.Choices.Add(ConvertChoice(c)); } } if (cpd.maxValueSpecified) { pd.MaxValue = cpd.maxValue; } if (cpd.minValueSpecified) { pd.MinValue = cpd.minValue; } if (cpd.precisionSpecified) { pd.Precision = (DecimalPrecision)CmisValue.SerializerToCmisEnum(cpd.precision); } ProcessAnyElements(cpd.Any, (element) => { if (element.LocalName.Equals("defaultValue")) { cmisChoiceDecimal prop = DeserializeElement(element); if (prop != null && prop.value != null) { pd.DefaultValue = new List(); foreach (decimal value in prop.value) { pd.DefaultValue.Add(value); } } } else if (element.LocalName.Equals("choice")) { cmisChoiceDecimal choice = DeserializeElement(element); if (choice != null) { if (pd.Choices == null) { pd.Choices = new List>(); } pd.Choices.Add(ConvertChoice(choice)); } } else if (element.LocalName.Equals("maxValue")) { pd.MaxValue = DeserializeDecimal(element); } else if (element.LocalName.Equals("minValue")) { pd.MinValue = DeserializeDecimal(element); } else if (element.LocalName.Equals("precision")) { pd.Precision = DeserializeEnum(element); } }); result = pd; } else if (propDef is cmisPropertyHtmlDefinitionType) { PropertyHtmlDefinition pd = new PropertyHtmlDefinition(); cmisPropertyHtmlDefinitionType cpd = (cmisPropertyHtmlDefinitionType)propDef; if (cpd.defaultValue != null && cpd.defaultValue.value != null) { pd.DefaultValue = new List(); foreach (string value in cpd.defaultValue.value) { pd.DefaultValue.Add(value); } } if (cpd.choice != null) { pd.Choices = new List>(); foreach (cmisChoiceHtml c in cpd.choice) { pd.Choices.Add(ConvertChoice(c)); } } ProcessAnyElements(cpd.Any, (element) => { if (element.LocalName.Equals("defaultValue")) { cmisPropertyHtml prop = DeserializeElement(element); if (prop != null && prop.value != null) { pd.DefaultValue = new List(); foreach (string value in prop.value) { pd.DefaultValue.Add(value); } } } else if (element.LocalName.Equals("choice")) { cmisChoiceHtml choice = DeserializeElement(element); if (choice != null) { if (pd.Choices == null) { pd.Choices = new List>(); } pd.Choices.Add(ConvertChoice(choice)); } } }); result = pd; } else if (propDef is cmisPropertyIdDefinitionType) { PropertyIdDefinition pd = new PropertyIdDefinition(); cmisPropertyIdDefinitionType cpd = (cmisPropertyIdDefinitionType)propDef; if (cpd.defaultValue != null && cpd.defaultValue.value != null) { pd.DefaultValue = new List(); foreach (string value in cpd.defaultValue.value) { pd.DefaultValue.Add(value); } } if (cpd.choice != null) { pd.Choices = new List>(); foreach (cmisChoiceId c in cpd.choice) { pd.Choices.Add(ConvertChoice(c)); } } ProcessAnyElements(cpd.Any, (element) => { if (element.LocalName.Equals("defaultValue")) { cmisPropertyId prop = DeserializeElement(element); if (prop != null && prop.value != null) { pd.DefaultValue = new List(); foreach (string value in prop.value) { pd.DefaultValue.Add(value); } } } else if (element.LocalName.Equals("choice")) { cmisChoiceId choice = DeserializeElement(element); if (choice != null) { if (pd.Choices == null) { pd.Choices = new List>(); } pd.Choices.Add(ConvertChoice(choice)); } } }); result = pd; } else if (propDef is cmisPropertyIntegerDefinitionType) { PropertyIntegerDefinition pd = new PropertyIntegerDefinition(); cmisPropertyIntegerDefinitionType cpd = (cmisPropertyIntegerDefinitionType)propDef; if (cpd.defaultValue != null && cpd.defaultValue.value != null) { pd.DefaultValue = new List(); foreach (string value in cpd.defaultValue.value) { pd.DefaultValue.Add(Int64.Parse(value)); } } if (cpd.choice != null) { pd.Choices = new List>(); foreach (cmisChoiceInteger c in cpd.choice) { pd.Choices.Add(ConvertChoice(c)); } } if (cpd.maxValue != null) { pd.MaxValue = Int64.Parse(cpd.maxValue); } if (cpd.minValue != null) { pd.MinValue = Int64.Parse(cpd.minValue); } ProcessAnyElements(cpd.Any, (element) => { if (element.LocalName.Equals("defaultValue")) { cmisPropertyInteger prop = DeserializeElement(element); if (prop != null && prop.value != null) { pd.DefaultValue = new List(); foreach (string value in prop.value) { pd.DefaultValue.Add(Int64.Parse(value)); } } } else if (element.LocalName.Equals("choice")) { cmisChoiceInteger choice = DeserializeElement(element); if (choice != null) { if (pd.Choices == null) { pd.Choices = new List>(); } pd.Choices.Add(ConvertChoice(choice)); } } else if (element.LocalName.Equals("maxValue")) { pd.MaxValue = DeserializeInteger(element); } else if (element.LocalName.Equals("minValue")) { pd.MinValue = DeserializeInteger(element); } }); result = pd; } else if (propDef is cmisPropertyStringDefinitionType) { PropertyStringDefinition pd = new PropertyStringDefinition(); cmisPropertyStringDefinitionType cpd = (cmisPropertyStringDefinitionType)propDef; if (cpd.defaultValue != null && cpd.defaultValue.value != null) { pd.DefaultValue = new List(); foreach (string value in cpd.defaultValue.value) { pd.DefaultValue.Add(value); } } if (cpd.choice != null) { pd.Choices = new List>(); foreach (cmisChoiceString c in cpd.choice) { pd.Choices.Add(ConvertChoice(c)); } } if (cpd.maxLength != null) { pd.MaxLength = Int64.Parse(cpd.maxLength); } ProcessAnyElements(cpd.Any, (element) => { if (element.LocalName.Equals("defaultValue")) { cmisPropertyString prop = DeserializeElement(element); if (prop != null && prop.value != null) { pd.DefaultValue = new List(); foreach (string value in prop.value) { pd.DefaultValue.Add(value); } } } else if (element.LocalName.Equals("choice")) { cmisChoiceString choice = DeserializeElement(element); if (choice != null) { if (pd.Choices == null) { pd.Choices = new List>(); } pd.Choices.Add(ConvertChoice(choice)); } } else if (element.LocalName.Equals("maxLength")) { pd.MaxLength = DeserializeInteger(element); } }); result = pd; } else if (propDef is cmisPropertyUriDefinitionType) { PropertyUriDefinition pd = new PropertyUriDefinition(); cmisPropertyUriDefinitionType cpd = (cmisPropertyUriDefinitionType)propDef; if (cpd.defaultValue != null && cpd.defaultValue.value != null) { pd.DefaultValue = new List(); foreach (string value in cpd.defaultValue.value) { pd.DefaultValue.Add(value); } } if (cpd.choice != null) { pd.Choices = new List>(); foreach (cmisChoiceUri c in cpd.choice) { pd.Choices.Add(ConvertChoice(c)); } } ProcessAnyElements(cpd.Any, (element) => { if (element.LocalName.Equals("defaultValue")) { cmisPropertyUri prop = DeserializeElement(element); if (prop != null && prop.value != null) { pd.DefaultValue = new List(); foreach (string value in prop.value) { pd.DefaultValue.Add(value); } } } else if (element.LocalName.Equals("choice")) { cmisChoiceUri choice = DeserializeElement(element); if (choice != null) { if (pd.Choices == null) { pd.Choices = new List>(); } pd.Choices.Add(ConvertChoice(choice)); } } }); result = pd; } result.Id = propDef.id; result.LocalName = propDef.localName; result.LocalNamespace = propDef.localNamespace; result.DisplayName = propDef.displayName; result.QueryName = propDef.queryName; result.Description = propDef.description; result.PropertyType = (PropertyType)CmisValue.SerializerToCmisEnum(propDef.propertyType); result.Cardinality = (Cardinality)CmisValue.SerializerToCmisEnum(propDef.cardinality); result.Updatability = (Updatability)CmisValue.SerializerToCmisEnum(propDef.updatability); result.IsInherited = (propDef.inheritedSpecified ? (bool?)propDef.inherited : null); result.IsRequired = propDef.required; result.IsQueryable = propDef.queryable; result.IsOrderable = propDef.orderable; result.IsOpenChoice = (propDef.openChoiceSpecified ? (bool?)propDef.openChoice : null); ConvertExtension(propDef, result); return result; } private static IChoice ConvertChoice(cmisChoiceBoolean choice) { if (choice == null) { return null; } Choice result = new Choice(); result.DisplayName = choice.displayName; if (choice.value != null) { result.Value = new List(); foreach (bool v in choice.value) { result.Value.Add(v); } } if (choice.choice != null) { result.Choices = new List>(); foreach (cmisChoiceBoolean sc in choice.choice) { result.Choices.Add(ConvertChoice(sc)); } } return result; } private static IChoice ConvertChoice(cmisChoiceDateTime choice) { if (choice == null) { return null; } Choice result = new Choice(); result.DisplayName = choice.displayName; if (choice.value != null) { result.Value = new List(); foreach (DateTime v in choice.value) { result.Value.Add(v); } } if (choice.choice != null) { result.Choices = new List>(); foreach (cmisChoiceDateTime sc in choice.choice) { result.Choices.Add(ConvertChoice(sc)); } } return result; } private static IChoice ConvertChoice(cmisChoiceDecimal choice) { if (choice == null) { return null; } Choice result = new Choice(); result.DisplayName = choice.displayName; if (choice.value != null) { result.Value = new List(); foreach (decimal v in choice.value) { result.Value.Add(v); } } if (choice.choice != null) { result.Choices = new List>(); foreach (cmisChoiceDecimal sc in choice.choice) { result.Choices.Add(ConvertChoice(sc)); } } return result; } private static IChoice ConvertChoice(cmisChoiceHtml choice) { if (choice == null) { return null; } Choice result = new Choice(); result.DisplayName = choice.displayName; if (choice.value != null) { result.Value = new List(); foreach (string v in choice.value) { result.Value.Add(v); } } if (choice.choice != null) { result.Choices = new List>(); foreach (cmisChoiceHtml sc in choice.choice) { result.Choices.Add(ConvertChoice(sc)); } } return result; } private static IChoice ConvertChoice(cmisChoiceId choice) { if (choice == null) { return null; } Choice result = new Choice(); result.DisplayName = choice.displayName; if (choice.value != null) { result.Value = new List(); foreach (string v in choice.value) { result.Value.Add(v); } } if (choice.choice != null) { result.Choices = new List>(); foreach (cmisChoiceId sc in choice.choice) { result.Choices.Add(ConvertChoice(sc)); } } return result; } private static IChoice ConvertChoice(cmisChoiceInteger choice) { if (choice == null) { return null; } Choice result = new Choice(); result.DisplayName = choice.displayName; if (choice.value != null) { result.Value = new List(); foreach (string v in choice.value) { result.Value.Add(Int64.Parse(v)); } } if (choice.choice != null) { result.Choices = new List>(); foreach (cmisChoiceInteger sc in choice.choice) { result.Choices.Add(ConvertChoice(sc)); } } return result; } private static IChoice ConvertChoice(cmisChoiceString choice) { if (choice == null) { return null; } Choice result = new Choice(); result.DisplayName = choice.displayName; if (choice.value != null) { result.Value = new List(); foreach (string v in choice.value) { result.Value.Add(v); } } if (choice.choice != null) { result.Choices = new List>(); foreach (cmisChoiceString sc in choice.choice) { result.Choices.Add(ConvertChoice(sc)); } } return result; } private static IChoice ConvertChoice(cmisChoiceUri choice) { if (choice == null) { return null; } Choice result = new Choice(); result.DisplayName = choice.displayName; if (choice.value != null) { result.Value = new List(); foreach (string v in choice.value) { result.Value.Add(v); } } if (choice.choice != null) { result.Choices = new List>(); foreach (cmisChoiceUri sc in choice.choice) { result.Choices.Add(ConvertChoice(sc)); } } return result; } /// /// Converts a type defintion list. /// public static ITypeDefinitionList Convert(cmisTypeDefinitionListType typeDefList) { if (typeDefList == null) { return null; } TypeDefinitionList result = new TypeDefinitionList(); if (typeDefList.types != null) { result.List = new List(); foreach (cmisTypeDefinitionType type in typeDefList.types) { result.List.Add(Convert(type)); } } result.HasMoreItems = typeDefList.hasMoreItems; result.NumItems = typeDefList.numItems == null ? null : (long?)Int64.Parse(typeDefList.numItems); ConvertExtension(typeDefList, result); return result; } /// /// Converts a type defintion container. /// public static ITypeDefinitionContainer Convert(cmisTypeContainer typeDefCont) { if (typeDefCont == null) { return null; } TypeDefinitionContainer result = new TypeDefinitionContainer(); result.TypeDefinition = Convert(typeDefCont.type); if (typeDefCont.children != null) { result.Children = new List(); foreach (cmisTypeContainer container in typeDefCont.children) { result.Children.Add(Convert(container)); } } ConvertExtension(typeDefCont, result); return result; } public static IObjectData Convert(cmisObjectType cmisObject) { if (cmisObject == null) { return null; } ObjectData result = new ObjectData(); result.Properties = Convert(cmisObject.properties); result.AllowableActions = Convert(cmisObject.allowableActions); if (cmisObject.relationship != null) { result.Relationships = new List(); foreach (cmisObjectType co in cmisObject.relationship) { result.Relationships.Add(Convert(co)); } } result.ChangeEventInfo = Convert(cmisObject.changeEventInfo); result.IsExactAcl = cmisObject.exactACLSpecified ? (bool?)cmisObject.exactACL : null; result.Acl = Convert(cmisObject.acl, result.IsExactAcl); result.PolicyIds = Convert(cmisObject.policyIds); if (cmisObject.rendition != null) { result.Renditions = new List(); foreach (cmisRenditionType rendition in cmisObject.rendition) { result.Renditions.Add(Convert(rendition)); } } ConvertExtension(cmisObject, result); return result; } public static IProperties Convert(cmisPropertiesType properties) { if (properties == null) { return null; } Properties result = new Properties(); if (properties.Items != null) { foreach (cmisProperty property in properties.Items) { result.AddProperty(Convert(property)); } } ConvertExtension(properties, result); return result; } public static IPropertyData Convert(cmisProperty property) { if (property == null) { return null; } PropertyData result = null; if (property is cmisPropertyString) { result = new PropertyData(PropertyType.String); if (((cmisPropertyString)property).value != null) { foreach (string value in ((cmisPropertyString)property).value) { result.AddValue(value); } } } else if (property is cmisPropertyId) { result = new PropertyData(PropertyType.Id); if (((cmisPropertyId)property).value != null) { foreach (string value in ((cmisPropertyId)property).value) { result.AddValue(value); } } } else if (property is cmisPropertyInteger) { result = new PropertyData(PropertyType.Integer); if (((cmisPropertyInteger)property).value != null) { foreach (string value in ((cmisPropertyInteger)property).value) { result.AddValue(Int64.Parse(value)); } } } else if (property is cmisPropertyBoolean) { result = new PropertyData(PropertyType.Boolean); if (((cmisPropertyBoolean)property).value != null) { foreach (bool value in ((cmisPropertyBoolean)property).value) { result.AddValue(value); } } } else if (property is cmisPropertyDateTime) { result = new PropertyData(PropertyType.DateTime); if (((cmisPropertyDateTime)property).value != null) { foreach (DateTime value in ((cmisPropertyDateTime)property).value) { result.AddValue(value); } } } else if (property is cmisPropertyDecimal) { result = new PropertyData(PropertyType.Decimal); if (((cmisPropertyDecimal)property).value != null) { foreach (decimal value in ((cmisPropertyDecimal)property).value) { result.AddValue(value); } } } else if (property is cmisPropertyHtml) { result = new PropertyData(PropertyType.Html); if (((cmisPropertyHtml)property).value != null) { foreach (string value in ((cmisPropertyHtml)property).value) { result.AddValue(value); } } } else if (property is cmisPropertyUri) { result = new PropertyData(PropertyType.Uri); if (((cmisPropertyUri)property).value != null) { foreach (string value in ((cmisPropertyUri)property).value) { result.AddValue(value); } } } result.Id = property.propertyDefinitionId; result.LocalName = property.localName; result.DisplayName = property.displayName; result.QueryName = property.queryName; ConvertExtension(property, result); return result; } public static cmisPropertiesType Convert(IProperties properties) { if (properties == null) { return null; } cmisPropertiesType result = new cmisPropertiesType(); if (properties.PropertyList != null) { result.Items = new cmisProperty[properties.PropertyList.Count]; for (int i = 0; i < properties.PropertyList.Count; i++) { result.Items[i] = Convert(properties.PropertyList[i]); } } ConvertExtension(properties, result); return result; } public static cmisProperty Convert(IPropertyData property) { if (property == null) { return null; } cmisProperty result = null; switch (property.PropertyType) { case PropertyType.String: result = new cmisPropertyString(); if (property.Values != null) { ((cmisPropertyString)result).value = new string[property.Values.Count]; for (int i = 0; i < property.Values.Count; i++) { ((cmisPropertyString)result).value[i] = property.Values[i] as string; } } break; case PropertyType.Id: result = new cmisPropertyId(); if (property.Values != null) { ((cmisPropertyId)result).value = new string[property.Values.Count]; for (int i = 0; i < property.Values.Count; i++) { ((cmisPropertyId)result).value[i] = property.Values[i] as string; } } break; case PropertyType.Integer: result = new cmisPropertyInteger(); if (property.Values != null) { ((cmisPropertyInteger)result).value = new string[property.Values.Count]; for (int i = 0; i < property.Values.Count; i++) { long? value = property.Values[i] as long?; if (value.HasValue) { ((cmisPropertyInteger)result).value[i] = value.ToString(); } } } break; case PropertyType.Boolean: result = new cmisPropertyBoolean(); if (property.Values != null) { ((cmisPropertyBoolean)result).value = new bool[property.Values.Count]; for (int i = 0; i < property.Values.Count; i++) { bool? value = property.Values[i] as bool?; if (value.HasValue) { ((cmisPropertyBoolean)result).value[i] = value.Value; } } } break; case PropertyType.DateTime: result = new cmisPropertyDateTime(); if (property.Values != null) { ((cmisPropertyDateTime)result).value = new DateTime[property.Values.Count]; for (int i = 0; i < property.Values.Count; i++) { DateTime? value = property.Values[i] as DateTime?; if (value.HasValue) { ((cmisPropertyDateTime)result).value[i] = value.Value; } } } break; case PropertyType.Decimal: result = new cmisPropertyDecimal(); if (property.Values != null) { ((cmisPropertyDecimal)result).value = new decimal[property.Values.Count]; for (int i = 0; i < property.Values.Count; i++) { decimal? value = property.Values[i] as decimal?; if (value.HasValue) { ((cmisPropertyDecimal)result).value[i] = value.Value; } } } break; case PropertyType.Html: result = new cmisPropertyHtml(); if (property.Values != null) { ((cmisPropertyHtml)result).value = new string[property.Values.Count]; for (int i = 0; i < property.Values.Count; i++) { ((cmisPropertyHtml)result).value[i] = property.Values[i] as string; } } break; case PropertyType.Uri: result = new cmisPropertyUri(); if (property.Values != null) { ((cmisPropertyUri)result).value = new string[property.Values.Count]; for (int i = 0; i < property.Values.Count; i++) { ((cmisPropertyUri)result).value[i] = property.Values[i] as string; } } break; } result.propertyDefinitionId = property.Id; result.localName = property.LocalName; result.displayName = property.DisplayName; result.queryName = property.QueryName; ConvertExtension(property, result); return result; } public static IAllowableActions Convert(cmisAllowableActionsType allowableActions) { if (allowableActions == null) { return null; } AllowableActions result = new AllowableActions(); result.Actions = new HashSet(); if (allowableActions.canDeleteObject && allowableActions.canDeleteObjectSpecified) { result.Actions.Add(Actions.CanDeleteObject); } if (allowableActions.canUpdateProperties && allowableActions.canUpdatePropertiesSpecified) { result.Actions.Add(Actions.CanUpdateProperties); } if (allowableActions.canGetFolderTree && allowableActions.canGetFolderTreeSpecified) { result.Actions.Add(Actions.CanGetFolderTree); } if (allowableActions.canGetProperties && allowableActions.canGetPropertiesSpecified) { result.Actions.Add(Actions.CanGetProperties); } if (allowableActions.canGetObjectRelationships && allowableActions.canGetObjectRelationshipsSpecified) { result.Actions.Add(Actions.CanGetObjectRelationships); } if (allowableActions.canGetObjectParents && allowableActions.canGetObjectParentsSpecified) { result.Actions.Add(Actions.CanGetObjectParents); } if (allowableActions.canGetFolderParent && allowableActions.canGetFolderParentSpecified) { result.Actions.Add(Actions.CanGetFolderParent); } if (allowableActions.canGetDescendants && allowableActions.canGetDescendantsSpecified) { result.Actions.Add(Actions.CanGetDescendants); } if (allowableActions.canMoveObject && allowableActions.canMoveObjectSpecified) { result.Actions.Add(Actions.CanMoveObject); } if (allowableActions.canDeleteContentStream && allowableActions.canDeleteContentStreamSpecified) { result.Actions.Add(Actions.CanDeleteContentStream); } if (allowableActions.canCheckOut && allowableActions.canCheckOutSpecified) { result.Actions.Add(Actions.CanCheckOut); } if (allowableActions.canCancelCheckOut && allowableActions.canCancelCheckOutSpecified) { result.Actions.Add(Actions.CanCancelCheckOut); } if (allowableActions.canCheckIn && allowableActions.canCheckInSpecified) { result.Actions.Add(Actions.CanCheckIn); } if (allowableActions.canSetContentStream && allowableActions.canSetContentStreamSpecified) { result.Actions.Add(Actions.CanSetContentStream); } if (allowableActions.canGetAllVersions && allowableActions.canGetAllVersionsSpecified) { result.Actions.Add(Actions.CanGetAllVersions); } if (allowableActions.canAddObjectToFolder && allowableActions.canAddObjectToFolderSpecified) { result.Actions.Add(Actions.CanAddObjectToFolder); } if (allowableActions.canRemoveObjectFromFolder && allowableActions.canRemoveObjectFromFolderSpecified) { result.Actions.Add(Actions.CanRemoveObjectFromFolder); } if (allowableActions.canGetContentStream && allowableActions.canGetContentStreamSpecified) { result.Actions.Add(Actions.CanGetContentStream); } if (allowableActions.canApplyPolicy && allowableActions.canApplyPolicySpecified) { result.Actions.Add(Actions.CanApplyPolicy); } if (allowableActions.canGetAppliedPolicies && allowableActions.canGetAppliedPoliciesSpecified) { result.Actions.Add(Actions.CanGetAppliedPolicies); } if (allowableActions.canRemovePolicy && allowableActions.canRemovePolicySpecified) { result.Actions.Add(Actions.CanRemovePolicy); } if (allowableActions.canGetChildren && allowableActions.canGetChildrenSpecified) { result.Actions.Add(Actions.CanGetChildren); } if (allowableActions.canCreateDocument && allowableActions.canCreateDocumentSpecified) { result.Actions.Add(Actions.CanCreateDocument); } if (allowableActions.canCreateFolder && allowableActions.canCreateFolderSpecified) { result.Actions.Add(Actions.CanCreateFolder); } if (allowableActions.canCreateRelationship && allowableActions.canCreateRelationshipSpecified) { result.Actions.Add(Actions.CanCreateRelationship); } if (allowableActions.canDeleteTree && allowableActions.canDeleteTreeSpecified) { result.Actions.Add(Actions.CanDeleteTree); } if (allowableActions.canGetRenditions && allowableActions.canGetRenditionsSpecified) { result.Actions.Add(Actions.CanGetRenditions); } if (allowableActions.canGetACL && allowableActions.canGetACLSpecified) { result.Actions.Add(Actions.CanGetAcl); } if (allowableActions.canApplyACL && allowableActions.canApplyACLSpecified) { result.Actions.Add(Actions.CanApplyAcl); } ConvertExtension(allowableActions, result); return result; } public static IAcl Convert(cmisAccessControlListType acl, bool? isExact) { if (acl == null) { return null; } Acl result = new Acl(); if (acl.permission != null) { result.Aces = new List(); foreach (cmisAccessControlEntryType ace in acl.permission) { result.Aces.Add(Convert(ace)); } } result.IsExact = isExact; ConvertExtension(acl, result); return result; } public static IAcl Convert(cmisACLType acl) { if (acl == null) { return null; } Acl result = new Acl(); if (acl.ACL != null && acl.ACL.permission != null) { result.Aces = new List(); foreach (cmisAccessControlEntryType ace in acl.ACL.permission) { result.Aces.Add(Convert(ace)); } } result.IsExact = (acl.exactSpecified ? (bool?)acl.exactSpecified : null); ConvertExtension(acl, result); return result; } public static IAce Convert(cmisAccessControlEntryType ace) { if (ace == null) { return null; } Ace result = new Ace(); if (ace.principal != null) { Principal principal = new Principal(); principal.Id = ace.principal.principalId; result.Principal = principal; ConvertExtension(ace.principal, principal); } if (ace.permission != null) { result.Permissions = new List(); foreach (string permission in ace.permission) { result.Permissions.Add(permission); } } result.IsDirect = ace.direct; ConvertExtension(ace, result); return result; } public static cmisAccessControlListType Convert(IAcl acl) { if (acl == null) { return null; } cmisAccessControlListType result = new cmisAccessControlListType(); if (acl.Aces != null) { result.permission = new cmisAccessControlEntryType[acl.Aces.Count]; for (int i = 0; i < acl.Aces.Count; i++) { result.permission[i] = Convert(acl.Aces[i]); } } ConvertExtension(acl, result); return result; } public static cmisAccessControlEntryType Convert(IAce ace) { if (ace == null) { return null; } cmisAccessControlEntryType result = new cmisAccessControlEntryType(); if (ace.Principal != null) { result.principal = new cmisAccessControlPrincipalType(); result.principal.principalId = ace.Principal.Id; ConvertExtension(ace.Principal, result.principal); } if (ace.Permissions != null) { result.permission = new string[ace.Permissions.Count]; for (int i = 0; i < ace.Permissions.Count; i++) { result.permission[i] = ace.Permissions[i]; } } result.direct = ace.IsDirect; ConvertExtension(ace, result); return result; } public static IPolicyIdList Convert(cmisListOfIdsType policyIdList) { if (policyIdList == null) { return null; } PolicyIdList result = new PolicyIdList(); if (policyIdList.id != null) { result.PolicyIds = new List(); foreach (string id in policyIdList.id) { result.PolicyIds.Add(id); } } ConvertExtension(policyIdList, result); return result; } public static cmisListOfIdsType ConvertPolicies(IList policyIds) { if (policyIds == null) { return null; } cmisListOfIdsType result = new cmisListOfIdsType(); result.id = new string[policyIds.Count]; for (int i = 0; i < policyIds.Count; i++) { result.id[i] = policyIds[i]; } return result; } public static IRenditionData Convert(cmisRenditionType rendition) { if (rendition == null) { return null; } RenditionData result = new RenditionData(); result.StreamId = rendition.streamId; result.MimeType = rendition.mimetype; result.Length = rendition.length == null ? null : (long?)Int64.Parse(rendition.length); result.Kind = rendition.kind; result.Title = rendition.title; result.Height = rendition.height == null ? null : (long?)Int64.Parse(rendition.height); result.Width = rendition.width == null ? null : (long?)Int64.Parse(rendition.width); result.RenditionDocumentId = rendition.renditionDocumentId; ConvertExtension(rendition, result); return result; } public static IChangeEventInfo Convert(cmisChangeEventType changeEvent) { if (changeEvent == null) { return null; } ChangeEventInfo result = new ChangeEventInfo(); result.ChangeType = (ChangeType)CmisValue.SerializerToCmisEnum(changeEvent.changeType); result.ChangeTime = changeEvent.changeTime; ConvertExtension(changeEvent, result); return result; } public static IObjectInFolderList Convert(cmisObjectInFolderListType list) { if (list == null) { return null; } ObjectInFolderList result = new ObjectInFolderList(); if (list.objects != null) { result.Objects = new List(); foreach (cmisObjectInFolderType fo in list.objects) { result.Objects.Add(Convert(fo)); } } result.HasMoreItems = list.hasMoreItems; result.NumItems = list.numItems == null ? null : (long?)Int64.Parse(list.numItems); ConvertExtension(list, result); return result; } public static IObjectInFolderData Convert(cmisObjectInFolderType objectInFolder) { if (objectInFolder == null) { return null; } ObjectInFolderData result = new ObjectInFolderData(); result.Object = Convert(objectInFolder.@object); result.PathSegment = objectInFolder.pathSegment; ConvertExtension(objectInFolder, result); return result; } public static IObjectInFolderContainer Convert(cmisObjectInFolderContainerType container) { if (container == null) { return null; } ObjectInFolderContainer result = new ObjectInFolderContainer(); result.Object = Convert(container.objectInFolder); if (container.children != null) { result.Children = new List(); foreach (cmisObjectInFolderContainerType child in container.children) { result.Children.Add(Convert(child)); } } ConvertExtension(container, result); return result; } public static IObjectParentData Convert(cmisObjectParentsType parent) { if (parent == null) { return null; } ObjectParentData result = new ObjectParentData(); result.Object = Convert(parent.@object); result.RelativePathSegment = parent.relativePathSegment; ConvertExtension(parent, result); return result; } public static IObjectList Convert(cmisObjectListType list) { if (list == null) { return null; } ObjectList result = new ObjectList(); if (list.objects != null) { result.Objects = new List(); foreach (cmisObjectType obj in list.objects) { result.Objects.Add(Convert(obj)); } } result.HasMoreItems = list.hasMoreItems; result.NumItems = list.numItems == null ? null : (long?)Int64.Parse(list.numItems); ConvertExtension(list, result); return result; } public static IContentStream Convert(cmisContentStreamType contentStream) { if (contentStream == null) { return null; } ContentStream result = new ContentStream(); if (contentStream.length != null) { result.Length = Int64.Parse(contentStream.length); } result.MimeType = contentStream.mimeType; result.FileName = contentStream.filename; // Todo: enable real streaming result.Stream = new MemoryStream(contentStream.stream); ConvertExtension(contentStream, result); return result; } public static cmisContentStreamType Convert(IContentStream contentStream) { if (contentStream == null) { return null; } cmisContentStreamType result = new cmisContentStreamType(); result.length = contentStream.Length.ToString(); result.mimeType = contentStream.MimeType; result.filename = contentStream.FileName; if (contentStream.Stream != null) { if (contentStream.Stream is MemoryStream) { result.stream = ((MemoryStream)contentStream.Stream).ToArray(); } else { MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[4096]; int bytes; while ((bytes = contentStream.Stream.Read(buffer, 0, buffer.Length)) > 0) { memStream.Write(buffer, 0, bytes); } result.stream = memStream.ToArray(); } } return result; } public static IFailedToDeleteData Convert(deleteTreeResponseFailedToDelete failedToDelete) { if (failedToDelete == null) { return null; } FailedToDeleteData result = new FailedToDeleteData(); if (failedToDelete.objectIds != null) { result.Ids = new List(); foreach (string id in failedToDelete.objectIds) { result.Ids.Add(id); } } ConvertExtension(failedToDelete, result); return result; } public static T[] ConvertList(IList list) { if (list == null) { return null; } T[] result = new T[list.Count]; for (int i = 0; i < list.Count; i++) { result[i] = list[i]; } return result; } /// /// Converts an extension. /// public static void ConvertExtension(object source, IExtensionsData target) { if (source == null || target == null) { return; } Type type = source.GetType(); PropertyInfo propInfo = type.GetProperty("Any"); if (propInfo == null) { return; } XmlElement[] elements = (XmlElement[])propInfo.GetValue(source, null); if (elements == null) { return; } target.Extensions = new List(); foreach (XmlElement xmlElement in elements) { ICmisExtensionElement element = CreateCmisExtensionElement(xmlElement); if (element != null) { target.Extensions.Add(element); } } } private static ICmisExtensionElement CreateCmisExtensionElement(XmlNode xmlElement) { CmisExtensionElement result = new CmisExtensionElement(); result.Name = xmlElement.Name; result.Namespace = xmlElement.NamespaceURI; if (xmlElement.Attributes != null && xmlElement.Attributes.Count > 0) { result.Attributes = new Dictionary(); foreach (XmlAttribute attr in xmlElement.Attributes) { result.Attributes[attr.Name] = attr.Value; } } if (xmlElement.HasChildNodes) { result.Children = new List(); foreach (XmlNode node in xmlElement.ChildNodes) { if (node.NodeType == XmlNodeType.Text || node.NodeType == XmlNodeType.CDATA || node.NodeType == XmlNodeType.SignificantWhitespace) { result.Value = node.Value; } else if (node.NodeType == XmlNodeType.Element) { ICmisExtensionElement element = CreateCmisExtensionElement(node); if (element != null) { result.Children.Add(element); } } } } else { result.Value = xmlElement.Value; } return result; } /// /// Converts an extension. /// public static cmisExtensionType ConvertExtension(IExtensionsData extension) { if (extension == null || extension.Extensions == null) { return null; } XmlDocument doc = new XmlDocument(); List elements = new List(); foreach (ICmisExtensionElement element in extension.Extensions) { if (element == null) { continue; } elements.Add(CreateXmlElement(doc, element)); } cmisExtensionType result = new cmisExtensionType(); result.Any = elements.ToArray(); return result; } public static void ConvertExtension(IExtensionsData source, object target) { if (source == null || source.Extensions == null || target == null) { return; } Type type = target.GetType(); PropertyInfo propInfo = type.GetProperty("Any"); if (propInfo == null) { return; } XmlDocument doc = new XmlDocument(); List elements = new List(); foreach (ICmisExtensionElement element in source.Extensions) { if (element == null) { continue; } elements.Add(CreateXmlElement(doc, element)); } propInfo.SetValue(target, elements.ToArray(), null); } private static XmlElement CreateXmlElement(XmlDocument doc, ICmisExtensionElement element) { if (element == null) { return null; } XmlElement result = doc.CreateElement(element.Name, element.Namespace); if (element.Attributes != null) { foreach (string key in element.Attributes.Keys) { XmlAttribute attr = doc.CreateAttribute(key); attr.Value = element.Attributes[key]; result.Attributes.Append(attr); } } if (element.Value != null) { result.InnerText = element.Value; } else if (element.Children != null) { List children = new List(); foreach (ICmisExtensionElement child in element.Children) { XmlElement xml = CreateXmlElement(doc, child); if (xml != null) { result.AppendChild(xml); } } } return result; } public static string GetXmlEnumAttributeValue(Enum xsdEnum) { FieldInfo fieldInfo = xsdEnum.GetType().GetField(xsdEnum.ToString()); XmlEnumAttribute[] xmlAttr = fieldInfo.GetCustomAttributes(typeof(XmlEnumAttribute), false) as XmlEnumAttribute[]; if (xmlAttr.Length == 0) { return null; } return xmlAttr[0].Name; } } }