private static ResourceShape CreateResourceShape(string baseURI,
                                                         string resourceShapesPath,
                                                         string resourceShapePath,
                                                         Type resourceType,
                                                         ISet <Type> verifiedTypes)
        {
            OslcResourceShape[] resourceShapeAttribute = (OslcResourceShape[])resourceType.GetCustomAttributes(typeof(OslcResourceShape), false);
            if (resourceShapeAttribute == null || resourceShapeAttribute.Length == 0)
            {
                throw new OslcCoreMissingAttributeException(resourceType, typeof(OslcResourceShape));
            }

            Uri           about         = new Uri(baseURI + "/" + resourceShapesPath + "/" + resourceShapePath);
            ResourceShape resourceShape = new ResourceShape(about);

            string title = resourceShapeAttribute[0].title;

            if ((title != null) && (title.Length > 0))
            {
                resourceShape.SetTitle(title);
            }

            foreach (string describesItem in resourceShapeAttribute[0].describes)
            {
                resourceShape.AddDescribeItem(new Uri(describesItem));
            }

            ISet <string> propertyDefinitions = new HashSet <string>();

            foreach (MethodInfo method in resourceType.GetMethods())
            {
                if (method.GetParameters().Length == 0)
                {
                    string methodName       = method.Name;
                    int    methodNameLength = methodName.Length;
                    if (((methodName.StartsWith(METHOD_NAME_START_GET)) && (methodNameLength > METHOD_NAME_START_GET_LENGTH)) ||
                        ((methodName.StartsWith(METHOD_NAME_START_IS)) && (methodNameLength > METHOD_NAME_START_IS_LENGTH)))
                    {
                        OslcPropertyDefinition propertyDefinitionAttribute = InheritedMethodAttributeHelper.GetAttribute <OslcPropertyDefinition>(method);
                        if (propertyDefinitionAttribute != null)
                        {
                            string propertyDefinition = propertyDefinitionAttribute.value;
                            if (propertyDefinitions.Contains(propertyDefinition))
                            {
                                throw new OslcCoreDuplicatePropertyDefinitionException(resourceType, propertyDefinitionAttribute);
                            }

                            propertyDefinitions.Add(propertyDefinition);

                            Property property = CreateProperty(baseURI, resourceType, method, propertyDefinitionAttribute, verifiedTypes);
                            resourceShape.AddProperty(property);

                            ValidateSetMethodExists(resourceType, method);
                        }
                    }
                }
            }

            return(resourceShape);
        }
示例#2
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);
        }