Ejemplo n.º 1
0
        /// <summary>
        /// Generates the class code for a service.
        /// </summary>
        /// <param name="service">The service to generate for.</param>
        /// <param name="className">The class name of the service or null to use the service type.</param>
        /// <param name="namespaceName">The namespace for the class.</param>
        /// <param name="classScope">The scope for the class.</param>
        /// <param name="partial">True to make the class partial, false otherwise.</param>
        /// <param name="testStateVars">
        /// True to test each state variable to ensure it is
        /// usuable for accessing as property, false to include
        /// all state variables as properties.</param>
        /// <returns>The string representing the code for the class.</returns>
        public string GenerateClassFor(
            Service service, string className, string namespaceName,
            ClassScope classScope, bool partial, bool testStateVars)
        {
            // If classname is not specified then default to service information
            if (className == null)
            {
                className = DefaultCodeGenClassName(service, CodeGenProvider);
            }
            else
            {
                // Otherwise ensure classname is Identifier compatible
                className = CodeGenProvider.CodeFriendlyIdentifier(className, false);
            }

            StringBuilder               lsbStateVarProps          = new StringBuilder();
            StringBuilder               lsbStateVarConversion     = new StringBuilder();
            StringBuilder               lsbStateVarEnums          = new StringBuilder();
            StringBuilder               lsbActionMethods          = new StringBuilder();
            StringBuilder               lsbEventHandlers          = new StringBuilder();
            StringBuilder               lsbEventCallers           = new StringBuilder();
            StringBuilder               lsbStateVarEventIntercept = new StringBuilder();
            HashSet <string>            lhsEnumStateVars          = new HashSet <string>();
            StringConstants             lscConsts       = new StringConstants(CodeGenProvider);
            Dictionary <string, string> ldStateVarTypes = new Dictionary <string, string>();

            // Get the service description
            ServiceDescription lsdDesc = service.Description();

            if (lsdDesc != null)
            {
                // Generate the state variable property declarations
                GenerateStateVarCode(
                    service, testStateVars, lscConsts, lsbStateVarProps,
                    lsbStateVarConversion, lsbStateVarEnums, lsbEventHandlers, lsbEventCallers,
                    lsbStateVarEventIntercept, lhsEnumStateVars, ldStateVarTypes, lsdDesc);

                // Generate the action methods
                GenerateActionCode(
                    lscConsts, lsbActionMethods, lhsEnumStateVars,
                    ldStateVarTypes, lsdDesc);
            }

            if (lsbStateVarEventIntercept.Length > 0)
            {
                lsbEventCallers.Append(
                    String.Format(CodeGenProvider.StateVarChangedEventHandler, lsbStateVarEventIntercept)
                    );
            }

            return
                (String.Format(
                     CodeGenProvider.ServiceBase,
                     namespaceName,
                     className,
                     service.ServiceTypeIdentifier,
                     CodeGenProvider.GenerateRegion(CodeGenProvider.PublicEnumerations, lsbStateVarEnums.ToString()),
                     (partial ? CodeGenProvider.PartialClass : String.Empty),
                     CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedMethods, lsbStateVarConversion.ToString()),
                     lsbActionMethods,
                     CodeGenProvider.GenerateRegion(CodeGenProvider.PublicProperties, lsbStateVarProps.ToString()),
                     CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedConstants, lscConsts.Definitions().ToString(), false, false),
                     CodeGenProvider.GetClassScope(classScope, CodeGenProvider.Space.ToString()),
                     CodeGenProvider.GenerateRegion(CodeGenProvider.EventHandlers, lsbEventHandlers.ToString(), true),
                     CodeGenProvider.GenerateRegion(CodeGenProvider.EventCallers, lsbEventCallers.ToString()),
                     string.Format(
                         CodeGenProvider.ServiceClassHeaderComment,
                         (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.FriendlyName : CodeGenProvider.Null),
                         (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.Type : CodeGenProvider.Null),
                         (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.SerialNumber : CodeGenProvider.Null),
                         (service.Device != null ? service.Device.FriendlyName : CodeGenProvider.Null),
                         (service.Device != null ? service.Device.Type : CodeGenProvider.Null),
                         service.Id, service.ServiceTypeIdentifier,
                         DateTime.Now.ToString(),
                         className,
                         namespaceName,
                         classScope.ToString(),
                         (partial ? CodeGenProvider.PartialClass : string.Empty),
                         testStateVars,
                         CodeGenProvider.ToString()
                         )
                     ));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the class code for a device.
        /// </summary>
        /// <param name="device">The device to generate code for.</param>
        /// <param name="className">The class name of the device class or null to use friendly name.</param>
        /// <param name="namespaceName">The namespace for the class.</param>
        /// <param name="classScope">The scope of the class.</param>
        /// <param name="partial">True to make the class partial, false otherwise.</param>
        /// <param name="specificDevices">True if generating properties for device specific class types, false to use device non-specific class types.</param>
        /// <param name="specificServiceNamespace">The name of the service namespace if using specific service class types, null to use service non-specific class types.</param>
        /// <param name="specificDeviceClasses">The dictionary of UDNs, ClassNames for the devices or null for default / none.</param>
        /// <param name="specificServiceClasses">The dictionary of IDs, ClassNames for the services or null for default / none.</param>
        /// <returns>The string representing the code for the class.</returns>
        public string GenerateClassFor(
            Device device, string className, string namespaceName, ClassScope classScope, bool partial,
            bool specificDevices, string specificServiceNamespace,
            Dictionary <string, string> specificDeviceClasses = null, Dictionary <string, string> specificServiceClasses = null)
        {
            if (className == null)
            {
                className = DefaultCodeGenClassName(device, CodeGenProvider);
            }
            else
            {
                className = CodeGenProvider.CodeFriendlyIdentifier(className, false);
            }

            // No namespace specified then set it not use one
            if (string.IsNullOrEmpty(specificServiceNamespace))
            {
                specificServiceNamespace = string.Empty;
            }

            // Set the specific services
            bool mbSpecificServices = specificServiceNamespace.Length > 0;

            string lsUsing;

            // If service namespace is different to device namespace
            if (specificServiceNamespace != namespaceName)
            {
                // Add the services namespace to the using clause
                lsUsing = (mbSpecificServices ? string.Format(CodeGenProvider.UsingClause, specificServiceNamespace) : string.Empty);
            }
            else
            {
                // Otherwise dont add anything to the uses clause
                lsUsing = string.Empty;
            }

            StringConstants lscConstants  = new StringConstants(CodeGenProvider);
            StringBuilder   lsbProperties = new StringBuilder();

            // Build the child devices properties
            GenerateDevicePropertyCode(device, specificDevices, lscConstants, lsbProperties, specificDeviceClasses);

            // Build the child services properties
            GenerateServicePropertyCode(device, mbSpecificServices, lscConstants, lsbProperties, specificServiceClasses);

            // Build the code for the device class
            return
                (string.Format(
                     CodeGenProvider.DeviceBase,
                     lsUsing,
                     namespaceName,
                     CodeGenProvider.GetClassScope(classScope, CodeGenProvider.Space.ToString()),
                     (partial ? CodeGenProvider.PartialClass : string.Empty),
                     className,
                     CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedConstants, lscConstants.Definitions().ToString(), false, false),
                     device.Type,
                     CodeGenProvider.GenerateRegion(CodeGenProvider.PublicProperties, lsbProperties.ToString()),
                     device.ModelName,
                     string.Format(
                         CodeGenProvider.DeviceClassHeaderComment,
                         (device.RootDevice != null ? device.RootDevice.FriendlyName : CodeGenProvider.Null),
                         (device.RootDevice != null ? device.RootDevice.Type : CodeGenProvider.Null),
                         (device.RootDevice != null ? device.RootDevice.SerialNumber : CodeGenProvider.Null),
                         device.FriendlyName, device.Type,
                         DateTime.Now.ToString(),
                         className,
                         namespaceName,
                         classScope.ToString(),
                         (partial ? CodeGenProvider.PartialClass : string.Empty),
                         CodeGenProvider.ToString()
                         )
                     ));
        }