geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From genspr...@apache.org
Subject svn commit: r897756 - in /geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation: ./ NavigationJsonGenerator.java TreeNode.java TreeNodeIdComparator.java
Date Mon, 11 Jan 2010 05:15:24 GMT
Author: genspring
Date: Mon Jan 11 05:15:24 2010
New Revision: 897756

URL: http://svn.apache.org/viewvc?rev=897756&view=rev
Log:
GERONIMO-4994 missed classes when applying patch.

Added:
    geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/
    geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/NavigationJsonGenerator.java
  (with props)
    geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNode.java
  (with props)
    geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNodeIdComparator.java
  (with props)

Added: geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/NavigationJsonGenerator.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/NavigationJsonGenerator.java?rev=897756&view=auto
==============================================================================
--- geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/NavigationJsonGenerator.java
(added)
+++ geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/NavigationJsonGenerator.java
Mon Jan 11 05:15:24 2010
@@ -0,0 +1,203 @@
+package org.apache.geronimo.console.navigation;
+
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.geronimo.pluto.impl.PageConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/*
+ * This class is used to generate JSON tree for a list of PageConfig.
+ * 
+ * Sample:  
+ * 
+ * pageName of pageConfig1: 1-1/server/server log
+ * pageName of pageConfig2: 2-1/applications/web applications
+ * pageName of pageConfig3: 1-2/server/server info
+ * 
+ * will result in following navigation tree:
+ * 
+ * --server
+ *      --server log
+ *      --server info
+ * --applications
+ *      --web applications
+ */
+public class NavigationJsonGenerator {
+     
+    private static final Logger log = LoggerFactory.getLogger(NavigationJsonGenerator.class);
+    
+    public String generateTreeJSON(List<PageConfig> pageConfigList, String contextPath,
String DefaultIcon) {
+       
+        Map<String, TreeNode> navigationTree = new TreeMap<String, TreeNode>();
+        
+        for (PageConfig pc : pageConfigList) {
+            try {
+                new TreeNode(pc).populateTree(navigationTree);
+            } catch (Exception e) {
+                log.error(e.getMessage(),e);
+                continue;
+            }
+        }
+
+        StringBuffer sb = new StringBuffer(10);
+        sb.append("[");
+        
+        
+        for (TreeNode node : navigationTree.values()) {
+
+            if (node.isTopNode()) {
+                sb.append("\n");
+                appendNodeToTreeJSON(sb, node, contextPath, DefaultIcon);
+            }
+
+        }
+        //remove the extra ','
+        sb.deleteCharAt(sb.length()-1);
+
+        sb.append("\n]");
+        return sb.toString();
+    }
+    
+    private void appendNodeToTreeJSON(StringBuffer sb, TreeNode node, String contextPath,
String DefaultIcon) {
+
+        sb.append("{");
+
+        
+
+        if (!node.isLeafNode()) {
+            sb.append("label: \'"+node.getLabel() + "\'");
+        } else {
+            sb.append("label: \'<img src=\"" + contextPath + node.getIcon() + "\" alt=\"\"
border=\"0\">&nbsp;");
+            sb.append("<a href=\"" + contextPath + "/portal/" + node.getId() + "/" + node.getPath()
+ "\">"
+                    + node.getLabel() + "</a>\'");
+        }
+
+        sb.append(",");
+        sb.append("id: \'" + node.getId() + "\'");
+
+        if (node.getChildren() != null) {
+            sb.append(",");
+            sb.append("children: [");
+
+            for (TreeNode child : node.getChildren().values()) {
+                appendNodeToTreeJSON(sb, child, contextPath, DefaultIcon);
+            }
+            
+            //remove the extra ','
+            sb.deleteCharAt(sb.length()-1);
+            
+            sb.append("]\n");
+        }
+
+        sb.append("},");
+
+    }
+    
+    public String generateQuickLauncherJSON(List<PageConfig> pageConfigList, String
contextPath, String DefaultIcon) {
+        
+        Map<String, TreeNode> navigationTree = new TreeMap<String, TreeNode>();
+        
+        for (PageConfig pc : pageConfigList) {
+            try {
+                new TreeNode(pc).populateTree(navigationTree);
+            } catch (Exception e) {
+                log.error(e.getMessage(),e);
+                continue;
+            }
+        }
+
+        StringBuffer sb = new StringBuffer(10);
+        sb.append("[\n");
+        
+        for (TreeNode node : navigationTree.values()) {
+
+            if (node.isTopNode()) {
+                appendNodeToQuickLauncherJSON(sb, node, contextPath, DefaultIcon);
+            }
+
+        }
+        //remove the extra ','
+        sb.deleteCharAt(sb.length()-1);
+
+        sb.append("\n]");
+        return sb.toString();
+    }
+
+    private void appendNodeToQuickLauncherJSON(StringBuffer sb, TreeNode node, String contextPath,
String DefaultIcon) {
+
+        if (node.isLeafNode()) {
+            sb.append("\n{");
+            sb.append("label: \'<img src=\"").append(contextPath).append(node.getIcon()).append("\">&nbsp;");
+            sb.append("<span>").append(node.getLabel()).append("</span>\'");
+            sb.append(",");
+            sb.append("name: \'").append(node.getLabel()).append("\'");
+            sb.append("},");
+        }
+        
+        if (node.getChildren() != null) {
+
+            for (TreeNode child : node.getChildren().values()) {
+                appendNodeToQuickLauncherJSON(sb, child, contextPath, DefaultIcon);
+            }
+            
+            //remove the extra ','
+            //sb.deleteCharAt(sb.length()-1);           
+        }
+
+
+
+    }
+    
+ public String generateLinks(List<PageConfig> pageConfigList, String contextPath, String
DefaultIcon) {
+        
+        Map<String, TreeNode> navigationTree = new TreeMap<String, TreeNode>();
+        
+        for (PageConfig pc : pageConfigList) {
+            try {
+                new TreeNode(pc).populateTree(navigationTree);
+            } catch (Exception e) {
+                log.error(e.getMessage(),e);
+                continue;
+            }
+        }
+
+        StringBuffer sb = new StringBuffer(10);
+        sb.append("<ul>\n");
+        
+        for (TreeNode node : navigationTree.values()) {
+
+            if (node.isTopNode()) {
+                appendNodeToLinks(sb, node, contextPath, DefaultIcon);
+            }
+
+        }
+
+        sb.append("</ul>");
+        return sb.toString();
+    }
+
+    private void appendNodeToLinks(StringBuffer sb, TreeNode node, String contextPath, String
DefaultIcon) {
+
+        if (node.isLeafNode()) {
+            sb.append("<li>");
+            sb.append("<a href=\"" + contextPath + "/portal/" + node.getId() + "/" + node.getPath()
+ "\">"
+                    + node.getLabel() + "</a>");
+            sb.append("</li>\n");
+        }
+        
+        if (node.getChildren() != null) {
+
+            for (TreeNode child : node.getChildren().values()) {
+                appendNodeToLinks(sb, child, contextPath, DefaultIcon);
+            }
+        
+        }
+
+
+
+    }
+
+}

