You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by fm...@apache.org on 2015/05/19 08:58:04 UTC

svn commit: r1680188 - /felix/trunk/webconsole-plugins/metatype/src/main/java/org/apache/felix/webconsole/plugins/metatype/internal/MetatypeInventoryPrinter.java

Author: fmeschbe
Date: Tue May 19 06:58:04 2015
New Revision: 1680188

URL: http://svn.apache.org/r1680188
Log:
FELIX-4890 Add a MetaType service InventoryPrinter

 * add default values and option labels/values to the
     AttributeDefinition output

Modified:
    felix/trunk/webconsole-plugins/metatype/src/main/java/org/apache/felix/webconsole/plugins/metatype/internal/MetatypeInventoryPrinter.java

Modified: felix/trunk/webconsole-plugins/metatype/src/main/java/org/apache/felix/webconsole/plugins/metatype/internal/MetatypeInventoryPrinter.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/metatype/src/main/java/org/apache/felix/webconsole/plugins/metatype/internal/MetatypeInventoryPrinter.java?rev=1680188&r1=1680187&r2=1680188&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/metatype/src/main/java/org/apache/felix/webconsole/plugins/metatype/internal/MetatypeInventoryPrinter.java (original)
+++ felix/trunk/webconsole-plugins/metatype/src/main/java/org/apache/felix/webconsole/plugins/metatype/internal/MetatypeInventoryPrinter.java Tue May 19 06:58:04 2015
@@ -20,7 +20,6 @@ package org.apache.felix.webconsole.plug
 
 import java.io.PrintWriter;
 import java.util.Hashtable;
-
 import org.apache.felix.inventory.Format;
 import org.apache.felix.inventory.InventoryPrinter;
 import org.json.JSONException;
@@ -92,7 +91,8 @@ class MetatypeInventoryPrinter implement
 
     private static final void printComponents(final Printer pw, final Bundle bundle, final MetaTypeInformation info)
     {
-        if (info == null) {
+        if (info == null)
+        {
             return;
         }
 
@@ -151,12 +151,47 @@ class MetatypeInventoryPrinter implement
         pw.keyValue("description", ad.getDescription());
         pw.keyValue("type", type(ad.getType()));
         pw.keyValue("cardinality", cardinality(ad.getCardinality()));
+        defaultValue(pw, ad.getDefaultValue());
+        options(pw, ad.getOptionLabels(), ad.getOptionValues());
         pw.endGroup();
+    }
+
+    private static final void defaultValue(final Printer pw, final String[] defaultValue)
+    {
+        if (defaultValue != null)
+        {
+            switch (defaultValue.length)
+            {
+                case 0: // ignore
+                    break;
+
+                case 1:
+                    pw.keyValue("default", defaultValue[0]);
+                    break;
+
+                default:
+                    pw.list("default");
+                    for (String value : defaultValue)
+                    {
+                        pw.entry(value);
+                    }
+                    pw.endList();
+                    break;
+            }
+        }
+    }
 
-        // Omitted:
-        // ad.getDefaultValue()
-        // ad.getOptionLabels()
-        // ad.getOptionValues()
+    private static final void options(final Printer pw, final String[] optionLabels, final String[] optionValues)
+    {
+        if (optionLabels != null && optionLabels.length > 0)
+        {
+            pw.group("options");
+            for (int i = 0; i < optionLabels.length; i++)
+            {
+                pw.keyValue(optionLabels[i], optionValues[i]);
+            }
+            pw.endGroup();
+        }
     }
 
     @SuppressWarnings("deprecation")
@@ -213,13 +248,20 @@ class MetatypeInventoryPrinter implement
     {
         void start();
 
-        void group(String name);
+        void end();
 
-        void keyValue(String key, Object value);
+        void group(String name);
 
         void endGroup();
 
-        void end();
+        void list(String name);
+
+        void entry(String value);
+
+        void endList();
+
+        void keyValue(String key, Object value);
+
     }
 
     private static class TextPrinter implements Printer
@@ -227,26 +269,30 @@ class MetatypeInventoryPrinter implement
 
         private final PrintWriter pw;
 
-        private String indent = "";
+        private String indent;
+
+        private boolean inList;
 
         TextPrinter(final PrintWriter pw)
         {
             this.pw = pw;
+
+            this.indent = "";
+            this.inList = false;
         }
 
         public void start()
         {
         }
 
-        public void group(String name)
+        public void end()
         {
-            this.pw.printf("%s%s:%n", indent, name);
-            this.indent += "  ";
         }
 
-        public void keyValue(String key, Object value)
+        public void group(String name)
         {
-            this.pw.printf("%s%s: %s%n", indent, key, value);
+            this.pw.printf("%s%s:%n", indent, name);
+            this.indent += "  ";
         }
 
         public void endGroup()
@@ -257,8 +303,33 @@ class MetatypeInventoryPrinter implement
             }
         }
 
-        public void end()
+        public void list(String name)
         {
+            this.pw.printf("%s%s: [", indent, name);
+        }
+
+        public void entry(String value)
+        {
+            if (this.inList)
+            {
+                this.pw.print(", ");
+            }
+            else
+            {
+                this.inList = true;
+            }
+            this.pw.print(value);
+        }
+
+        public void endList()
+        {
+            this.inList = false;
+            this.pw.println("]");
+        }
+
+        public void keyValue(String key, Object value)
+        {
+            this.pw.printf("%s%s: %s%n", indent, key, value);
         }
     }
 
@@ -283,22 +354,22 @@ class MetatypeInventoryPrinter implement
             }
         }
 
-        public void group(String name)
+        public void end()
         {
             try
             {
-                this.pw.key(name).object();
+                this.pw.endObject();
             }
             catch (JSONException ignore)
             {
             }
         }
 
-        public void keyValue(String key, Object value)
+        public void group(String name)
         {
             try
             {
-                this.pw.key(key).value(value);
+                this.pw.key(name).object();
             }
             catch (JSONException ignore)
             {
@@ -316,11 +387,44 @@ class MetatypeInventoryPrinter implement
             }
         }
 
-        public void end()
+        public void list(String name)
         {
             try
             {
-                this.pw.endObject();
+                this.pw.key(name).array();
+            }
+            catch (JSONException ignore)
+            {
+            }
+        }
+
+        public void entry(String value)
+        {
+            try
+            {
+                this.pw.value(value);
+            }
+            catch (JSONException ignore)
+            {
+            }
+        }
+
+        public void endList()
+        {
+            try
+            {
+                this.pw.endArray();
+            }
+            catch (JSONException ignore)
+            {
+            }
+        }
+
+        public void keyValue(String key, Object value)
+        {
+            try
+            {
+                this.pw.key(key).value(value);
             }
             catch (JSONException ignore)
             {