Esempio n. 1
0
        public void TestConcreteExample()
        {
            //
            // Parse Properties File
            //
            TextReader    propertiesReader = new System.IO.StringReader("%enum:ExecuteEnum Local Remote Ignore Unsupported;Script{%props:1;Source:enum(File Remote);File:0-1;ListenPort:0-1 int4;}RebootCommand {%props:1;Execute:1 enum(Ignore Unsupported Emulator);RemoteHost:0-1;RemotePort:0-1 int4;}UsbSwitchCommand {	%props:1;	Execute:1 enum ExecuteEnum;	Platform:0-1 enum(Windows Linux);	RemoteHost:0-1;	RemotePort:0-1 int4;}TestCommands {	%props:1;	Execute:1 enum ExecuteEnum;	RemoteHost:0-1;	RemotePort:0-1 int4;}");
            HmdProperties properties       = HmdFileParser.ParsePropertiesFile(propertiesReader, null);

            properties.ResolveChildParentReferences();
            properties.Print(Console.Out);


            HmdEnum hmdEnum;

            hmdEnum = properties.TryGetEnum("ExecuteEnum");
            Assert.IsNotNull(hmdEnum);
            Assert.IsTrue(hmdEnum.IsValidEnumValue("local"));
            Assert.IsTrue(hmdEnum.IsValidEnumValue("remote"));
            Assert.IsTrue(hmdEnum.IsValidEnumValue("IGNORE"));
            Assert.IsTrue(hmdEnum.IsValidEnumValue("unsupported"));

            hmdEnum = properties.TryGetEnum("script.source");
            Assert.IsNotNull(hmdEnum);
            Assert.IsTrue(hmdEnum.IsValidEnumValue("FILE"));
            Assert.IsTrue(hmdEnum.IsValidEnumValue("remote"));

            HmdBlockID fileRoot = new HmdBlockID("THE_ROOT!", null);

            HmdBlockID script = new HmdBlockID("script", fileRoot);

            Assert.IsNotNull(properties.GetProperties(script));

            HmdValueID scriptSource = new HmdValueID("source", "file", script);

            Assert.IsNotNull(properties.GetProperties(scriptSource));



            //
            // Parse Hmd File
            //
            TextReader hmdReader = new System.IO.StringReader("Script {Source:Remote;}RebootCommand {Execute:Unsupported;}UsbSwitchCommand {Execute:Unsupported;}TestCommands {Execute:Local;}");
            HmdBlockID rootID    = new HmdBlockID(String.Empty, null);

            HmdFileParser.Parse(rootID, hmdReader, "", null);


            //HmdValidator.ValidateStatic(rootID, properties);
        }
Esempio n. 2
0
        public Boolean IsValidValue(String value, HmdProperties hmdProperties)
        {
            switch (hmdType)
            {
            case HmdType.String:
                return(true);

            case HmdType.Boolean:
                return(value.Equals("true", StringComparison.CurrentCultureIgnoreCase) ||
                       value.Equals("false", StringComparison.CurrentCultureIgnoreCase));

            case HmdType.Int:
                return(value.IsValidInteger(false, 4));

            case HmdType.Int1:
                return(value.IsValidInteger(false, 1));

            case HmdType.Int2:
                return(value.IsValidInteger(false, 2));

            case HmdType.Int3:
                return(value.IsValidInteger(false, 3));

            case HmdType.Int4:
                return(value.IsValidInteger(false, 4));

            case HmdType.Int5:
                return(value.IsValidInteger(false, 5));

            case HmdType.Int6:
                return(value.IsValidInteger(false, 6));

            case HmdType.Int7:
                return(value.IsValidInteger(false, 7));

            case HmdType.Int8:
                return(value.IsValidInteger(false, 8));

            case HmdType.Int9:
                return(value.IsValidInteger(false, 9));

            case HmdType.Int10:
                return(value.IsValidInteger(false, 10));

            case HmdType.Int11:
                return(value.IsValidInteger(false, 11));

            case HmdType.Int12:
                return(value.IsValidInteger(false, 12));

            case HmdType.Int13:
                return(value.IsValidInteger(false, 13));

            case HmdType.Int14:
                return(value.IsValidInteger(false, 14));

            case HmdType.Int15:
                return(value.IsValidInteger(false, 15));

            case HmdType.Int16:
                return(value.IsValidInteger(false, 16));

            case HmdType.UInt:
                return(value.IsValidInteger(true, 4));

            case HmdType.UInt1:
                return(value.IsValidInteger(true, 1));

            case HmdType.UInt2:
                return(value.IsValidInteger(true, 2));

            case HmdType.UInt3:
                return(value.IsValidInteger(true, 3));

            case HmdType.UInt4:
                return(value.IsValidInteger(true, 4));

            case HmdType.UInt5:
                return(value.IsValidInteger(true, 5));

            case HmdType.UInt6:
                return(value.IsValidInteger(true, 6));

            case HmdType.UInt7:
                return(value.IsValidInteger(true, 7));

            case HmdType.UInt8:
                return(value.IsValidInteger(true, 8));

            case HmdType.UInt9:
                return(value.IsValidInteger(true, 9));

            case HmdType.UInt10:
                return(value.IsValidInteger(true, 10));

            case HmdType.UInt11:
                return(value.IsValidInteger(true, 11));

            case HmdType.UInt12:
                return(value.IsValidInteger(true, 12));

            case HmdType.UInt13:
                return(value.IsValidInteger(true, 13));

            case HmdType.UInt14:
                return(value.IsValidInteger(true, 14));

            case HmdType.UInt15:
                return(value.IsValidInteger(true, 15));

            case HmdType.UInt16:
                return(value.IsValidInteger(true, 16));

            case HmdType.Decimal:
                throw new NotImplementedException();

            case HmdType.Enumeration:
                HmdEnum hmdEnum = enumReference.TryGetReference;
                if (hmdEnum == null)
                {
                    hmdEnum = hmdProperties.TryGetEnum(enumReference.Name);
                    if (hmdEnum == null)
                    {
                        throw new InvalidOperationException(String.Format("Can't resolve enum reference '{0}'", enumReference.Name));
                    }
                    this.enumReference = hmdEnum;
                }
                return(hmdEnum.IsValidEnumValue(value.Trim()));

            case HmdType.Empty:
                throw new InvalidOperationException("Cannot validate the type of a null type");

            default:
                throw new InvalidOperationException(String.Format("HmdType {0} ({1}) is unrecognized", hmdType, (Int32)hmdType));
            }
        }