Propchange: geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/NavigationJsonGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNode.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNode.java?rev=897756&view=auto
==============================================================================
--- geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNode.java
(added)
+++ geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNode.java
Mon Jan 11 05:15:24 2010
@@ -0,0 +1,135 @@
+package org.apache.geronimo.console.navigation;
+
+import java.util.Map;
+import java.util.TreeMap;
+import org.apache.geronimo.pluto.impl.PageConfig;
+
+/*
+ * Class to represent a node in navigation tree.
+ */
+public class TreeNode {
+    
+    static final String ID_DELIMIT = "-";
+    private static final String NAME_DELIMIT = "/";
+    private String ID;
+    private PageConfig pageConfig;
+    private String label;
+    private String icon;
+    private String path;
+    private Map<String, TreeNode> children;
+
+    /*
+     * Used for category node.
+     */
+    TreeNode(String id, String categoryPath) {
+
+        ID = id;
+        path = categoryPath;
+        label = categoryPath.substring(categoryPath.lastIndexOf(NAME_DELIMIT) + 1, categoryPath.length());
+        icon = "/images/ico_folder_16x16.gif";
+
+    }
+
+    /*
+     * Used for leaf node.
+     */
+    TreeNode(PageConfig _pageConfig) throws Exception {
+
+        this.pageConfig = _pageConfig;
+        String pageName = pageConfig.getName();
+        path = pageName.substring(pageName.indexOf(NAME_DELIMIT) + 1, pageName.length());
+        String[] fragments = pageName.split(NAME_DELIMIT);
+        ID = fragments[0];
+
+        // check ID format
+        String[] idNumbers = ID.split(ID_DELIMIT);
+        try {
+            for (String numberString : idNumbers) {
+
+                Integer.parseInt(numberString);
+
+            }
+        } catch (NumberFormatException e) {
+            throw new Exception("Ilegal ID:" + ID + "in pageConfig:" + pageName, e);
+        }
+
+        label = fragments[fragments.length - 1];
+        icon = pageConfig.getIcon()==null?"/images/ico_geronimo_16x16.gif":pageConfig.getIcon();
+    }
+
+    String getIcon() {
+        return icon;
+    }
+
+    String getLabel() {
+        return label;
+    }
+
+    String getId() {
+        return ID;
+    }
+
+    /*
+     * only category node has children.
+     */
+    Map<String, TreeNode> getChildren() {
+        
+        if (isLeafNode())
+            return null;
+        
+        if (children == null) {
+            children = new TreeMap<String, TreeNode>(new TreeNodeIdComparator());
+        }
+        
+        return children;
+    }
+
+    String getPath() {
+        return path;
+    }
+
+    boolean isLeafNode() {
+        return pageConfig == null ? false : true;
+    }
+
+    boolean isTopNode() {
+        return ID.indexOf(ID_DELIMIT) < 0;
+    }
+
+    void populateTree(Map<String, TreeNode> tree) {
+
+        tree.put(ID, this);
+        // no parents available
+        if (this.ID.indexOf(ID_DELIMIT) < 0) {
+            return;
+        }
+
+        String parentId = ID.substring(0, ID.lastIndexOf(ID_DELIMIT));
+
+        String parentPath = path.substring(0, path.lastIndexOf(NAME_DELIMIT));
+
+        if (!tree.containsKey(parentId)) {
+            TreeNode parentNode = new TreeNode(parentId, parentPath);
+            parentNode.populateTree(tree);
+            tree.put(parentId, parentNode);
+        }
+
+        tree.get(parentId).addChild(this);
+
+    }
+
+    private void addChild(TreeNode child) {
+        
+        if (isLeafNode())
+            return;
+
+        this.getChildren().put(child.getId(), child);
+    }
+
+    @Override
+    public String toString() {
+        return "TreeNode [Id=" + ID + ", label=" + label + "]";
+    }
+
+    
+}

