示例#1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="resourceType"></param>
 /// <param name="method"></param>
 /// <param name="oslcOccurs"></param>
 public OslcCoreInvalidOccursException(Type resourceType, MethodInfo method, OslcOccurs oslcOccurs) :
     base(MESSAGE_KEY, new Object[] { resourceType.Name, method.Name, OccursExtension.ToString(oslcOccurs.value) })
 {
     this.method       = method;
     this.oslcOccurs   = oslcOccurs;
     this.resourceType = resourceType;
 }
示例#2
0
        private static void ValidateUserSpecifiedOccurs(
            Type resourceType,
            MethodInfo method,
            OslcOccurs occursAttribute)
        {
            var returnType = method.ReturnType;
            var 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);
                }
            }
        }
        private static Property CreateProperty(string baseURI, Type resourceType, MethodInfo method, OslcPropertyDefinition propertyDefinitionAttribute, ISet <Type> verifiedTypes)
        {
            string   name;
            OslcName nameAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcName>(method);

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

            string propertyDefinition = propertyDefinitionAttribute.value;

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

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

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

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

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

                Type[] interfaces = componentType.GetInterfaces();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            OslcValueShape 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);
        }