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

📄 producttextfileprocessor.java

📁 一个搜索引擎,希望对大家有用
💻 JAVA
字号:
package com.luceneheritrixbook.core;

import java.io.*;
import java.util.*;

import com.luceneheritrixbook.database.ProductJDBC;
import com.luceneheritrixbook.extractor.Extractor;
import com.luceneheritrixbook.index.ProductIndexer;
import com.luceneheritrixbook.searchengine.config.PropertyConfiguration;

public class ProductTextFileProcessor {

	/**
	 *  directories for those stored product info txt files
	 */
	private String[] directories;
	
	private static final String dbUrl = PropertyConfiguration.getDBUrl();
	
	private static final String dbUsr = PropertyConfiguration.getDBUsr();
	
	private static final String dbPwd = PropertyConfiguration.getDBPwd();
	
	private ProductJDBC productJDBC = null;
	
	private ProductIndexer indexer = null;
	
	public final static int SUMMARY_LENGTH = 80;
	
	private static final String indexPath = PropertyConfiguration.getIndexStorePath();
	

	/**
	 * Default constructor
	 * 
	 */
	public ProductTextFileProcessor() {
		initialize();
	}
	
	public void initialize() {
		try {
			productJDBC = new ProductJDBC(dbUrl,dbUsr,dbPwd);
			indexer = new ProductIndexer(indexPath);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public void setDirectories(String[] directories) {
		this.directories = directories;
	}

	protected void process() throws Exception {

		if (productJDBC == null) {
			throw new Exception("Database connection failed, pls retry!!");
		}
		
		if (directories == null || directories.length == 0) {
			return;
		}

		try {
			for (int i = 0; i < directories.length; i++) {
				File f = new File(directories[i]);
				traverse(f);
			}
			
			closeDB();
			closeIndex();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void traverse(File file) throws Exception {
		
		String [] files = file.list();
		for (int i = 0; i < files.length; i++) {
			
			File productfile = new File(file, files[i]);
			String fname = productfile.getName();
			System.out.println(productfile);
			
			BufferedReader reader = new BufferedReader(new FileReader(productfile));
			
			String url = reader.readLine();
			String name = reader.readLine();
			String type = reader.readLine();
			String imageURI = "";
			String updatedtime = fname.substring(fname.lastIndexOf("-")+1, fname.lastIndexOf("."));
			
			StringBuffer content = new StringBuffer();
			String line = reader.readLine();
			while (line != null && !line.equals(Extractor.SEPARATOR)){
				content.append(line).append("\r\n");
				line = reader.readLine();
			}
			imageURI = reader.readLine();
			
			
			// make the Product object
			Product p = new Product();
			p.setCategory("手机");
			p.setName(name);
			p.setType(type);
			p.setImageURI(imageURI);
			p.setOriginalUrl(url);
			
			String contentstr = content.toString();
			p.setContent(contentstr);
			if (contentstr.length() > SUMMARY_LENGTH) {
				p.setSummary(contentstr.substring(0,SUMMARY_LENGTH-1));
			}
			else
				p.setSummary(contentstr);
			
			p.setUpdatedtime(updatedtime);
			
			// save to db first, and get the returned ID
			int nextid = insert2DB(p);
			
			// now we are trying to build Lucene document
			buildIndex(p, nextid);
			
		}
		
		optimizeIndex();
	}

	protected int insert2DB(Product p) throws Exception {
		return productJDBC.addProduct(p);
	}

	protected void buildIndex(Product p, int nextid) throws Exception {
		indexer.addProduct(p, nextid);
	}
	
	private void optimizeIndex() throws Exception {
		indexer.optimizeIndex();
	}
	
	private void closeIndex() throws Exception {
		indexer.close();
	}
	
	private void closeDB() {
		productJDBC.close();
	}

	public String getDbPwd() {
		return dbPwd;
	}
	
	public String getDbUrl() {
		return dbUrl;
	}

	public String getDbUsr() {
		return dbUsr;
	}

	public String getIndexPath() {
		return indexPath;
	}


	public static void main (String [] args) throws Exception {
		ProductTextFileProcessor pro = new ProductTextFileProcessor();
		pro.initialize();
		
		String path1 = "c:\\product\\mobile\\";
		pro.setDirectories(new String[]{path1});
		pro.process();
	}
	
}

⌨️ 快捷键说明

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