Propchange: geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNode.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNodeIdComparator.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNodeIdComparator.java?rev=897756&view=auto
==============================================================================
--- geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNodeIdComparator.java
(added)
+++ geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNodeIdComparator.java
Mon Jan 11 05:15:24 2010
@@ -0,0 +1,49 @@
+package org.apache.geronimo.console.navigation;
+
+import java.util.Comparator;
+
+/*
+ * NodeId is in format like this: 1-2-3
+ */
+class TreeNodeIdComparator implements Comparator<String>{
+    
+    public int compare(String idString1, String idString2) {
+        
+        String[] ids1 = idString1.split(TreeNode.ID_DELIMIT);
+        String[] ids2 = idString2.split(TreeNode.ID_DELIMIT);
+
+        int ids1_length = ids1.length;
+        int ids2_length = ids2.length;
+        
+        // Sample: for 1-2-3 and 1-3, only compare 1-2 and 1-3.
+        int shorterLength = ids1_length <= ids2_length ? ids1_length : ids2.length;
+
+        for (int i = 0; i < shorterLength; i++) {
+
+            int id1 = Integer.parseInt(ids1[i]);
+            int id2 = Integer.parseInt(ids2[i]);
+
+            if (id1 == id2) {
+                continue;
+            }
+
+            if (id1 > id2) {
+                return 1;
+            } else {
+                return -1;
+            }
+
+        }
+     // Sample: 1-2 >1-2-2
+        if (ids1_length < ids2_length) {
+            return -1;
+        }
+
+        if (ids1_length > ids2_length) {
+            return 1;
+        }
+
+        return 0;
+    }
+    
+}

Propchange: geronimo/server/trunk/plugins/console/console-portal-driver/src/main/java/org/apache/geronimo/console/navigation/TreeNodeIdComparator.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message