Questo sito utilizza cookies, anche di terze parti, per mostrare pubblicità e servizi in linea con il tuo account. Leggi l'informativa sui cookies.
Username: Password: oppure
JSiteMap - Java Site Map Generator - ScriptGenerator.java

ScriptGenerator.java

Caricato da: Teutoburgo
Scarica il programma completo

  1. /*
  2.     JSiteMap 0.9.0 - A Java Site Map Generator
  3.     Copyright (C) 2003 Pierre Blanc
  4.     Pierre Blanc: blanc_teutoburgo@yahoo.it
  5.     http://www.site-map.tk
  6.     http://it.geocities.com/teutoburgo
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 2 of the License, or
  11.     (at your option) any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.     or go to      http://www.gnu.org/copyleft/gpl.html
  22. */
  23.  
  24. package tk.teutoburgo.jsitemap;
  25.  
  26. import java.util.*;
  27.  
  28. public class ScriptGenerator
  29. {
  30.     private TeutoTreeNode root;
  31.     private int nodeNumber=-1;
  32.  
  33.     public ScriptGenerator(){
  34.  
  35.     }
  36.  
  37.     public ScriptGenerator(TeutoTreeNode root){
  38.         this.root=root;
  39.     }
  40.  
  41.     public void resetNodeNumber(){
  42.         nodeNumber=-1;
  43.     }
  44.  
  45.     public String generate(TeutoTreeNode node){
  46.         String code=recGenerate(node, 0);
  47.         code+="menu.addItem(\"Generated by JSiteMap\","+
  48.             "\"http://it.geocities.com/teutoburgo/java/jsitemap.html\");";
  49.         return code;
  50.     }
  51.  
  52.     public String recGenerate(TeutoTreeNode node, int parentIndex){
  53.         nodeNumber++;
  54.         //System.out.println(nodeNumber);
  55.         String script="", name=node.getName(), URL=node.findAttribute("URL"),
  56.             nodeId="node"+nodeNumber;
  57.         if(nodeNumber==0)
  58.             nodeId="menu";
  59.         script+="var "+nodeId+" = null;\n";
  60.         script+=nodeId+" = new MTMenu();\n";
  61.         Enumeration iter = node.findChildren();
  62.         int children=0, thisIndex=nodeNumber;
  63.         if(iter!=null)
  64.             while(iter.hasMoreElements()){
  65.                 children++;
  66.                 TeutoTreeNode child=(TeutoTreeNode)iter.nextElement();
  67.                 /*System.out.println("figlio "+child.getName()+" num="+
  68.                   nodeNumber+" pare "+parentIndex);*/
  69.                 name=child.getName(); URL=child.findAttribute("URL");
  70.                 script+=nodeId+".addItem(\""+name+"\", \""+URL+"\");\n";
  71.                 Enumeration childrenIter=child.findChildren();
  72.                 if(childrenIter!=null){
  73.                     //System.out.println("rec");
  74.                     script+=recGenerate(child, thisIndex);
  75.                 } else{
  76.                     //System.out.println("no rec");
  77.                 }
  78.             }
  79.  
  80.         if(parentIndex==0){
  81.             if(!nodeId.equals("menu"))
  82.                 script+="menu"+".makeLastSubmenu("+nodeId+");\n";
  83.         } else
  84.             script+="node"+(parentIndex)+".makeLastSubmenu("+nodeId+");\n";
  85.         return script;
  86.     }
  87. }