-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathFileLoadReport.cs
More file actions
136 lines (122 loc) · 5 KB
/
Copy pathFileLoadReport.cs
File metadata and controls
136 lines (122 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright 2018 faddenSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace CommonUtil {
/// <summary>
/// File load item, identifying the location, severity, and details of the issue.
/// </summary>
public class FileLoadItem {
public const int NO_LINE = -1;
public const int NO_COLUMN = -1;
public enum Type {
Unknown = 0,
Notice,
Warning,
Error
}
public int Line { get; private set; }
public int Column { get; private set; }
public Type MsgType { get; private set; }
public string Message { get; private set; }
public FileLoadItem(int line, int col, Type msgType, string msg) {
Line = line;
Column = col;
MsgType = msgType; ;
Message = msg;
}
}
/// <summary>
/// A structured collection of errors and warnings generated when reading data from a file.
/// </summary>
public class FileLoadReport : IEnumerable<FileLoadItem> {
// List of items. Currently unsorted; items will appear in the order they were added.
private List<FileLoadItem> mItems = new List<FileLoadItem>();
public string FileName { get; private set; }
public bool HasWarnings { get; private set; }
public bool HasErrors { get; private set; }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="fileName">Name of file being loaded. This is just for output; this
/// class doesn't actually try to access the file.</param>
public FileLoadReport(string fileName) {
FileName = fileName;
}
public IEnumerator<FileLoadItem> GetEnumerator() {
return mItems.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return mItems.GetEnumerator();
}
public int Count { get { return mItems.Count; } }
/// <summary>
/// Adds a new message to the report.
/// </summary>
/// <param name="isWarn">Is this a warning or an error?</param>
/// <param name="msg">Human-readable message.</param>
public void Add(FileLoadItem.Type msgType, string msg) {
Add(FileLoadItem.NO_LINE, FileLoadItem.NO_COLUMN, msgType, msg);
}
/// <summary>
/// Adds a new message to the report.
/// </summary>
/// <param name="line">Line where the issue was seen.</param>
/// <param name="col">Column where the problem starts, or NO_COLUMN.</param>
/// <param name="isWarn">Is this a warning or an error?</param>
/// <param name="msg">Human-readable message.</param>
public void Add(int line, int col, FileLoadItem.Type msgType, string msg) {
mItems.Add(new FileLoadItem(line, col, msgType, msg));
switch (msgType) {
case FileLoadItem.Type.Warning:
HasWarnings = true;
break;
case FileLoadItem.Type.Error:
HasErrors = true;
break;
}
}
/// <summary>
/// Formats the entire collection into a single multi-line string.
/// </summary>
/// <returns>Formatted string.</returns>
public string Format() {
StringBuilder sb = new StringBuilder();
if (mItems.Count > 0) {
sb.AppendFormat("File {0}:\r\n", FileName);
}
foreach (FileLoadItem item in mItems) {
if (item.Line != FileLoadItem.NO_LINE && item.Column != FileLoadItem.NO_COLUMN) {
sb.AppendFormat(" Line {0}.{1}: {2}: {3}\r\n", item.Line, item.Column,
item.MsgType.ToString().ToLower(), item.Message);
} else if (item.Line != FileLoadItem.NO_LINE) {
sb.AppendFormat(" Line {0}: {1}: {2}\r\n", item.Line,
item.MsgType.ToString().ToLower(), item.Message);
} else {
// Capitalized form looks nicer here.
sb.AppendFormat(" {0}: {1}\r\n", item.MsgType.ToString(), item.Message);
}
}
return sb.ToString();
}
public override string ToString() {
return "FileLoadReport: count=" + mItems.Count + " hasWarn=" +
HasWarnings + " hasErr=" + HasErrors;
}
}
}