Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JSiteMap - Java Site Map Generator - Parser.java

Parser.java

Caricato da: Teutoburgo
Scarica il programma completo

  1. /*
  2.     JSiteMap 0.9.3 - 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 Parser
  29. {
  30.  
  31.     private String startTag="<A name=sitemap>", hrefTag="<A href=\"",
  32.         hrefEndTag="</A>", startListTag="<UL>", endListTag="</UL>",
  33.         listItemTag="<LI>", endListItemTag="</LI>",
  34.         htmlCode="", bracket="\"", endURLTag="\">",
  35.         STOPTag="<A href=\"#top\">";
  36.     int hrefLen = hrefTag.length(), endURLTagLen=endURLTag.length(),
  37.         hrefEndLen=hrefEndTag.length(), start=0, htmlLen, level=0,
  38.         stop;
  39.  
  40.     public Parser(){
  41.  
  42.     }
  43.  
  44.     public Parser(String startTag, String hrefTag, String hrefEndTag,
  45.                   String startListTag, String endListTag, String listItemTag,
  46.                   String endListItemTag, String endURLTag, String STOPTag){
  47.  
  48.         this.startTag = startTag;
  49.         this.hrefTag = hrefTag;
  50.         this.hrefEndTag = hrefEndTag;
  51.         this.startListTag = startListTag;
  52.         this.endListTag = endListTag;
  53.         this.listItemTag = listItemTag;
  54.         this.endListItemTag = endListItemTag;
  55.         this.endURLTag = endURLTag;
  56.         this.STOPTag = STOPTag;
  57.     }
  58.  
  59.         public int findStartPoint(String htmlCode){
  60.         start=htmlCode.indexOf(startTag);
  61.         if(start==-1){
  62.             //System.out.println("to lower");
  63.             startTag=startTag.toLowerCase();
  64.             start=htmlCode.indexOf(startTag);
  65.             if(start==-1)
  66.                 return -1;
  67.             hrefTag=hrefTag.toLowerCase();
  68.             hrefEndTag=hrefEndTag.toLowerCase();
  69.             startListTag=startListTag.toLowerCase();
  70.             endListTag=endListTag.toLowerCase();
  71.             listItemTag=listItemTag.toLowerCase();
  72.             endListItemTag=endListItemTag.toLowerCase();
  73.             STOPTag=STOPTag.toLowerCase();
  74.         }
  75.         return start;
  76.         }
  77.  
  78.         public String getHTMLMap(String htmlCode){
  79.         this.htmlCode = htmlCode;
  80.         start=findStartPoint(htmlCode);
  81.         if(start==-1)
  82.         return null;
  83.         stop=htmlCode.indexOf(STOPTag, start);
  84.         stop=htmlCode.lastIndexOf(endListTag, stop);
  85.         if(stop==-1)
  86.             return null;
  87.         String htmlMap="<html>\n<body>\n"+htmlCode.substring(start,stop)+
  88.                 "\n</body>\n</html>";
  89.         return htmlMap;
  90.         }
  91.  
  92.     public TeutoTreeNode parse(String htmlCode){
  93.         this.htmlCode = htmlCode;
  94.         htmlLen=htmlCode.length();
  95.         start=findStartPoint(htmlCode);
  96.         if(start==-1)
  97.         return null;
  98.         stop=htmlCode.indexOf(STOPTag, start);
  99.         stop=htmlCode.lastIndexOf(endListTag, stop);
  100.         if(stop==-1)
  101.             return null;
  102.         //System.out.println("start="+start+"stop="+stop);
  103.         TeutoTreeNode root=new TeutoTreeNode("root");
  104.         start=findHref(root, start);
  105.         Enumeration iter=root.findChildren();
  106.         if(iter!=null){
  107.             TeutoTreeNode homePage=(TeutoTreeNode)iter.nextElement();
  108.             exploreTree(homePage);
  109.             //System.out.println(homePage);
  110.             return homePage;} else
  111.                 return null;
  112.     }
  113.  
  114.     public String checkString(String s){
  115.  
  116.         s=s.replace('"','\'');
  117.         s=s.replace('\\','/');
  118.  
  119.         return s;
  120.     }
  121.  
  122.     public int findHref(TeutoTreeNode parent, int start){
  123.         int nextH = htmlCode.indexOf(hrefTag, start);
  124.         if(nextH==-1) return -1;
  125.         int endURL = htmlCode.indexOf(endURLTag, nextH);
  126.         int hrefEnd = htmlCode.indexOf(hrefEndTag, nextH);
  127.         String URL = htmlCode.substring(nextH+hrefLen, endURL);
  128.         String name = htmlCode.substring(endURL+endURLTagLen, hrefEnd);
  129.         URL=checkString(URL);
  130.         name=checkString(name);
  131.         //System.out.println("name="+name+" URL="+URL);
  132.         initNodeList(parent, name, URL);
  133.         return hrefEnd+hrefEndLen;
  134.     }
  135.  
  136.     public TeutoTreeNode findHrefList(TeutoTreeNode parent, int s){
  137.         int nextH = htmlCode.indexOf(hrefTag, s);
  138.         if(nextH==-1) {
  139.             start=stop+1;
  140.             return null;
  141.         }
  142.         int endURL = htmlCode.indexOf(endURLTag, nextH);
  143.         int hrefEnd = htmlCode.indexOf(hrefEndTag, nextH);
  144.         String URL = htmlCode.substring(nextH+hrefLen, endURL);
  145.         String name = htmlCode.substring(endURL+endURLTagLen, hrefEnd);
  146.         URL=checkString(URL);
  147.         name=checkString(name);
  148.         //System.out.println("name="+name+" URL="+URL);
  149.         TeutoTreeNode nodeList=initNodeList(parent, name, URL);
  150.         start=hrefEnd+hrefEndLen;
  151.         return nodeList;
  152.     }
  153.  
  154.     public TeutoTreeNode initNodeList (TeutoTreeNode parent, String name,
  155.         String URL){
  156.         TeutoTreeNode child=new TeutoTreeNode(name, parent);
  157.         child.addAttribute("URL", URL);
  158.         return child;
  159.     }
  160.  
  161.     public void exploreTree(TeutoTreeNode parent){
  162.         level++;
  163.         if(level>999){
  164.             start=htmlLen*2;
  165.             return;
  166.         }
  167.         //System.out.println("entro a: "+start+" level: "+level);
  168.         boolean ended=false;
  169.         TeutoTreeNode current=parent;
  170.         int startList, subList;
  171.         startList=htmlCode.indexOf(listItemTag, start);
  172.         if(start>stop || start<0)
  173.             return;
  174.         if(startList>0){
  175.             while(!ended){
  176.                 if(start>stop)
  177.                     ended=true;
  178.                 else{
  179.                     int nextEndL = htmlCode.indexOf(endListTag, start);
  180.                     int nextH = htmlCode.indexOf(hrefTag, start);
  181.                     int nextL = htmlCode.indexOf(startListTag, start);
  182.                     if(nextL==-1)
  183.                       nextL=Integer.MAX_VALUE;
  184.  
  185.                     /*  System.out.println("\nstart="+start+" EndL="+nextEndL+
  186.                         " H="+nextH+" L="+nextL);*/
  187.                     if(nextEndL<nextH && nextEndL<nextL) {
  188.                         start=nextEndL+1;
  189.                         //System.out.println("ret");
  190.                         return;
  191.                     }
  192.                     if(nextH<nextL || nextL==Integer.MAX_VALUE){
  193.                         if(nextH>stop)
  194.                             return;
  195.                         //System.out.println("--href");
  196.                         current=findHrefList(parent, start);
  197.                         if(current==null)
  198.                             ended=true;
  199.                     } else {
  200.                         //System.out.println("--list");
  201.                         //System.out.println("ok List");
  202.                         start=nextL+1;
  203.                         exploreTree(current);
  204.                         level--;
  205.                     }
  206.                 }
  207.             }
  208.         }// else //System.out.println("error list");
  209.     }
  210.  
  211. }