org.apache.commons.configuration2.interpol.ConfigurationInterpolator Java Examples
The following examples show how to use
org.apache.commons.configuration2.interpol.ConfigurationInterpolator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BasicBuilderParameters.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Obtains a specification for a {@link ConfigurationInterpolator} from the * specified map with parameters. All properties related to interpolation * are evaluated and added to the specification object. * * @param params the map with parameters (must not be <b>null</b>) * @return an {@code InterpolatorSpecification} object constructed with data * from the map * @throws IllegalArgumentException if the map is <b>null</b> or contains * invalid data */ public static InterpolatorSpecification fetchInterpolatorSpecification( final Map<String, Object> params) { checkParameters(params); return new InterpolatorSpecification.Builder() .withInterpolator( fetchParameter(params, PROP_INTERPOLATOR, ConfigurationInterpolator.class)) .withParentInterpolator( fetchParameter(params, PROP_PARENT_INTERPOLATOR, ConfigurationInterpolator.class)) .withPrefixLookups(fetchAndCheckPrefixLookups(params)) .withDefaultLookups(fetchAndCheckDefaultLookups(params)) .create(); }
Example #2
Source File: AbstractConfiguration.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Adds all {@code Lookup} objects in the given collection as default * lookups (i.e. lookups without a variable prefix) to the * {@code ConfigurationInterpolator} object of this configuration. In * addition, it adds a specialized default {@code Lookup} object which * queries this {@code Configuration}. The set of {@code Lookup} objects * with prefixes is not modified by this method. If this configuration does * not have a {@code ConfigurationInterpolator}, a new instance is created. * Note: This method is mainly intended to be used for initializing a * configuration when it is created by a builder. Normal client code should * better call {@link #installInterpolator(Map, Collection)} to define the * {@code ConfigurationInterpolator} in a single step. * * @param lookups the collection with default {@code Lookup} objects to be * added * @since 2.0 */ public void setDefaultLookups(final Collection<? extends Lookup> lookups) { boolean success; do { final ConfigurationInterpolator ciOld = getInterpolator(); final ConfigurationInterpolator ciNew = ciOld != null ? ciOld : new ConfigurationInterpolator(); Lookup confLookup = findConfigurationLookup(ciNew); if (confLookup == null) { confLookup = new ConfigurationLookup(this); } else { ciNew.removeDefaultLookup(confLookup); } ciNew.addDefaultLookups(lookups); ciNew.addDefaultLookup(confLookup); success = interpolator.compareAndSet(ciOld, ciNew); } while (!success); }
Example #3
Source File: TestBasicBuilderParameters.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether a custom {@code ConfigurationInterpolator} overrides * settings for custom lookups. */ @Test public void testSetLookupsAndInterpolator() { final Lookup look1 = EasyMock.createMock(Lookup.class); final Lookup look2 = EasyMock.createMock(Lookup.class); final ConfigurationInterpolator parent = EasyMock.createMock(ConfigurationInterpolator.class); final ConfigurationInterpolator ci = EasyMock.createMock(ConfigurationInterpolator.class); params.setDefaultLookups(Collections.singleton(look1)); params.setPrefixLookups(Collections.singletonMap("test", look2)); params.setInterpolator(ci); params.setParentInterpolator(parent); final Map<String, Object> map = params.getParameters(); assertFalse("Got prefix lookups", map.containsKey("prefixLookups")); assertFalse("Got default lookups", map.containsKey("defaultLookups")); assertFalse("Got a parent interpolator", map.containsKey("parentInterpolator")); }
Example #4
Source File: AbstractConfiguration.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Finds a {@code ConfigurationLookup} pointing to the specified * configuration in the default lookups for the specified * {@code ConfigurationInterpolator}. * * @param ci the {@code ConfigurationInterpolator} in question * @param targetConf the target configuration of the searched lookup * @return the found {@code Lookup} object or <b>null</b> */ private static Lookup findConfigurationLookup(final ConfigurationInterpolator ci, final ImmutableConfiguration targetConf) { for (final Lookup l : ci.getDefaultLookups()) { if (l instanceof ConfigurationLookup) { if (targetConf == ((ConfigurationLookup) l).getConfiguration()) { return l; } } } return null; }
Example #5
Source File: TestBasicBuilderParameters.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether a cloned instance contains the same data as the original * object. */ @Test public void testCloneValues() { final ConfigurationLogger log = EasyMock.createMock(ConfigurationLogger.class); final ConfigurationInterpolator ci = EasyMock.createMock(ConfigurationInterpolator.class); final ListDelimiterHandler handler1 = EasyMock.createMock(ListDelimiterHandler.class); final ListDelimiterHandler handler2 = EasyMock.createMock(ListDelimiterHandler.class); params.setListDelimiterHandler(handler1); params.setLogger(log); params.setInterpolator(ci); params.setThrowExceptionOnMissing(true); final BasicBuilderParameters clone = params.clone(); params.setListDelimiterHandler(handler2); params.setThrowExceptionOnMissing(false); final Map<String, Object> map = clone.getParameters(); assertSame("Wrong logger", log, map.get("logger")); assertSame("Wrong interpolator", ci, map.get("interpolator")); assertEquals("Wrong list delimiter handler", handler1, map.get("listDelimiterHandler")); assertEquals("Wrong exception flag", Boolean.TRUE, map.get("throwExceptionOnMissing")); }
Example #6
Source File: TestSubnodeConfiguration.java From commons-configuration with Apache License 2.0 | 6 votes |
@Test public void testLocalLookupsInInterpolatorAreInherited() { parent.addProperty("tablespaces.tablespace.name", "default"); parent.addProperty("tablespaces.tablespace(-1).name", "test"); parent.addProperty("tables.table(0).var", "${brackets:x}"); final ConfigurationInterpolator interpolator = parent.getInterpolator(); interpolator.registerLookup("brackets", key -> "(" + key + ")"); setUpSubnodeConfig(); assertEquals("Local lookup was not inherited", "(x)", config.getString("var", "")); }
Example #7
Source File: TestDefaultConversionHandler.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Creates a special test ConfigurationInterpolator. This object only * replaces the test variable by its replacement. Other substitutions are * not performed. * * @return the test {@code ConfigurationInterpolator} */ private static ConfigurationInterpolator createInterpolator() { return new ConfigurationInterpolator() { @Override public Object interpolate(final Object value) { if (VAR.equals(value)) { return REPLACEMENT; } return value; } }; }
Example #8
Source File: InterpolationTestHelper.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests accessing and manipulating the interpolator object. * * @param config the configuration to test */ public static void testGetInterpolator(final AbstractConfiguration config) { config.addProperty("var", "${echo:testVar}"); final ConfigurationInterpolator interpol = config.getInterpolator(); interpol.registerLookup("echo", varName -> "Value of variable " + varName); assertEquals("Wrong value of echo variable", "Value of variable testVar", config.getString("var")); }
Example #9
Source File: DefaultConversionHandler.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Performs a conversion of a single value to the specified target class. * The passed in source object is guaranteed to be a single value, but it * can be <b>null</b>. Derived classes that want to extend the available * conversions, but are happy with the handling of complex objects, just * need to override this method. * * @param <T> the desired target type of the conversion * @param src the source object (a single value) * @param targetCls the target class of the conversion * @param ci the {@code ConfigurationInterpolator} (not <b>null</b>) * @return the converted value * @throws ConversionException if conversion is not possible */ protected <T> T convertValue(final Object src, final Class<T> targetCls, final ConfigurationInterpolator ci) { if (src == null) { return null; } // This is a safe cast because PropertyConverter either returns an // object of the correct class or throws an exception. @SuppressWarnings("unchecked") final T result = (T) PropertyConverter.to(targetCls, src, this); return result; }
Example #10
Source File: TestMultiFileConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether the ConfigurationInterpolator is reset, too. */ @Test public void testInterpolatorReset() { final BasicBuilderParameters params = new MultiFileBuilderParametersImpl().setFilePattern(PATTERN); final MultiFileConfigurationBuilder<XMLConfiguration> builder = new MultiFileConfigurationBuilder<>( XMLConfiguration.class); builder.configure(params); final ConfigurationInterpolator interpolator = builder.getInterpolator(); assertNotNull("No interpolator", interpolator); builder.resetParameters(); assertNotSame("No new interpolator", interpolator, builder.getInterpolator()); }
Example #11
Source File: DefaultConversionHandler.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} This implementation extracts all values stored in the * passed in source object, converts them to the target type, and adds them * to the target collection. The target collection must not be <b>null</b>. * If the source object is <b>null</b>, nothing is added to the collection. * * @throws IllegalArgumentException if the target collection is <b>null</b> */ @Override public <T> void toCollection(final Object src, final Class<T> elemClass, final ConfigurationInterpolator ci, final Collection<T> dest) { if (dest == null) { throw new IllegalArgumentException( "Target collection must not be null!"); } if (src != null && !isEmptyElement(src)) { final ConfigurationInterpolator interpolator = fetchInterpolator(ci); convertToCollection(src, elemClass, interpolator, dest); } }
Example #12
Source File: DefaultConversionHandler.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} This implementation extracts all values stored in the * passed in source object, converts them to the target type, and adds them * to a result array. Arrays of objects and of primitive types are * supported. If the source object is <b>null</b>, result is <b>null</b>, * too. */ @Override public Object toArray(final Object src, final Class<?> elemClass, final ConfigurationInterpolator ci) { if (src == null) { return null; } if (isEmptyElement(src)) { return Array.newInstance(elemClass, 0); } final ConfigurationInterpolator interpolator = fetchInterpolator(ci); return elemClass.isPrimitive() ? toPrimitiveArray(src, elemClass, interpolator) : toObjectArray(src, elemClass, interpolator); }
Example #13
Source File: MultiFileConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Returns the {@code ConfigurationInterpolator} used by this instance. This * is the object used for evaluating the file name pattern. It is created on * demand. * * @return the {@code ConfigurationInterpolator} */ protected ConfigurationInterpolator getInterpolator() { ConfigurationInterpolator result; boolean done; // This might create multiple instances under high load, // however, always the same instance is returned. do { result = interpolator.get(); if (result != null) { done = true; } else { result = createInterpolator(); done = interpolator.compareAndSet(null, result); } } while (!done); return result; }
Example #14
Source File: TestMultiFileConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether infinite loops on constructing the file name using * interpolation can be handled. This can happen if a pattern cannot be * resolved and the {@code ConfigurationInterpolator} causes again a lookup * of the builder's configuration. */ @Test public void testRecursiveInterpolation() { final DynamicCombinedConfiguration config = new DynamicCombinedConfiguration(); config.setKeyPattern(PATTERN_VAR); final BasicBuilderParameters params = createTestBuilderParameters(null); final ConfigurationInterpolator ci = new ConfigurationInterpolator(); ci.addDefaultLookup(new ConfigurationLookup(config)); params.setInterpolator(ci); final MultiFileConfigurationBuilder<XMLConfiguration> builder = new MultiFileConfigurationBuilder<>( XMLConfiguration.class, null, true); builder.configure(params); final BuilderConfigurationWrapperFactory wrapFactory = new BuilderConfigurationWrapperFactory(); config.addConfiguration(wrapFactory.createBuilderConfigurationWrapper( HierarchicalConfiguration.class, builder), "Multi"); assertTrue("Got configuration data", config.isEmpty()); }
Example #15
Source File: TestBaseConfiguration.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether a {@code ConfigurationInterpolator} can be created and * installed. */ @Test public void testInstallInterpolator() { final Lookup prefixLookup = EasyMock.createMock(Lookup.class); final Lookup defLookup = EasyMock.createMock(Lookup.class); EasyMock.replay(prefixLookup, defLookup); final Map<String, Lookup> prefixLookups = new HashMap<>(); prefixLookups.put("test", prefixLookup); final List<Lookup> defLookups = new ArrayList<>(); defLookups.add(defLookup); config.installInterpolator(prefixLookups, defLookups); final ConfigurationInterpolator interpolator = config.getInterpolator(); assertEquals("Wrong prefix lookups", prefixLookups, interpolator.getLookups()); final List<Lookup> defLookups2 = interpolator.getDefaultLookups(); assertEquals("Wrong number of default lookups", 2, defLookups2.size()); assertSame("Wrong default lookup 1", defLookup, defLookups2.get(0)); final String var = "testVariable"; final Object value = 42; config.addProperty(var, value); assertEquals("Wrong lookup result", value, defLookups2.get(1).lookup(var)); }
Example #16
Source File: MetaborgConversionHandler.java From spoofax with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override protected <T> T convertValue(Object src, Class<T> target, ConfigurationInterpolator interp) { if(target == LanguageIdentifier.class) { return (T) convertToLanguageIdentifier(src); } else { return super.convertValue(src, target, interp); } }
Example #17
Source File: AbstractDeployer.java From studio with GNU General Public License v3.0 | 5 votes |
protected void addChildParams(Map<String, Object> childParams, ImmutableNode parentNode, ConfigurationInterpolator interpolator) { for (ImmutableNode childParamNode : parentNode.getChildren()) { if (childParamNode.getChildren().isEmpty()) { Object value = interpolator.interpolate(childParamNode.getValue()); MapUtils.add(childParams, childParamNode.getNodeName(), value); } else { Map<String, Object> params = new LinkedHashMap<>(); addChildParams(params, childParamNode, interpolator); MapUtils.add(childParams, childParamNode.getNodeName(), params); } } }
Example #18
Source File: AbstractConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} This implementation creates a new * {@code ConfigurationInterpolator} instance and initializes it with the * given {@code Lookup} objects. In addition, it adds a specialized default * {@code Lookup} object which queries this {@code Configuration}. * * @since 2.0 */ @Override public final void installInterpolator( final Map<String, ? extends Lookup> prefixLookups, final Collection<? extends Lookup> defLookups) { final InterpolatorSpecification spec = new InterpolatorSpecification.Builder() .withPrefixLookups(prefixLookups) .withDefaultLookups(defLookups) .withDefaultLookup(new ConfigurationLookup(this)) .create(); setInterpolator(ConfigurationInterpolator.fromSpecification(spec)); }
Example #19
Source File: TestBaseConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether a {@code ConfigurationInterpolator} can be set. */ @Test public void testSetInterpolator() { final ConfigurationInterpolator interpolator = EasyMock.createMock(ConfigurationInterpolator.class); EasyMock.replay(interpolator); config.setInterpolator(interpolator); assertSame("Interpolator not set", interpolator, config.getInterpolator()); }
Example #20
Source File: DefaultConversionHandler.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Converts the given source object to an array of objects. * * @param src the source object * @param elemClass the element class of the array * @param ci the {@code ConfigurationInterpolator} * @return the result array * @throws ConversionException if a conversion cannot be performed */ private <T> T[] toObjectArray(final Object src, final Class<T> elemClass, final ConfigurationInterpolator ci) { final Collection<T> convertedCol = new LinkedList<>(); convertToCollection(src, elemClass, ci, convertedCol); // Safe to cast because the element class is specified @SuppressWarnings("unchecked") final T[] result = (T[]) Array.newInstance(elemClass, convertedCol.size()); return convertedCol.toArray(result); }
Example #21
Source File: DynamicCombinedConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Creates a {@code ConfigurationInterpolator} instance for performing local * variable substitutions. This implementation returns an object which * shares the prefix lookups from this configuration's * {@code ConfigurationInterpolator}, but does not define any other lookups. * * @return the {@code ConfigurationInterpolator} */ private ConfigurationInterpolator initLocalInterpolator() { return new ConfigurationInterpolator() { @Override protected Lookup fetchLookupForPrefix(final String prefix) { return ConfigurationInterpolator .nullSafeLookup(getInterpolator().getLookups().get( prefix)); } }; }
Example #22
Source File: CombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Processes custom {@link Lookup} objects that might be declared in the * definition configuration. Each {@code Lookup} object is registered at the * definition configuration and at the result configuration. It is also * added to all child configurations added to the resulting combined * configuration. * * @param defConfig the definition configuration * @param resultConfig the resulting configuration * @throws ConfigurationException if an error occurs */ protected void registerConfiguredLookups( final HierarchicalConfiguration<?> defConfig, final Configuration resultConfig) throws ConfigurationException { final Map<String, Lookup> lookups = new HashMap<>(); final List<? extends HierarchicalConfiguration<?>> nodes = defConfig.configurationsAt(KEY_CONFIGURATION_LOOKUPS); for (final HierarchicalConfiguration<?> config : nodes) { final XMLBeanDeclaration decl = new XMLBeanDeclaration(config); final String key = config.getString(KEY_LOOKUP_KEY); final Lookup lookup = (Lookup) fetchBeanHelper().createBean(decl); lookups.put(key, lookup); } if (!lookups.isEmpty()) { final ConfigurationInterpolator defCI = defConfig.getInterpolator(); if (defCI != null) { defCI.registerLookups(lookups); } resultConfig.getInterpolator().registerLookups(lookups); } }
Example #23
Source File: AbstractConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Sets the specified {@code ConfigurationInterpolator} as the parent of * this configuration's {@code ConfigurationInterpolator}. If this * configuration does not have a {@code ConfigurationInterpolator}, a new * instance is created. Note: This method is mainly intended to be used for * initializing a configuration when it is created by a builder. Normal * client code can directly update the {@code ConfigurationInterpolator}. * * @param parent the parent {@code ConfigurationInterpolator} to be set * @since 2.0 */ public void setParentInterpolator(final ConfigurationInterpolator parent) { boolean success; do { final ConfigurationInterpolator ciOld = getInterpolator(); final ConfigurationInterpolator ciNew = ciOld != null ? ciOld : new ConfigurationInterpolator(); ciNew.setParentInterpolator(parent); success = interpolator.compareAndSet(ciOld, ciNew); } while (!success); }
Example #24
Source File: TestSubsetConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
@Test public void testLocalLookupsInInterpolatorAreInherited() { final BaseConfiguration config = new BaseConfiguration(); final ConfigurationInterpolator interpolator = config.getInterpolator(); interpolator.registerLookup("brackets", key -> "(" + key +")"); config.setProperty("prefix.var", "${brackets:x}"); final AbstractConfiguration subset = (AbstractConfiguration) config .subset("prefix"); assertEquals("Local lookup was not inherited", "(x)", subset .getString("var", "")); }
Example #25
Source File: TestAbstractConfigurationBasicFeatures.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether a parent {@code ConfigurationInterpolator} can be set if * currently no {@code ConfigurationInterpolator} is available. */ @Test public void testSetParentInterpolatorNoInterpolator() { final ConfigurationInterpolator parent = EasyMock.createMock(ConfigurationInterpolator.class); EasyMock.replay(parent); final AbstractConfiguration config = new TestConfigurationImpl(new PropertiesConfiguration()); config.setInterpolator(null); config.setParentInterpolator(parent); assertSame("Parent was not set", parent, config.getInterpolator() .getParentInterpolator()); }
Example #26
Source File: BasicBuilderParameters.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} This implementation stores the passed in * {@code ConfigurationInterpolator} object in the internal parameters map. */ @Override public BasicBuilderParameters setParentInterpolator( final ConfigurationInterpolator parent) { return setProperty(PROP_PARENT_INTERPOLATOR, parent); }
Example #27
Source File: TestAbstractConfigurationBasicFeatures.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether a parent {@code ConfigurationInterpolator} can be set if * already a {@code ConfigurationInterpolator} is available. */ @Test public void testSetParentInterpolatorExistingInterpolator() { final ConfigurationInterpolator parent = EasyMock.createMock(ConfigurationInterpolator.class); EasyMock.replay(parent); final AbstractConfiguration config = new TestConfigurationImpl(new PropertiesConfiguration()); final ConfigurationInterpolator ci = config.getInterpolator(); config.setParentInterpolator(parent); assertSame("Parent was not set", parent, config.getInterpolator() .getParentInterpolator()); assertSame("Interpolator was changed", ci, config.getInterpolator()); }
Example #28
Source File: CombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Creates and initializes a default {@code EntityResolver} if the * definition configuration contains a corresponding declaration. * * @param config the definition configuration * @param xmlParams the (already partly initialized) object with XML * parameters; here the new resolver is to be stored * @throws ConfigurationException if an error occurs */ protected void configureEntityResolver(final HierarchicalConfiguration<?> config, final XMLBuilderParametersImpl xmlParams) throws ConfigurationException { if (config.getMaxIndex(KEY_ENTITY_RESOLVER) == 0) { final XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY_ENTITY_RESOLVER, true); final EntityResolver resolver = (EntityResolver) fetchBeanHelper().createBean(decl, CatalogResolver.class); final FileSystem fileSystem = xmlParams.getFileHandler().getFileSystem(); if (fileSystem != null) { BeanHelper.setProperty(resolver, "fileSystem", fileSystem); } final String basePath = xmlParams.getFileHandler().getBasePath(); if (basePath != null) { BeanHelper.setProperty(resolver, "baseDir", basePath); } final ConfigurationInterpolator ci = new ConfigurationInterpolator(); ci.registerLookups(fetchPrefixLookups()); BeanHelper.setProperty(resolver, "interpolator", ci); xmlParams.setEntityResolver(resolver); } }
Example #29
Source File: CatalogResolver.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Performs character normalization on a URI reference. * * @param uriref The URI reference * @return The normalized URI reference. */ @Override protected String normalizeURI(final String uriref) { final ConfigurationInterpolator ci = ((CatalogManager) catalogManager).getInterpolator(); final String resolved = ci != null ? String.valueOf(ci.interpolate(uriref)) : uriref; return super.normalizeURI(resolved); }
Example #30
Source File: CombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Sets up a parent {@code ConfigurationInterpolator} object. This object * has a default {@link Lookup} querying the resulting combined * configuration. Thus interpolation works globally across all configuration * sources. * * @param resultConfig the result configuration * @param defConfig the definition configuration */ private void setUpParentInterpolator(final Configuration resultConfig, final Configuration defConfig) { parentInterpolator = new ConfigurationInterpolator(); parentInterpolator.addDefaultLookup(new ConfigurationLookup( resultConfig)); final ConfigurationInterpolator defInterpolator = defConfig.getInterpolator(); if (defInterpolator != null) { defInterpolator.setParentInterpolator(parentInterpolator); } }