public async ValueTask WriteStringAsync(StringBuilder sb, Stream stream, CancellationToken token) { var remaining = FreeCapacity; var value = sb.ToString(); // Try with an approximate cell value length var bytesNeeded = value.Length * Utf8Helper.MaxBytePerChar; if (bytesNeeded > remaining) { // Try with a more accurate cell value length bytesNeeded = Utf8Helper.GetByteCount(value); } if (bytesNeeded > remaining) { await FlushToStreamAsync(stream, token).ConfigureAwait(false); } // Write whole value if it fits in the buffer if (bytesNeeded <= _buffer.Length) { _index += Utf8Helper.GetBytes(value, GetSpan()); return; } // Otherwise, write value piece by piece var valueIndex = 0; while (!WriteLongString(value.AsSpan(), ref valueIndex)) { await FlushToStreamAsync(stream, token).ConfigureAwait(false); } }
protected virtual ILocalizationDictionary CreateDictionaryFromFile(IFileInfo file) { using (var stream = file.CreateReadStream()) { return(CreateDictionaryFromFileContent(Utf8Helper.ReadStringFromStream(stream))); } }
public override void Initialize(string sourceName, ILocalizationConfiguration localizationConfiguration) { CultureInfo[] allCultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures); List<string> resourceNames = _assembly.GetManifestResourceNames().Where(resourceName => allCultureInfos.Any(culture => resourceName.EndsWith($"{sourceName}.json", true, null) || resourceName.EndsWith($"{sourceName}-{culture.Name}.json", true, null))).ToList(); foreach (string resourceName in resourceNames) { if (!resourceName.StartsWith(_rootNamespace)) continue; using (Stream stream = _assembly.GetManifestResourceStream(resourceName)) { string jsonString = Utf8Helper.ReadStringFromStream(stream); JsonLocalizationDictionary dictionary = CreateJsonLocalizationDictionary(jsonString); string dicCultureInfoName = dictionary.CultureInfo.Name; if (Dictionaries.ContainsKey(dicCultureInfoName)) { throw new Exception(sourceName + " source contains more than one dictionary for the culture: " + dicCultureInfoName); } Dictionaries[dicCultureInfoName] = dictionary; LanguageIcon languageIcon = LocalizationConsts.LanguageIcons.Where(p => dicCultureInfoName.StartsWith(p.CountryName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); localizationConfiguration.Languages.Add( new LanguageInfo(dicCultureInfoName, dictionary.CultureInfo.NativeName, icon: languageIcon?.Icon, isDefault: CultureInfo.CurrentUICulture.Name.Equals(dicCultureInfoName))); if (!resourceName.EndsWith(sourceName + ".json")) continue; if (DefaultDictionary != null) { throw new Exception("Only one default localization dictionary can be for source: " + sourceName); } DefaultDictionary = dictionary; } } }
private List <MenuDefinition> LoadMenu() { string xmlString; using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("JK.Web.appMenus.config")) { xmlString = Utf8Helper.ReadStringFromStream(stream); } using (var sr = new StringReader(xmlString)) { using (var xr = XmlReader.Create(sr, new XmlReaderSettings { CloseInput = true, IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true })) { var xmlDocument = new XmlDocument(); xmlDocument.Load(xr); var root = xmlDocument.SelectSingleNode("/root"); var menuDefinitionNode = root.SelectNodes("menuDefinition"); if (menuDefinitionNode == null || menuDefinitionNode.Count <= 0) { throw new AbpException("A Menu Xml must include menuDefinition as root node."); } List <MenuDefinition> list = new List <MenuDefinition>(); foreach (XmlNode node in menuDefinitionNode) { var name = node.GetAttributeValueOrNull("name"); if (string.IsNullOrEmpty(name)) { throw new AbpException("name is not defined in menu XML file!"); } var displayName = node.GetAttributeValueOrNull("displayName"); var menuDefinition = new MenuDefinition(name, L(displayName)); if (node.HasChildNodes) { foreach (XmlNode item in node.ChildNodes) { var itemName = item.GetAttributeValueOrNull("name"); if (string.IsNullOrEmpty(name)) { throw new AbpException("name is not defined in menu XML file!"); } var itemDisplayName = item.GetAttributeValueOrNull("displayName"); var menuItemDefinition = new MenuItemDefinition(itemName, L(itemDisplayName)); Iterate(menuItemDefinition, item); menuDefinition.AddItem(menuItemDefinition); } } list.Add(menuDefinition); } return(list); } } }
public override void Initialize(string sourceName) { var resourceNames = assembly.GetManifestResourceNames(); foreach (var resourceName in resourceNames) { if (resourceName.StartsWith(rootNamespace)) { using (var stream = assembly.GetManifestResourceStream(resourceName)) { var jsonString = Utf8Helper.ReadStringFromStream(stream); var dictionary = CreateJsonLocalizationDictionary(jsonString); if (Dictionaries.ContainsKey(dictionary.CultureInfo.Name)) { throw new StudioXInitializationException(sourceName + " source contains more than one dictionary for the culture: " + dictionary.CultureInfo.Name); } Dictionaries[dictionary.CultureInfo.Name] = dictionary; if (resourceName.EndsWith(sourceName + ".json")) { if (DefaultDictionary != null) { throw new StudioXInitializationException("Only one default localization dictionary can be for source: " + sourceName); } DefaultDictionary = dictionary; } } } } }
public async ValueTask WriteHeadAsync(WorksheetOptions?options, CancellationToken token) { _buffer.Advance(Utf8Helper.GetBytes(SheetHeader, _buffer.GetSpan())); if (options is null) { _buffer.Advance(Utf8Helper.GetBytes(SheetDataBegin, _buffer.GetSpan())); return; } var sb = new StringBuilder(); if (options.FrozenColumns is not null || options.FrozenRows is not null) { await _buffer.FlushToStreamAsync(_stream, token).ConfigureAwait(false); WriteSheetViewsXml(sb, options); await _buffer.WriteAsciiStringAsync(sb.ToString(), _stream, token).ConfigureAwait(false); sb.Clear(); } await WriteColsXmlAsync(sb, options, token).ConfigureAwait(false); _buffer.Advance(Utf8Helper.GetBytes(SheetDataBegin, _buffer.GetSpan())); }
private static async ValueTask WriteAsync( Stream stream, SpreadsheetBuffer buffer, List <Style> styles, CancellationToken token) { buffer.Advance(Utf8Helper.GetBytes(Header, buffer.GetSpan())); var fontLookup = await WriteFontsAsync(stream, buffer, styles, token).ConfigureAwait(false); var fillLookup = await WriteFillsAsync(stream, buffer, styles, token).ConfigureAwait(false); await buffer.WriteAsciiStringAsync(XmlPart1, stream, token).ConfigureAwait(false); var styleCount = styles.Count + 1; if (styleCount.GetNumberOfDigits() > buffer.FreeCapacity) { await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); } var sb = new StringBuilder(); sb.Append(styleCount); // The default style must be the first one (index 0) sb.Append("\"><xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\"/>"); await buffer.WriteAsciiStringAsync(sb.ToString(), stream, token).ConfigureAwait(false); sb.Clear(); for (var i = 0; i < styles.Count; ++i) { var style = styles[i]; var fontIndex = fontLookup[style.Font]; var fillIndex = fillLookup[style.Fill]; sb.Clear(); sb.Append("<xf numFmtId=\"0\""); sb.Append(" fontId=\"").Append(fontIndex).Append('"'); if (fontIndex > 0) { sb.Append(" applyFont=\"1\""); } sb.Append(" fillId=\"").Append(fillIndex).Append('"'); if (fillIndex > 1) { sb.Append(" applyFill=\"1\""); } sb.Append(" xfId=\"0\"/>"); await buffer.WriteAsciiStringAsync(sb.ToString(), stream, token).ConfigureAwait(false); } await buffer.WriteAsciiStringAsync(XmlPart2, stream, token).ConfigureAwait(false); await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); }
public static int GetRowStartBytes(int rowIndex, Span <byte> bytes) { var bytesWritten = SpanHelper.GetBytes(RowStart, bytes); bytesWritten += Utf8Helper.GetBytes(rowIndex, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(RowStartEndTag, bytes.Slice(bytesWritten)); return(bytesWritten); }
private static int GetSheetElementBytes(string path, Span <byte> bytes) { var bytesWritten = SpanHelper.GetBytes(SheetStart, bytes); bytesWritten += Utf8Helper.GetBytes(path, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(SheetEnd, bytes.Slice(bytesWritten)); return(bytesWritten); }
private static int GetStylesXmlElementBytes(int relationId, Span <byte> bytes) { var bytesWritten = SpanHelper.GetBytes(StylesStart, bytes); bytesWritten += SpanHelper.GetBytes(SharedMetadata.RelationIdPrefix, bytes.Slice(bytesWritten)); bytesWritten += Utf8Helper.GetBytes(relationId, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(RelationEnd, bytes.Slice(bytesWritten)); return(bytesWritten); }
public Utf8String TrimEnd() { CodePointReverseEnumerator it = CodePoints.GetReverseEnumerator(); while (it.MoveNext() && Utf8Helper.IsWhitespace(it.Current)) { } return(Substring(0, it.PositionInCodeUnits)); }
private static bool GetBytes(StyleId styleId, SpreadsheetBuffer buffer) { var bytes = buffer.GetSpan(); var bytesWritten = SpanHelper.GetBytes(StyledCellHelper.BeginStyledNumberCell, bytes); bytesWritten += Utf8Helper.GetBytes(styleId.Id, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(StyledCellHelper.EndStyleNullValue, bytes.Slice(bytesWritten)); buffer.Advance(bytesWritten); return(true); }
public async ValueTask WriteAsciiStringAsync(string value, Stream stream, CancellationToken token) { // When value is ASCII, the number of bytes equals the length of the string if (value.Length > FreeCapacity) { await FlushToStreamAsync(stream, token).ConfigureAwait(false); } _index += Utf8Helper.GetBytes(value, GetSpan()); }
private bool GetBytes(StyleId styleId, SpreadsheetBuffer buffer) { var bytes = buffer.GetSpan(); var bytesWritten = SpanHelper.GetBytes(StyledCellHelper.BeginStyledBooleanCell, bytes); bytesWritten += Utf8Helper.GetBytes(styleId.Id, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(EndStyleValueBytes(), bytes.Slice(bytesWritten)); buffer.Advance(bytesWritten); return(true); }
public Utf8String TrimStart() { CodePointEnumerator it = GetCodePointEnumerator(); while (it.MoveNext() && Utf8Helper.IsWhitespace(it.Current)) { } return(Substring(it.PositionInCodeUnits)); }
private void SkipEmpty() { var nextByte = _buffer[_index]; while (Utf8Helper.IsWhitespace(nextByte)) { _index++; nextByte = _buffer[_index]; } }
private static int GetUtf8LengthInBytes(IEnumerable <uint> codePoints) { int len = 0; foreach (var codePoint in codePoints) { len += Utf8Helper.GetNumberOfEncodedBytes(codePoint); } return(len); }
private static int GetSheetElementBytes(string path, int sheetId, Span <byte> bytes) { var bytesWritten = SpanHelper.GetBytes(SheetStart, bytes); bytesWritten += Utf8Helper.GetBytes(path, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(BetweenPathAndRelationId, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(SharedMetadata.RelationIdPrefix, bytes.Slice(bytesWritten)); bytesWritten += Utf8Helper.GetBytes(sheetId, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(RelationEnd, bytes.Slice(bytesWritten)); return(bytesWritten); }
private static int GetSheetElementByteCount(string path, int sheetId) { var sheetIdDigits = sheetId.GetNumberOfDigits(); return(SheetStart.Length + Utf8Helper.GetByteCount(path) + BetweenPathAndRelationId.Length + SharedMetadata.RelationIdPrefix.Length + sheetIdDigits + RelationEnd.Length); }
public bool WriteLongString(ReadOnlySpan <char> value, ref int valueIndex) { var remainingBuffer = FreeCapacity; var maxCharCount = remainingBuffer / Utf8Helper.MaxBytePerChar; var remainingLength = value.Length - valueIndex; var lastIteration = remainingLength <= maxCharCount; var length = Math.Min(remainingLength, maxCharCount); _index += Utf8Helper.GetBytes(value.Slice(valueIndex, length), GetSpan()); valueIndex += length; return(lastIteration); }
protected void LoadPermission(IPermissionDefinitionContext context) { string xmlString; using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("JK.Web.appPermissions.config")) { if (stream == null) { return; } xmlString = Utf8Helper.ReadStringFromStream(stream); } using (var sr = new StringReader(xmlString)) { using (var xr = XmlReader.Create(sr, new XmlReaderSettings { CloseInput = true, IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true })) { var xmlDocument = new XmlDocument(); xmlDocument.Load(xr); var root = xmlDocument.SelectSingleNode("/root"); if (root == null) { throw new AbpException("A Permission Xml must include root as root node."); } if (root.HasChildNodes) { foreach (XmlNode item in root.ChildNodes) { var name = item.GetAttributeValueOrNull("name"); if (string.IsNullOrEmpty(name)) { throw new AbpException("name is not defined in menu XML file!"); } var displayName = item.GetAttributeValueOrNull("displayName"); var permission = context.CreatePermission(name, L(displayName)); Iterate(permission, item); } } } } }
public static int GetRowStartBytes(int rowIndex, RowOptions options, Span <byte> bytes) { if (options.Height is null) { return(GetRowStartBytes(rowIndex, bytes)); } var bytesWritten = SpanHelper.GetBytes(RowStart, bytes); bytesWritten += Utf8Helper.GetBytes(rowIndex, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(RowHeightStart, bytes.Slice(bytesWritten)); bytesWritten += Utf8Helper.GetBytes(options.Height.Value, bytes.Slice(bytesWritten)); bytesWritten += SpanHelper.GetBytes(RowHeightEnd, bytes.Slice(bytesWritten)); return(bytesWritten); }
private JsonDb.JsonValueType GetJsonDb.JsonValueType() { var nextByte = _buffer[_index]; while (Utf8Helper.IsWhitespace(nextByte)) { _index++; nextByte = _buffer[_index]; } if (nextByte == '"') { return(JsonDb.JsonValueType.String); } if (nextByte == '{') { return(JsonDb.JsonValueType.Object); } if (nextByte == '[') { return(JsonDb.JsonValueType.Array); } if (nextByte == 't') { return(JsonDb.JsonValueType.True); } if (nextByte == 'f') { return(JsonDb.JsonValueType.False); } if (nextByte == 'n') { return(JsonDb.JsonValueType.Null); } if (nextByte == '-' || (nextByte >= '0' && nextByte <= '9')) { return(JsonDb.JsonValueType.Number); } throw new FormatException("Invalid json, tried to read char '" + nextByte + "'."); }
public static async ValueTask WriteAsync( ZipArchive archive, CompressionLevel compressionLevel, SpreadsheetBuffer buffer, List <string> worksheetPaths, bool hasStylesXml, CancellationToken token) { var stream = archive.CreateEntry("xl/_rels/workbook.xml.rels", compressionLevel).Open(); #if NETSTANDARD2_0 using (stream) #else await using (stream.ConfigureAwait(false)) #endif { buffer.Advance(Utf8Helper.GetBytes(Header, buffer.GetSpan())); for (var i = 0; i < worksheetPaths.Count; ++i) { var path = worksheetPaths[i]; var sheetId = i + 1; var sheetElementLength = GetSheetElementByteCount(path, sheetId); if (sheetElementLength > buffer.FreeCapacity) { await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); } buffer.Advance(GetSheetElementBytes(path, sheetId, buffer.GetSpan())); } var bufferNeeded = Footer.Length + (hasStylesXml ? MaxStylesXmlElementByteCount : 0); if (bufferNeeded > buffer.FreeCapacity) { await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); } if (hasStylesXml) { buffer.Advance(GetStylesXmlElementBytes(worksheetPaths.Count + 1, buffer.GetSpan())); } buffer.Advance(Utf8Helper.GetBytes(Footer, buffer.GetSpan())); await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); } }
public static async ValueTask WriteAsync( ZipArchive archive, CompressionLevel compressionLevel, SpreadsheetBuffer buffer, CancellationToken token) { var stream = archive.CreateEntry("_rels/.rels", compressionLevel).Open(); #if NETSTANDARD2_0 using (stream) #else await using (stream.ConfigureAwait(false)) #endif { buffer.Advance(Utf8Helper.GetBytes(Content, buffer.GetSpan())); await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); } }
protected override void InitializeDictionaries() { var allCultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures); var resourceNames = _assembly.GetManifestResourceNames().Where(resouceName => allCultureInfos.Any(culture => resouceName.EndsWith($"{SourceName}.json", true, null) || resouceName.EndsWith($"{SourceName}-{culture.Name}.json", true, null))).ToList(); foreach (var resourceName in resourceNames) { if (resourceName.StartsWith(_rootNamespace)) { using (var stream = _assembly.GetManifestResourceStream(resourceName)) { var jsonString = Utf8Helper.ReadStringFromStream(stream); InitializeDictionary(CreateJsonLocalizationDictionary(jsonString), isDefault: resourceName.EndsWith(SourceName + ".json")); } } } }
public static async ValueTask WriteAsync( ZipArchive archive, CompressionLevel compressionLevel, SpreadsheetBuffer buffer, List <string> worksheetPaths, bool hasStylesXml, CancellationToken token) { var stream = archive.CreateEntry("[Content_Types].xml", compressionLevel).Open(); #if NETSTANDARD2_0 using (stream) #else await using (stream.ConfigureAwait(false)) #endif { buffer.Advance(Utf8Helper.GetBytes(Header, buffer.GetSpan())); if (hasStylesXml) { await buffer.WriteAsciiStringAsync(Styles, stream, token).ConfigureAwait(false); } for (var i = 0; i < worksheetPaths.Count; ++i) { var path = worksheetPaths[i]; var sheetElementLength = GetSheetElementByteCount(path); if (sheetElementLength > buffer.FreeCapacity) { await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); } buffer.Advance(GetSheetElementBytes(path, buffer.GetSpan())); } await buffer.WriteAsciiStringAsync(Footer, stream, token).ConfigureAwait(false); await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); } }
public JsonParser(byte[] buffer, int lengthOfJson) { _buffer = buffer; _insideObject = 0; _insideArray = 0; TokenType = 0; _index = 0; _end = lengthOfJson; var nextByte = _buffer[_index]; while (Utf8Helper.IsWhitespace(nextByte) || nextByte == 0) { _index++; nextByte = _buffer[_index]; } _dbIndex = _end + 1; _jsonStartIsObject = _buffer[_index] == '{'; }
public override void Initialize(string sourceName) { var allCultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures); var resourceNames = _assembly.GetManifestResourceNames().ToList(); //var resourceNames = _assembly.GetManifestResourceNames().Where(resouceName => // allCultureInfos.Any(culture => resouceName.EndsWith($"{sourceName}.xml", true, null) || // resouceName.EndsWith($"{sourceName}-{culture.Name}.xml", true, // null))).ToList(); foreach (var resourceName in resourceNames) { if (resourceName.StartsWith(_rootNamespace)) { using (var stream = _assembly.GetManifestResourceStream(resourceName)) { var xmlString = Utf8Helper.ReadStringFromStream(stream); var dictionary = CreateXmlLocalizationDictionary(xmlString); if (Dictionaries.ContainsKey(dictionary.CultureInfo.Name)) { throw new Exception(sourceName + " source contains more than one dictionary for the culture: " + dictionary.CultureInfo.Name); } Dictionaries[dictionary.CultureInfo.Name] = dictionary; if (resourceName.EndsWith(sourceName + ".xml")) { if (DefaultDictionary != null) { throw new Exception("Only one default localization dictionary can be for source: " + sourceName); } DefaultDictionary = dictionary; } } } } }
public override void Initialize(string sourceName) { var allCultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures); var resourceNames = _assembly.GetManifestResourceNames().Where(resouceName => allCultureInfos.Any(culture => resouceName.EndsWith($"{sourceName}.xml", true, null) || resouceName.EndsWith($"{sourceName}-{culture.Name}.xml", true, null))).ToList(); foreach (var resourceName in resourceNames) { if (resourceName.StartsWith(_rootNamespace)) { using (var stream = _assembly.GetManifestResourceStream(resourceName)) { var xmlString = Utf8Helper.ReadStringFromStream(stream); var dictionary = CreateXmlLocalizationDictionary(xmlString); if (Dictionaries.ContainsKey(dictionary.CultureInfo.Name)) { throw new SharePlatformInitializationException(sourceName + " 源包含多个区域性字典: " + dictionary.CultureInfo.Name); } Dictionaries[dictionary.CultureInfo.Name] = dictionary; if (resourceName.EndsWith(sourceName + ".xml")) { if (DefaultDictionary != null) { throw new SharePlatformInitializationException("源只能有一个默认本地化词典: " + sourceName); } DefaultDictionary = dictionary; } } } } }