home *** CD-ROM | disk | FTP | other *** search
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- class web_structure_builder {
- public Vector web_list = new Vector();
- public Vector web_tree = new Vector();
- public int max_level = 0;
- boolean continu = true;
- static final String seperator_string = "%";
- static final String level_string = "#";
- static final String dont_publish_string = "|";
-
- String build_web_structures(Vector branch, int current_level, String current_entry, StringTokenizer tokenizer) {
- int count = 0;
- this.record_max_level(current_level);
-
- while(this.continu) {
- int level = this.find_level(current_entry);
- if (level == current_level) {
- ++count;
- this.add_entry(branch, level, current_entry.substring(current_entry.lastIndexOf("#") + 1));
- if (!tokenizer.hasMoreTokens()) {
- this.continu = false;
- return null;
- }
-
- current_entry = tokenizer.nextToken();
- } else {
- if (level <= current_level) {
- return current_entry;
- }
-
- if (count == 0) {
- System.out.println("heirarchy file format error");
- return null;
- }
-
- tree_entry prev_entry = (tree_entry)branch.elementAt(count - 1);
- prev_entry.children = new Vector();
- current_entry = this.build_web_structures(prev_entry.children, current_level + 1, current_entry, tokenizer);
- }
- }
-
- return null;
- }
-
- web_structure_builder(StringTokenizer tokenizer) {
- this.build_web_structures(this.web_tree, 0, tokenizer.nextToken(), tokenizer);
- }
-
- void restrict_tree(Vector branch, int max_level) {
- for(int i = 0; i < branch.size(); ++i) {
- tree_entry entry = (tree_entry)branch.elementAt(i);
- if (entry.level > max_level) {
- entry.in_use = false;
- } else {
- entry.in_use = true;
- this.web_list.addElement((Object)entry);
- }
-
- if (entry.children != null) {
- this.restrict_tree(entry.children, max_level);
- }
- }
-
- }
-
- void record_max_level(int current_level) {
- if (current_level > this.max_level) {
- this.max_level = current_level;
- }
-
- }
-
- void add_entry(Vector branch, int level, String entry) {
- boolean publish = true;
- if (entry.startsWith("|")) {
- publish = false;
- entry = entry.substring(1);
- }
-
- StringTokenizer st = new StringTokenizer(entry, "%");
- if (st.countTokens() > 1) {
- String name = st.nextToken();
- String url = st.nextToken();
- tree_entry new_entry = new tree_entry(name, url, level, publish);
- branch.addElement(new_entry);
- this.web_list.addElement(new_entry);
- } else {
- System.out.println("incorrect format");
- }
- }
-
- int find_level(String entry) {
- int level = 0;
- String temp_level_string = "#";
-
- for(int i = 0; i < entry.length(); ++i) {
- if (!entry.startsWith(temp_level_string)) {
- return level;
- }
-
- temp_level_string = temp_level_string + "#";
- ++level;
- }
-
- return 0;
- }
- }
-