The console application demonstrates the main functionality of the OTX-Runtime Converter API. It can be used as a reference and the source code below is like a reference guide about the proper programming of the API.
When the OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication is started without arguments, the usage of API will be exposed.
Suppose the current directory is installation location of OTX-Runtime Converter API.
Code snippet of the OTX-Runtime Converter API Sample Program.
5 using System.Diagnostics;
9 using System.Threading;
10 using System.Threading.Tasks;
12 namespace OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication
16 static void Main(
string[] args)
18 LicenseManager.SetLicenseKey(Constants.LICENSE);
19 IRuntimeConverter runtimeConverter =
null;
22 if (!Arguments.ReadArguments(args, out arguments, out message))
24 System.Console.WriteLine(message);
28 Config.TraceLevel = arguments.TraceLevel;
29 if (!
string.IsNullOrEmpty(arguments.OutputFolder))
31 Config.OutputFolder = arguments.OutputFolder;
36 Console.WriteLine(
"*** Tracer was setup");
37 Console.WriteLine(
"- Output folder".PadRight(20) + Config.OutputFolder);
38 Console.WriteLine(
"- Trace folder".PadRight(20) + Config.TraceFolder);
39 Console.WriteLine(
"- Trace level".PadRight(20) + Config.TraceLevel);
42 if (arguments.DiagPort != 0)
44 runtimeConverter = RuntimeConverterFactory.CreateSocketRuntimeConverter(arguments.DiagPort);
45 Console.WriteLine(
"Diag port is " + arguments.DiagPort);
47 else if (!
string.IsNullOrEmpty(arguments.DiagPipe))
49 runtimeConverter = RuntimeConverterFactory.CreatePipeRuntimeConverter(arguments.DiagPipe);
50 Console.WriteLine(
"Diag pipe is " + arguments.DiagPipe);
54 runtimeConverter = RuntimeConverterFactory.CreateSocketRuntimeConverter(8888);
55 Console.WriteLine(
"Diag port is 8888");
58 runtimeConverter.XmlDbPort = arguments.XmlDbPort;
59 Console.WriteLine(
"XmlDb port is " + runtimeConverter.XmlDbPort);
61 if (!
string.IsNullOrEmpty(arguments.ImportDVGs))
64 Console.WriteLine(
"Importing DiagValidationGroups from {0}", GetAbsolutePath(arguments.ImportDVGs));
65 runtimeConverter.DiagValidationGroups =
new DiagValidationGroups();
66 runtimeConverter.DiagValidationGroups.Import(GetAbsolutePath(arguments.ImportDVGs));
67 Console.WriteLine(
"All DiagValidationGroups are imported");
71 if (arguments.Type == Arguments.Types.ptx)
73 Console.WriteLine(
"Loading ptx {0}", GetAbsolutePath(arguments.InputPath));
74 runtimeConverter.LoadPtx(GetAbsolutePath(arguments.InputPath), arguments.Password);
76 else if (arguments.Type == Arguments.Types.ppx)
78 Console.WriteLine(
"Loading ppx {0}", GetAbsolutePath(arguments.InputPath));
79 runtimeConverter.LoadPpx(GetAbsolutePath(arguments.InputPath), arguments.Password);
83 Console.WriteLine(
"Loading project {0}", GetAbsolutePath(arguments.InputPath));
84 runtimeConverter.LoadProject(GetAbsolutePath(arguments.InputPath));
87 if (arguments.Validate)
90 StartClock(
"Validating...");
91 IError[] errors = runtimeConverter.Validate();
94 Console.WriteLine(
"Validation is completed.");
95 if (errors.Length > 0)
98 Console.WriteLine(
"*** {0} Error(s):", errors.Count(err => err.SeverityType ==
SeverityTypes.Critical));
99 foreach (IError error
in errors.Where(err => err.SeverityType ==
SeverityTypes.Critical))
105 Console.WriteLine(
"*** {0} Warning(s):", errors.Count(err => err.SeverityType ==
SeverityTypes.Warning));
106 foreach (IError error
in errors.Where(err => err.SeverityType ==
SeverityTypes.Warning))
112 Console.WriteLine(
"*** {0} Deprecation(s):", errors.Count(err => err.SeverityType ==
SeverityTypes.Deprecation));
113 foreach (IError error
in errors.Where(err => err.SeverityType ==
SeverityTypes.Deprecation))
119 Console.WriteLine(
"*** {0} Specification(s):", errors.Count(err => err.SeverityType ==
SeverityTypes.Specification));
120 foreach (IError error
in errors.Where(err => err.SeverityType ==
SeverityTypes.Specification))
125 if (errors.FirstOrDefault(err => err.SeverityType ==
SeverityTypes.Critical) !=
null)
128 Console.WriteLine(
"*** The {0} cannot be compiled because it has {1} error(s) and {2} warning(s).", arguments.Type, errors.Count(err => err.SeverityType ==
SeverityTypes.Critical), errors.Count(err => err.SeverityType ==
SeverityTypes.Warning));
134 if (File.Exists(arguments.OutputFile))
136 string selection =
string.Empty;
137 while (selection.Trim() !=
"y" && selection.Trim() !=
"n")
140 Console.Write(
"Override existing file {0}? y(yes)/n(no): ", arguments.OutputFile);
141 selection = Console.ReadLine();
144 if (selection ==
"n")
151 Console.WriteLine(
"Compiling ...");
152 runtimeConverter.Compile(arguments.OutputFile);
153 if (File.Exists(arguments.OutputFile))
155 Console.WriteLine(
"{0} is written successfully.", arguments.OutputFile);
159 private static string GetAbsolutePath(
string path)
161 return Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), path);
164 private static void WriteError(IError error)
166 Console.Write(
"<SeverityType>");
167 Console.Write(error.SeverityType);
168 Console.Write(
"</SeverityType>");
169 Console.Write(
"<Description>");
170 Console.Write(error.Description);
171 Console.Write(
"</Description>");
172 Console.Write(
"<ScopeId>");
173 Console.Write(error.ScopeId);
174 Console.Write(
"</ScopeId>");
175 Console.Write(
"<ScopeName>");
176 Console.Write(error.ScopeName);
177 Console.Write(
"</ScopeName>");
178 Console.Write(
"<ElementType>");
179 Console.Write(error.ElementType);
180 Console.Write(
"</ElementType>");
181 Console.Write(
"<ErrorCode>");
182 Console.Write(error.ErrorCode);
183 Console.Write(
"</ErrorCode>");
184 Console.Write(
"<Procedure>");
185 Console.Write(error.Procedure);
186 Console.Write(
"</Procedure>");
187 Console.Write(
"<Document>");
188 Console.Write(error.Document);
189 Console.Write(
"</Document>");
190 Console.Write(
"<Package>");
191 Console.Write(error.Package);
192 Console.Write(
"</Package>");
193 Console.Write(
"<Project>");
194 Console.Write(error.Project);
195 Console.Write(
"</Project>");
199 private static CancellationTokenSource cts;
201 private static void StartClock(
string text)
203 cts =
new CancellationTokenSource();
205 Stopwatch sw = Stopwatch.StartNew();
209 while (!cts.Token.IsCancellationRequested)
211 Console.Write($
"\r{text} {sw.Elapsed:mm\\:ss}");
219 private static void StopClock()
7 namespace OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication
11 using System.Collections.
Generic;
18 public class Arguments
27 private const string UnrecognizeOption =
"Unrecognized option: ";
28 private const string optionType =
"-type";
29 private const string optionPassword =
"-password";
30 private const string optionValidate =
"-validate";
31 private const string optionOut =
"-out";
32 private const string optionImportDVGs =
"-importDVGs";
33 private const string optionDiagPort =
"-diagPort";
34 private const string optionDiagPipe =
"-diagPipe";
35 private const string optionTraceLevel =
"-traceLevel";
36 private const string optionOutputFolder =
"-outputFolder";
37 private const string optionDbPort =
"-dbPort";
52 public string InputPath {
get;
set; }
53 public Types Type {
get;
private set; }
54 public string Password {
get;
private set; }
55 public bool Validate {
get;
private set; }
56 public string OutputFile {
get;
set; }
57 public string ImportDVGs {
get;
private set; }
58 public ushort DiagPort {
get;
private set; }
59 public string DiagPipe {
get;
private set; }
61 public string OutputFolder {
get;
private set; }
62 public ushort XmlDbPort {
get;
private set; }
66 public static bool ReadArguments(
string[] args, out Arguments arguments, out
string message)
69 arguments =
new Arguments();
70 arguments.Type = Types.ptx;
71 arguments.DiagPort = 8888;
72 arguments.XmlDbPort = 1984;
75 message = PrintUsage();
79 foreach (
string arg
in args)
81 if (arg.StartsWith(
"-"))
83 if (!ProcessOption(arg, arguments, out message))
90 if (
string.IsNullOrEmpty(arguments.InputPath))
92 arguments.InputPath = arg;
96 message =
string.Format(
"More than 1 input path: {0}, {1}.", arguments.InputPath, arg);
102 if (
string.IsNullOrEmpty(arguments.InputPath))
104 message = PrintUsage();
108 if (
string.IsNullOrEmpty(arguments.OutputFile))
110 if ((arguments.Type == Types.ptx || arguments.Type == Types.ppx))
112 arguments.OutputFile = arguments.InputPath;
116 message =
"The -out:<file> is mandatory if -type:project is set.";
124 private static bool ProcessOption(
string value, Arguments arguments, out
string error)
127 if (!value.StartsWith(
"-"))
129 throw new ArgumentException(value +
" is not an option.");
132 List<string> arr = value.Split(
':').ToList();
133 string optionName = arr[0];
135 string optionValue =
string.Join(
":", arr);
139 if (optionValue ==
"ptx")
141 arguments.Type = Types.ptx;
143 else if (optionValue ==
"ppx")
145 arguments.Type = Types.ppx;
147 else if (optionValue ==
"project")
149 arguments.Type = Types.project;
153 error = UnrecognizeOption + value +
".";
159 arguments.Password = optionValue;
162 if (!
string.IsNullOrEmpty(optionValue))
164 error = UnrecognizeOption + value +
".";
168 arguments.Validate =
true;
171 arguments.OutputFile = optionValue;
173 case optionImportDVGs:
174 arguments.ImportDVGs = optionValue;
178 if (ushort.TryParse(optionValue, out diagPort))
180 arguments.DiagPort = diagPort;
184 error = UnrecognizeOption + value +
".";
190 arguments.DiagPipe = optionValue;
191 arguments.DiagPort = 0;
193 case optionTraceLevel:
195 if (Enum.TryParse(optionValue.ToUpper(), out traceLevel))
197 arguments.TraceLevel = traceLevel;
201 error = UnrecognizeOption + value +
".";
205 case optionOutputFolder:
206 arguments.OutputFolder = optionValue;
210 if (ushort.TryParse(optionValue, out dbPort))
212 arguments.XmlDbPort = dbPort;
216 error = UnrecognizeOption + value +
".";
221 error = UnrecognizeOption + value +
".";
228 private static bool ParseDictionary(
string str, out Dictionary<string, string> dictionary)
230 dictionary =
new Dictionary<string, string>();
232 if (str.StartsWith(
"{") && str.EndsWith(
"}"))
234 str = str.Substring(1, str.Length - 2);
235 string[] arr = str.Split(
',');
236 foreach (
string pair
in arr)
238 string[] nameAndValue = pair.Split(
'=');
239 if (nameAndValue.Length != 2)
244 string name = nameAndValue[0];
245 string value = nameAndValue[1];
246 if (dictionary.ContainsKey(arr[0]))
251 dictionary.Add(name, value);
260 private static string PrintUsage()
262 StringBuilder builder =
new StringBuilder();
264 builder.AppendLine(
string.Format(
"RuntimeConverter version {0}", Config.Version));
265 builder.AppendLine(
"Usage: OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication [options] <path>");
266 builder.AppendLine();
267 builder.AppendLine(
"<path>".PadRight(count) +
"ptx file, ppx file or project directory");
268 builder.AppendLine();
269 builder.AppendLine(
"options".PadLeft(count));
270 builder.AppendLine(
string.Format(
"{0}:ptx", optionType).PadRight(count) +
"Load a ptx file (default)");
271 builder.AppendLine(
string.Format(
"{0}:ppx", optionType).PadRight(count) +
"Load a ppx file");
272 builder.AppendLine(
string.Format(
"{0}:project", optionType).PadRight(count) +
"Load a project directory containing project file (*.otfPrj)");
273 builder.AppendLine(
string.Format(
"{0}:<password>", optionPassword).PadRight(count) +
"Specify password to decrypt the input ptx or ppx");
274 builder.AppendLine(
string.Format(
"{0}", optionValidate).PadRight(count) +
"Validate before compiling. If an error occurs, compiling will be canceled");
275 builder.AppendLine(
string.Format(
"{0}:<file>", optionOut).PadRight(count) +
"Specify output file");
276 builder.AppendLine(
new string(
' ', count) +
string.Format(
"If {0}:ptx or {0}:ppx is set and this option is ommitted, output file will be the same as input file", optionType));
277 builder.AppendLine(
new string(
' ', count) +
string.Format(
"If {0}:project is set, this option is mandatory", optionType));
278 builder.AppendLine(
string.Format(
"{0}:<file>", optionImportDVGs).PadRight(count) +
"Import a DiagValidationGroups file (xml)");
279 builder.AppendLine(
string.Format(
"{0}:<port>", optionDiagPort).PadRight(count) +
"Specify diag port. Default is 8888");
280 builder.AppendLine(
string.Format(
"{0}:<pipe name>", optionDiagPipe).PadRight(count) +
"Specify diag pipe name");
281 builder.AppendLine(
string.Format(
"{0}:<db port>", optionDbPort).PadRight(count) +
"Specify XmlDb port. Default is 1984");
282 builder.AppendLine(
string.Format(
"{0}:<trace level>", optionTraceLevel).PadRight(count) +
"Specify trace level: ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL or OFF (default)");
283 builder.AppendLine(
string.Format(
"{0}:<folder>", optionOutputFolder).PadRight(count) +
"Specify a folder where BaseX databases, extracted files, trace files are put in");
284 return builder.ToString();