/// <summary> /// Compares two JB64Value elements for equality. /// </summary> /// <param name="OtherVal">The JB64Value object to compare this one with.</param> /// <returns>True if equal, false if not.</returns> public bool Equals(JB64Value OtherVal) { if (this.Type != OtherVal.Type) { return(false); } if (this.Val == null && OtherVal.Val == null) { return(true); } if (this.Val == null || OtherVal.Val == null) { return(false); } switch (this.Type) { case "boolean": return((bool)this.Val == (bool)OtherVal.Val); case "integer": return((int)this.Val == (int)OtherVal.Val); case "number": return((double)this.Val == (double)OtherVal.Val); case "date": case "time": case "string": return((string)this.Val == (string)OtherVal.Val); default: byte[] Val1 = (byte[])this.Val; byte[] Val2 = (byte[])OtherVal.Val; if (Val1.Length != Val2.Length) { return(false); } int y = Val1.Length; for (int x = 0; x < y; x++) { if (Val1[x] != Val2[x]) { return(false); } } return(true); } }
/// <summary> /// Accepts a JSON-Base64 string representing record data, decodes it, and maps it using the headers or the header map into an array of JB64Value entries. /// </summary> /// <param name="Line">A line of JSON-Base64 encoded input.</param> /// <returns>An array of JB64Value entries.</returns> /// <exception cref="JB64Exception">Thrown if the record data is unable to be decoded.</exception> public JB64Value[] DecodeRecord(string Line) { if (this.Headers == null) throw new JB64Exception("JSON-Base64 headers must be decoded before record data can be decoded."); JArray Data = DecodeLine(Line); if (Data.Count != this.Headers.Count) throw new JB64Exception("JSON-Base64 record data does not have the same number of columns as the headers."); // Convert the data and normalize the input. int x = 0; JB64Value[] Result = new JB64Value[Data.Count]; foreach (JToken Col in Data) { if (Col.Type == JTokenType.Null) Result[x] = new JB64Value(); else if (Col.Type == JTokenType.Boolean) Result[x] = new JB64Value((bool)Col); else if (Col.Type == JTokenType.Integer) Result[x] = new JB64Value((int)Col); else if (Col.Type == JTokenType.Float) Result[x] = new JB64Value((double)Col); else if (Col.Type == JTokenType.String) Result[x] = new JB64Value((string)Col); else throw new JB64Exception("Invalid JSON-Base64 column type found."); Result[x].ConvertTo(this.Headers[x].Type, "decode"); x++; } // Map the result type. if (this.Map != null) { x = 0; foreach (JB64HeaderMap Header in this.Map) { if (Result[x].Val == null && !Header.NullAllowed) Result[x].ConvertToNotNull(Header.Type); if (Result[x].Val != null) { if (Result[x].Type == "boolean" && Header.Type != "boolean") Result[x].ConvertToInteger(); Result[x].ConvertTo(Header.Type); } x++; } } return Result; }
public JB64Value(JB64Value Val) { this.Val = Val.Val; this.Type = Val.Type; }
/// <summary> /// Accepts a JSON-Base64 string representing record data, decodes it, and maps it using the headers or the header map into an array of JB64Value entries. /// </summary> /// <param name="Line">A line of JSON-Base64 encoded input.</param> /// <returns>An array of JB64Value entries.</returns> /// <exception cref="JB64Exception">Thrown if the record data is unable to be decoded.</exception> public JB64Value[] DecodeRecord(string Line) { if (this.Headers == null) { throw new JB64Exception("JSON-Base64 headers must be decoded before record data can be decoded."); } JArray Data = DecodeLine(Line); if (Data.Count != this.Headers.Count) { throw new JB64Exception("JSON-Base64 record data does not have the same number of columns as the headers."); } // Convert the data and normalize the input. int x = 0; JB64Value[] Result = new JB64Value[Data.Count]; foreach (JToken Col in Data) { if (Col.Type == JTokenType.Null) { Result[x] = new JB64Value(); } else if (Col.Type == JTokenType.Boolean) { Result[x] = new JB64Value((bool)Col); } else if (Col.Type == JTokenType.Integer) { Result[x] = new JB64Value((int)Col); } else if (Col.Type == JTokenType.Float) { Result[x] = new JB64Value((double)Col); } else if (Col.Type == JTokenType.String) { Result[x] = new JB64Value((string)Col); } else { throw new JB64Exception("Invalid JSON-Base64 column type found."); } Result[x].ConvertTo(this.Headers[x].Type, "decode"); x++; } // Map the result type. if (this.Map != null) { x = 0; foreach (JB64HeaderMap Header in this.Map) { if (Result[x].Val == null && !Header.NullAllowed) { Result[x].ConvertToNotNull(Header.Type); } if (Result[x].Val != null) { if (Result[x].Type == "boolean" && Header.Type != "boolean") { Result[x].ConvertToInteger(); } Result[x].ConvertTo(Header.Type); } x++; } } return(Result); }
/// <summary> /// Compares two JB64Value elements for equality. /// </summary> /// <param name="OtherVal">The JB64Value object to compare this one with.</param> /// <returns>True if equal, false if not.</returns> public bool Equals(JB64Value OtherVal) { if (this.Type != OtherVal.Type) return false; if (this.Val == null && OtherVal.Val == null) return true; if (this.Val == null || OtherVal.Val == null) return false; switch (this.Type) { case "boolean": return ((bool)this.Val == (bool)OtherVal.Val); case "integer": return ((int)this.Val == (int)OtherVal.Val); case "number": return ((double)this.Val == (double)OtherVal.Val); case "date": case "time": case "string": return ((string)this.Val == (string)OtherVal.Val); default: byte[] Val1 = (byte[])this.Val; byte[] Val2 = (byte[])OtherVal.Val; if (Val1.Length != Val2.Length) return false; int y = Val1.Length; for (int x = 0; x < y; x++) { if (Val1[x] != Val2[x]) return false; } return true; } }
/// <summary> /// Accepts a set of data and then encodes it using the header information for each column as per the JSON-Base64 spec. /// </summary> /// <param name="Data">An array of JB64Value entries. Must have the same number of columns as the header.</param> /// <returns>A JSON-Base64 encoded string suitable for writing out to a binary streaming resource (e.g. file, network, etc).</returns> /// <exception cref="JB64Exception">Thrown if headers haven't been encoded first or the number of Data elements don't match the number of header elements.</exception> public string EncodeRecord(JB64Value[] Data) { if (this.Headers == null) throw new JB64Exception("JSON-Base64 headers must be encoded before record data can be encoded."); if (Data.Length != this.Headers.Count) throw new JB64Exception("The number of data elements must match the number of headers."); // Normalize the input. JArray Result = new JArray(); int x = 0; foreach (JB64Header Header in this.Headers) { Data[x].ConvertTo(Header.Type, "encode"); Result.Add(Data[x].Val); x++; } return EncodeLine(Result); }
public List<SetOptionsEntry[]> GetSetOptionsEntryList(bool AllowBinary) { List<SetOptionsEntry[]> SetOptionsEntries = new List<SetOptionsEntry[]>(); foreach (ISheet TempSheet in Workbook) { // Get the headers and first record. if (TempSheet.FirstRowNum >= 0) { IRow HeaderRow = TempSheet.GetRow(TempSheet.FirstRowNum); IRow RecordRow = TempSheet.GetRow(TempSheet.FirstRowNum + 1); if (HeaderRow != null && RecordRow != null) { SetOptionsEntries.Add(new SetOptionsEntry[HeaderRow.LastCellNum - HeaderRow.FirstCellNum]); int y = HeaderRow.LastCellNum - HeaderRow.FirstCellNum; for (int x = 0; x < y; x++) { ICell TempHeaderCell = HeaderRow.GetCell(HeaderRow.FirstCellNum + x); ICell TempRecordCell = RecordRow.GetCell(RecordRow.FirstCellNum + x); string HeaderName = (TempHeaderCell == null ? "" : TempHeaderCell.StringCellValue); JB64Value Result; if (TempRecordCell == null) { Result = new JB64Value(""); } else if (TempRecordCell.CellType == CellType.Boolean) { Result = new JB64Value(TempRecordCell.BooleanCellValue); } else if (TempRecordCell.CellType == CellType.Numeric) { Result = new JB64Value(TempRecordCell.NumericCellValue); } else { Result = new JB64Value(TempRecordCell.StringCellValue); } SetOptionsEntries[SetOptionsEntries.Count - 1][x] = new SetOptionsEntry(HeaderName, Result.Type, Result, true); } } } } return SetOptionsEntries; }
public JB64Value[] ReadRecord() { IRow RecordRow = this.CurrSheet.GetRow(this.CurrRowNum); this.CurrRowNum++; if (RecordRow == null) return null; JB64Value[] Result = new JB64Value[this.NumCols]; for (int x = 0; x < this.NumCols; x++) { ICell TempRecordCell = RecordRow.GetCell(RecordRow.FirstCellNum + x); if (TempRecordCell == null) { Result[x] = new JB64Value(""); } else if (TempRecordCell.CellType == CellType.Boolean) { Result[x] = new JB64Value(TempRecordCell.BooleanCellValue); } else if (TempRecordCell.CellType == CellType.Numeric) { Result[x] = new JB64Value(TempRecordCell.NumericCellValue); } else { Result[x] = new JB64Value(TempRecordCell.StringCellValue); } } return Result; }
private string SetOptions_GetDisplayValue(JB64Value Val, string Type) { if (Val.Type == "number") { if (Type == "date") Val = new JB64Value(ExcelDateToUTC((double)Val.Val)); else if (Type == "date-alt") Val = new JB64Value(ExcelDateToLocal((double)Val.Val)); else if (Type == "time") Val = new JB64Value(ExcelTimeToUTC((double)Val.Val)); else if (Type == "time-alt") Val = new JB64Value(ExcelTimeToLocal((double)Val.Val)); else Val = new JB64Value(Val); } else { Val = new JB64Value(Val); } Val.ConvertTo(Type.Replace("-alt", "")); Val.ConvertToString(); string Val2 = (string)Val.Val; if (Type == "boolean") { Val2 = (Val2 == "0" ? "false" : "true"); } if (Val2.Length > 50) { Val2 = Val2.Substring(0, 50) + "..."; } return Val2; }
private void RunConversion(object sender, DoWorkEventArgs e) { BackgroundWorker TempWorker = (BackgroundWorker)sender; int RecordNum = 1; bool NullAllowed = this.WriteConvert.IsNullAllowed(); SetOptionsEntry[] TempOptionsEntry = this.SetOptionsEntries[this.SetOptionsActiveEntry]; while (!this.ReadConvert.EndOfRecords()) { if (TempWorker.CancellationPending) { this.AllowNext = false; break; } // Read in a record. try { JB64Value[] TempRecord = this.ReadConvert.ReadRecord(); // Transform the record. try { for (int x = 0; x < TempRecord.Length; x++) { if (TempOptionsEntry[x].Enabled) { if (!NullAllowed) TempRecord[x].ConvertToNotNull(TempOptionsEntry[x].Type); if (TempOptionsEntry[x].OrigType == "number") { if (TempOptionsEntry[x].Type == "date") TempRecord[x] = new JB64Value(ExcelDateToUTC((double)TempRecord[x].Val)); else if (TempOptionsEntry[x].Type == "date-alt") TempRecord[x] = new JB64Value(ExcelDateToLocal((double)TempRecord[x].Val)); else if (TempOptionsEntry[x].Type == "time") TempRecord[x] = new JB64Value(ExcelTimeToUTC((double)TempRecord[x].Val)); else if (TempOptionsEntry[x].Type == "time-alt") TempRecord[x] = new JB64Value(ExcelTimeToLocal((double)TempRecord[x].Val)); } TempRecord[x].ConvertTo(TempOptionsEntry[x].Type.Replace("-alt", "")); } } // Write the record. try { this.WriteConvert.WriteRecord(TempRecord); } catch (Exception) { this.ConversionResults += "Record #" + RecordNum + ": Error writing the record.\r\n"; } } catch (Exception) { this.ConversionResults += "Record #" + RecordNum + ": Error transforming the record.\r\n"; } } catch (Exception) { this.ConversionResults += "Record #" + RecordNum + ": Error reading the record.\r\n"; } RecordNum++; int Percent = this.ReadConvert.PercentRead(); TempWorker.ReportProgress(Percent, Percent.ToString() + "%"); } this.WriteConvert.Close(); this.ConversionResults += "Conversion completed.\r\n"; }
public void WriteRecord(JB64Value[] Data) { this.TempFile.Write(Encoder.EncodeRecord(Data)); }
public void WriteRecord(JB64Value[] Data) { IRow TempRow = this.CurrSheet.CreateRow(this.CurrRow); this.CurrRow++; for (int x = 0; x < Data.Length; x++) { ICell TempCell = TempRow.CreateCell(x); if (Data[x].Type == "boolean") { TempCell.SetCellValue((bool)Data[x].Val); } else if (Data[x].Type == "integer" || Data[x].Type == "number") { Data[x].ConvertToNumber(); TempCell.SetCellValue((double)Data[x].Val); } else { if (Data[x].Type == "date") Data[x] = new JB64Value(MainWizard.UTCToLocalDate((string)Data[x].Val), "date"); if (Data[x].Type == "time") Data[x] = new JB64Value(MainWizard.UTCToLocalTime((string)Data[x].Val), "time"); Data[x].ConvertToString(); TempCell.SetCellValue((string)Data[x].Val); } } }
public SetOptionsEntry(string Name, string OrigType, JB64Value OrigVal, bool Enabled) { this.Name = Name; this.OrigType = OrigType; this.OrigVal = OrigVal; this.Type = OrigType; this.Enabled = Enabled; }