public OdpSdeStDescriptor(Config config, string component, ConnectionInfoDecryptionDelegate decryptionDelegate) : base(config.GetParameter(component, "Server", null), config.GetParameter(component, "User", null), GetDecryptedConfigParameter(config, component, "Password", decryptionDelegate), config.GetParameterAsInt(component, "Connect_Timeout", null)) { }
public void TestReplacement() { XmlDocument doc = new XmlDocument(); doc.LoadXml(@" <components> <component name='Geocoder'> <parameter name='DummyGeocoder' value='Azavea.Open.Geocoding.Tests.GeocoderSourceDummy,Azavea.Open.Geocoding' /> </component> <component name='DummyGeocoder'> <parameter name='CandidateTextReplacer' value='Azavea.Open.Geocoding.Processors.CandidateTextReplacer,Azavea.Open.Geocoding' /> </component> <component name='CandidateTextReplacer'> <parameter name='ReplaceField' value='Address' /> <parameter name='Find' value='340' /> <parameter name='ReplaceWith' value='680' /> </component> </components> "); Config gcCfg = new Config("GcConfig", doc); Geocoder gc = new Geocoder(gcCfg, "Geocoder"); GeocodeRequest gReq = new GeocodeRequest(); gReq.Address = "340 N 12th St"; IList<GeocodeCandidate> candidates = gc.Geocode(gReq).Candidates; Assert.AreEqual(1, candidates.Count); Assert.AreEqual("680 N 12th St", candidates[0].Address); }
/// <summary> /// Get the config for characters to be removed if the defaults aren't good enough. /// </summary> /// <param name="config">The config file to use.</param> /// <param name="component">The component to use. Only one parameter is /// available and it's optional: CharactersToRemove</param> public SpecialCharacterRemover(Config config, string component) { if (config.ParameterExists(component, "CharactersToRemove")) { _charactersToRemove = config.GetParameter(component, "CharactersToRemove"); } }
/// <summary> /// Create a new ScoreNormalizer with the given config. Specify a "Modifiers" parameter in your config /// to weight the scores. The config is a string in the format "LocatorName:Score|LocatorName:Score". /// See the tests for an example /// </summary> /// <param name="config">An Azavea Config object</param> /// <param name="component">The component with the configuration for this normalizer</param> public ScoreNormalizer(Config config, string component) { _scoreModifiers = new Dictionary<string, int>(); string modifierConfig = config.GetParameter(component, "Modifiers"); if (!string.IsNullOrEmpty(modifierConfig)) { string[] configs = modifierConfig.Split('|'); foreach (string s in configs) { string[] values = s.Split(':'); if (values.Length == 2) { int score; if (int.TryParse(values[1], out score)) { _scoreModifiers.Add(values[0], score); } } } } if (_scoreModifiers.Count == 0) { throw new LoggingException("No modifier list in this config"); } }
public void TestRemoval() { XmlDocument doc = new XmlDocument(); doc.LoadXml(@" <components> <component name='Geocoder'> <parameter name='DummyGeocoder' value='Azavea.Open.Geocoding.Tests.GeocoderSourceDummy,Azavea.Open.Geocoding' /> </component> <component name='DummyGeocoder'> <parameter name='SpecialCharacterRemover' value='Azavea.Open.Geocoding.Processors.SpecialCharacterRemover,Azavea.Open.Geocoding' /> </component> <component name='SpecialCharacterRemover'> </component> </components> "); Config gcCfg = new Config("GcConfig", doc); SpecialCharacterRemover remover = new SpecialCharacterRemover(gcCfg, "SpecialCharacterRemoverTest"); GeocodeRequest gReq = new GeocodeRequest(); gReq.Address = "230 No%rtheast @1st$ St."; gReq.City = "O*klahoma? (City)"; gReq.PostalCode = "[73104]"; gReq.State = "OK!"; gReq.Country = "US@"; GeocodeRequest processed = remover.ProcessRequest(gReq); Assert.AreEqual("Oklahoma City", processed.City); Assert.AreEqual("230 Northeast 1st St.", processed.Address); Assert.AreEqual("73104", processed.PostalCode); Assert.AreEqual("OK", processed.State); Assert.AreEqual("US", processed.Country); }
/// <summary> /// Create a new Processor with the given configuration /// </summary> /// <param name="config">An Azavea.Open Config object</param> /// <param name="sectionName">The section with the country list required by this processor</param> public CountrySelectorProcessor(Config config, string sectionName) { string myCountryList = config.GetParameter(sectionName, "CountryList"); if (myCountryList != null) { _myCountries = new List<string>(myCountryList.Split('|')); } else throw new LoggingException("No country list in this config"); }
/// <summary> /// Create a new Processor with the given configuration /// </summary> /// <param name="config">An Azavea.Open Config object</param> /// <param name="sectionName">The section with the country list required by this processor</param> public MatchTypeSelectorProcessor(Config config, string sectionName) { string matchList = config.GetParameter(sectionName, "MatchTypes"); if (matchList != null) { _allowedMatchTypes = new List<string>(matchList.Split('|')); } else throw new LoggingException("No MatchType list in this config"); }
public void TestLoadDummyGeocoderWithProcessorWithConfig() { Config dummyConfig = new Config("../../Tests/TestConfig.config", "TestConfig"); Geocoder gc = new Geocoder(dummyConfig, "GeocoderSourcesWithProcessorsWithConfigs"); GeocodeRequest gReq = new GeocodeRequest(); gReq.Address = "tS ht21 N 043"; IList<GeocodeCandidate> candidates = gc.Geocode(gReq).Candidates; Assert.AreEqual(1, candidates.Count); }
public void TestLoadDummyGeocoder() { Config dummyConfig = new Config("../../Tests/TestConfig.config", "TestConfig"); Geocoder gc = new Geocoder(dummyConfig); GeocodeRequest gReq = new GeocodeRequest(); gReq.Address = "340 N 12th St"; IList<GeocodeCandidate> candidates = gc.Geocode(gReq).Candidates; Assert.AreEqual(1, candidates.Count); }
/// <summary> /// Get the config for the field and text to be replaced. /// </summary> /// <param name="config">The config file to use.</param> /// <param name="component">The component to use. This should have three /// parameters: ReplaceField, Find, and ReplaceWith.</param> public CandidateTextReplacer(Config config, string component) { _replaceField = typeof (GeocodeCandidate).GetProperty(config.GetParameter(component, "ReplaceField")); _matchRegex = config.GetParameter(component, "Find"); _replaceWith = config.GetParameter(component, "ReplaceWith"); MatchCollection matches = Regex.Matches(_replaceWith, "{.*?}"); _sourceFields = new PropertyInfo[matches.Count]; for (int i = 0; i < _sourceFields.Length; i++) { string sourceFieldName = matches[i].Value.TrimStart('{').TrimEnd('}'); _sourceFields[i] = typeof (GeocodeCandidate).GetProperty(sourceFieldName); } }
public void Split1() { const string address = "340 N 12th St #402, Philadelphia, PA, USA"; GeocodeRequest req = new GeocodeRequest(); req.TextString = address; Config config = new Config("../../Tests/TestConfig.config", "TestConfig"); Processors.AddressSplitter splitter = new Processors.AddressSplitter(config, "AddressSplitterTest"); req = splitter.ProcessRequest(req); Assert.AreEqual("340 N 12th St #402", req.Address, "Test address doesn't match."); Assert.AreEqual("Philadelphia", req.City, "Test city doesn't match."); Assert.AreEqual("PA", req.State, "Test state doesn't match."); Assert.AreEqual("USA", req.Country, "Test country doesn't match."); }
/// <summary> /// This method is provided for convenience. If decryptionDelegate is not null, /// will use it to decrypt whatever value is in the config parameter. /// </summary> /// <param name="config">Config file to get the parameter from.</param> /// <param name="component">Section within the config file.</param> /// <param name="paramName">Name of the paraneter within the section.</param> /// <param name="decryptionDelegate">Method to call to decrypt the parameter. May be null if using plain text.</param> /// <returns></returns> protected static string GetDecryptedConfigParameter(Config config, string component, string paramName, ConnectionInfoDecryptionDelegate decryptionDelegate) { string retVal = config.GetParameter(component, paramName, null); if ((decryptionDelegate != null) && (!String.IsNullOrEmpty(retVal))) { retVal = decryptionDelegate.Invoke(retVal); } return retVal; }
/// <summary> /// This is a factory method, that will load the appropriate type of connection /// descriptor using the given config. /// /// It first searches for config item(s) called "ConnectionConfigSection" and/or /// "ConnectionConfig". (ConnectionConfig should be an "app name" for a config, not a file name). /// If present, it will use those to load from another section in this or another /// config file. This allows more dynamic install-time configuration of DB connections. /// You may daisy-chain the configuration if you wish. /// /// Once in the connection configuration section, it will first search for the "DescriptorClass" /// config item, and use that class if specified. If not, defaults to an OleDbDescriptor /// (which means it should be backwards compatible for all our existing config files). /// </summary> /// <param name="cfg">Config to load the descriptor info from.</param> /// <param name="section">What section of that config has the DB connection info in it.</param> /// <param name="decryptionDelegate">Method to call to decrypt information, if the actual /// connection descriptor type supports decryption. May be null.</param> /// <returns>A fully populated ConnectionDescriptor.</returns> public static IConnectionDescriptor LoadFromConfig(Config cfg, string section, ConnectionInfoDecryptionDelegate decryptionDelegate) { if (!cfg.ComponentExists(section)) { throw new BadDaoConfigurationException("Config section " + section + " does not exist in " + cfg.Application); } IConnectionDescriptor retVal; // First see if we're redirected to another config and/or section. if (cfg.ParameterExists(section, "ConnectionConfig") || cfg.ParameterExists(section, "ConnectionConfigSection")) { string otherName = cfg.GetParameter(section, "ConnectionConfig", cfg.Application); string otherSection = cfg.GetParameter(section, "ConnectionConfigSection", section); if (_log.IsDebugEnabled) { _log.Debug("Loading " + section + " connection info from " + otherName + "[" + otherSection + "]"); } // Recurse with different config values. retVal = LoadFromConfig(Config.GetConfig(otherName), otherSection, decryptionDelegate); } else { // Not overridden, read from this config section. // For backwards compatibility, default to using an OleDb descriptor. string typeName = cfg.GetParameter(section, "DescriptorClass", "Azavea.Open.DAO.OleDb.OleDbDescriptor,Azavea.Open.DAO.OleDb"); Type[] paramTypes = new Type[] {typeof (Config), typeof (string), typeof(ConnectionInfoDecryptionDelegate)}; Type descType = Type.GetType(typeName); if (descType == null) { throw new BadDaoConfigurationException("DescriptorClass '" + typeName + "' was specified, but we were unable to get type info. Are you missing a DLL?"); } ConstructorInfo constr = descType.GetConstructor(paramTypes); if (constr == null) { throw new BadDaoConfigurationException("DescriptorClass '" + typeName + "' was specified, but we were unable to get constructor info."); } retVal = (IConnectionDescriptor)constr.Invoke(new object[] { cfg, section, decryptionDelegate }); } return retVal; }
public void SplitZip2() { const string address = "12th St & Callowhill St, 19107"; GeocodeRequest req = new GeocodeRequest(); req.TextString = address; Config config = new Config("../../Tests/TestConfig.config", "TestConfig"); Processors.AddressSplitter splitter = new Processors.AddressSplitter(config, "AddressSplitterTest"); req = splitter.ProcessRequest(req); Assert.AreEqual("12th St & Callowhill St", req.Address, "Test address doesn't match."); Assert.AreEqual("19107", req.PostalCode, "Test postal code doesn't match."); }
/// <summary> /// This handles running through the configuration and getting the request /// and response processors. /// </summary> /// <param name="config">The config file we are using.</param> /// <param name="sectionName">The config section we are using to look for RequestProcessors and ResponseProcessors.</param> private void GetProcessors(Config config, string sectionName) { if (config.ComponentExists(sectionName)) { IList<KeyValuePair<string, string>> kvps = config.GetParametersAsList(sectionName); foreach (KeyValuePair<string, string> kvp in kvps) { string typeName = kvp.Value; Type processorType = Type.GetType(typeName); if (processorType != null && typeof(IProcessor).IsAssignableFrom(processorType)) { ConstructorInfo ci = processorType.GetConstructor(new [] {typeof (Config), typeof (string)}); IProcessor p; if (ci != null) { p = (IProcessor) ci.Invoke(new object[] {config, kvp.Key}); } else { ci = processorType.GetConstructor(new Type[] {}); if (ci == null) { throw new ConfigurationErrorsException("Processor '" + typeName + "' was specified, but we were unable to get constructor info."); } p = (IProcessor) ci.Invoke(new object[] {}); } // At this point we have a processor object. Add it to the dictionary. if (p as IRequestProcessor != null) { _processors[typeof (IRequestProcessor)].Add(p); } if (p as IResponseProcessor != null) { _processors[typeof (IResponseProcessor)].Add(p); } } } } }
/// <summary> /// This is a factory method, that will load the appropriate type of connection /// descriptor using the given config. /// /// It first searches for config item(s) called "ConnectionConfigSection" and/or /// "ConnectionConfig". (ConnectionConfig should be an "app name" for a config, not a file name). /// If present, it will use those to load from another section in this or another /// config file. This allows more dynamic install-time configuration of DB connections. /// You may daisy-chain the configuration if you wish. /// /// Once in the connection configuration section, it will first search for the "DescriptorClass" /// config item, and use that class if specified. If not, defaults to an OleDbDescriptor /// (which means it should be backwards compatible for all our existing config files). /// </summary> /// <param name="cfg">Config to load the descriptor info from.</param> /// <param name="section">What section of that config has the DB connection info in it.</param> /// <returns>A fully populated ConnectionDescriptor.</returns> public static IConnectionDescriptor LoadFromConfig(Config cfg, string section) { return LoadFromConfig(cfg, section, null); }
/// <summary> /// This constructor reads all the appropriate values from our standard config file /// in the normal format. /// </summary> /// <param name="config">Config to get params from.</param> /// <param name="component">Section of the config XML to look in for db params.</param> /// <param name="decryptionDelegate">Delegate to call to decrypt password fields. /// May be null if passwords are in plain text.</param> public SQLiteDescriptor(Config config, string component, ConnectionInfoDecryptionDelegate decryptionDelegate) { SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder(); builder.Pooling = false; _usePooling = false; builder.DataSource = _databasePath = config.GetParameterWithSubstitution(component, "Database", true); // We don't currently support passwords, so the clean conn str is the same // as the real one. _cleanConnStr = builder.ToString(); _connectionStr = builder.ToString(); }
/// <summary> /// Gets the type based on a couple optional parameters in the DB config file. /// </summary> /// <param name="config">Config to get params from.</param> /// <param name="component">Section of the config XML to look in for db params.</param> /// <returns>The type as specified in the config file, or throws an exception if /// there is no type correctly specified.</returns> private static DatabaseType GetTypeFromConfig(Config config, string component) { if (config.ParameterExists(component, "Type")) { return (DatabaseType)Enum.Parse(typeof(DatabaseType), config.GetParameter(component, "Type").Trim().ToUpper()); } if (config.ParameterExists(component, "Provider")) { return GuessTypeFromProvider(config.GetParameter(component, "Provider").Trim()); } throw new BadDaoConfigurationException( "Database connection config for '" + component + "' is missing both type and provider."); }
public void SetUp() { _config = new Config("../../Tests/TestConfig.config", "TestConfig"); GeocoderSourceDummy dummy = new GeocoderSourceDummy(_config, "DummyProcessorWithConfig"); _normalizer = new Processors.ScoreNormalizer(_config, "ScoreNormalizerTest"); _zero = new GeocodeCandidate { MatchType = "Locator0" }; _one = new GeocodeCandidate { MatchType = "Locator1" }; _two = new GeocodeCandidate { MatchType = "Locator2" }; List<GeocodeCandidate> candidates = new List<GeocodeCandidate>{_zero, _one, _two}; _response = new GeocodeResponse(candidates, dummy); }
/// <summary> /// Creates the geocoder. If the username and password are not set, then the publicly /// available interface will be used. This should only be used in *non-commercial* products. /// </summary> /// <param name="config">The config file we are using.</param> /// <param name="sectionName">The config section we are using to look for RequestProcessors and ResponseProcessors.</param> public GeocoderUSGeocoder(Config config, string sectionName) : base(config, sectionName) { _username = config.GetParameter(sectionName, "UserName"); _password = config.GetParameter(sectionName, "Password"); }
/// <summary> /// This constructor does the default stuff of getting the pre- and post-processors /// from the config section. /// </summary> /// <param name="config">The config file we are using.</param> /// <param name="sectionName">The config section we are using to look for RequestProcessors and ResponseProcessors.</param> protected GeocoderSource(Config config, string sectionName) { _processors[typeof(IRequestProcessor)] = new List<IProcessor>(); _processors[typeof(IResponseProcessor)] = new List<IProcessor>(); GetProcessors(config, sectionName); }
/// <summary> /// Get the config for the fields to be replaced. /// </summary> /// <param name="config">The config to use.</param> /// <param name="component">The component name that defines the replacements.</param> public AddressPartDefaulter(Config config, string component) { _configParams = config.GetParametersAsDictionary(component); }
/// <summary> /// This constructor reads all the appropriate values from our standard config file /// in the normal format. /// </summary> /// <param name="config">Config to get params from.</param> /// <param name="component">Section of the config XML to look in for db params.</param> /// <param name="decryptionDelegate">Delegate to call to decrypt password fields. /// May be null if passwords are in plain text.</param> public PostgreSqlDescriptor(Config config, string component, ConnectionInfoDecryptionDelegate decryptionDelegate) { _server = config.GetParameter(component, "Server"); _port = config.GetParameter(component, "Port", _port); _databaseName = config.GetParameter(component, "Database"); _user = config.GetParameter(component, "User", null); _password = GetDecryptedConfigParameter(config, component, "Password", decryptionDelegate); _encoding = config.GetParameter(component, "Encoding", _encoding); _connectionStr = MakeConnectionString(_server, _port, _databaseName, _user, _password, _encoding); _cleanConnStr = MakeConnectionString(_server, _port, _databaseName, _user, null, _encoding); }
/// <summary> /// This constructor reads all the appropriate values from our standard config file /// in the normal format. /// </summary> /// <param name="config">Config to get params from.</param> /// <param name="component">Section of the config XML to look in for db params.</param> /// <param name="decryptionDelegate">Delegate to call to decrypt password fields. /// May be null if passwords are in plain text.</param> public FirebirdDescriptor(Config config, string component, ConnectionInfoDecryptionDelegate decryptionDelegate) { FbConnectionStringBuilder builder = new FbConnectionStringBuilder(); Server = config.GetParameter(component, "Server", null); if (StringHelper.IsNonBlank(Server)) { builder.DataSource = Server; } else { builder.ServerType = FbServerType.Embedded; // For the embedded server, we want to disable pooling so we don't wind up locking // the file indefinitely. builder.Pooling = false; _usePooling = false; } builder.Database = Database = config.GetParameterWithSubstitution(component, "Database", true); builder.UserID = User = config.GetParameter(component, "User", null); // First make it without a password. _cleanConnStr = builder.ToString(); // Now with the password for the real one. Password = GetDecryptedConfigParameter(config, component, "Password", decryptionDelegate); builder.Password = Password; _connectionStr = builder.ToString(); }
public void SplitZip4() { const string address = "340 N 12th St, Philadelphia, PA 19107, USA"; GeocodeRequest req = new GeocodeRequest(); req.TextString = address; Config config = new Config("../../Tests/TestConfig.config", "TestConfig"); Processors.AddressSplitter splitter = new Processors.AddressSplitter(config, "AddressSplitterTest"); req = splitter.ProcessRequest(req); Assert.AreEqual("340 N 12th St", req.Address, "Test address doesn't match."); Assert.AreEqual("19107", req.PostalCode, "Test postal code doesn't match."); Assert.AreEqual( "USA", req.Country, "Test country doesn't match." ); }
/// <summary> /// Populates the descriptor's values from a config file. /// </summary> /// <param name="config">Config to get params from.</param> /// <param name="component">Section of the config XML to look in for db params.</param> /// <param name="decryptionDelegate">Delegate to call to decrypt password fields. /// May be null if passwords are in plain text.</param> public CsvDescriptor(Config config, string component, ConnectionInfoDecryptionDelegate decryptionDelegate) : this(CsvConnectionType.Unknown, config.GetParameterWithSubstitution(component, "Path", true), null, null, config.ParameterExists(component, "OutputQuoteLevel") ? (CsvQuoteLevel) Enum.Parse(typeof (CsvQuoteLevel),config.GetParameter(component, "OutputQuoteLevel")) : CsvQuoteLevel.QuoteStrings) { }
/// <summary> /// Constructor for the Google geocoder source. /// </summary> /// <param name="config">The config to use to construct the Google geocoder.</param> /// <param name="component">The component in the config in which to look for /// the configuration parameters.</param> public GoogleGeocoder(Config config, string component) : base(config, component) { _authKey = config.GetParameter(component, "GoogleApiKey"); }
public void SetUp() { _config = new Config("../../Tests/TestConfig.config", "TestConfig"); }
public void TestRemovalWithConfig() { XmlDocument doc = new XmlDocument(); doc.LoadXml(@" <components> <component name='Geocoder'> <parameter name='DummyGeocoder' value='Azavea.Open.Geocoding.Tests.GeocoderSourceDummy,Azavea.Open.Geocoding' /> </component> <component name='DummyGeocoder'> <parameter name='SpecialCharacterRemover' value='Azavea.Open.Geocoding.Processors.SpecialCharacterRemover,Azavea.Open.Geocoding' /> </component> <component name='SpecialCharacterRemover'> <parameter name='CharactersToRemove' value='|' /> </component> </components> "); Config gcCfg = new Config("GcConfig", doc); SpecialCharacterRemover remover = new SpecialCharacterRemover(gcCfg, "SpecialCharacterRemover"); GeocodeRequest gReq = new GeocodeRequest(); gReq.TextString = "14th @ Impossible | Philadelphia | PA | 19107"; GeocodeRequest processed = remover.ProcessRequest(gReq); Assert.AreEqual("14th @ Impossible Philadelphia PA 19107", processed.TextString); }
/// <exclude /> public RequestProcessorDummyWithConfig(Config config, string component) { _doReversing = Convert.ToBoolean(config.GetParameter(component, "DoReversing", "false")); }