private static Occurs GetDefaultOccurs(Type type)
 {
     if ((type.IsArray) ||
         (InheritedGenericInterfacesHelper.ImplementsGenericInterface(typeof(ICollection <>), type)))
     {
         return(Occurs.ZeroOrMany);
     }
     return(Occurs.ZeroOrOne);
 }
 private static Type GetComponentType(Type resourceType, MethodInfo method, Type type)
 {
     if (type.IsArray)
     {
         return(type.GetElementType());
     }
     else if (InheritedGenericInterfacesHelper.ImplementsGenericInterface(typeof(ICollection <>), type))
     {
         Type[] actualTypeArguments = type.GetGenericArguments();
         if (actualTypeArguments.Length == 1)
         {
             return(actualTypeArguments[0]);
         }
         throw new OslcCoreInvalidPropertyTypeException(resourceType, method, type);
     }
     else
     {
         return(type);
     }
 }
        private static void ValidateUserSpecifiedOccurs(Type resourceType, MethodInfo method, OslcOccurs occursAttribute)
        {
            Type   returnType = method.ReturnType;
            Occurs occurs     = occursAttribute.value;

            if ((returnType.IsArray) ||
                (InheritedGenericInterfacesHelper.ImplementsGenericInterface(typeof(ICollection <>), returnType)))
            {
                if ((!Occurs.ZeroOrMany.Equals(occurs)) &&
                    (!Occurs.OneOrMany.Equals(occurs)))
                {
                    throw new OslcCoreInvalidOccursException(resourceType, method, occursAttribute);
                }
            }
            else
            {
                if ((!Occurs.ZeroOrOne.Equals(occurs)) &&
                    (!Occurs.ExactlyOne.Equals(occurs)))
                {
                    throw new OslcCoreInvalidOccursException(resourceType, method, occursAttribute);
                }
            }
        }
示例#4
0
        private static Property CreateProperty(
            string baseURI,
            Type resourceType,
            MethodInfo method,
            OslcPropertyDefinition propertyDefinitionAttribute,
            ISet <Type> verifiedTypes)
        {
            string name;
            var    nameAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcName>(method);

            if (nameAttribute != null)
            {
                name = nameAttribute.value;
            }
            else
            {
                name = GetDefaultPropertyName(method);
            }

            var propertyDefinition = propertyDefinitionAttribute.value;

            if (!propertyDefinition.EndsWith(name))
            {
                throw new OslcCoreInvalidPropertyDefinitionException(resourceType, method, propertyDefinitionAttribute);
            }

            var    returnType = method.ReturnType;
            Occurs occurs;
            var    occursAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcOccurs>(method);

            if (occursAttribute != null)
            {
                occurs = occursAttribute.value;
                ValidateUserSpecifiedOccurs(resourceType, method, occursAttribute);
            }
            else
            {
                occurs = GetDefaultOccurs(returnType);
            }

            var componentType = GetComponentType(resourceType, method, returnType);

            // Reified resources are a special case.
            if (InheritedGenericInterfacesHelper.ImplementsGenericInterface(typeof(IReifiedResource <>), componentType))
            {
                var genericType = typeof(IReifiedResource <object>).GetGenericTypeDefinition();

                var interfaces = componentType.GetInterfaces();

                foreach (var interfac in interfaces)
                {
                    if (interfac.IsGenericType && genericType == interfac.GetGenericTypeDefinition())
                    {
                        componentType = interfac.GetGenericArguments()[0];
                        break;
                    }
                }
            }

            ValueType valueType;
            var       valueTypeAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcValueType>(method);

            if (valueTypeAttribute != null)
            {
                valueType = valueTypeAttribute.value;
                ValidateUserSpecifiedValueType(resourceType, method, valueType, componentType);
            }
            else
            {
                valueType = GetDefaultValueType(resourceType, method, componentType);
            }

            var property = new Property(name, occurs, new Uri(propertyDefinition), valueType);

            property.SetTitle(property.GetName());
            var titleAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcTitle>(method);

            if (titleAttribute != null)
            {
                property.SetTitle(titleAttribute.value);
            }

            var descriptionAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcDescription>(method);

            if (descriptionAttribute != null)
            {
                property.SetDescription(descriptionAttribute.value);
            }

            var rangeAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcRange>(method);

            if (rangeAttribute != null)
            {
                foreach (var range in rangeAttribute.value)
                {
                    property.AddRange(new Uri(range));
                }
            }

            var representationAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcRepresentation>(method);

            if (representationAttribute != null)
            {
                var representation = representationAttribute.value;
                ValidateUserSpecifiedRepresentation(resourceType, method, representation, componentType);
                property.SetRepresentation(new Uri(RepresentationExtension.ToString(representation)));
            }
            else
            {
                var defaultRepresentation = GetDefaultRepresentation(componentType);
                if (defaultRepresentation != Representation.Unknown)
                {
                    property.SetRepresentation(new Uri(RepresentationExtension.ToString(defaultRepresentation)));
                }
            }

            var allowedValueAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcAllowedValue>(method);

            if (allowedValueAttribute != null)
            {
                foreach (var allowedValue in allowedValueAttribute.value)
                {
                    property.AddAllowedValue(allowedValue);
                }
            }

            var allowedValuesAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcAllowedValues>(method);

            if (allowedValuesAttribute != null)
            {
                property.SetAllowedValuesRef(new Uri(allowedValuesAttribute.value));
            }

            var defaultValueAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcDefaultValue>(method);

            if (defaultValueAttribute != null)
            {
                property.SetDefaultValue(defaultValueAttribute.value);
            }

            var hiddenAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcHidden>(method);

            if (hiddenAttribute != null)
            {
                property.SetHidden(hiddenAttribute.value);
            }

            var memberPropertyAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcMemberProperty>(method);

            if (memberPropertyAttribute != null)
            {
                property.SetMemberProperty(memberPropertyAttribute.value);
            }

            var readOnlyAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcReadOnly>(method);

            if (readOnlyAttribute != null)
            {
                property.SetReadOnly(readOnlyAttribute.value);
            }

            var maxSizeAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcMaxSize>(method);

            if (maxSizeAttribute != null)
            {
                property.SetMaxSize(maxSizeAttribute.value);
            }

            var valueShapeAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcValueShape>(method);

            if (valueShapeAttribute != null)
            {
                property.SetValueShape(new Uri(baseURI + "/" + valueShapeAttribute.value));
            }

            if (ValueType.LocalResource.Equals(valueType))
            {
                // If this is a nested class we potentially have not yet verified
                if (verifiedTypes.Add(componentType))
                {
                    // Validate nested resource ignoring return value, but throwing any exceptions
                    CreateResourceShape(
                        baseURI,
                        OslcConstants.PATH_RESOURCE_SHAPES,
                        "unused",
                        componentType,
                        verifiedTypes);
                }
            }

            return(property);
        }