You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by an...@apache.org on 2021/09/02 10:15:10 UTC

[sling-org-apache-sling-feature-cpconverter] branch SLING-10783 created (now 9a93c24)

This is an automated email from the ASF dual-hosted git repository.

angela pushed a change to branch SLING-10783
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git.


      at 9a93c24  SLING-10783 : XmlConfigurationEntryHandler - sonar findings

This branch includes the following new commits:

     new 9a93c24  SLING-10783 : XmlConfigurationEntryHandler - sonar findings

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[sling-org-apache-sling-feature-cpconverter] 01/01: SLING-10783 : XmlConfigurationEntryHandler - sonar findings

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

angela pushed a commit to branch SLING-10783
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git

commit 9a93c24a91643c43df165200f1e0f7fc69068f0b
Author: angela <an...@adobe.com>
AuthorDate: Thu Sep 2 12:14:47 2021 +0200

    SLING-10783 : XmlConfigurationEntryHandler - sonar findings
---
 .../handlers/XmlConfigurationEntryHandler.java     | 118 ++++++++++-----------
 1 file changed, 58 insertions(+), 60 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/cpconverter/handlers/XmlConfigurationEntryHandler.java b/src/main/java/org/apache/sling/feature/cpconverter/handlers/XmlConfigurationEntryHandler.java
index d9054b0..ad6e6d4 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/handlers/XmlConfigurationEntryHandler.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/handlers/XmlConfigurationEntryHandler.java
@@ -20,7 +20,8 @@ import java.io.InputStream;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Dictionary;
-import java.util.List;
+import java.util.Objects;
+import java.util.function.Function;
 
 import javax.jcr.PropertyType;
 
@@ -40,22 +41,21 @@ public final class XmlConfigurationEntryHandler extends AbstractConfigurationEnt
 
     @Override
     protected @Nullable Dictionary<String, Object> parseConfiguration(@NotNull String name, @NotNull InputStream input) {
-        JcrConfigurationHandler configurationHandler = new JcrConfigurationHandler();
         try {
-            return configurationHandler.parse(input);
+            return new JcrConfigurationParser().parse(input);
         } catch (Exception e) {
             logger.warn("Current OSGi configuration does not represent a valid XML document, see nested exceptions", e);
             return null;
         }
     }
 
-    protected static final class JcrConfigurationHandler extends AbstractJcrNodeParser<Dictionary<String, Object>> {
+    private static final class JcrConfigurationParser extends AbstractJcrNodeParser<Dictionary<String, Object>> {
 
         private static final String SLING_OSGICONFIG = "sling:OsgiConfig";
 
         private Dictionary<String, Object> configuration;
 
-        public JcrConfigurationHandler() {
+        public JcrConfigurationParser() {
             super(SLING_OSGICONFIG);
         }
 
@@ -69,73 +69,71 @@ public final class XmlConfigurationEntryHandler extends AbstractConfigurationEnt
                 // ignore jcr: and similar properties
                 if (attributeQName.indexOf(':') == -1) {
                     String attributeValue = attributes.getValue(i);
-
-                    if (attributeValue != null && !attributeValue.isEmpty()) {
+                    if (!isEmptyOrNull(attributeValue)) {
                         DocViewProperty property = DocViewProperty.parse(attributeQName, attributeValue);
-                        Object value = property.values;
-                        List<String> strValues = Arrays.asList(property.values);
-                        switch (property.type) {
-                            case PropertyType.DATE:
-                                // Date was never properly supported as osgi configs don't support dates so converting to millis 
-                                // Scenario should just be theoretical
-                                value = strValues.stream().map(s -> {
-                                    Long res = null;
-                                    if (s != null) {
-                                        Calendar cal = ISO8601.parse(s);
-                                        if (cal != null) {
-                                            res = cal.getTimeInMillis();
-                                        }
-                                    }
-                                    return res;
-                                }).toArray();
-                                break;
-                            case PropertyType.DOUBLE:
-                                value = strValues.stream().map(s -> {
-                                    Double res = null;
-                                    if (!s.isEmpty()) {
-                                        res = Double.parseDouble(s);
-                                    }
-                                    return res;
-                                }).toArray();
-                                break;
-                            case PropertyType.LONG:
-                                value = strValues.stream().map(s -> {
-                                    Long res = null;
-                                    if (!s.isEmpty()) {
-                                        res = Long.parseLong(s);
-                                    }
-                                    return res;
-                                }).toArray();
-                                break;
-                            case PropertyType.BOOLEAN:
-                                value = strValues.stream().map(s -> {
-                                    Boolean res = null;
-                                    if (s != null) {
-                                        res = Boolean.valueOf(s);
-                                    }
-                                    return res;
-                                }).toArray();
-                                break;
+                        Object[] values = getValues(property);
+                        if (values.length == 0) {
+                            // ignore empty values (either property.values were empty or value mapping resulted in null 
+                            // results that got filtered)
+                            continue;
                         }
                         if (!property.isMulti) {
-                            // first element to be used in case of singlevalue
-                            value = ((Object[])value)[0];
-                        }
-                         
-                        
-                        if (property.values.length > 0) {
-                            configuration.put(attributeQName, value);
+                            // first element to be used in case of single-value property
+                            configuration.put(attributeQName, values[0]);
+                        } else {
+                            configuration.put(attributeQName, values);
                         }
                     }
                 }
             }
         }
+        
+        private static boolean isEmptyOrNull(@Nullable String s) {
+            return s == null || s.isEmpty();
+        }
+        
+        @NotNull 
+        private static Object[] getValues(@NotNull DocViewProperty property) {
+            Object[] values;
+            switch (property.type) {
+                case PropertyType.DATE:
+                    // Date was never properly supported as osgi configs don't support dates so converting to millis 
+                    // Scenario should just be theoretical
+                    values = mapValues(property.values, s -> {
+                        Calendar cal = ISO8601.parse(s);
+                        return (cal != null) ? cal.getTimeInMillis() : null;
+                    });
+                    break;
+                case PropertyType.DOUBLE:
+                    values = mapValues(property.values, Double::parseDouble);
+                    break;
+                case PropertyType.LONG:
+                    values = mapValues(property.values, Long::parseLong);
+                    break;
+                case PropertyType.BOOLEAN:
+                    values = mapValues(property.values, Boolean::valueOf);
+                    break;
+                default:
+                    values = property.values;
+            }
+            return values;
+        }
+        
+        @NotNull 
+        private static Object[] mapValues(@NotNull String[] strValues, Function<String, Object> function) {
+            return Arrays.stream(strValues).map(s -> {
+                Object res = null;
+                if (!isEmptyOrNull(s)) {
+                    res = function.apply(s);
+                }
+                return res;
+            }).filter(Objects::nonNull).toArray();
+        }
 
         @Override
-        protected Dictionary<String, Object> getParsingResult() {
+        protected @NotNull Dictionary<String, Object> getParsingResult() {
             return configuration;
         }
-
     }
 
 }