public static void Parse(HmdBlockID root, String filename, String importPath, HmdProperties hmdProperties) { using (FileStream fileStream = new FileStream(filename, FileMode.Open)) { Parse(root, fileStream, importPath, hmdProperties); } }
public HmdTypeverifydevicesarenotpresent(HmdBlockID blockID, HmdProperties hmdProperties) { for (int i = 0; i < blockID.ChildCount; i++) { HmdID childID = blockID.GetChild(i); if (childID.isBlock) { HmdBlockID childBlockID = (HmdBlockID)childID; // parse field UsbDevice if (childBlockID.idLowerCase.Equals("usbdevice", StringComparison.CurrentCultureIgnoreCase)) { // set List to not null this.UsbDevice.Add(new HmdTypeusbdevice(childBlockID, hmdProperties)); } else { throw new FormatException(String.Format("Unrecognized child block id \"{0}\"", childID.idOriginalCase)); } } else { HmdValueID childValueID = (HmdValueID)childID; throw new FormatException(String.Format("Unrecognized child value id \"{0}\"", childID.idOriginalCase)); } } }
public String CreateContextString() { HmdBlockID currentParent = parent; Stack <HmdBlockID> parents = new Stack <HmdBlockID>(); while (currentParent != null) { parents.Push(currentParent); currentParent = currentParent.parent; } if (parents.Count <= 1) { return(idLowerCase); } // Pop off the root parents.Pop(); StringBuilder stringBuilder = new StringBuilder(); while (parents.Count > 0) { stringBuilder.Append(parents.Pop().idLowerCase); stringBuilder.Append('.'); } stringBuilder.Append(idLowerCase); return(stringBuilder.ToString()); }
public static void Iterate(this HmdBlockID blockID, HmdProperties hmdProperties, AtValueIDWithProperties atValueIDWithProperties, AtBlockIDWithProperties atBlockIDWithProperties) { for (int i = 0; i < blockID.ChildCount; i++) { HmdID childID = blockID.GetChild(i); if (childID.isBlock) { HmdBlockID childBlockID = childID.CastAsBlockID; HmdBlockIDProperties childBlockIDProperties = hmdProperties.GetProperties(childBlockID); if (childBlockIDProperties == null) { throw new InvalidOperationException(String.Format("Found a block id \"{0}\", but it was not defined in the property dictionary", childID.idOriginalCase)); } atBlockIDWithProperties(childBlockID, childBlockIDProperties, hmdProperties); } else { HmdValueID childValueID = childID.CastAsValueID; HmdValueIDProperties childValueIDProperties = hmdProperties.GetProperties(childValueID); if (childValueIDProperties == null) { throw new InvalidOperationException(String.Format("Found a value id \"{0}\", but it was not defined in the property dictionary", childID.idOriginalCase)); } atValueIDWithProperties(childValueID, childValueIDProperties, hmdProperties); } } }
protected HmdID(Boolean isBlockID, String id, HmdBlockID parent) { this.isBlock = isBlockID; this.idOriginalCase = id; this.idLowerCase = id.ToLower(); this.parent = parent; }
public void Validate(HmdBlockID root, HmdProperties hmdProperties) { debugOutput = HmdDebug.DebugOutput; if (root == null) { throw new ArgumentNullException("root"); } if (hmdProperties == null) { throw new ArgumentNullException("hmdProperties"); } hmdProperties.ResolveChildParentReferences(); debugOutput.WriteLine("[Validating HMD file...]"); hmdProperties.PrintEnums(debugOutput); currentBlock = null; blockStack = new Stack <HmdBlockValidator>(); ValidateBlockID(root, hmdProperties.root, hmdProperties); debugOutput.WriteLine("[Done validating HMD file]"); }
public static void Parse(HmdBlockID root, Stream stream, String importPath, HmdProperties hmdProperties) { using (StreamReader fileStream = new StreamReader(stream)) { Parse(root, fileStream, importPath, hmdProperties); } }
public DefaultRootClassName(HmdBlockID blockID, HmdProperties hmdProperties) { for (int i = 0; i < blockID.ChildCount; i++) { HmdID childID = blockID.GetChild(i); if (childID.isBlock) { HmdBlockID childBlockID = (HmdBlockID)childID; // parse field VerifyDevicesArePresent if (childBlockID.idLowerCase.Equals("verifydevicesarepresent", StringComparison.CurrentCultureIgnoreCase)) { // set List to not null this.VerifyDevicesArePresent.Add(new HmdTypeverifydevicesarepresent(childBlockID, hmdProperties)); } // parse field VerifyDevicesAreNotPresent else if (childBlockID.idLowerCase.Equals("verifydevicesarenotpresent", StringComparison.CurrentCultureIgnoreCase)) { // set List to not null this.VerifyDevicesAreNotPresent.Add(new HmdTypeverifydevicesarenotpresent(childBlockID, hmdProperties)); } else { throw new FormatException(String.Format("Unrecognized child block id \"{0}\"", childID.idOriginalCase)); } } else { HmdValueID childValueID = (HmdValueID)childID; // parse field Message if (childValueID.idLowerCase.Equals("message", StringComparison.CurrentCultureIgnoreCase)) { this.Message.Add(childValueID.value); } // parse field UsbSwitch else if (childValueID.idLowerCase.Equals("usbswitch", StringComparison.CurrentCultureIgnoreCase)) { this.UsbSwitch.Add((usbswitch)Enum.Parse(typeof(usbswitch), childValueID.value, true)); } // parse field Sleep else if (childValueID.idLowerCase.Equals("sleep", StringComparison.CurrentCultureIgnoreCase)) { this.Sleep.Add(UInt32.Parse(childValueID.value)); } else { throw new FormatException(String.Format("Unrecognized child value id \"{0}\"", childID.idOriginalCase)); } } } }
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); }
public static void Iterate(this HmdBlockID blockID, AtValueID atValueID, AtBlockID atBlockID) { for (int i = 0; i < blockID.ChildCount; i++) { HmdID id = blockID.GetChild(i); if (id.isBlock) { atBlockID((HmdBlockID)id); } else { atValueID((HmdValueID)id); } } }
private void ValidateBlockID(HmdBlockID blockID, HmdBlockIDProperties blockIDProperties, HmdProperties hmdProperties) { // // Check that the current parent is valid // if (currentBlock == null) { debugOutput.Write(blockStack.Count, "Checking that \"{0}\" is the root...", blockID.idOriginalCase); if (!blockIDProperties.IsRoot) { throw new FormatException(String.Format("Block ID \"{0}\" was expected to be the root, but it wasn't?", blockIDProperties.idOriginalCase)); } debugOutput.WriteLine("Pass."); } else { debugOutput.Write(blockStack.Count, "Checking that \"{0}\" has \"{1}\" as a valid parent...", blockID.idOriginalCase, currentBlock.blockIDProperties.idOriginalCase); if (!blockIDProperties.IsValidParent(currentBlock.blockIDProperties)) { throw new FormatException(String.Format("Block ID \"{0}\" appeared in Block \"{1}\", but this is not allowed with the current properties", blockIDProperties.idOriginalCase, currentBlock.blockIDProperties.idOriginalCase)); } debugOutput.WriteLine("Pass."); // Add ID to current block validator currentBlock.NewChild(blockIDProperties); } // // Verify the Children of the Block ID // debugOutput.WriteLine(blockStack.Count, "{"); blockStack.Push(currentBlock); currentBlock = new HmdBlockValidator(blockIDProperties); blockID.Iterate(hmdProperties, ValidateValueID, ValidateBlockID); // Validate Children debugOutput.Write(blockStack.Count, "Checking counts of all children for \"{0}\"...", blockID.idOriginalCase); currentBlock.ValidateChildren(hmdProperties); debugOutput.WriteLine("Pass."); currentBlock = blockStack.Pop(); debugOutput.WriteLine(blockStack.Count, "}} (end of \"{0}\")", blockID.idOriginalCase); }
public void SimpleTest() { String testString = "ABlockID { AValueID: value; AnotherValue: value2; } OtherValue: v; "; HmdSingleIDTokenizer tokenizer = new HmdSingleIDTokenizer(new StringReader(testString)); HmdBlockID root = new HmdBlockID(String.Empty, null); HmdFileParser.Parse(root, tokenizer, null, null); Assert.AreEqual(1, root.ChildCount); Assert.IsTrue(root.GetChild(0).isBlock); Assert.AreEqual("ABlockID", root.GetChild(0).idOriginalCase); HmdBlockID childBlock = root.GetChild(0).CastAsBlockID; Assert.AreEqual(2, childBlock.ChildCount); Assert.IsFalse(childBlock.GetChild(0).CastAsValueID.isBlock); Assert.AreEqual("AValueID", childBlock.GetChild(0).idOriginalCase); Assert.AreEqual(" value", childBlock.GetChild(0).CastAsValueID.value); Assert.IsFalse(childBlock.GetChild(1).CastAsValueID.isBlock); Assert.AreEqual("AnotherValue", childBlock.GetChild(1).idOriginalCase); Assert.AreEqual(" value2", childBlock.GetChild(1).CastAsValueID.value); root = new HmdBlockID(String.Empty, null); tokenizer.Reset(); HmdFileParser.Parse(root, tokenizer, null, null); Assert.AreEqual(1, root.ChildCount); Assert.IsFalse(root.GetChild(0).isBlock); Assert.AreEqual("OtherValue", root.GetChild(0).idOriginalCase); Assert.AreEqual(" v", root.GetChild(0).CastAsValueID.value); }
public HmdTypeusbdevice(HmdBlockID blockID, HmdProperties hmdProperties) { for (int i = 0; i < blockID.ChildCount; i++) { HmdID childID = blockID.GetChild(i); if (childID.isBlock) { HmdBlockID childBlockID = (HmdBlockID)childID; throw new FormatException(String.Format("Unrecognized child block id \"{0}\"", childID.idOriginalCase)); } else { HmdValueID childValueID = (HmdValueID)childID; // parse field LegacyName if (childValueID.idLowerCase.Equals("legacyname", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.LegacyName != null) { throw new FormatException("Found multiple value id's \"LegacyName\""); } this.LegacyName = (usbdevicelegacyname)Enum.Parse(typeof(usbdevicelegacyname), childValueID.value, true); } // parse field Protocol else if (childValueID.idLowerCase.Equals("protocol", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.Protocol != null) { throw new FormatException("Found multiple value id's \"Protocol\""); } this.Protocol = Byte.Parse(childValueID.value); } // parse field SubClass else if (childValueID.idLowerCase.Equals("subclass", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.SubClass != null) { throw new FormatException("Found multiple value id's \"SubClass\""); } this.SubClass = Byte.Parse(childValueID.value); } // parse field Class else if (childValueID.idLowerCase.Equals("class", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.Class != null) { throw new FormatException("Found multiple value id's \"Class\""); } this.Class = Byte.Parse(childValueID.value); } // parse field Interface else if (childValueID.idLowerCase.Equals("interface", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.Interface != null) { throw new FormatException("Found multiple value id's \"Interface\""); } this.Interface = Byte.Parse(childValueID.value); } // parse field SerialNumber else if (childValueID.idLowerCase.Equals("serialnumber", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.SerialNumber != null) { throw new FormatException("Found multiple value id's \"SerialNumber\""); } this.SerialNumber = childValueID.value; } // parse field Product else if (childValueID.idLowerCase.Equals("product", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.Product != null) { throw new FormatException("Found multiple value id's \"Product\""); } this.Product = childValueID.value; } // parse field Manufacturer else if (childValueID.idLowerCase.Equals("manufacturer", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.Manufacturer != null) { throw new FormatException("Found multiple value id's \"Manufacturer\""); } this.Manufacturer = childValueID.value; } // parse field ProductID else if (childValueID.idLowerCase.Equals("productid", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.ProductID != null) { throw new FormatException("Found multiple value id's \"ProductID\""); } this.ProductID = UInt16.Parse(childValueID.value); } // parse field VendorID else if (childValueID.idLowerCase.Equals("vendorid", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.VendorID != null) { throw new FormatException("Found multiple value id's \"VendorID\""); } this.VendorID = UInt16.Parse(childValueID.value); } // parse field Speed else if (childValueID.idLowerCase.Equals("speed", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.Speed != null) { throw new FormatException("Found multiple value id's \"Speed\""); } this.Speed = (usbdevicespeed)Enum.Parse(typeof(usbdevicespeed), childValueID.value, true); } // parse field PortName else if (childValueID.idLowerCase.Equals("portname", StringComparison.CurrentCultureIgnoreCase)) { // check that field is not set already if (this.PortName != null) { throw new FormatException("Found multiple value id's \"PortName\""); } this.PortName = childValueID.value; } else { throw new FormatException(String.Format("Unrecognized child value id \"{0}\"", childID.idOriginalCase)); } } } }
public HmdBlockIDProperties GetProperties(HmdBlockID blockID) { return(blockIDTable.GetProperties(blockID)); }
public HmdValueID(String idString, String value, HmdBlockID parent) : base(IsBlockID, idString, parent) { this.value = value; }
public HmdBlockID(String idString, HmdBlockID parent) : base(IsBlockID, idString, parent) { }
public static void Parse(HmdBlockID root, TextReader reader, String importPath, HmdProperties hmdProperties) { Parse(root, new HmdTokenizer(reader, 1), importPath, hmdProperties); }
public static void ValidateStatic(HmdBlockID root, HmdProperties hmdProperties) { new HmdValidator().Validate(root, hmdProperties); }
public static void Parse(HmdBlockID root, String filename, HmdProperties hmdProperties) { Parse(root, filename, Path.GetDirectoryName(filename), hmdProperties); }
public static void Parse(HmdBlockID root, HmdTokenizer tokenizer, String importPath, HmdProperties hmdProperties) { if (root == null) { throw new ArgumentNullException("root"); } TextWriter debugOutput = HmdDebug.DebugOutput; if (debugOutput == null) { debugOutput = TextWriter.Null; } if (importPath == null) { importPath = String.Empty; } HmdBlockID parentID = root; Stack <HmdBlockID> parentIDStack = new Stack <HmdBlockID>(); while (true) { HmdGlobalToken token = tokenizer.NextGlobalToken(); // // Check for EOF // if (token.type == HmdGlobalTokenType.EOF) { debugOutput.WriteLine("EOF"); break; } if (token.type == HmdGlobalTokenType.ID) { String idString = token.text; // // Normal Parse // Boolean isBlockID; Boolean isEmptyID = tokenizer.NextIDType(out isBlockID); if (isBlockID) { debugOutput.WriteLine("Block ID : {0}", idString); parentIDStack.Push(parentID); HmdBlockID temp = parentID; parentID = new HmdBlockID(idString, temp); } else { debugOutput.WriteLine("Value ID : {0}", idString); if (isEmptyID) { parentID.AddChild(new HmdValueID(idString, null, parentID)); } else { String nextValue = tokenizer.NextValue(); debugOutput.WriteLine("Value : {0}", nextValue); parentID.AddChild(new HmdValueID(idString, nextValue, parentID)); } } } else if (token.type == HmdGlobalTokenType.Directive) { String directiveID = token.text; // // Handling Directives // Boolean isBlockID; Boolean isEmptyID = tokenizer.NextIDType(out isBlockID); if (isEmptyID) { throw new NotImplementedException(); } else if (!isBlockID) { if (directiveID.Equals("import", StringComparison.CurrentCulture) || directiveID.Equals("pimport", StringComparison.CurrentCulture)) { Boolean isPimport = directiveID[0] == 'p'; String nextValue = tokenizer.NextValue(); debugOutput.WriteLine("{0} : {1}", isPimport ? "%PImport" : "%Import ", nextValue); if (isPimport && hmdProperties == null) { debugOutput.WriteLine("%PImport : Ignoring props, so skipping this %pimport"); } else { String importFileAndPathName = Path.Combine(importPath, nextValue); using (FileStream importStream = new FileStream(importFileAndPathName, FileMode.Open)) { debugOutput.WriteLine("File Start: {0}", importFileAndPathName); if (isPimport) { ParsePropertiesFile(hmdProperties.root, hmdProperties, new HmdTokenizer(new StreamReader(importStream), 1), importPath, true); } else { Parse(parentID, new HmdTokenizer(new StreamReader(importStream), 1), importPath, hmdProperties); } debugOutput.WriteLine("File End : {0}", importFileAndPathName); } } } else if (directiveID.Equals("enum", StringComparison.CurrentCulture)) { throw new FormatException("%enum value directive must be in a %props block directive"); } else if (directiveID.Equals("props", StringComparison.CurrentCulture)) { throw new FormatException("%props value directive must be in a %props block directive"); } else { throw new Exception(String.Format("Parser (line {0}): Unrecognized value directive \"{1}\"", token.line, directiveID)); } } else { if (directiveID.Equals("props", StringComparison.CurrentCulture)) { debugOutput.WriteLine("%Props Blk:"); if (hmdProperties == null) { debugOutput.WriteLine("Skipping %props block..."); IgnoreCurrentBlock(debugOutput, tokenizer); } else { ParsePropertiesFile(hmdProperties.root, hmdProperties, tokenizer, importPath, false); } } else if (directiveID.Equals("group", StringComparison.CurrentCulture)) { throw new FormatException("%group value directive must be in a %props block directive"); } else { throw new Exception(String.Format("Parser (line {0}): Unrecognized block directive \"{1}\"", token.line, directiveID)); } } } else if (token.type == HmdGlobalTokenType.CloseBrace) { if (parentIDStack.Count <= 0) { throw new FormatException(String.Format("Parser (line {0}): Unmatched close brace {1}", token.line, token)); } debugOutput.WriteLine("Block End : {0}", parentID.idOriginalCase); HmdBlockID temp = parentID; parentID = parentIDStack.Pop(); parentID.AddChild(temp); } else { throw new FormatException(String.Format("Parser (line {0}): Unexpected token {1}", token.line, token)); } } if (parentIDStack.Count > 0) { throw new FormatException(String.Format("Parser (EOF): Block \"{0}\" was not ended with '}'", parentIDStack.Peek().idOriginalCase)); } }