/*
JSiteMap 0.9.0 - A Java Site Map Generator
Copyright (C) 2003 Pierre Blanc
Pierre Blanc: blanc_teutoburgo@yahoo.it
http://www.site-map.tk
http://it.geocities.com/teutoburgo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or go to http://www.gnu.org/copyleft/gpl.html
*/
package tk.teutoburgo.jsitemap;
import java.util.*;
public class ScriptGenerator
{
private TeutoTreeNode root;
private int nodeNumber=-1;
public ScriptGenerator(){
}
public ScriptGenerator(TeutoTreeNode root){
this.root=root;
}
public void resetNodeNumber(){
nodeNumber=-1;
}
public String generate
(TeutoTreeNode node
){
String code
=recGenerate
(node, 0
);
code+="menu.addItem(\"Generated by JSiteMap\","+
"\"http://it.geocities.com/teutoburgo/java/jsitemap.html\");";
return code;
}
public String recGenerate
(TeutoTreeNode node,
int parentIndex
){
nodeNumber++;
//System.out.println(nodeNumber);
String script
="", name
=node.
getName(),
URL=node.
findAttribute("URL"),
nodeId="node"+nodeNumber;
if(nodeNumber==0)
nodeId="menu";
script+="var "+nodeId+" = null;\n";
script+=nodeId+" = new MTMenu();\n";
int children=0, thisIndex=nodeNumber;
if(iter!=null)
while(iter.hasMoreElements()){
children++;
TeutoTreeNode child=(TeutoTreeNode)iter.nextElement();
/*System.out.println("figlio "+child.getName()+" num="+
nodeNumber+" pare "+parentIndex);*/
name
=child.
getName(); URL=child.
findAttribute("URL");
script
+=nodeId
+".addItem(\""+name
+"\", \""+URL+"\");\n";
if(childrenIter!=null){
//System.out.println("rec");
script+=recGenerate(child, thisIndex);
} else{
//System.out.println("no rec");
}
}
if(parentIndex==0){
if(!nodeId.equals("menu"))
script+="menu"+".makeLastSubmenu("+nodeId+");\n";
} else
script+="node"+(parentIndex)+".makeLastSubmenu("+nodeId+");\n";
return script;
}
}