⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexbuilder.java

📁 JAVA在线商店带JSP的
💻 JAVA
字号:
package index;import java.util.*;import java.io.*;import DebugLog;public class IndexBuilder{	public static DebugLog log;		static	{	    log = DebugLog.getSharedLog();	}		/**	 * Method to index a directory	 */	protected static void buildIndexForDir(File dir										,Vector files										,Hashtable entries)	{		String[] list;		File curFile;		int i,max;				list = dir.list();				log.log("Indexing Directory: "+dir);				max = list.length;				for(i=0;i<max;i++)		{			curFile = new File(dir,list[i]);						if(curFile.isDirectory())			{				buildIndexForDir(curFile,files,entries);			}			else if(list[i].endsWith(".html")					||list[i].endsWith(".htm"))			{				log.log("Indexing file: "+curFile);								buildIndexForFile(curFile,files,entries);			}		}	}		/**	 * Private method to index a file	 */	protected static void buildIndexForFile(File file										,Vector files										,Hashtable entries)	{		FileReader fileIn;		BufferedReader bufIn;		int cur;		int fileInd;		boolean inTag;		StringBuffer curWord;		String curString;		Vector entryForWord;		IndexEntry newEntry;				try		{			files.addElement(file);					fileInd = files.indexOf(file);						fileIn = new FileReader(file);			bufIn = new BufferedReader(fileIn);						inTag = false;					curWord = new StringBuffer();						while((cur = bufIn.read()) != -1)			{				if(cur == '<')				{					inTag = true;				}				else if(cur == '>')				{					inTag = false;				}				else if((Character.isWhitespace((char)cur)							||(cur == '.')							||(cur == ',')							||(cur == '[')							||(cur == ']')							||(cur == '{')							||(cur == '}')							||(cur == '(')							||(cur == ')')							||(cur == '=')							||(cur == ';')							||(cur == ':')							||(cur == '=')							||(cur == '-')							||(cur == '/')							||(cur == '\\'))							&& !inTag)				{					if(curWord.length()>0)					{						curString = curWord.toString().toLowerCase();						curWord.setLength(0);												if(!SkipTable.skip(curString))						{    						entryForWord = (Vector) entries.get(curString);						    						if(entryForWord == null)    						{    							entryForWord = new Vector();							    							entries.put(curString,entryForWord);    						}						    						newEntry = new IndexEntry();    						newEntry.file = fileInd;    						newEntry.occurences = 1;					    						if(!entryForWord.contains(newEntry))    						{    							entryForWord.addElement(newEntry);    						}    						else    						{    						    int oldInd =    						        entryForWord.indexOf(newEntry);    						    IndexEntry oldEnt = (IndexEntry)    						        entryForWord.elementAt(oldInd);    						        						    oldEnt.occurences++;    						}					    }					}				}				else if(!inTag)				{					//Don't add the separator					//or quotes					if(cur == '|')					{						curWord.append('-');					}					else if((cur != '\"')							&&(cur != '\''))					{						curWord.append((char)cur);					}				}			}						if(curWord.length()>0)			{				curString = curWord.toString().toLowerCase();				curWord.setLength(0);								if(!SkipTable.skip(curString))				{    				entryForWord = (Vector) entries.get(curString);				    				if(entryForWord == null)    				{    					entryForWord = new Vector();					    					entries.put(curString,entryForWord);    				}				    				newEntry = new IndexEntry();    				newEntry.file = fileInd;    				newEntry.occurences = 1;		    				if(!entryForWord.contains(newEntry))    				{    					entryForWord.addElement(newEntry);    				}    				else    				{    				    int oldInd =    				        entryForWord.indexOf(newEntry);    				    IndexEntry oldEnt = (IndexEntry)    				        entryForWord.elementAt(oldInd);				        				    oldEnt.occurences++;    				}				}			}					bufIn.close();			fileIn.close();		}		catch(Exception exp)		{			//skip this file		}	}		/**	 * Private method write the index file	 */	protected static void writeIndex(File dir										,Vector files										,Hashtable entries)	{		FileWriter fileOut;		BufferedWriter bufOut;		PrintWriter printOut;		File curFile;		int i,max;		Enumeration words;		String curWord;		Vector entryForWord;		IndexEntry curEntry;		File file;		String dirPath,fullPath,relPath;		int len;				try		{			file = new File(dir,HTMLIndex.INDEX_FILE_NAME);			fileOut = new FileWriter(file);			bufOut = new BufferedWriter(fileOut);			printOut = new PrintWriter(bufOut);				    dirPath = dir.getAbsolutePath();		    len = dirPath.length();		    		    if(File.separatorChar != dirPath.charAt(len-1)) len++;		    			//Print files in order			//Use relative paths incase dir moves						max = files.size();						for(i=0;i<max;i++)			{				curFile = (File) files.elementAt(i);				fullPath = curFile.getAbsolutePath();								//remove dir, and separator				relPath=fullPath.substring(len);				    				printOut.println(relPath);			}						//blank line to break files from words						printOut.println();						//print words						words = entries.keys();						while(words.hasMoreElements())			{				curWord = (String) words.nextElement();								entryForWord = (Vector) entries.get(curWord);								max = entryForWord.size();				printOut.print(curWord);								for(i=0;i<max;i++)				{					printOut.print('|');					printOut.print(entryForWord.elementAt(i));				}				//add new line char				printOut.println();			}						printOut.close();			bufOut.close();			fileOut.close();		}		catch(Exception exp)		{			//failed to write index		}	}    protected static boolean checkRebuildForDir(File dir,long refTime)	{		String[] list;		File curFile;		int i,max;		boolean retVal = false;				list = dir.list();				log.log("Checking Directory: "+dir);				max = list.length;				for(i=0;i<max;i++)		{			curFile = new File(dir,list[i]);						if(curFile.isDirectory())			{				retVal = checkRebuildForDir(curFile,refTime);			}			else if(list[i].endsWith(".html")					||list[i].endsWith(".htm"))			{				log.log("Checking file: "+curFile);								if(curFile.lastModified()>refTime)				{				    retVal = true;				}			}						if(retVal == true) break;		}				return retVal;	}		/**	 * Builds an index file for a directory	 */	public static void buildIndex(File dir)	{        if((dir == null)||!dir.isDirectory()) return;    		Vector files = new Vector();		Hashtable entries = new Hashtable();				try		{		    buildIndexForDir(dir,files,entries);			    	log.log("Writing Index");		    writeIndex(dir,files,entries);		}		catch(Exception exp)		{		    if(log != null) log.log(exp);		}	}		public static boolean indexNeedsRebuilding(File dir)	{	    File file;	    long refTime;	    boolean retVal = true;	    	    try	    {		    file = new File(dir,HTMLIndex.INDEX_FILE_NAME);		    refTime = file.lastModified();		    		    retVal = checkRebuildForDir(dir,refTime);	    }	    catch(Exception exp)	    {	        retVal = true;	    }	    	    return retVal;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -