/// <summary> /// Clones this instance. /// </summary> /// <returns></returns> public virtual TokenizerArgs Clone(object context) { TokenizerArgs clone = (TokenizerArgs)MemberwiseClone(); clone._context = context; return(clone); }
/// <summary> /// Tries to parse a command line /// </summary> /// <typeparam name="T">The type to parse to</typeparam> /// <param name="commandLine">The command line.</param> /// <param name="args">The args.</param> /// <param name="to">If successfull the result of the parsed commandline</param> /// <returns></returns> public static bool TryParseCommandLine <T>(string commandLine, TokenizerArgs args, out T to) where T : class, new() { if (commandLine == null) { throw new ArgumentNullException("commandLine"); } else if (args == null) { throw new ArgumentNullException("args"); } return(TryParseCommandLine <T>(GetCommandlineWords(commandLine), args, out to)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="from"></param> /// <param name="args"></param> /// <returns></returns> public static string GenerateConnectionString <T>(T from, TokenizerArgs args) where T : class, new() { if (from == null) { throw new ArgumentNullException("from"); } else if (args == null) { throw new ArgumentNullException("args"); } return(""); }
/// <summary> /// Tries to parse the XML attributes of an element to a T instance; Unkown attributes are ignored /// </summary> /// <typeparam name="T"></typeparam> /// <param name="element">The element.</param> /// <param name="args">The args.</param> /// <param name="to">To.</param> /// <returns></returns> public static bool TryParseXml <T>(IXPathNavigable element, TokenizerArgs args, out T to) where T : class, new() { if (element == null) { throw new ArgumentNullException("element"); } else if (args == null) { throw new ArgumentNullException("args"); } return(XmlTokenizer <T> .TryParse(element, args, out to)); }
internal static TokenizerState <T> NewState <T>(TokenizerArgs args) where T : class, new() { T instance = new T(); IHasTokenDefinition htd = instance as IHasTokenDefinition; TokenizerDefinition def = null; if (htd != null) { def = htd.GetTokenizerDefinition(args); } if (def == null) { def = StaticTokenizerDefinition <T> .Definition; } return(new TokenizerState <T>(instance, def, args, true)); }
/// <summary> /// Tries the parse connection string. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="connectionString">The connection string.</param> /// <param name="args">The args.</param> /// <param name="to">To.</param> /// <returns></returns> public static bool TryParseConnectionString <T>(string connectionString, TokenizerArgs args, out T to) where T : class, new() { if (connectionString == null) { throw new ArgumentNullException("connectionString"); } else if (args == null) { throw new ArgumentNullException("args"); } to = null; using (TokenizerState <T> state = NewState <T>(args)) { IList <string> groups = GetWords(connectionString, new string[] { "\"\"", "\'\'" }, '\0', EscapeMode.DoubleItem, ";".ToCharArray()); foreach (string group in groups) { IList <string> parts = GetWords(group, new string[] { "\"\"", "\'\'" }, '\0', EscapeMode.DoubleItem, "=".ToCharArray()); TokenItem token; if ((parts.Count == 2) && state.Definition.TryGetToken(parts[0], args.CaseSensitive, out token)) { token.Evaluate(parts[1], state); } else if (args.SkipUnknownNamedItems) { continue; } else { return(false); } } // TODO: Parse connectionstring using definition to = state.Instance; return(true); } }
internal static TokenizerState <T> NewState <T>(TokenizerArgs args, T instance) where T : class, new() { if (instance == null) { throw new ArgumentNullException("instance"); } IHasTokenDefinition htd = instance as IHasTokenDefinition; TokenizerDefinition def = null; if (htd != null) { def = htd.GetTokenizerDefinition(args); } if (def == null) { def = StaticTokenizerDefinition <T> .Definition; } return(new TokenizerState <T>(instance, def, args, false)); }
/// <summary> /// Tries to parse the name value collection. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collection">The collection.</param> /// <param name="args">The args.</param> /// <param name="to">To.</param> /// <returns></returns> public static bool TryParseNameValueCollection <T>(NameValueCollection collection, TokenizerArgs args, out T to) where T : class, new() { if (collection == null) { throw new ArgumentNullException("collection"); } else if (args == null) { throw new ArgumentNullException("args"); } to = null; using (TokenizerState <T> state = NewState <T>(args)) { for (int i = 0; i < collection.Count; i++) { TokenItem ti; if (!state.Definition.TryGetToken(collection.Keys[i], args.CaseSensitive, out ti)) { if (args.SkipUnknownNamedItems) { continue; } else { return(false); } } ti.Evaluate(collection[i], state); } to = state.Instance; return(true); } }
/// <summary> /// Tries to parse the name value collection. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collection">The collection.</param> /// <param name="args">The args.</param> /// <param name="to">To.</param> /// <returns></returns> public static bool TryParseNameValueCollection <T>(IDictionary <string, string> collection, TokenizerArgs args, out T to) where T : class, new() { if (collection == null) { throw new ArgumentNullException("collection"); } else if (args == null) { throw new ArgumentNullException("args"); } to = null; using (TokenizerState <T> state = NewState <T>(args)) { foreach (KeyValuePair <string, string> kvp in collection) { TokenItem ti; if (!state.Definition.TryGetToken(kvp.Key, args.CaseSensitive, out ti)) { if (args.SkipUnknownNamedItems) { continue; } else { return(false); } } ti.Evaluate(kvp.Value, state); } to = state.Instance; return(true); } }
/// <summary> /// Tries to parse a command line /// </summary> /// <typeparam name="T">The type to parse to</typeparam> /// <param name="commandLineArguments">The command line arguments.</param> /// <param name="args">Arguments to customize the parsing rules</param> /// <param name="to">If successfull the result of the parsed commandline</param> /// <returns></returns> public static bool TryParseCommandLine <T>(IList <string> commandLineArguments, TokenizerArgs args, out T to) where T : class, new() { if (commandLineArguments == null) { throw new ArgumentNullException("commandLineArgs"); } else if (args == null) { throw new ArgumentNullException("args"); } EnsureItems(commandLineArguments, "commandLineArguments"); return(CommandLineTokenizer <T> .TryParse(commandLineArguments, args, out to)); }
/// <summary> /// Tries to write XML. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xmlWriter">The XML writer.</param> /// <param name="value">The value.</param> /// <param name="args">The args.</param> /// <returns></returns> public static bool TryWriteXml <T>(XmlWriter xmlWriter, T value, TokenizerArgs args) where T : class, new() { return(XmlTokenizer <T> .TryWrite(xmlWriter, value, args)); }