/// <summary> /// Clone <paramref name="line"/>. /// </summary> /// <param name="factory"></param> /// <param name="line"></param> /// <returns></returns> /// <exception cref="LineException">on append error</exception> public static ILine Clone(this ILineFactory factory, ILine line) { ILine result = null; StructList16 <ILine> args = new StructList16 <ILine>(); for (ILine l = line; l != null; l = l.GetPreviousPart()) { if (l is ILineArgument || l is ILineArgumentEnumerable) { args.Add(l); } } for (int i = args.Count - 1; i >= 0; i--) { ILine l = args[i]; if (l is ILineArgumentEnumerable enumr) { foreach (ILineArgument args_ in enumr) { result = factory.Create(result, args_); } } if (l is ILineArgument arg) { result = factory.Create(result, arg); } } return(result); }
/// <summary> /// Clone <see cref="ILineKey"/> parts. /// </summary> /// <param name="line"></param> /// <param name="appender">(optional) appender to use for clone. If null uses the appender of <paramref name="line"/></param> /// <param name="parameterInfos">(optional) for checking which parameters are keys</param> /// <returns>clone of key parts</returns> /// <exception cref="LineException">If cloning failed.</exception> public static ILine CloneKey(this ILine line, ILineFactory appender = default, IParameterInfos parameterInfos = null) { if (appender == null) { appender = line.GetAppender(); } ILine result = null; if (parameterInfos == null) { appender.TryGetParameterInfos(out parameterInfos); } StructList16 <ILine> args = new StructList16 <ILine>(); for (ILine l = line; l != null; l = l.GetPreviousPart()) { if (l is ILineArgument || l is ILineArgumentEnumerable) { args.Add(l); } } for (int i = args.Count - 1; i >= 0; i--) { ILine l = args[i]; if (l is ILineParameterEnumerable lineParameters) { foreach (ILineParameter lineParameter in lineParameters) { if (lineParameter.IsCanonicalKey(parameterInfos)) { result = appender.Create <ILineCanonicalKey, string, string>(result, lineParameter.ParameterName, lineParameter.ParameterValue); } else if (lineParameter.IsNonCanonicalKey(parameterInfos)) { result = appender.Create <ILineNonCanonicalKey, string, string>(result, lineParameter.ParameterName, lineParameter.ParameterValue); } } } if (l is ILineParameter parameter) { if (parameter.IsCanonicalKey(parameterInfos)) { result = appender.Create <ILineCanonicalKey, string, string>(result, parameter.ParameterName, parameter.ParameterValue); } else if (parameter.IsNonCanonicalKey(parameterInfos)) { result = appender.Create <ILineNonCanonicalKey, string, string>(result, parameter.ParameterName, parameter.ParameterValue); } } } return(result ?? appender.Create <ILinePart>(null)); }
/// <summary> /// Inline <paramref name="value"/> to <paramref name="culture"/>. /// </summary> /// <param name="lineFactory"></param> /// <param name="culture">subkey in parametrized format, e.g. "Culture:en", or "Culture:en:N:One"</param> /// <param name="value">(optional) value to append, if null removes previously existing the inline</param> /// <returns>new line with inlines or <paramref name="lineFactory"/></returns> /// <exception cref="LineException">If key can't be inlined.</exception> public static ILine InlineCulture(this ILineFactory lineFactory, string culture, IString value) { ILineInlines inlines = lineFactory.Create <ILineInlines>(null); ILine subkey = lineFactory.Create <ILineNonCanonicalKey, string, string>(null, "Culture", culture); if (value != null) { subkey = subkey.String(value); } if (subkey != null) { inlines[subkey] = subkey; } return(inlines); }
/// <summary> /// Create new parameter part as <see cref="ILineParameter"/>, as <see cref="ILineCanonicalKey"/>, or as <see cref="ILineNonCanonicalKey"/> depending /// on parameter name and policy in <paramref name="parameterInfos"/>. /// </summary> /// <param name="lineFactory"></param> /// <param name="parameterName">parameter name</param> /// <param name="parameterValue">parameter value</param> /// <param name="parameterInfos">(optional) instructions on whether to instantiate as parameter or key. See ParameterInfos.Default for default configuration</param> /// <returns>new parameter part</returns> /// <exception cref="LineException">If part could not be appended</exception> /// <returns>new part</returns> public static ILine Parameter(this ILineFactory lineFactory, string parameterName, string parameterValue, IParameterInfos parameterInfos) { IParameterInfo info = null; if (parameterInfos != null && parameterInfos.TryGetValue(parameterName, out info)) { if (info.InterfaceType == typeof(ILineHint)) { return(lineFactory.Create <ILineHint, string, string>(null, parameterName, parameterValue)); } if (info.InterfaceType == typeof(ILineCanonicalKey)) { return(lineFactory.Create <ILineCanonicalKey, string, string>(null, parameterName, parameterValue)); } if (info.InterfaceType == typeof(ILineNonCanonicalKey)) { return(lineFactory.Create <ILineNonCanonicalKey, string, string>(null, parameterName, parameterValue)); } } return(lineFactory.Create <ILineParameter, string, string>(null, parameterName, parameterValue)); }
public void ParseTokensOnDifferentLinesShouldParseAsDifferentLines() { IList <TokenBase> firstLine = new List <TokenBase>(); firstLine.Add(TokenCreator.Create <IdentifierToken>("test1")); TokenCreator.NewLine(1); IList <TokenBase> secondLine = new List <TokenBase>(); secondLine.Add(TokenCreator.Create <IdentifierToken>("test2")); ILineFactory factory = Mocker.StrictMock <ILineFactory>(); Expect.Call(factory.Create(firstLine)).Return(new Line(firstLine)).Repeat.Once(); Expect.Call(factory.Create(secondLine)).Return(new Line(secondLine)).Repeat.Once(); StubLineFactoryProvider provider = new StubLineFactoryProvider(factory); CalidusLineParser parser = new CalidusLineParser(provider); IList <LineBase> expected = new List <LineBase>(); expected.Add(new Line(firstLine)); expected.Add(new Line(secondLine)); IList <TokenBase> toParse = new List <TokenBase>(); toParse.Add(firstLine[0]); toParse.Add(secondLine[0]); Mocker.ReplayAll(); IEnumerable <LineBase> actual = parser.Parse(toParse); CollectionAssert.AreEquivalent(expected, actual); Mocker.VerifyAll(); }
/// <summary> /// Create inlined <paramref name="subkey"/> resource. /// </summary> /// <param name="lineFactory"></param> /// <param name="subkey">subkey in parametrized format, e.g. "Culture:en"</param> /// <param name="value">(optional) value to append, if null removes previously existing the inline</param> /// <returns>new line with inlines or <paramref name="lineFactory"/></returns> /// <exception cref="LineException">If key can't be inlined.</exception> public static ILine Inline(this ILineFactory lineFactory, ILine subkey, byte[] value) { ILineInlines inlines = lineFactory.Create <ILineInlines>(null); if (value != null) { subkey = subkey.Resource(value); } if (subkey != null) { inlines[subkey] = subkey; } return(inlines); }
/// <summary> /// Create a clone of <paramref name="previous"/>'s appender, and add new components to it. /// </summary> /// <param name="previous">previous part</param> /// <param name="appender">appender to add</param> /// <param name="policy">add policy</param> /// <returns>part with another appender</returns> public static ILine AddAppender(this ILine previous, ILineFactory appender, LineFactoryAddPolicy policy = LineFactoryAddPolicy.OverwriteIfExists) { ILineFactory previousAppender; if (previous.TryGetAppender(out previousAppender)) { ILineFactory newAppender = previousAppender == null ? appender : (ILineFactory) new LineFactoryComposition().Add(previousAppender, policy).Add(appender, policy); return(newAppender.Create <ILinePart>(previous)); } else { return(appender.Create <ILinePart>(previous)); } }
/// <summary> /// Append enumeration of parameters. /// </summary> /// <param name="part"></param> /// <param name="parameters">enumeration of parameters to append</param> /// <returns>new key that is appended to this key</returns> /// <exception cref="LineException">If key doesn't implement ILineParameterAssignable, or append failed</exception> public static ILine Parameters(this ILine part, IEnumerable <KeyValuePair <string, string> > parameters) { ILineFactory appender = part.GetAppender(); if (appender == null) { throw new LineException(part, "Appender is not found."); } foreach (var parameter in parameters) { part = appender.Create <ILineParameter, string, string>(part, parameter.Key, parameter.Value); } return(part); }
/// <summary> /// Parse string into key. /// </summary> /// <param name="lineFormat"></param> /// <param name="str">key as string</param> /// <param name="prevPart">(optional) previous part to append to</param> /// <param name="appender">(optional) line appender to append with. If null, uses appender from <paramref name="prevPart"/>. If null, uses default appender.</param> /// <returns>key result or null if contained no content</returns> /// <exception cref="LineException">If parse failed</exception> /// <exception cref="LineException">If <paramref name="lineFormat"/> doesn't implement <see cref="ILineFormatParser"/>.</exception> /// <exception cref="LineException">Error if appender is not available</exception> public static ILine Parse(this ILineFormat lineFormat, string str, ILine prevPart = default, ILineFactory appender = default) { if (lineFormat is ILineFormatAppendParser appendParser) { return appendParser.Parse(str, prevPart, appender); } if (lineFormat is ILineFormatParser parser) { if (appender == null) appender = prevPart.GetAppender(); foreach (ILineArgument arg in parser.ParseArgs(str)) prevPart = appender.Create(prevPart, arg); return prevPart; } else throw new LineException(prevPart, $"Cannot parse strings to {nameof(ILine)} with {lineFormat.GetType().FullName}. {lineFormat} doesn't implement {nameof(ILineFormatParser)}."); }
private void DrawLocks() { canvas.RemoveAll <Line>(); foreach (var sessionCircle in sessionCircles.Where(x => x.Session.BlockedBy.HasValue)) { var blocking = sessionCircles.FirstOrDefault(x => x.Session.SPID == sessionCircle.Session.BlockedBy); if (blocking != null) { var line = lineFactory.Create( sessionCircle.X + sessionCircle.Size / 2, sessionCircle.Y + sessionCircle.Size / 2, blocking.X + blocking.Size / 2, blocking.Y + blocking.Size / 2, color: Colors.Red ); canvas.Add(line, 2); } } }
/// <summary> /// Create localization resolver. /// </summary> /// <param name="lineFactory"></param> /// <param name="stringResolver">Assembly qualified class name to <see cref="IStringResolver"/></param> /// <returns>new key</returns> /// <exception cref="LineException">If part append fails</exception> public static ILineHint StringResolver(this ILineFactory lineFactory, string stringResolver) => lineFactory.Create <ILineHint, string, string>(null, "StringResolver", stringResolver);
/// <summary> /// Create localization resolver. /// </summary> /// <param name="lineFactory"></param> /// <param name="stringResolver"></param> /// <returns>new key</returns> /// <exception cref="LineException">If part append fails</exception> public static ILineStringResolver StringResolver(this ILineFactory lineFactory, IStringResolver stringResolver) => lineFactory.Create <ILineStringResolver, IStringResolver>(null, stringResolver);
/// <summary> /// Append format provider key. /// /// Format provider is requested for following interfaces: /// <list type="bullet"> /// <item><see cref="ICustomFormatter"/></item> /// </list> /// </summary> /// <param name="lineFactory"></param> /// <param name="formatProvider">assembly qualified class name to <see cref="IFormatProvider"/></param> /// <returns>new key</returns> /// <exception cref="LineException">If <see cref="ILineFormatProvider"/> could not be appended</exception> public static ILineHint FormatProvider(this ILineFactory lineFactory, string formatProvider) => lineFactory.Create <ILineHint, string, string>(null, "FormatProvider", formatProvider);
/// <summary> /// Create <see cref="ILineString"/> part. /// </summary> /// <param name="lineFactory"></param> /// <param name="value"></param> /// <returns></returns> public static ILineString String(this ILineFactory lineFactory, IString value) => lineFactory.Create <ILineString, IString>(null, value);
/// <summary> /// Append localizatoin asset. /// </summary> /// <param name="lineFactory"></param> /// <param name="asset"></param> /// <returns>part with asset</returns> public static ILineAsset Asset(this ILineFactory lineFactory, IAsset asset) => lineFactory.Create <ILineAsset, IAsset>(null, asset);
/// <summary> /// Assign a specific instance of rules. /// </summary> /// <param name="lineFactory"></param> /// <param name="rules"></param> /// <returns>new key with rules</returns> public static ILinePluralRules PluralRules(this ILineFactory lineFactory, IPluralRules rules) => lineFactory.Create <ILinePluralRules, IPluralRules>(null, rules);
/// <summary> /// Append string format. /// </summary> /// <param name="lineFactory"></param> /// <param name="stringFormat"></param> /// <returns>new key</returns> /// <exception cref="LineException">If could not be appended</exception> public static ILineStringFormat StringFormat(this ILineFactory lineFactory, IStringFormat stringFormat) => lineFactory.Create <ILineStringFormat, IStringFormat>(null, stringFormat);
/// <summary> /// Append culture policy /// </summary> /// <param name="lineFactory"></param> /// <param name="culturePolicy"></param> /// <returns></returns> public static ILineCulturePolicy CulturePolicy(this ILineFactory lineFactory, ICulturePolicy culturePolicy) => lineFactory.Create <ILineCulturePolicy, ICulturePolicy>(null, culturePolicy);
/// <summary> /// Create localization resolver. /// </summary> /// <param name="lineFactory"></param> /// <param name="resourceResolver"></param> /// <returns>new key</returns> /// <exception cref="LineException">If part append fails</exception> public static ILineResourceResolver ResourceResolver(this ILineFactory lineFactory, IResourceResolver resourceResolver) => lineFactory.Create <ILineResourceResolver, IResourceResolver>(null, resourceResolver);
/// <summary> /// Create parameter part. /// </summary> /// <param name="lineFactory"></param> /// <param name="parameterName">parameter name</param> /// <param name="parameterValue">(optional) parameter value</param> /// <returns>new parameter part</returns> /// <exception cref="LineException">If part could not be appended</exception> /// <returns>new part</returns> public static ILine Parameter(this ILineFactory lineFactory, string parameterName, string parameterValue) => lineFactory.Create <ILineParameter, string, string>(null, parameterName, parameterValue);
/// <summary> /// Create localization resolver. /// </summary> /// <param name="lineFactory"></param> /// <param name="resourceResolver">Assembly qualified class name to <see cref="IResourceResolver"/></param> /// <returns>new key</returns> /// <exception cref="LineException">If part append fails</exception> public static ILineHint ResourceResolver(this ILineFactory lineFactory, string resourceResolver) => lineFactory.Create <ILineHint, string, string>(null, "ResourceResolver", resourceResolver);
/// <summary> /// Append format arguments. Format arguments /// </summary> /// <param name="lineFactory"></param> /// <param name="args"></param> /// <returns></returns> /// <exception cref="LineException">If key can't be formatted</exception> public static ILineValue Value(this ILineFactory lineFactory, params object[] args) => lineFactory.Create <ILineValue, Object[]>(null, args);
/// <summary> /// Append observer that monitors resolving of localization strings. /// </summary> /// <param name="lineFactory"></param> /// <param name="logger"></param> /// <returns>new key</returns> /// <exception cref="LineException">Append logger</exception> public static ILineLogger Logger(this ILineFactory lineFactory, ILogger logger) => lineFactory.Create <ILineLogger, ILogger>(null, logger);
/// <summary> /// Create "Type" as <see cref="ILineNonCanonicalKey"/> key. /// </summary> /// <param name="lineFactory"></param> /// <param name="typeName"></param> /// <returns>new key</returns> /// <exception cref="LineException">If key doesn't implement <see cref="ILineNonCanonicalKey"/></exception> public static ILineNonCanonicalKey Type(this ILineFactory lineFactory, string typeName) => lineFactory.Create <ILineNonCanonicalKey, string, string>(null, "Type", typeName);
/// <summary> /// Append string format. /// </summary> /// <param name="lineFactory"></param> /// <param name="stringFormat"></param> /// <returns>new key</returns> /// <exception cref="LineException">If could not be appended</exception> public static ILineHint StringFormat(this ILineFactory lineFactory, string stringFormat) => lineFactory.Create <ILineHint, string, string>(null, "StringFormat", stringFormat);
/// <summary> /// Create <see cref="ILineType"/> key. /// </summary> /// <param name="lineFactory"></param> /// <param name="type"></param> /// <returns>new key</returns> /// <exception cref="LineException">If key doesn't implement <see cref="ILineType"/></exception> public static ILineType Type(this ILineFactory lineFactory, Type type) => lineFactory.Create <ILineType, Type>(null, type);
/// <summary> /// Set new appender by appending a dummy <see cref="ILinePart"/> with the new <paramref name="appender"/>. /// </summary> /// <param name="previous"></param> /// <param name="appender"></param> /// <returns>part with another appender</returns> public static ILine SetAppender(this ILine previous, ILineFactory appender) => appender.Create <ILinePart>(previous);
/// <summary> /// Create <see cref="ILine{T}"/> key. /// </summary> /// <param name="lineFactory"></param> /// <returns>new key</returns> /// <typeparam name="T"></typeparam> /// <exception cref="LineException">If key cannot be appended</exception> public static ILine <T> Type <T>(this ILineFactory lineFactory) => (ILine <T>)lineFactory.Create <ILineType, Type>(null, typeof(T));
/// <summary> /// Create "String" hint. /// /// StringResolver will parse the hint using the active StringFormat. /// If there is no active string format, then CSharp format is used /// </summary> /// <param name="lineFactory"></param> /// <param name="value"></param> /// <returns></returns> public static ILineHint String(this ILineFactory lineFactory, String value) => lineFactory.Create <ILineHint, string, string>(null, "String", value);
/// <summary> /// Append format provider key. /// /// Format provider is requested for following interfaces: /// <list type="bullet"> /// <item><see cref="ICustomFormatter"/></item> /// </list> /// </summary> /// <param name="lineFactory"></param> /// <param name="formatProvider"></param> /// <returns>new key</returns> /// <exception cref="LineException">If <see cref="ILineFormatProvider"/> could not be appended</exception> public static ILineFormatProvider FormatProvider(this ILineFactory lineFactory, IFormatProvider formatProvider) => lineFactory.Create <ILineFormatProvider, IFormatProvider>(null, formatProvider);