use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.
the class ConfigDescriptionConverter method unmarshal.
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
ConfigDescription configDescription = null;
// read attributes
Map<String, String> attributes = this.attributeMapValidator.readValidatedAttributes(reader);
String uriText = attributes.get("uri");
if (uriText == null) {
// the URI could be overridden by a context field if it could be
// automatically extracted
uriText = (String) context.get("config-description.uri");
}
URI uri = null;
try {
uri = new URI(uriText);
} catch (NullPointerException | URISyntaxException ex) {
throw new ConversionException("The URI '" + uriText + "' in node '" + reader.getNodeName() + "' is invalid!", ex);
}
// create the lists to hold parameters and groups
List<ConfigDescriptionParameter> configDescriptionParams = new ArrayList<ConfigDescriptionParameter>();
List<ConfigDescriptionParameterGroup> configDescriptionGroups = new ArrayList<ConfigDescriptionParameterGroup>();
// read values
List<?> nodes = (List<?>) context.convertAnother(context, List.class);
NodeIterator nodeIterator = new NodeIterator(nodes);
// respective arrays
while (nodeIterator.hasNext() == true) {
Object node = nodeIterator.next();
if (node instanceof ConfigDescriptionParameter) {
configDescriptionParams.add((ConfigDescriptionParameter) node);
}
if (node instanceof ConfigDescriptionParameterGroup) {
configDescriptionGroups.add((ConfigDescriptionParameterGroup) node);
}
}
ConverterAssertion.assertEndOfType(reader);
// create object
configDescription = new ConfigDescription(uri, configDescriptionParams, configDescriptionGroups);
return configDescription;
}
use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.
the class ConfigDescriptionsTest method ConfigDescriptionsShouldLoadProperly.
@Test
public void ConfigDescriptionsShouldLoadProperly() throws Exception {
int initialNumberOfConfigDescriptions = configDescriptionRegistry.getConfigDescriptions().size();
// install test bundle
Bundle bundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
assertThat(bundle, is(notNullValue()));
Collection<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions();
assertThat(configDescriptions.size(), is(initialNumberOfConfigDescriptions + 3));
URI bridgeURI = new URI("thing-type:hue:bridge");
ConfigDescription bridgeConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(bridgeURI)).findFirst().get();
assertThat(bridgeConfigDescription, is(notNullValue()));
Collection<ConfigDescriptionParameter> parameters = bridgeConfigDescription.getParameters();
assertThat(parameters.size(), is(2));
ConfigDescriptionParameter ipParameter = parameters.stream().filter(it -> it.getName().equals("ip")).findFirst().get();
assertThat(ipParameter, is(notNullValue()));
assertThat(ipParameter.getType(), is(Type.TEXT));
assertThat(ipParameter.getContext(), is("network-address"));
assertThat(ipParameter.getLabel(), is("Network Address"));
assertThat(ipParameter.getDescription(), is("Network address of the hue bridge."));
assertThat(ipParameter.isRequired(), is(true));
ConfigDescriptionParameter userNameParameter = parameters.stream().filter(it -> it.getName().equals("username")).findFirst().get();
assertThat(userNameParameter, is(notNullValue()));
assertThat(userNameParameter.isAdvanced(), is(true));
assertThat(userNameParameter.getType(), is(Type.TEXT));
assertThat(userNameParameter.getContext(), is("password"));
assertThat(userNameParameter.getLabel(), is("Username"));
assertThat(userNameParameter.getDescription(), is("Name of a registered hue bridge user, that allows to access the API."));
URI colorURI = new URI("channel-type:hue:color");
ConfigDescription colorConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(colorURI)).findFirst().get();
assertThat(colorConfigDescription, is(notNullValue()));
parameters = colorConfigDescription.getParameters();
assertThat(parameters.size(), is(1));
ConfigDescriptionParameter lastDimValueParameter = parameters.stream().filter(it -> it.getName().equals("lastDimValue")).findFirst().get();
assertThat(lastDimValueParameter, is(notNullValue()));
assertThat(lastDimValueParameter.getType(), is(Type.BOOLEAN));
Collection<ConfigDescriptionParameterGroup> groups = bridgeConfigDescription.getParameterGroups();
assertThat(groups.size(), is(2));
ConfigDescriptionParameterGroup group1 = groups.stream().filter(it -> it.getName().equals("group1")).findFirst().get();
assertThat(group1, is(notNullValue()));
assertThat(group1.getContext(), is("Group1-context"));
assertThat(group1.getLabel(), is("Group Label 1"));
assertThat(group1.getDescription(), is("Group description 1"));
// uninstall test bundle
bundle.uninstall();
assertThat(bundle.getState(), is(Bundle.UNINSTALLED));
}
use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.
the class ConfigI18nLocalizationService method getLocalizedConfigDescription.
/**
* Localize a config description.
*
* @param bundle the bundle the i18n resources are located
* @param configDescription the config description that should be localized
* @param locale the locale it should be localized to
* @return a localized configuration description on success, a non-localized one on error (e.g. no translation is
* found).
*/
public ConfigDescription getLocalizedConfigDescription(final Bundle bundle, final ConfigDescription configDescription, @Nullable final Locale locale) {
final List<ConfigDescriptionParameter> localizedConfigDescriptionParameters = new ArrayList<>(configDescription.getParameters().size());
// Loop through all the configuration parameters and localize them
for (final ConfigDescriptionParameter configDescriptionParameter : configDescription.getParameters()) {
final ConfigDescriptionParameter localizedConfigDescriptionParameter = getLocalizedConfigDescriptionParameter(bundle, configDescription, configDescriptionParameter, locale);
localizedConfigDescriptionParameters.add(localizedConfigDescriptionParameter);
}
final List<ConfigDescriptionParameterGroup> localizedConfigDescriptionGroups = new ArrayList<>(configDescription.getParameterGroups().size());
// Loop through all the configuration groups and localize them
for (final ConfigDescriptionParameterGroup configDescriptionParameterGroup : configDescription.getParameterGroups()) {
final ConfigDescriptionParameterGroup localizedConfigDescriptionGroup = getLocalizedConfigDescriptionGroup(bundle, configDescription, configDescriptionParameterGroup, locale);
localizedConfigDescriptionGroups.add(localizedConfigDescriptionGroup);
}
return new ConfigDescription(configDescription.getUID(), localizedConfigDescriptionParameters, localizedConfigDescriptionGroups);
}
use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.
the class ConfigI18nLocalizationService method getLocalizedConfigDescriptionGroup.
/**
* Localize a config description parameter group.
*
* @param bundle the bundle the i18n resources are located
* @param configDescription the config description the parameter group is part of
* @param group the parameter group that should be localized
* @param locale the locale it should be localized to
* @return a localized parameter group on success, a non-localized one on error (e.g. no translation is found).
*/
public ConfigDescriptionParameterGroup getLocalizedConfigDescriptionGroup(final Bundle bundle, final ConfigDescription configDescription, final ConfigDescriptionParameterGroup group, @Nullable final Locale locale) {
final URI configDescriptionURI = configDescription.getUID();
final String name = group.getName();
final String label = this.configDescriptionGroupI18nUtil.getGroupLabel(bundle, configDescriptionURI, name, group.getLabel(), locale);
final String description = this.configDescriptionGroupI18nUtil.getGroupDescription(bundle, configDescriptionURI, name, group.getDescription(), locale);
final ConfigDescriptionParameterGroup localizedGroup = new ConfigDescriptionParameterGroup(name, group.getContext(), group.isAdvanced(), label, description);
return localizedGroup;
}
use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.
the class ConfigDescriptionParameterGroupConverter method unmarshal.
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext marshallingContext) {
String name = reader.getAttribute("name");
// Read values
ConverterValueMap valueMap = new ConverterValueMap(reader, marshallingContext);
String context = valueMap.getString("context");
String description = valueMap.getString("description");
String label = valueMap.getString("label");
Boolean advanced = valueMap.getBoolean("advanced", false);
return new ConfigDescriptionParameterGroup(name, context, advanced, label, description);
}
Aggregations