By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,718 Members | 1,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,718 IT Pros & Developers. It's quick & easy.

How to create tree from jar file contents stored as an array of strings

P: n/a
I want to create a hierarchical tree from the array of strings
obtained by extracting a jar file.I want to do this using Javascript.

For ex,
I am having the entries in the array as
com/asd/many/a.class
com/asd/many/b.class
com/asd/xyz/c.class
com/asd/d.class
com/asd/man/you/e.class

These are obtained by extracting any jar file.
I want to create the tree using this information.
Please send me the required algorithm or the source code as soon as
possible.

Thank You.
Mriganka.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <82**************************@posting.google.com >,
mr*********@yahoo.co.in enlightened us with...

These are obtained by extracting any jar file.
I want to create the tree using this information.
Please send me the required algorithm or the source code as soon as
possible.


Please learn the difference between Java and Javascript.

Please pay me to do your work for you.
--
--
~kaeli~
Is it possible to be totally partial?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2

P: n/a
mr*********@yahoo.co.in (Mriganka) writes:
I want to create a hierarchical tree from the array of strings
obtained by extracting a jar file.I want to do this using Javascript. For ex,
I am having the entries in the array as
com/asd/many/a.class
com/asd/many/b.class
com/asd/xyz/c.class
com/asd/d.class
com/asd/man/you/e.class
Ok, format is simple. Do you expect to have only one root, or several?
If only "com" exists at the top level, you will get just a tree,
otherwise you get a forest.
These are obtained by extracting any jar file.
Whatever :)
I want to create the tree using this information.
Please send me the required algorithm or the source code as soon as
possible.


The algorithm is easy. Split each string at the slashes, and build
a tree structure dynamically. Each node is an array, where the integer
indexed elements are the leaves and non-integer indexed elements are
branches to other tree nodes.
---
function plant(seed) { // seed is an array of strings
var forest = new Array();
for (var i=0; i<seed.length;i++) {
var path = seed[i].split("/");
var node = forest;
for(var j=0; j<path.length-1;j++) { // follow path, ot build it
var branchName = path[j];
var childNode = node[branchName];
if (!childNode) {
childNode = node[branchName] = new Array();
}
node = childNode;
}
node[node.length] = path[path.length-1];
}
return forest;
}
---

Your example strings would generate a structure similar to this
object literal (except it uses arrays, not objects):
---
{com: {asd: { many: { "0": "a.class",
"1": "b.class" },
xyz: { "0" : "c.class" },
"0": "d.class",
man: { you: { "0" : "e.class" }}}}}
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.