KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > graph > NameSpaceTreeBuilder


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.graph;
26
27 import org.snipsnap.container.Components;
28 import org.snipsnap.graph.builder.TreeBuilder;
29 import org.snipsnap.graph.graph.Tree;
30 import org.snipsnap.graph.graph.TreeNode;
31 import org.snipsnap.snip.Snip;
32 import org.snipsnap.snip.SnipSpace;
33
34 public class NameSpaceTreeBuilder implements TreeBuilder {
35   private String JavaDoc root;
36
37   public NameSpaceTreeBuilder(String JavaDoc root) {
38     this.root = root;
39   }
40
41   public Tree build() {
42     SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
43
44     Snip[] snips = space.match(root);
45
46     if (root.endsWith("/")) {
47       root = root.substring(0, root.length() - 1);
48     }
49
50     TreeNode parent = new TreeNode(root);
51     TreeNode lastNode = parent;
52     Tree tree = new Tree(parent);
53
54     int depth = -1;
55     int currentDepth = -1;
56     for (int i = 0; i < snips.length; i++) {
57       Snip snip = snips[i];
58       String JavaDoc elements[] = snip.getName().split("/");
59       currentDepth = elements.length - 1;
60       String JavaDoc element = elements[currentDepth];
61
62       if (-1 == depth) {
63         depth = currentDepth;
64       }
65       if (currentDepth > depth) {
66         depth = currentDepth;
67         TreeNode child = new TreeNode(element, lastNode);
68         parent = lastNode;
69         parent.addChild(child);
70         lastNode = child;
71       } else if (currentDepth < depth) {
72         depth = currentDepth;
73         parent = parent.getParent();
74         TreeNode child = new TreeNode(element, parent);
75         parent.addChild(child);
76         lastNode = child;
77       } else {
78         TreeNode child = new TreeNode(element, parent);
79         parent.addChild(child);
80         lastNode = child;
81       }
82     }
83     // tree.setRowCounter(tree.getDepth());
84
// int maxChildren[] = new Maximum().getMaxChildren(tree);
85
// int maxAttributes[] = new Maximum().getMaxAttributes(tree);
86
// tree.setMaxChildren(maxChildren);
87
// tree.setMaxAttributes(maxAttributes);
88

89     System.err.println("Tree=" + tree);
90     System.err.println("Tree depth=" + tree.getDepth());
91     return tree;
92   }
93 }
94
Popular Tags