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

📄 categoryviewthread.java

📁 实现了从Google
💻 JAVA
字号:
/*
 * 创建日期 2004-11-7
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.ct.hotweb.getter;

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

import com.ct.hotweb.bean.*;
import com.ct.hotweb.dao.*;
import com.ct.hotweb.util.*;

/**
 * @author Administrator
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class CategoryViewThread extends Thread {
	SearchKeyStack stack = null;
	private HotWebWorkManager manager = null;
	private Category cat = null;
	private List categorys = null;
	//private HttpClient httpClient = null;
	//private MultiThreadedHttpConnectionManager httpManager = null;
	//private static Log log = LogFactory.getLog(CategoryViewThread.class);

	/**
	 * 
	 * @param manager
	 * @param cat
	 * @param categorys
	 */
	public CategoryViewThread(
		HotWebWorkManager manager,
		Category cat,
		List categorys) {
		this.manager = manager;
		this.cat = cat;
		this.categorys = categorys;
		//this.httpManager = manager.getHttpManager();
		//this.httpClient = new HttpClient(this.httpManager);

	}

	public void run() {
		int connCount = 0;
		categorys.add(cat);
		CategoryDAO catDAO = new CategoryDAO();
		try {
			Random rand = new Random();

			ArrayList pathList = new ArrayList();
			ArrayList catsList = new ArrayList();
			ArrayList keysList = new ArrayList();
			ArrayList blockVarList = null;

			int catCount = categorys.size();
			for (int i = 0;
				i < categorys.size()
					&& !manager.isKilled()
					&& !stack.isKilled();
				i++) {
				try {
					System.out.println(
						"共" + catCount + "个目录. 正在产生第" + (i + 1) + "个目录...");
					Category c = (Category) categorys.get(i);
					String id = (c == null) ? "0" : "" + c.getId();
					
					if (c != null) {
						boolean fail = false;
						int count = 0;
						{
							count++;
							try {
								HotWebGetter hwg =
									new HotWebGetter(
										manager.getHttpClient(),
										manager.getHotWebGetterConfig(),
										c.getName(),
										manager.getBlockAnalyzer());
								blockVarList = hwg.getHotWeb().getBlocksVar();
							} catch (Exception e1) {
								//Thread.sleep(1000);
								//i--;
								//connCount++;
								//continue;
								//System.out.println("error...");
								//save to db
					
								Thread.sleep(3000);
								//continue;
							}
						}
					
					} else {
						blockVarList = null;
					}
					
					HashMap head = new HashMap();
					
					if (c == null)
						head.put("CategoryName", "");
					else
						head.put(
							"CategoryName",
							Utils.charsetChange(
								c.getName(),
								manager.getHtmlCharset()));
					
					HashMap map = new HashMap(2);
					
					if (c != null) {
						/*
						map.put("PathPartName", "首页");
						map.put(
							"PathPartUrl",
							GetterHelper.getCategoryUrl(manager, null));
						pathList.add(map);
						*/
						StringTokenizer st = new StringTokenizer(c.getPath(), "/");
						while (st.hasMoreTokens()) {
							map = new HashMap(2);
							String partUrl = st.nextToken();
							Category pathCat = catDAO.getCategory(partUrl.trim());
							map.put(
								"PathPartName",
								Utils.charsetChange(
									pathCat.getName(),
									manager.getHtmlCharset()));
							map.put(
								"PathPartUrl",
								GetterHelper.getCategoryUrl(manager, pathCat));
							pathList.add(map);
						}
					}
					
					List subCats = catDAO.getSubCategory(id);
					List subKeys = catDAO.getSubSearchKey(id);
					
					for (int j = 0; j < subCats.size(); j++) {
						Category sc = (Category) subCats.get(j);
						map = new HashMap(2);
						map.put(
							"CategoryName",
							Utils.charsetChange(
								sc.getName(),
								manager.getHtmlCharset()));
						map.put(
							"CategoryUrl",
							GetterHelper.getCategoryUrl(manager, sc));
						catsList.add(map);
					}
					
					for (int j = 0; j < subKeys.size(); j++) {
						SearchKey sk = (SearchKey) subKeys.get(j);
						Category cp =
							(Category) catDAO.getCategory("" + sk.getParentID());
						map = new HashMap(2);
						map.put(
							"KeyName",
							Utils.charsetChange(
								sk.getName(),
								manager.getHtmlCharset()));
						map.put("KeyUrl", GetterHelper.getKeyUrl(manager, cp, sk));
						keysList.add(map);
					}
					
					String content =
						generate(
							manager.getIndexHtmlTemplate(),
							head,
							pathList,
							catsList,
							keysList,
							blockVarList,
							new HashMap());
					outputStaticHtml(c, content);
					
					pathList.clear();
					catsList.clear();
					keysList.clear();
					
					//从任务中去除
					try {
						if (blockVarList != null && blockVarList.size() > 0)
							catDAO.removeError("" + c.getId());
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					
				} catch (RuntimeException e1) {
					e1.printStackTrace();
				}
			}
		} catch (Exception e) {
			System.out.println(
				"category file generate error, " + e.getMessage());
		} finally {

		}

	}

	public void outputStaticHtml(Category cat, String content) {
		File f;
		BufferedOutputStream bos = null;

		String fileName = GetterHelper.getCategoryFileName(manager, cat);
		String path = GetterHelper.getCategoryPath(manager, cat);
		if (!(f = new File(path)).exists())
			f.mkdirs();

		path += fileName;
		f = new File(path);
		if (f.exists())
			f.delete();
		try {
			bos = new BufferedOutputStream(new FileOutputStream(f));
			bos.write(content.getBytes(manager.getHtmlCharset()));
			bos.flush();
		} catch (Exception e) {
			System.out.println("output static html error. " + e.getMessage());
		} finally {
			try {
				bos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public String generate(
		TemplateAnalyzer template,
		HashMap headVars,
		ArrayList pathVars,
		ArrayList catVars,
		ArrayList keyVars,
		ArrayList skeyVars,
		HashMap footVars) {
		String htmlContent = "";

		//generate head
		htmlContent
			+= HtmlGenerateUtils.generateBlock(template.getBlock(0), headVars);

		//generate path
		htmlContent
			+= HtmlGenerateUtils.generateBlock(
				template.getBlock(1),
				HtmlGenerateUtils.EMPTY_VARS);
		htmlContent
			+= HtmlGenerateUtils.generateBlocks(template.getBlock(2), pathVars);
		htmlContent
			+= HtmlGenerateUtils.generateBlock(
				template.getBlock(3),
				HtmlGenerateUtils.EMPTY_VARS);
				
		//generate category
		if (catVars != null && catVars.size() > 0) {
			htmlContent
				+= HtmlGenerateUtils.generateBlock(template.getBlock(4), HtmlGenerateUtils.EMPTY_VARS);
			htmlContent 
				+= HtmlGenerateUtils.generateBlocks(
					template.getBlock(5),
					HtmlGenerateUtils.EMPTY_VARS,
					template.getBlock(6),
					catVars,
					template.getBlock(7),
					HtmlGenerateUtils.EMPTY_VARS,
					manager.getCategoryColumn()				
				);
			htmlContent
				+= HtmlGenerateUtils.generateBlock(template.getBlock(8), HtmlGenerateUtils.EMPTY_VARS);
		}

		//generate keys
		if (keyVars != null && keyVars.size() > 0) {
			htmlContent
				+= HtmlGenerateUtils.generateBlock(template.getBlock(9), HtmlGenerateUtils.EMPTY_VARS);

			htmlContent 
				+= HtmlGenerateUtils.generateBlocks(
					template.getBlock(10),
					HtmlGenerateUtils.EMPTY_VARS,
					template.getBlock(11),
					keyVars,
					template.getBlock(12),
					HtmlGenerateUtils.EMPTY_VARS,
					manager.getKeyColumn()				
				);			
			htmlContent
				+= HtmlGenerateUtils.generateBlock(template.getBlock(13), HtmlGenerateUtils.EMPTY_VARS);
		}

		//generate searchkey
		if (skeyVars != null && skeyVars.size() > 0) {
			htmlContent
				+= HtmlGenerateUtils.generateBlock(
					template.getBlock(14),
					headVars);
			htmlContent
				+= HtmlGenerateUtils.generateBlocks(
					template.getBlock(15),
					skeyVars);
			htmlContent
				+= HtmlGenerateUtils.generateBlock(
					template.getBlock(16),
					new HashMap());
		}

		//generate foot
		htmlContent
			+= HtmlGenerateUtils.generateBlock(template.getBlock(17), footVars);
		return htmlContent;
	}

}

⌨️ 快捷键说明

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