%@ page language="java" import="java.util.*,java.text.*,java.io.*" %>
<%@page import="org.apache.xerces.parsers.*" %>
<%@page import="org.w3c.dom.*" %>
<%@page import="org.xml.sax.SAXException" %>
<%--
////////////////////////////////////////////////////////////////////////////
//
// Revize Search Engine (jsp version)
//
// All source code is the intellectual property of Idetix Incorporated
// and cannot be copied, distributed, or modified in any manner
// without the expressed written permission of Idetix Incorporated
// or its successors.
//
// Copyright (C) 2000-2002 Idetix Software Systems
// All rights reserved.
//
// $Archive: Q:/PDG Current/archives/Current/tang/www/revize/util/enable_search_results.jsp-arc $
// $Revision: 1.2 $
// $Date: 06 Feb 2003 11:30:00 $
// $Author: daveotto $
//
////////////////////////////////////////////////////////////////////////////
--%>
<%!
final static int DETAIL = 2;
final static int CLEAN = 1;
final static int NO = 0;
int debug = NO; // change this debug flag value to have different debugging levels
// the StringTokenizer delimiter
String delimiter = " \t\n\r\f~`!@#$%^&*()_-+={}[]|\\:;\"',.?>";
String str_error_detail = "";
/**
* Append global error detail message with additional detail
* @param msg Error details to be appended
*/
void setError(String msg)
{
if ( !str_error_detail.equals("") )
str_error_detail += " ";
str_error_detail += msg;
}
/**
* get the index within the String s of the first occurrence of the specified String target.
* either ignore case or not
* @param s the String to look into
* @param target the substring to search for
* @param ignoreCase the boolean true if ignore case, false otherwise
* @return int the index of the first occurrence of the specified target string
*/
int indexOf(String s, String target, boolean ignoreCase) {
return indexOf(s, target, 0, ignoreCase);
}
/**
* get the index within the String s of the first occurrence of the specified String target,
* starting from the specified start index.
* either ignore case or not
* @param s the String to look into
* @param target the substring to search for
* @param start the start index
* @param ignoreCase the boolean true if ignore case, false otherwise
* @return int the index of the first occurrence of the specified target string
*/
int indexOf(String s, String target, int start, boolean ignoreCase) {
int index = -1;
if(ignoreCase) {
String sUpperCase = s.toUpperCase();
String tUpperCase = target.toUpperCase();
index = sUpperCase.indexOf(tUpperCase, start);
} else {
index = s.indexOf(target, start);
}
return index;
}
/**
* find if the exact key exists in a String. This will exclude partial word matching.
* @param line a String
* @param key the key to look for
* @return boolean true if found, false otherwise
*/
boolean findExact(String line, String key) {
boolean found = false;
StringTokenizer words = new StringTokenizer(line, delimiter);
while(words.hasMoreTokens()) {
String word = words.nextToken();
if(word.equalsIgnoreCase(key)) {
found = true;
break;
}
}
return found;
}
/**
* find if the key exists in a String. This includes partial word matching.
* @param line a String
* @param key the key to look for
* @return boolean true if found, false otherwise
*/
boolean find(String line, String key) {
boolean found = false;
if(indexOf(line, key, true) != -1)
found = true;
return found;
}
/**
* initialize a hashmap {key, isFound}, setting all isFound to false
* @param found the HashMap of {key, isFound}
* @param keys the Vector of keys
*
*/
void initHashMap(HashMap found, Vector keys) {
for(Iterator i = keys.iterator(); i.hasNext();) {
String key = i.next().toString();
found.put(key, new Boolean(false));
}
}
/**
* find if all the keys exist in the file
* @param fileName a file name
* @param keys a Vector of keys
* @return HashMap a HashMap of {keyword, isFound}
* @exception Exception thrown if IO access of file fails
*/
HashMap fileANDFind(String fileName, Vector keys) throws Exception {
HashMap found = new HashMap();
initHashMap(found, keys);
BufferedReader file = new BufferedReader(new FileReader(fileName));
String line = null;
try {
while((line = file.readLine())!= null) {
for(Iterator i = keys.iterator(); i.hasNext();) {
String key = i.next().toString();
boolean foundKey = find(line, key);
if(foundKey == true)
found.put(key, new Boolean(foundKey));
}
if(!found.containsValue(new Boolean(false)))
break;
}
} catch (IOException ioe) {
throw new Exception("Exception: " + ioe.getMessage());
} finally {
file.close();
}
return found;
}
/**
* find if all the keys exist in the file
* @param fileName a file name
* @param keys a Vector of keys
* @return HashMap a HashMap of {keyword, isFound}
* @exception Exception thrown if IO access of file fails
*/
HashMap fileANDFindFast(String fileName, Vector keys) throws Exception {
HashMap found = new HashMap();
initHashMap(found, keys);
StringBuffer entireHead = new StringBuffer();
BufferedReader file = new BufferedReader(new FileReader(fileName));
try {
String line = null;
boolean startHead = false;
boolean endHead = false;
file.mark(100);
while(!endHead && (line = file.readLine())!= null ) {
int start = 0;
int end = line.length();
String lineUpperCase = line.toUpperCase();
int headIndex = lineUpperCase.indexOf("
");
if( headIndex != -1 ) {
start = headIndex + 6;
startHead = true;
}
int endHeadIndex = lineUpperCase.indexOf("");
if(endHeadIndex != -1) {
end = endHeadIndex;
endHead = true;
}
if(startHead)
entireHead.append(lineUpperCase.substring(start, end));
}
file.reset();
while((line = file.readLine())!= null) {
for(Iterator i = keys.iterator(); i.hasNext();) {
String key = i.next().toString();
boolean foundKey = find(line, key);
if(foundKey == true)
found.put(key, new Boolean(foundKey));
}
if(!found.containsValue(new Boolean(false)))
break;
}
} catch (IOException ioe) {
throw new Exception("IOException: " + ioe.getMessage());
} finally {
file.close();
}
String head = entireHead.toString();
String title = null;
String desc = null;
if(head != null && head.length() > 0) {
title = getTitle(head);
HashMap metas = getMETA(head);
desc = (String)metas.get("DESCRIPTION");
}
HashMap finalResult = new HashMap();
finalResult.put("TITLE", title);
finalResult.put("DESCRIPTION", desc);
finalResult.put("PERCENTAGE", new Integer(getPercentageInANDFind(found)));
return finalResult;
}
/**
* find if any of the keys exists in the file
* @param fileName a file name
* @param keys a Vector of keys
* @return boolean true if any of the key is found in the file
* @exception Exception thrown if IO access of file fails
*/
boolean fileORFind(String fileName, Vector keys) throws Exception {
boolean found = false;
BufferedReader file = new BufferedReader(new FileReader(fileName));
try {
String line = null;
while(!found && (line = file.readLine())!= null) {
for(Iterator i = keys.iterator(); i.hasNext();) {
String key = i.next().toString();
found = find(line, key);
if(found == true)
break;
}
}
} catch (IOException ioe) {
throw new Exception("IOException: " + ioe.getMessage());
} finally {
file.close();
}
return found;
}
/**
* find if one key exists in the file
* @param fileName a file name
* @param key the key
* @return boolean true if the key is found in the file
* @exception Exception thrown if IO access of file fails
*/
boolean fileFind(String fileName, String key) throws Exception {
boolean found = false;
BufferedReader file = new BufferedReader(new FileReader(fileName));
try {
String line = null;
while((line = file.readLine())!= null) {
found = find(line, key);
if(found == true)
break;
}
} catch (IOException ioe) {
throw new Exception("IOException: " + ioe.getMessage());
} finally {
file.close();
}
return found;
}
/**
* count the number of times the key appears in the string
* @param line the string
* @param key the key
* @return int the number of times the key appears in the string
*/
int count(String line, String key) {
int count = 0;
StringTokenizer words = new StringTokenizer(line, delimiter);
while(words.hasMoreTokens()) {
String word = words.nextToken();
if(word.equalsIgnoreCase(key))
count++;
}
return count;
}
/**
* count the number of times the key appears in the file
* @param fileName the file name
* @param key the key
* @return int the number of times the key appears in the file
* @exception Exception thrown if IO access fails
*/
int fileCount(String fileName, String key) throws Exception {
int count = 0;
BufferedReader file = new BufferedReader(new FileReader(fileName));
try {
String line = null;
while((line = file.readLine())!= null) {
count += count(line, key);
}
} catch (IOException ioe) {
throw new Exception("IOException: " + ioe.getMessage());
} finally {
file.close();
}
return count;
}
/**
* get the head part of the file
* @param fileName the file name
* @return String the head as a string
*/
String getHead(String fileName) throws Exception {
StringBuffer entireHead = new StringBuffer();
BufferedReader file = new BufferedReader(new FileReader(fileName));
try {
String line = null;
boolean startHead = false;
boolean endHead = false;
while(!endHead && (line = file.readLine())!= null ) {
int start = 0;
int end = line.length();
int headIndex = indexOf(line, "", true);
if( headIndex != -1 ) {
start = headIndex + 6;
startHead = true;
}
int endHeadIndex = indexOf(line, "", true);
if(endHeadIndex != -1) {
end = endHeadIndex;
endHead = true;
}
if(startHead)
entireHead.append(line.substring(start, end));
}
if(debug >= DETAIL) System.out.println("head is:" + entireHead);
} catch (IOException ioe) {
throw new Exception("IOException: " + ioe.getMessage());
} finally {
file.close();
}
return entireHead.toString();
}
/**
* get the title in the head
* @param head the head as a string
* @return String the title
*/
String getTitle(String head) {
String title = "";
int start = indexOf(head, "", true);
int end = indexOf(head, "", true);
try
{
title = head.substring(start+7, end);
}
catch (IndexOutOfBoundsException ie)
{
if(debug >= DETAIL) System.out.println("head without title.");
}
if(debug >= DETAIL) System.out.println("Title: " + title);
return title;
}
/**
* get the META tag content in the head
* @param head the head
* @return HashMap a hashmap of meta name tags {tag, content}
*/
HashMap getMETA(String head) {
HashMap metas = new HashMap();
String left = head;
do
{
int start = indexOf(left, "", start, true);
if(start == -1 || end == -1)
break; // got all metas
String meta = left.substring(start+5, end);
if(indexOf(meta, "NAME=", true) != -1 && indexOf(meta, "CONTENT=", true) != -1) {
StringTokenizer st = new StringTokenizer(meta, "=\"");
String name = null;
String content = null;
if(st.hasMoreTokens()) {
String word = st.nextToken().trim();
if(word.equalsIgnoreCase("NAME")) {
if(st.hasMoreTokens()) {
name = st.nextToken().trim();
}
}
if(st.hasMoreTokens()) {
String word2 = st.nextToken().trim();
if(word2.equalsIgnoreCase("CONTENT")) {
if(st.hasMoreTokens()) {
content = st.nextToken().trim();
}
}
}
}
if(name != null && content != null)
metas.put(name.toUpperCase(), content);
}
left = left.substring(end);
} while (left.length()>0);
if(debug >= DETAIL) System.out.println("META HashMap: " + metas);
return metas;
}
/**
* find if the keys exist in the specified meta content of the head
* @param head the head
* @param metaName the meta tag name
* @param keys the Vector of keys to search
* @return HashMap a HashMap of {key, isFound}
*/
HashMap metaFind(String head, String metaName, Vector keys) {
HashMap found = new HashMap();
initHashMap(found, keys);
HashMap metas = getMETA(head);
String metaContent = (String)metas.get(metaName.toUpperCase()); // get the content of the meta tag
if(metaContent != null) {
for(Iterator i = keys.iterator(); i.hasNext();) {
String key = i.next().toString();
boolean foundKey = find(metaContent, key);
if(foundKey == true)
found.put(key, new Boolean(true));
}
}
return found;
}
/**
* find if all the words exists in the "KEYWORDS" META tag
* @param fileName the file name
* @param subscope the subscope to search within the head, e.g., "title", or "keywords" meta tag.
* @param keys a Vector of keys to look for
* @return HashMap a HashMap of {"TITLE"-value, "DESCRIPTION"-value, "PERCENTAGE"-value}
*/
HashMap headANDFind(String fileName, String subscope, Vector keys) throws Exception
{
String head = getHead(fileName);
HashMap result = null;
if(head != null && head.length() > 0) {
HashMap keysFound = null;
String title = getTitle(head);
if(subscope.equals("TITLE")) { // search in HTML title section
HashMap found = new HashMap();
initHashMap(found, keys);
for(Iterator i = keys.iterator(); i.hasNext();) {
String key = i.next().toString();
boolean foundKey = find(title, key);
if(foundKey == true)
found.put(key, new Boolean(true));
}
keysFound = found;
} else { // search in HTML meta tags
keysFound = metaFind(head, subscope, keys);
}
Integer percentage = new Integer(getPercentageInANDFind(keysFound));
result = new HashMap();
HashMap metas = getMETA(head);
String desc = (String)metas.get("DESCRIPTION");
result.put("TITLE", title);
result.put("DESCRIPTION", desc);
result.put("PERCENTAGE", percentage); // percentage could be 0 - means did not find any
}
return result;
}
/**
* return the matching percentage of the file for the specified set of key words.
* the percentage is calculated as this:
* if looking for four words a, b, c and d, a and b are found in the file, then
* the matching percentage is 50%, as two out of four are found.
*
* @param result a HashMap of {keyword, isFound}
* @return int the percentage
*/
int getPercentageInANDFind (HashMap result)
{
float count = 0;
for(Iterator i = result.values().iterator(); i.hasNext(); )
{
Boolean value = (Boolean)i.next();
if(value.booleanValue()== true)
count++;
}
int percentage = Math.round(100*count/result.size());
return percentage;
}
// construct the final result as a HashMap of {TITLE-value, DESCRIPTION-value, PERCENTAGE-value}
HashMap constructResult(String filePathName, Integer percentage) throws Exception
{
//get the title and description in the head, if the head exists
String head = getHead(filePathName);
String title = null;
String desc = null;
if(head != null && head.length() > 0)
{
title = getTitle(head);
HashMap metas = getMETA(head);
desc = (String)metas.get("DESCRIPTION");
}
HashMap finalResult = new HashMap();
finalResult.put("TITLE", title);
finalResult.put("DESCRIPTION", desc);
finalResult.put("PERCENTAGE", percentage);
return finalResult;
}
/**
* find all the files that have all the keys existing
*
* @param filePathName the file or the path name
* @param keys a Vector of keys
* @param includes a list of file names or types that are included in the search
* @param excludeFiles a list of file names or types that are excluded in the search
* @param isTop a boolean that is true if the directory is the top level one, false otherwise
* @param includeSubDir a boolean that is true if search includes subfolders, false otherwise
* @return HashMap a HashMap of {filename, a HashMap of [TITLE-value, DESCRIPTION-value, PERCENTAGE-value]}
*/
HashMap filePathANDFind(String filePathName, Vector keys, Vector includes, Vector excludeFiles, Vector excludeDirs, boolean isTop, boolean includeSubDir) throws Exception {
HashMap filesFound = new HashMap();
File file = new File(filePathName);
if(!file.exists()) {
if(debug >= CLEAN) System.out.println("!!!!!!!!!!!!!----------File not exist--------------!!!!!!!!!!!!!");
throw new Exception("File not exist");
}
if(file.isDirectory())
{
if(debug >= DETAIL) System.out.println("dir path >>>>>>>>>>>>" + filePathName);
if(excludeDirs == null || !isDirInList(filePathName, excludeDirs)) { // execute only if the dir is not excluded
if(includeSubDir || isTop) { // include sub dir
File[] subFiles = file.listFiles();
for(int i = 0; i < subFiles.length; i++) {
HashMap found = filePathANDFind(subFiles[i].getAbsolutePath(), keys, includes, excludeFiles, excludeDirs, false, includeSubDir);
filesFound.putAll(found);
}
} // else, either is not top dir, or not include sub dir, therefore no recursion
}
}
else if(file.isFile())
{
if(debug >= DETAIL) System.out.println("file path >>>>>>>>>>>>" + filePathName);
if((includes == null || isFileInList(filePathName, includes) == true) && (excludeFiles == null || isFileInList(filePathName, excludeFiles) == false)) {
HashMap result = fileANDFind(filePathName, keys);
Integer percentage = new Integer(getPercentageInANDFind(result));
if(percentage.intValue() > 0) {
HashMap finalResult = constructResult(filePathName, percentage);
filesFound.put(filePathName, finalResult);
}
/*
HashMap result = fileANDFindFast(filePathName, keys);
if(result != null && ((Integer)result.get("PERCENTAGE")).intValue() >0)
filesFound.put(filePathName, result);
*/
}
}
return filesFound;
}
/**
* find all the files that have any of the keys existing
* @param filePathName the file or the path name
* @param keys a Vector of keys
* @param includes a list of file names or types that are included in the search
* @param excludeFiles a list of file names or types that are excluded in the search
* @param isTop a boolean that is true if the directory is the top level one, false otherwise
* @param includeSubDir a boolean that is true if search includes subfolders, false otherwise
* @return HashMap a HashMap of {file names, a HashMap of [TITLE-value, DESCRIPTION-value, PERCENTAGE-value] }
*/
HashMap filePathORFind(String filePathName, Vector keys, Vector includes, Vector excludeFiles, Vector excludeDirs, boolean isTop, boolean includeSubDir) throws Exception {
HashMap filesFound = new HashMap();
File file = new File(filePathName);
if(!file.exists())
{
if(debug >= CLEAN) System.out.println("!!!!!!!!!!!!!----------File not exist--------------!!!!!!!!!!!!!");
throw new Exception("File not exist");
}
if(file.isDirectory())
{
if(debug >= DETAIL) System.out.println("dir path >>>>>>>>>>>>" + filePathName);
if(excludeDirs == null || !isDirInList(filePathName, excludeDirs)) // execute only if the dir is not excluded
{
if(includeSubDir || isTop) { // include sub dir or is top dir
File[] subFiles = file.listFiles();
for(int i = 0; i < subFiles.length; i++) {
HashMap found = filePathORFind(subFiles[i].getAbsolutePath(), keys, includes, excludeFiles, excludeDirs, false, includeSubDir);
filesFound.putAll(found);
}
}
}
}
else if(file.isFile())
{
if(debug >= DETAIL) System.out.println("file path >>>>>>>>>>>>" + filePathName);
if((includes == null || isFileInList(filePathName, includes) == true) && (excludeFiles == null || isFileInList(filePathName, excludeFiles) == false)) {
boolean result = fileORFind(filePathName, keys);
if(result == true) {
HashMap finalResult = constructResult(filePathName, new Integer(100));
filesFound.put(filePathName, finalResult);
}
}
}
return filesFound;
}
/**
* find all the files that have all the keys existing in the head
* @param filePathName a file or path name
* @param subscope the subscope to search within the head, e.g., title, or keywords meta tag
* @param keys a Vector of keys
* @param includes a list of file names or types that are included in the search
* @param excludeFiles a list of file names or types that are excluded in the search
* @param isTop a boolean that is true if the directory is the top level one, false otherwise
* @param includeSubDir a boolean that is true if search includes subfolders, false otherwise
* @return HashMap a HashMap of {filename, HashMap of [title-value, desc-value, percentage-value]}
*/
HashMap headPathANDFind(String filePathName, String subscope, Vector keys, Vector includes, Vector excludeFiles, Vector excludeDirs, boolean isTop, boolean includeSubDir) throws Exception {
HashMap filesFound = new HashMap();
File file = new File(filePathName);
if(!file.exists()) {
if(debug >= CLEAN) System.out.println("!!!!!!!!!!!!!----------File not exist--------------!!!!!!!!!!!!!");
throw new Exception("File not exist");
}
if(file.isDirectory())
{
if(debug >= DETAIL) System.out.println("dir path >>>>>>>>>>>>" + filePathName);
if(excludeDirs == null || !isDirInList(filePathName, excludeDirs)) { // execute only if the dir is not excluded
if(includeSubDir || isTop) { // include sub dir or is top dir
File[] subFiles = file.listFiles();
for(int i = 0; i < subFiles.length; i++) {
HashMap found = headPathANDFind(subFiles[i].getAbsolutePath(), subscope, keys, includes, excludeFiles, excludeDirs, false, includeSubDir);
filesFound.putAll(found);
}
}
}
}
else if(file.isFile())
{
if(debug >= DETAIL) System.out.println("file path >>>>>>>>>>>>" + filePathName);
if((includes == null || isFileInList(filePathName, includes) == true) && (excludeFiles == null || isFileInList(filePathName, excludeFiles) == false)) {
HashMap result = headANDFind(filePathName, subscope, keys);
if(result != null && ((Integer)result.get("PERCENTAGE")).intValue() > 0)
filesFound.put(filePathName, result);
}
}
return filesFound;
}
/**
* find if the exact key exists in the file
* @param fileName the file name
* @param key the key
* @teturn boolean true if the exact word is found in the file
*/
boolean fileEXACTFind(String fileName, String key) throws Exception {
boolean found = false;
BufferedReader file = new BufferedReader(new FileReader(fileName));
try {
String line = null;
while((line = file.readLine())!= null) {
if(indexOf(line, key, false) != -1) {
found = true;
break;
}
}
} catch (IOException ioe) {
throw new Exception("IOException: " + ioe.getMessage());
} finally {
file.close();
}
return found;
}
/**
* find if the exact key exists in the file head
* @param fileName the file name
* @param subscope the subscope to search within the head, e.g., title, or keywords meta tag
* @param key the key
* @return boolean true if the exact word is found in the head of the file
*/
HashMap headEXACTFind(String fileName, String subscope, String key) throws Exception {
HashMap result = null;
String head = getHead(fileName);
if(head != null && head.length() > 0) {
result = new HashMap();
String title = getTitle(head);
HashMap metas = getMETA(head);
String desc = (String)metas.get("DESCRIPTION");
result.put("TITLE", title);
result.put("DESCRIPTION", desc);
Integer percentage = new Integer(0);
String searchContent = null;
if(subscope.equals("TITLE")) { // title search
searchContent = title;
} else { // meta tag search
searchContent = (String)metas.get(subscope.toUpperCase()); // get the META tag content
}
if(searchContent != null) {
if(searchContent.indexOf(key) != -1)
percentage = new Integer(100);
}
result.put("PERCENTAGE", percentage);
}
return result;
}
/**
* find all the files that have the exact key existing in the file
* @param fileName the file name
* @param key the key
* @param includes a list of file names or types that are included in the search
* @param excludeFiles a list of file names or types that are excluded in the search
* @param isTop a boolean that is true if the directory is the top level one, false otherwise
* @param includeSubDir a boolean that is true if search includes subfolders, false otherwise
* @return HashMap a HashMap of {file names, a HashMap of result}
*/
HashMap filePathEXACTFind(String filePathName, String key, Vector includes, Vector excludeFiles, Vector excludeDirs, boolean isTop, boolean includeSubDir) throws Exception {
HashMap filesFound = new HashMap();
File file = new File(filePathName);
if(!file.exists()) {
if(debug >= CLEAN) System.out.println("!!!!!!!!!!!!!----------File not exist--------------!!!!!!!!!!!!!");
throw new Exception("File not exist");
}
if(file.isDirectory())
{
if(debug >= DETAIL) System.out.println("dir path >>>>>>>>>>>>" + filePathName);
if(excludeDirs == null || !isDirInList(filePathName, excludeDirs)) { // execute only if the dir is not excluded
if(includeSubDir || isTop) { // include sub dir or is top dir
File[] subFiles = file.listFiles();
for(int i = 0; i < subFiles.length; i++) {
HashMap found = filePathEXACTFind(subFiles[i].getAbsolutePath(), key, includes, excludeFiles, excludeDirs, false, includeSubDir);
filesFound.putAll(found);
}
}
}
}
else if(file.isFile())
{
if(debug >= DETAIL) System.out.println("file path >>>>>>>>>>>>" + filePathName);
if((includes == null || isFileInList(filePathName, includes) == true) && (excludeFiles == null || isFileInList(filePathName, excludeFiles) == false)) {
boolean result = fileEXACTFind(filePathName, key);
if(result == true) {
HashMap finalResult = constructResult(filePathName, new Integer(100));
filesFound.put(filePathName, finalResult);
}
}
}
return filesFound;
}
/**
* find all the files that have the exact key existing in the file head
* @param fileName the file name
* @param subscope the subscope to search within the head, e.g., title, or keywords meta tag
* @param key the key
* @param includes a list of file names or types that are included in the search
* @param excludeFiles a list of file names or types that are excluded in the search
* @param isTop a boolean that is true if the directory is the top level one, false otherwise
* @param includeSubDir a boolean that is true if search includes subfolders, false otherwise
* @return HashMap a HashMap of { file names,a HashMap of result}
*/
HashMap headPathEXACTFind(String filePathName, String subscope, String key, Vector includes, Vector excludeFiles, Vector excludeDirs, boolean isTop, boolean includeSubDir) throws Exception {
HashMap filesFound = new HashMap();
File file = new File(filePathName);
if(!file.exists()) {
if(debug >= CLEAN) System.out.println("!!!!!!!!!!!!!----------File not exist--------------!!!!!!!!!!!!!");
throw new Exception("File not exist");
}
if(file.isDirectory())
{
if(debug >= DETAIL) System.out.println("dir path >>>>>>>>>>>>" + filePathName);
if(excludeDirs == null || !isDirInList(filePathName, excludeDirs)) { // execute only if the dir is not excluded
if(includeSubDir || isTop) { // include sub dir or is top dir
File[] subFiles = file.listFiles();
for(int i = 0; i < subFiles.length; i++) {
HashMap found = headPathEXACTFind(subFiles[i].getAbsolutePath(), subscope, key, includes, excludeFiles, excludeDirs, false, includeSubDir);
filesFound.putAll(found);
}
}
}
}
else if(file.isFile())
{
if(debug >= DETAIL) System.out.println("file path >>>>>>>>>>>>" + filePathName);
if((includes == null || isFileInList(filePathName, includes) == true) && (excludeFiles == null || isFileInList(filePathName, excludeFiles) == false)) {
HashMap result = headEXACTFind(filePathName, subscope, key);
if(result != null && ((Integer)result.get("PERCENTAGE")).intValue() > 0 )
filesFound.put(filePathName, result);
}
}
return filesFound;
}
/**
* find all files that do not have the excluded keys
* @param fileName the file name
* @param key the key
* @param includes a list of file names or types that are included in the search
* @param excludeFiles a list of file names or types that are excluded in the search
* @param excludeDirs a list of directory names that are excluded in the search
* @param isTop a boolean that is true if the directory is the top level one, false otherwise
* @param includeSubDir a boolean that is true if search includes subfolders, false otherwise
* @return HashMap a HashMap of { file names, a HashMap of result}
*/
HashMap filePathNOTFind(String filePathName, Vector keys, Vector includes, Vector excludeFiles, Vector excludeDirs, boolean isTop, boolean includeSubDir) throws Exception {
HashMap filesFound = new HashMap();
File file = new File(filePathName);
if(!file.exists()) {
if(debug >= CLEAN) System.out.println("!!!!!!!!!!!!!----------File not exist--------------!!!!!!!!!!!!!");
throw new Exception("File not exist");
}
if(file.isDirectory())
{
if(debug >= DETAIL) System.out.println("dir path >>>>>>>>>>>>" + filePathName);
if(excludeDirs == null || !isDirInList(filePathName, excludeDirs)) { // execute only if the dir is not excluded
if(includeSubDir || isTop) {
File[] subFiles = file.listFiles();
for(int i = 0; i < subFiles.length; i++) {
HashMap found = filePathNOTFind(subFiles[i].getAbsolutePath(), keys, includes, excludeFiles, excludeDirs, false, includeSubDir);
filesFound.putAll(found);
}
}
}
}
else if(file.isFile())
{
if(debug >= DETAIL) System.out.println("file path >>>>>>>>>>>>" + filePathName);
if((includes == null || isFileInList(filePathName, includes) == true) && (excludeFiles == null || isFileInList(filePathName, excludeFiles) == false)) {
HashMap result = fileANDFind(filePathName, keys);
Integer percentage = new Integer(getPercentageInANDFind(result)); // percentage of found
if(percentage.intValue() < 100) {
HashMap finalResult = constructResult(filePathName, new Integer(100 - percentage.intValue()));
filesFound.put(filePathName, finalResult);
if(debug >= DETAIL) System.out.println("file [" + filePathName + "] found NOT having: " + keys);
}
}
}
return filesFound;
}
/**
* find all files that do not have the excluded keys in the head
* @param fileName the file name
* @param subscope the subscope to search within the head, e.g., title, or keywords meta tag
* @param key the key
* @param includes a list of file names or types that are included in the search
* @param excludeFiles a list of file names or types that are excluded in the search
* @param excludeDirs a list of directory names that are excluded in the search
* @param isTop a boolean that is true if the directory is the top level one, false otherwise
* @param includeSubDir a boolean that is true if search includes subfolders, false otherwise
* @return HashMap a HashMap of { file names, a HashMap of result}
*/
HashMap headPathNOTFind(String filePathName, String subscope, Vector keys, Vector includes, Vector excludeFiles, Vector excludeDirs, boolean isTop, boolean includeSubDir) throws Exception {
HashMap filesFound = new HashMap();
File file = new File(filePathName);
if(!file.exists()) {
if(debug >= CLEAN) System.out.println("!!!!!!!!!!!!!----------File not exist--------------!!!!!!!!!!!!!");
throw new Exception("File not exist");
}
if(file.isDirectory())
{
if(debug >= DETAIL) System.out.println("dir path >>>>>>>>>>>>" + filePathName);
if(excludeDirs == null || !isDirInList(filePathName, excludeDirs)) { // execute only if the dir is not excluded
if(includeSubDir || isTop) {
File[] subFiles = file.listFiles();
for(int i = 0; i < subFiles.length; i++) {
HashMap found = headPathNOTFind(subFiles[i].getAbsolutePath(), subscope, keys, includes, excludeFiles, excludeDirs, false, includeSubDir);
filesFound.putAll(found);
}
}
}
}
else if(file.isFile())
{
if(debug >= DETAIL) System.out.println("file path >>>>>>>>>>>>" + filePathName);
if((includes == null || isFileInList(filePathName, includes) == true) && (excludeFiles == null || isFileInList(filePathName, excludeFiles) == false)) {
HashMap result = headANDFind(filePathName, subscope, keys);
if(result != null) {
Integer percentage = (Integer)result.get("PERCENTAGE"); // percentage of found
if(percentage.intValue() < 100) {
result.put("PERCENTAGE", new Integer(100 - percentage.intValue())); // update percentage
filesFound.put(filePathName, result);
if(debug >= DETAIL) System.out.println("head [" + filePathName + "] found NOT having: " + keys);
}
}
}
}
return filesFound;
}
/**
* parse a String of fields into a Vector of fields, comma and space as delimiter
* this is used to parse file names and search excludeFiles, search excludeDirs.
* @param str_fields a String of fields
* @return Vector a Vector of fields
*/
Vector parseFields(String str_fields) {
Vector fields = null;
if(str_fields != null && str_fields.length() > 0) {
StringTokenizer t_fields = new StringTokenizer( str_fields, ", " );
fields = new Vector();
while(t_fields.hasMoreTokens()) {
String key = t_fields.nextToken().toString();
fields.add(key);
}
}
if(debug >= DETAIL) System.out.println("parse fields:" + fields);
return fields;
}
/**
* file name wild card match: check if the file name is accepted by the filter
* @param fileName the file name
* @param filter the filter name
* @return boolean true if the file name matches the filter, false otherwise
*/
boolean wildCardMatchFileName(String fileName, String filter) {
boolean match = true;
String fileNameWOPath = fileName.substring(fileName.lastIndexOf(System.getProperty("file.separator"))+1);
StringTokenizer st = new StringTokenizer(filter, "*");
int start = 0;
int count = 0;
while(st.hasMoreTokens() && start <= fileNameWOPath.length()) {
count++;
String partialName = st.nextToken();
if(!filter.startsWith("*") && count==1) { // filter starts with non-wild card character as first token
if(!fileNameWOPath.startsWith(partialName)) { // file name has to start the same, otherwise not matching
match = false;
break;
}
}
int position = fileNameWOPath.indexOf(partialName, start);
if(position == -1) {
match = false;
break;
}
start = position + partialName.length();
}
if(debug >= DETAIL) System.out.println("file name [" + fileName + "] in filter: " + match);
return match;
}
/**
* check if the file is listed in the file list
* this supports exact file name match and file type match. It does not support wild card match
* @param fileName the file name
* @param fileList a Vector of file names
* @return boolean true if the file is listed in the list
*/
boolean isFileInList(String fileName, Vector fileList) throws Exception {
boolean isInList = false;
for(int i = 0; i < fileList.size(); i++) {
String match = (String)fileList.elementAt(i);
if(match.equals("*.*")) { // all types *.*
isInList = true;
if(debug >= DETAIL) System.out.println("file is included in *.*");
break;
} else if(match.indexOf("*") == -1) { // exact match xxx.yyy
if(fileName.equals(match)) {
isInList = true;
if(debug >= DETAIL) System.out.println("file is of exact name " + match);
break;
}
} else if(match.startsWith("*.") && match.lastIndexOf("*") == 0) { // *.yyy
String matchFileType = match.substring(2);
if(fileName.endsWith(matchFileType)) {
isInList = true;
if(debug >= DETAIL) System.out.println("file is of type " + match);
break;
}
} else if(match.startsWith(".") || match.endsWith(".")) {
throw new Exception("Error: incorrect file name specified.");
} else { // wild card match xxx*yyy.aaa*bbb
isInList = wildCardMatchFileName(fileName, match);
}
}
return isInList;
}
/**
* check if the directory is in the directory list
* @param dir a directory name
* @param dirList a Vector of directory names
* @return boolean true if the directory is listed in the list
*/
boolean isDirInList(String dir, Vector dirList) {
boolean isInList = false;
for(int i = 0; i < dirList.size(); i++) {
String ex = (String)dirList.elementAt(i);
if(dir.equals(ex)) {
isInList = true;
break;
}
}
if(debug >= DETAIL) System.out.println("dir is in List [" + dirList + "]: " + isInList);
return isInList;
}
// construct the real path using the relative path
String constructRealPath(String prefixPath, String relativePath) {
String realPath = prefixPath;
if(!prefixPath.endsWith(System.getProperty("file.separator")))
realPath += System.getProperty("file.separator");
realPath += relativePath.replace('/', System.getProperty("file.separator").charAt(0));
return realPath;
}
/**
* merge the two sets of files' names, the set of files with found keys + the set of files with excluded keys
* percentage or score of matching recalculated.
* @param matchings a HashMap of matching files who have the keys {filename, percentage}
* @param numKeys the number of included keys
* @param matchingsNOT a HashMap of matching files who do not have the excluded keys {filename, percentage}
* @param numExKeys the number of excluded keys
* @return HashMap a HashMap of merged file names with updated percentage
*/
HashMap mergeMatchings(HashMap matchings, int numKeys, HashMap matchingsNOT, int numExKeys) {
int total = numKeys + numExKeys;
HashMap merged = new HashMap();
if(matchings != null && matchings.size() > 0)
merged.putAll(matchings);
if(matchingsNOT != null && matchingsNOT.size() > 0)
merged.putAll(matchingsNOT);
// change percentage: if a file appears in both matchings and matchingsNOT,
// or it appears in only one set
for(Iterator it = merged.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
String fileName = (String)entry.getKey();
Integer origPercentageNOT = new Integer(0);
Integer origPercentage = new Integer(0);
if(matchings != null && matchings.containsKey(fileName)) {
HashMap result = (HashMap)matchings.get(fileName);
origPercentage = (Integer)result.get("PERCENTAGE");
}
if(matchingsNOT != null && matchingsNOT.containsKey(fileName)) {
HashMap result = (HashMap)matchingsNOT.get(fileName);
origPercentageNOT = (Integer)result.get("PERCENTAGE");
}
Integer percentage = new Integer((origPercentage.intValue()*numKeys + origPercentageNOT.intValue()*numExKeys)/total);
HashMap mergedResult = (HashMap)entry.getValue();
mergedResult.put("PERCENTAGE", percentage);
//merged.put(fileName, percentage); // replace the percentage
}
return merged;
}
/**
* find the total number of files in the directory
* @param dir the directory or file name
* @param isTop the boolean that is true if the directory is the top dir
* @param includeSubDir the boolean that is true when including sub directory
* @return long the number of files in the directory
*/
long getNumOfFiles(String dir, boolean isTop, boolean includeSubDir) {
long n = 0;
File parent = new File(dir);
if(parent.isDirectory()) {
if(includeSubDir || isTop) {
File[] children = parent.listFiles();
for(int i = 0; i < children.length; i++) {
n += getNumOfFiles(children[i].getAbsolutePath(), false, includeSubDir);
}
}
} else if(parent.isFile()) {
n = 1;
}
return n;
}
/**
* get vector out of hashmap
*/
Vector getNodeVector(HashMap hsh) {
Vector nodes = new Vector();
Iterator iter = hsh.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
HashMap node = new HashMap(1);
node.put(key, hsh.get(key));
nodes.add(node);
}
return nodes;
}
/**
* sort the Vector of [filename, HashMap of {field-value}]
* @param nodes an unsorted Vector of [filename, HashMap of {field-value}]
* @param low
* @param high
*/
void sort(Vector nodes, int low, int high) {
int lo_ind = low;
int hi_ind = high;
//each node of the vector is a hashmap containing only one pair --
//key: file_name, value: another HashMap containing PERCENTAGE/value pair, among other pairs.
if (lo_ind >= hi_ind)
return;
else
if (lo_ind == hi_ind - 1) {
// sort a two element list by swapping if necessary
HashMap lo_node = (HashMap) nodes.get(lo_ind);
HashMap lo_node_inner = (HashMap) lo_node.values().iterator().next();
int lo_percent = ((Integer) lo_node_inner.get("PERCENTAGE")).intValue();
HashMap hi_node = (HashMap) nodes.get(hi_ind);
HashMap hi_node_inner = (HashMap) hi_node.values().iterator().next();
int hi_percent = ((Integer) hi_node_inner.get("PERCENTAGE")).intValue();
if (lo_percent > hi_percent) {
Object node = nodes.get(lo_ind);
nodes.set(lo_ind, nodes.get(hi_ind));
nodes.set(hi_ind, node);
}
return;
}
//Pick a pivot and move it out of the way
int pivot_ind = (lo_ind + hi_ind) / 2;
HashMap pivot_node = (HashMap) nodes.get(pivot_ind);
HashMap pivot_node_inner = (HashMap) pivot_node.values().iterator().next();
int pivot_percent = ((Integer) pivot_node_inner.get("PERCENTAGE")).intValue();
nodes.set(pivot_ind, nodes.get(hi_ind));
nodes.set(hi_ind, pivot_node);
while (lo_ind < hi_ind) {
//Search forward from lo until an element is found that
//is greater than the pivot or lo >= hi
HashMap lo_node = (HashMap) nodes.get(lo_ind);
HashMap lo_node_inner = (HashMap) lo_node.values().iterator().next();
int lo_percent = ((Integer) lo_node_inner.get("PERCENTAGE")).intValue();
while (lo_percent <= pivot_percent && lo_ind < hi_ind) {
lo_ind++;
lo_node = (HashMap) nodes.get(lo_ind);
lo_node_inner = (HashMap) lo_node.values().iterator().next();
lo_percent = ((Integer) lo_node_inner.get("PERCENTAGE")).intValue();
}
//Search backward from hi until element is found that
//is less than the pivot, or lo >= hi
HashMap hi_node = (HashMap) nodes.get(hi_ind);
HashMap hi_node_inner = (HashMap) hi_node.values().iterator().next();
int hi_percent = ((Integer) hi_node_inner.get("PERCENTAGE")).intValue();
while (pivot_percent <= hi_percent && lo_ind < hi_ind) {
hi_ind--;
hi_node = (HashMap) nodes.get(hi_ind);
hi_node_inner = (HashMap) hi_node.values().iterator().next();
hi_percent = ((Integer) hi_node_inner.get("PERCENTAGE")).intValue();
}
//Swap elements lo and hi
if (lo_ind < hi_ind) {
Object node = nodes.get(lo_ind);
nodes.set(lo_ind, nodes.get(hi_ind));
nodes.set(hi_ind, node);
}
}
//Put the median in the "center" of the list
nodes.set(high, nodes.get(hi_ind));
nodes.set(hi_ind, pivot_node);
sort(nodes, low, lo_ind - 1);
sort(nodes, hi_ind + 1, high);
}
String replaceAll(String inputString, String toReplace, String toSubstitute) {
String parsedString = inputString;
int startReplaceIndex = parsedString.indexOf(toReplace);
while (startReplaceIndex != -1 && startReplaceIndex < parsedString.length()) {
parsedString = new StringBuffer(parsedString).replace(startReplaceIndex,
startReplaceIndex + toReplace.length(), toSubstitute).toString();
startReplaceIndex = parsedString.indexOf(toReplace, startReplaceIndex + toSubstitute.length());
}
return parsedString;
}
/////////////////////// added 12/08/02 for searching xml file ////////////////////
/**
* search the xml file for the keyword in keywords meta tag
* @param xmlFileName the file name of the xml file
* @param keyword (TODO: permit multiple keywords and boolean logic)
* @return matchingsInXMLFile a HashMap of matchings {filename, HashMap of {field-value}}
* @throw Exception if error in opening XML file or no keyword specified
*/
HashMap matchingsInXMLFile;
void searchXMLFile(String xmlFileName, String location, String keyword) throws Exception
{
DOMParser domParser = new DOMParser();
domParser.parse(xmlFileName);
Document doc = domParser.getDocument();
NodeList files = doc.getElementsByTagName("file");
if(keyword.trim().length() == 0) throw new Exception("No keyword specified");
//----- For all components, check for keyword in
for (int i = 0; i < files.getLength(); i++)
{
String titleContent = "";
String desc = "";
String fileName = "";
Element file = (Element) files.item(i);
fileName = file.getAttribute("url");
if (fileName.length()==0)
fileName = "Filename Not Specfied";
NodeList titleList = file.getElementsByTagName("title");
NodeList metaList = file.getElementsByTagName("meta");
Element title = (Element)titleList.item(0);
if (title != null)
titleContent = getText(title);
else
titleContent = fileName;
boolean keywordMatch = false;
//----- Extract meta content (only keywords and description currently supported)
// TODO: build vector of all requested meta tags
for (int j = 0; j < metaList.getLength(); j++)
{
Element meta = (Element) metaList.item(j);
String metaName = meta.getAttribute("name");
if (metaName == null || metaName.length() == 0)
throw new IllegalArgumentException("Missing meta name attribute or value");
if (metaName.equalsIgnoreCase("keywords"))
{
String keywords = getText(meta);
if(keywords.indexOf(keyword) != -1) keywordMatch = true;
}
else if (metaName.equalsIgnoreCase("description"))
desc = meta.getAttribute("content");
}
//----- keyword match found in keywords
if (keywordMatch)
{
HashMap fields = new HashMap();
fields.put("TITLE", titleContent);
fields.put("DESCRIPTION", desc);
fields.put("PERCENTAGE", new Integer(100));
fields.put("URL", location+fileName); //base url appended later
matchingsInXMLFile.put(fileName, fields);
}
} // end files loop
}
// get text content from xml node
String getText(Node node)
{
// We need to retrieve the text from elements, entity
// references, CDATA sections, and text nodes; but not
// comments or processing instructions
int type = node.getNodeType();
if (type == Node.COMMENT_NODE
|| type == Node.PROCESSING_INSTRUCTION_NODE) return "";
StringBuffer text = new StringBuffer();
String value = node.getNodeValue();
if (value != null) text.append(value);
if (node.hasChildNodes())
{
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
{
Node child = children.item(i);
text.append(getText(child));
}
}
return text.toString();
}
////////////////////////// end of declarations ////////////////////////////
%>
<%
////////////////////////// start of main line /////////////////////////////
// Don't disable cache so back button works after clicking on a link
str_error_detail = ""; //clear prior error messages
//----- Get search parameters
String str_keywords = request.getParameter("RZkeywords");
String str_option = request.getParameter("RZoption");
String str_scope = request.getParameter("RZscope");
String str_subscope = request.getParameter("RZsubscope");
String str_metaName = request.getParameter("RZmeta_name");
String str_pathOption = request.getParameter("RZpath_option");
String str_base_url = request.getParameter("RZbase_url_override");
String str_base_dir = request.getParameter("RZbase_dir_override");
String str_location = request.getParameter("RZlocation_override") == null ? "/" : request.getParameter("RZlocation_override");
String str_filenames = request.getParameter("RZfilenames"); //(e.g. "*.html,*.htm")
String str_excludeFiles = request.getParameter("RZexclude_files"); //(e.g. "*.gif,*.jpg")
String str_excludeDirs = request.getParameter("RZexclude_dirs"); //(e.g. "images,docs")
String str_linkTarget = request.getParameter("RZtarget");
String str_subfolders = request.getParameter("RZsubfolders");
//MVW added 11/4/04 to default search to location of this file
if (str_base_url == null || str_base_url.equals("")) {
String requestURL = request.getRequestURL().toString();
int separatorIndex = requestURL.lastIndexOf("/");
if (separatorIndex != -1) {
str_base_url = requestURL.substring(0, separatorIndex);
}
}
if (str_base_dir == null || str_base_dir.equals("") && request.getServletPath() != null) {
String servletPath = request.getServletPath();
int separatorIndex = servletPath.lastIndexOf("/");
if (separatorIndex >= 0) {
servletPath = servletPath.substring(0, separatorIndex);
}
servletPath = replaceAll(servletPath, "/", File.separator);
str_base_dir = application.getRealPath("/")
+ (servletPath.length() > 1 ? servletPath.substring(1) : "");
}
//End MVW additions
///////////////// added 12/08/02 for searching xml file ////////////
// new option - search xml file for keywords
String str_xmlfile = request.getParameter("RZxmlfile");
////////////////////////////////////////////////////////////////////
if(debug >= CLEAN) {
out.println("