Esempio n. 3
0
        public void GenerateParserClasses(TextWriter output, HmdBlockIDProperties block,
                                          HmdProperties hmdProperties, String hmdTypePrefix, Boolean isRoot)
        {
            String className = isRoot ? rootClassName : (hmdTypePrefix + typeNameTable.GetTypeName(block));

            //
            // Generate the class for the current block
            //
            int tabs = 1;

            output.WriteLine(tabs, "public class {0}", className);
            output.WriteLine(tabs++, "{");

            //
            // Print Fields
            //
            foreach (HmdIDProperties childIDProperties in block)
            {
                if (childIDProperties.isBlock)
                {
                    HmdBlockIDProperties childBlockProperties = childIDProperties.CastAsBlockIDProperties;

                    String type = hmdTypePrefix + typeNameTable.GetTypeName(childBlockProperties);

                    if (childBlockProperties.CountProperty.Multiple)
                    {
                        type = languageGenerator.ListType(type);
                    }

                    output.WriteLine(tabs, "public {0} {1};", type, childBlockProperties.idOriginalCase);
                }
                else
                {
                    HmdValueIDProperties childValueProperties = childIDProperties.CastAsValueIDProperties;
                    if (childValueProperties.hmdType != HmdType.Empty)
                    {
                        String type;
                        if (childValueProperties.hmdType == HmdType.Enumeration)
                        {
                            HmdEnum hmdEnum = childValueProperties.EnumReference.TryGetReference;
                            if (hmdEnum == null)
                            {
                                hmdEnum = hmdProperties.TryGetEnum(childValueProperties.EnumReference.Name);
                                if (hmdEnum == null)
                                {
                                    throw new FormatException(String.Format("Can't resolve enum reference '{0}'", childValueProperties.EnumReference.Name));
                                }
                                childValueProperties.ResolveEnumReference(hmdEnum);
                            }
                            type = typeNameTable.GetTypeName(hmdEnum);
                        }
                        else
                        {
                            type = languageGenerator.HmdTypeToLanguageType(childValueProperties.hmdType);
                        }

                        if (childValueProperties.CountProperty.Multiple)
                        {
                            type = languageGenerator.ListType(type);
                        }
                        output.WriteLine(tabs, "public {0} {1};", type, childValueProperties.idOriginalCase);
                    }
                }
            }

            //
            // Print Constructor
            //
            output.WriteLine(tabs, "public {0}(HmdBlockID blockID, HmdProperties hmdProperties)", className);
            output.WriteLine(tabs++, "{");
            output.WriteLine(tabs, "for(int i = 0; i < blockID.ChildCount; i++)");
            output.WriteLine(tabs++, "{");
            output.WriteLine(tabs, "HmdID childID = blockID.GetChild(i);");
            output.WriteLine(tabs, "if(childID.isBlock)");
            output.WriteLine(tabs++, "{");
            output.WriteLine(tabs, "HmdBlockID childBlockID = (HmdBlockID)childID;");
            Int32 blockChildCount = 0;

            foreach (HmdIDProperties childIDProperties in block)
            {
                if (childIDProperties.isBlock)
                {
                    HmdBlockIDProperties childBlockProperties = childIDProperties.CastAsBlockIDProperties;
                    blockChildCount++;

                    output.WriteLine(tabs, "// parse field {0}", childIDProperties.idOriginalCase);
                    output.WriteLine(tabs, "{0}if(childBlockID.idLowerCase.Equals(\"{1}\",StringComparison.CurrentCultureIgnoreCase))", blockChildCount > 1 ? "else " : String.Empty, childIDProperties.idLowerCase);
                    output.WriteLine(tabs++, "{");
                    if (childIDProperties.CountProperty.Multiple)
                    {
                        output.WriteLine(tabs, "// set List to not null");
                        output.WriteLine(tabs, "this.{0}.Add(new {1}{2}(childBlockID, hmdProperties));",
                                         childIDProperties.idOriginalCase, hmdTypePrefix, typeNameTable.GetTypeName(childBlockProperties));
                    }
                    else
                    {
                        output.WriteLine(tabs, "// check that field is not set already");
                        output.WriteLine(tabs, "if(this.{0} != null)", childIDProperties.idOriginalCase);
                        output.WriteLine(tabs++, "{");
                        output.WriteLine(tabs, "throw new FormatException(\"Found multiple block id's \\\"{0}\\\"\");", childIDProperties.idOriginalCase);
                        output.WriteLine(--tabs, "}");
                        output.WriteLine(tabs, "this.{0} = new {1}{2}(childBlockID, hmdProperties);",
                                         childIDProperties.idOriginalCase, hmdTypePrefix, typeNameTable.GetTypeName(childBlockProperties));
                    }
                    output.WriteLine(--tabs, "}");
                }
            }
            if (blockChildCount > 0)
            {
                output.WriteLine(tabs, "else");
                output.WriteLine(tabs++, "{");
            }
            output.WriteLine(tabs, "throw new FormatException(String.Format(\"Unrecognized child block id \\\"{0}\\\"\",childID.idOriginalCase));");
            if (blockChildCount > 0)
            {
                output.WriteLine(--tabs, "}");
            }
            output.WriteLine(--tabs, "}");
            output.WriteLine(tabs, "else");
            output.WriteLine(tabs++, "{");
            output.WriteLine(tabs, "HmdValueID childValueID = (HmdValueID)childID;");
            Int32 valueChildCount = 0;

            foreach (HmdIDProperties childIDProperties in block)
            {
                if (!childIDProperties.isBlock)
                {
                    HmdValueIDProperties childValueIDProperties = childIDProperties.CastAsValueIDProperties;
                    valueChildCount++;
                    output.WriteLine(tabs, "// parse field {0}", childIDProperties.idOriginalCase);
                    output.WriteLine(tabs, "{0}if(childValueID.idLowerCase.Equals(\"{1}\",StringComparison.CurrentCultureIgnoreCase))", valueChildCount > 1 ? "else " : String.Empty, childIDProperties.idLowerCase);
                    output.WriteLine(tabs++, "{");

                    String variableName = "childValueID.value";
                    String parseCode    = null;
                    if (childValueIDProperties.hmdType == HmdType.Enumeration)
                    {
                        parseCode = languageGenerator.GenerateStringToEnumParseCode(
                            typeNameTable.GetTypeName(childValueIDProperties.EnumReference.TryGetReference),
                            variableName);
                    }
                    else
                    {
                        parseCode = languageGenerator.GenerateStringToTypeParseCode(
                            childValueIDProperties.hmdType,
                            variableName);
                    }

                    if (childIDProperties.CountProperty.Multiple)
                    {
                        output.WriteLine(tabs, "this.{0}.Add({1});", childIDProperties.idOriginalCase, parseCode);
                    }
                    else
                    {
                        output.WriteLine(tabs, "// check that field is not set already");
                        output.WriteLine(tabs, "if(this.{0} != null)", childIDProperties.idOriginalCase);
                        output.WriteLine(tabs++, "{");
                        output.WriteLine(tabs, "throw new FormatException(\"Found multiple value id's \\\"{0}\\\"\");", childIDProperties.idOriginalCase);
                        output.WriteLine(--tabs, "}");
                        output.WriteLine(tabs, "this.{0} = {1};", childIDProperties.idOriginalCase, parseCode);
                    }
                    output.WriteLine(--tabs, "}");
                }
            }
            if (valueChildCount > 0)
            {
                output.WriteLine(tabs, "else");
                output.WriteLine(tabs++, "{");
            }
            output.WriteLine(tabs, "throw new FormatException(String.Format(\"Unrecognized child value id \\\"{0}\\\"\",childID.idOriginalCase));");
            if (valueChildCount > 0)
            {
                output.WriteLine(--tabs, "}");
            }
            output.WriteLine(--tabs, "}");

            output.WriteLine(--tabs, "}");
            output.WriteLine(--tabs, "}");
            output.WriteLine(--tabs, "}");
        }