/*
JSiteMap 0.9.3 - 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.*;
{
private String startTag
="<A name=sitemap>", hrefTag
="<A href=\"",
hrefEndTag="</A>", startListTag="<UL>", endListTag="</UL>",
listItemTag="<LI>", endListItemTag="</LI>",
htmlCode="", bracket="\"", endURLTag="\">",
STOPTag="<A href=\"#top\">";
int hrefLen = hrefTag.length(), endURLTagLen=endURLTag.length(),
hrefEndLen=hrefEndTag.length(), start=0, htmlLen, level=0,
stop;
}
this.startTag = startTag;
this.hrefTag = hrefTag;
this.hrefEndTag = hrefEndTag;
this.startListTag = startListTag;
this.endListTag = endListTag;
this.listItemTag = listItemTag;
this.endListItemTag = endListItemTag;
this.endURLTag = endURLTag;
this.STOPTag = STOPTag;
}
public int findStartPoint
(String htmlCode
){
start=htmlCode.indexOf(startTag);
if(start==-1){
//System.out.println("to lower");
startTag=startTag.toLowerCase();
start=htmlCode.indexOf(startTag);
if(start==-1)
return -1;
hrefTag=hrefTag.toLowerCase();
hrefEndTag=hrefEndTag.toLowerCase();
startListTag=startListTag.toLowerCase();
endListTag=endListTag.toLowerCase();
listItemTag=listItemTag.toLowerCase();
endListItemTag=endListItemTag.toLowerCase();
STOPTag=STOPTag.toLowerCase();
}
return start;
}
this.htmlCode = htmlCode;
start=findStartPoint(htmlCode);
if(start==-1)
return null;
stop=htmlCode.indexOf(STOPTag, start);
stop=htmlCode.lastIndexOf(endListTag, stop);
if(stop==-1)
return null;
String htmlMap
="<html>\n<body>\n"+htmlCode.
substring(start,stop
)+
"\n</body>\n</html>";
return htmlMap;
}
public TeutoTreeNode parse
(String htmlCode
){
this.htmlCode = htmlCode;
htmlLen=htmlCode.length();
start=findStartPoint(htmlCode);
if(start==-1)
return null;
stop=htmlCode.indexOf(STOPTag, start);
stop=htmlCode.lastIndexOf(endListTag, stop);
if(stop==-1)
return null;
//System.out.println("start="+start+"stop="+stop);
TeutoTreeNode root=new TeutoTreeNode("root");
start=findHref(root, start);
if(iter!=null){
TeutoTreeNode homePage=(TeutoTreeNode)iter.nextElement();
exploreTree(homePage);
//System.out.println(homePage);
return homePage;} else
return null;
}
s=s.replace('"','\'');
s=s.replace('\\','/');
return s;
}
public int findHref(TeutoTreeNode parent, int start){
int nextH = htmlCode.indexOf(hrefTag, start);
if(nextH==-1) return -1;
int endURL = htmlCode.indexOf(endURLTag, nextH);
int hrefEnd = htmlCode.indexOf(hrefEndTag, nextH);
String URL = htmlCode.
substring(nextH
+hrefLen, endURL
);
String name
= htmlCode.
substring(endURL
+endURLTagLen, hrefEnd
);
name=checkString(name);
//System.out.println("name="+name+" URL="+URL);
initNodeList
(parent, name,
URL);
return hrefEnd+hrefEndLen;
}
public TeutoTreeNode findHrefList(TeutoTreeNode parent, int s){
int nextH = htmlCode.indexOf(hrefTag, s);
if(nextH==-1) {
start=stop+1;
return null;
}
int endURL = htmlCode.indexOf(endURLTag, nextH);
int hrefEnd = htmlCode.indexOf(hrefEndTag, nextH);
String URL = htmlCode.
substring(nextH
+hrefLen, endURL
);
String name
= htmlCode.
substring(endURL
+endURLTagLen, hrefEnd
);
name=checkString(name);
//System.out.println("name="+name+" URL="+URL);
TeutoTreeNode nodeList
=initNodeList
(parent, name,
URL);
start=hrefEnd+hrefEndLen;
return nodeList;
}
public TeutoTreeNode initNodeList
(TeutoTreeNode parent,
String name,
TeutoTreeNode child=new TeutoTreeNode(name, parent);
child.
addAttribute("URL",
URL);
return child;
}
public void exploreTree(TeutoTreeNode parent){
level++;
if(level>999){
start=htmlLen*2;
return;
}
//System.out.println("entro a: "+start+" level: "+level);
boolean ended=false;
TeutoTreeNode current=parent;
int startList, subList;
startList=htmlCode.indexOf(listItemTag, start);
if(start>stop || start<0)
return;
if(startList>0){
while(!ended){
if(start>stop)
ended=true;
else{
int nextEndL = htmlCode.indexOf(endListTag, start);
int nextH = htmlCode.indexOf(hrefTag, start);
int nextL = htmlCode.indexOf(startListTag, start);
if(nextL==-1)
/* System.out.println("\nstart="+start+" EndL="+nextEndL+
" H="+nextH+" L="+nextL);*/
if(nextEndL<nextH && nextEndL<nextL) {
start=nextEndL+1;
//System.out.println("ret");
return;
}
if(nextH
<nextL
|| nextL
==Integer.
MAX_VALUE){
if(nextH>stop)
return;
//System.out.println("--href");
current=findHrefList(parent, start);
if(current==null)
ended=true;
} else {
//System.out.println("--list");
//System.out.println("ok List");
start=nextL+1;
exploreTree(current);
level--;
}
}
}
}// else //System.out.println("error list");
}
}