public boolean applyDefaultValues()

in src/main/java/org/apache/sling/feature/extension/apiregions/api/config/validation/FeatureValidator.java [216:342]


    public boolean applyDefaultValues(final Feature feature, final FeatureValidationResult result) {
        boolean changed = false;

        for(final Map.Entry<String, ConfigurationValidationResult> entry : result.getConfigurationResults().entrySet()) {
            if ( entry.getValue().isUseDefaultValue() ) {
                final Configuration cfg = feature.getConfigurations().getConfiguration(entry.getKey());
                if ( cfg != null ) {
                    boolean hasPrivateProperty = false;
                    final List<String> keys = new ArrayList<>(Collections.list(cfg.getConfigurationProperties().keys()));
                    for(final String k : keys ) {
                        final PropertyValidationResult pvr = entry.getValue().getPropertyResults().get(k);
                        if ( pvr != null && pvr.isUseDefaultValue() ) {
                            cfg.getProperties().remove(k);
                            changed = true;    
                        } else {
                            hasPrivateProperty = true;
                        }
                    }
                    if ( !hasPrivateProperty ) {
                        feature.getConfigurations().remove(cfg);
                        changed = true;
                    }
                }
            }
            for(final Map.Entry<String, PropertyValidationResult> propEntry : entry.getValue().getPropertyResults().entrySet()) {
                if ( propEntry.getValue().isUseDefaultValue() ) {
                    final Configuration cfg = feature.getConfigurations().getConfiguration(entry.getKey());
                    if ( cfg != null ) {
                        if ( propEntry.getValue().getDefaultValue() == null ) {
                            if ( propEntry.getValue().getUseExcludes() != null || propEntry.getValue().getUseIncludes() != null ) {
                                final List<String> includes = new ArrayList<>();
                                final Set<String> excludes = new LinkedHashSet<>();
                                if ( propEntry.getValue().getUseIncludes() != null ) {
                                    for(final String v : propEntry.getValue().getUseIncludes()) {
                                        includes.add(0, v);
                                    }
                                }
                                if ( propEntry.getValue().getUseExcludes() != null ) {
                                    for(final String v : propEntry.getValue().getUseExcludes()) {
                                        excludes.add(v);
                                    }
                                }

                                Object value = cfg.getProperties().get(propEntry.getKey());
                                if ( value.getClass().isArray() ) {
                                    // array
                                    int l = Array.getLength(value);
                                    int i = 0;
                                    while ( i < l ) {
                                        final String val = Array.get(value, i).toString();
                                        if ( excludes.contains(val) ) {
                                            final Object newArray = Array.newInstance(value.getClass().getComponentType(), l - 1);
                                            int newIndex = 0;
                                            for(int oldIndex = 0; oldIndex < l; oldIndex++) {
                                                if ( oldIndex != i ) {
                                                    Array.set(newArray, newIndex, Array.get(value, oldIndex));
                                                    newIndex++;
                                                }
                                            }
                                            value = newArray;
                                            i--;
                                            l--;
                                            changed = true;
                                            cfg.getProperties().put(propEntry.getKey(), value);
                                        } else if ( includes.contains(val) ) {
                                            includes.remove(val);
                                        }
                                        i++;
                                    }
                                    for(final String val : includes) {
                                        final Object newArray = Array.newInstance(value.getClass().getComponentType(), Array.getLength(value) + 1);
                                        System.arraycopy(value, 0, newArray, 1, Array.getLength(value));
                                        Array.set(newArray, 0, 
                                            Converters.standardConverter().convert(val).to(value.getClass().getComponentType()));
                                        value = newArray;
                                        cfg.getProperties().put(propEntry.getKey(), value);
                                        changed = true;
                                    }
                                } else if ( value instanceof Collection ) { 
                                    // collection
                                    final Collection c = (Collection)value;
                                    final Class collectionType = c.isEmpty() ? String.class : c.iterator().next().getClass();
                                    final Iterator<?> i = c.iterator();
                                    while ( i.hasNext() ) {
                                        final String val = i.next().toString();
                                        if ( excludes.contains(val) ) {
                                            i.remove();
                                            changed = true;
                                        } else if ( includes.contains(val) ) {
                                            includes.remove(val);
                                        }
                                    }
                                    for(final String val : includes) {
                                        final Object newValue = Converters.standardConverter().convert(val).to(collectionType);
                                        if ( c instanceof List ) {
                                            ((List)c).add(0, newValue);
                                        } else {
                                            c.add(newValue);
                                        }
                                        changed = true;
                                    }
                                }                    
                            } else {
                                cfg.getProperties().remove(propEntry.getKey());
                            }
                        } else {
                            cfg.getProperties().put(propEntry.getKey(), propEntry.getValue().getDefaultValue());
                        }
                        changed = true;
                    }
                }
            }
        }

        for(final Map.Entry<String, PropertyValidationResult> propEntry : result.getFrameworkPropertyResults().entrySet()) {
            if ( propEntry.getValue().isUseDefaultValue() ) {
                if ( propEntry.getValue().getDefaultValue() == null ) {
                    feature.getFrameworkProperties().remove(propEntry.getKey());
                } else {
                    feature.getFrameworkProperties().put(propEntry.getKey(), propEntry.getValue().getDefaultValue().toString());
                }
                changed = true;
            }
        }

        return changed;
    }