public HeaderField(string attribColName, VnaFileSection section, int indexInFileLine, string sourceFieldName) { AttribColName = attribColName; Section = section; SourceFieldIndex = indexInFileLine; SourceFieldName = sourceFieldName; }
public void ReadStarLine_Changes_State_If_Star_Line_Match_Is_Found(string inputLine, VnaFileSection expectedState) { var reader = new VnaAdjListReader(); Assert.Equal(VnaFileSection.None, reader.State); var state = reader.ReadStarLine(inputLine); Assert.Equal(expectedState, state); }
public void ReadNodePropertyFields_Throws_If_Not_In_Required_State(VnaFileSection state) { var reader = new VnaAdjListReader(); reader.State = state; var ex = Assert.Throws<InvalidOperationException>(() => reader.ReadNodePropertyFields(new string[] { "X" }, null, -1, null)); }
public void ReadHeadersIntoTargetList_Populates_List_Of_Header_Fields(int index, string[] inputs, string[] expectedResults, int[] expectedIndices, VnaFileSection state) { var reader = new VnaAdjListReader(); reader.State = state; var target = new List<HeaderField>(); var count = reader.ReadHeadersIntoTargetList(inputs, target); Assert.Equal(expectedResults.Length, count); Assert.Equal(expectedResults.Length, target.Count); for (int i = 0; i < expectedResults.Length; i++) { Assert.False(string.IsNullOrWhiteSpace(target[i].AttribColName)); Assert.Equal(expectedResults[i], target[i].AttribColName); Assert.Equal(state, target[i].Section); Assert.Equal(expectedIndices[i], target[i].SourceFieldIndex); } }
public void ValidateHeaderFields_Checks_For_Min_Required_Fields(VnaFileSection state, string[] headerNames, bool expectedResult) { string mssg = null; var reader = new VnaAdjListReader(); bool result = reader.ValidateHeaderFields(headerNames, state, out mssg); Assert.Equal(expectedResult, result); if (expectedResult) Assert.Null(mssg); else Assert.NotNull(mssg); }
/// <summary> /// Verifies the minimum number of header fields are present depending on what the state is. /// </summary> internal bool ValidateHeaderFields(string[] headerFields, VnaFileSection state, out string message) { message = null; if(state == VnaFileSection.NodeData) { // minimum of 1 if(headerFields == null || headerFields.Length == 0) { message = string.Format("At least 1 header field name (e.g. Id) is required for the *Node data section."); return false; } } else if (state == VnaFileSection.EdgeData) { // minimum of 2 if (headerFields == null || headerFields.Length < 2) { message = string.Format("At least 2 header field names (e.g. From, To) are required for the *Edge data section."); return false; } } return true; }
/// <summary> /// Populates the appropriate header attribute name list with all items in <paramref name="headers"/> /// that are not null/empty/whitespace. The list populated depends on the value of <paramref name="state"/>. /// </summary> /// <param name="headers">An array of header fields parsed from a header line</param> /// <param name="state">The current state of the reader</param> internal void ReadHeaders(string[] headers, VnaFileSection state) { switch (state) { case VnaFileSection.NodeData: // headers must have at least the Id field ReadHeadersIntoTargetList(headers, NodeDataHeaders); break; case VnaFileSection.NodeProperties: ReadHeadersIntoTargetList(headers, NodePropertyHeaders); break; case VnaFileSection.EdgeData: // headers must have at least the from, to fields ReadHeadersIntoTargetList(headers, EdgeDataHeaders); break; case VnaFileSection.EdgeProperties: ReadHeadersIntoTargetList(headers, EdgePropertyHeaders); break; default: throw new InvalidOperationException(); } }
internal void DoSecondPass(StreamReader reader, Microsoft.VisualBasic.FileIO.TextFieldParser parser, IBasicAdjList network) { // now, the network had been created based on the headers of each section, the list of // header fields has been populated for each section, nodes have been created. // so populate the node data, create the edges, and populate edge data // 1) read line // 2) if line is a '*' line // read line and set state, skip next line // 3) read items based on state int index = 0; string line = null; string[] fields = null; try { while (!parser.EndOfData) { if (parser.PeekChars(1) == "*") { line = parser.ReadLine(); State = ReadStarLine(line); // skip the header line parser.ReadLine(); // reset the index at each * section index = 0; continue; } // if here, then it is not a header line or star line fields = Clean(parser.ReadFields()); switch (State) { case VnaFileSection.NodeData: ReadNodeDataFields(fields, network, index++, NodeDataHeaders); break; case VnaFileSection.NodeProperties: ReadNodePropertyFields(fields, network, index++, NodePropertyHeaders); break; case VnaFileSection.EdgeData: ReadEdgeDataFields(fields, network, EdgeDataHeaders); break; case VnaFileSection.EdgeProperties: ReadEdgePropertyFields(fields, network, index++, EdgePropertyHeaders); break; default: throw new InvalidOperationException(string.Format("{0} is an invalid state", State)); } } } catch (FormatException ex) { var sb = new StringBuilder(); sb.AppendFormat("A format error occured while reading the line {0}.{1}", parser.LineNumber, Environment.NewLine); sb.Append(ex.Message); ErrorMessages.Add(sb.ToString()); } catch(NullReferenceException ex) { var sb = new StringBuilder(); sb.AppendFormat("A null value occured while reading the line {0}.{1}", parser.LineNumber, Environment.NewLine); sb.Append(ex.Message); ErrorMessages.Add(sb.ToString()); } catch (Exception ex) { var sb = new StringBuilder(); sb.AppendFormat("An error occurred while reading the line {0}.{1}", parser.LineNumber, Environment.NewLine); sb.Append(ex.Message); ErrorMessages.Add(sb.ToString()); } }
internal void DoFirstPass(TextReader reader, Microsoft.VisualBasic.FileIO.TextFieldParser parser, out int nodeCount, out int edgeCount) { string mssg = null; nodeCount = 0; edgeCount = 0; string line = null; string[] fields = null; try { while (!parser.EndOfData) { if (parser.PeekChars(1) == "*") { line = parser.ReadLine(); State = ReadStarLine(line); // next line is header fields = Clean(parser.ReadFields()); if (ValidateHeaderFields(fields, State, out mssg)) ReadHeaders(fields, State); else { ErrorMessages.Add(mssg); break; } continue; } line = parser.ReadLine(); // if here, then it is not a header line or star line switch (State) { case VnaFileSection.NodeData: nodeCount++; break; case VnaFileSection.EdgeData: edgeCount++; break; } } } catch (NullReferenceException ex) { var sb = new StringBuilder(); sb.AppendFormat("A null value occured while reading the line {0}.{1}", parser.LineNumber, Environment.NewLine); sb.Append(ex.Message); ErrorMessages.Add(sb.ToString()); } catch (Exception ex) { var sb = new StringBuilder(); sb.AppendFormat("An error occurred while reading the line {0}.{1}", parser.LineNumber, Environment.NewLine); sb.Append(ex.Message); ErrorMessages.Add(sb.ToString()); } }
public void Reset() { Network = null; State = VnaFileSection.None; NodeKeyToNodeIndexMap.Clear(); NodeDataHeaders.Clear(); NodePropertyHeaders.Clear(); EdgeDataHeaders.Clear(); EdgePropertyHeaders.Clear(); IsCancellationPending = false; }