Example #1
0
 public void SetRepresentation(Uri representation)
 {
     if (representation != null)
     {
         _representation = RepresentationExtension.FromString(representation.ToString());
     }
     else
     {
         _representation = Representation.Unknown;
     }
 }
Example #2
0
        public Uri GetRepresentation()
        {
            if (representation != null)
            {
                try {
                    return(new Uri(RepresentationExtension.ToString(representation)));
                } catch (UriFormatException exception) {
                    // This should never happen since we control the possible values of the Representation enum.
                    throw new SystemException(exception.Message, exception);
                }
            }

            return(null);
        }
Example #3
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);
        }