/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chann.mxd.shopping.service;
import chann.mxd.shopping.domain.CodeInfoDate;
import chann.mxd.shopping.domain.FuzzySearchInfo;
import chann.mxd.shopping.domain.ItemInfoDate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author ml123
*/
public class ItemCodeNameServic {
// public void searchItemName(String one_item_name, String two_item_name, String three_item_name) {
// ItemInfoDate itemInfoDate = new ItemInfoDate(new StringBuffer(""), new StringBuffer(""), new StringBuffer(""));
// FileReader fr = null;
// try {
// File file = new File("代码文本.txt");
// fr = new FileReader(file);
// BufferedReader bf = new BufferedReader(fr);
//
// if (one_item_name == null || one_item_name.equals("")) {
// one_item_name = "江大龙";
// } else {
// one_item_name = "名称:" + one_item_name;
// }
//
// if (two_item_name == null || two_item_name.equals("")) {
// two_item_name = "江大龙";
// } else {
// two_item_name = "名称:" + two_item_name;
// }
//
// if (three_item_name == null || three_item_name.equals("")) {
// three_item_name = "江大龙";
// } else {
// three_item_name = "名称:" + three_item_name;
// }
//
// char[] c = new char[1024 * 20];
// int len;
// while ((len = bf.read(c)) != -1) {
//
// String item_info = new String(c, 0, len);
//
// if (item_info.indexOf(one_item_name) > 0) {
//// System.out.println(item_info.substring(item_info.indexOf(one_item_name) - 5, item_info.indexOf(one_item_name) + 20));
//// itemInfoDate.setOne_item_info(new StringBuffer("").append(item_info.substring(item_info.indexOf(one_item_name) - 5, item_info.indexOf(one_item_name) + 20)));
//
//// System.out.println(one_item_name);
// int[] validateIndex = validateIndexNumber(one_item_name, item_info);
// itemInfoDate.getOne_item_info().append((item_info.substring(validateIndex[0], validateIndex[1])));
// itemInfoDate.getOne_item_info().append("\n");
// }
//
// if (item_info.indexOf(two_item_name) > 0) {
//// System.out.println(item_info.substring(item_info.indexOf(two_item_name) - 5, item_info.indexOf(two_item_name) + 20));
//// itemInfoDate.setTwo_item_info(new StringBuffer(item_info.substring(item_info.indexOf(two_item_name) - 5, item_info.indexOf(two_item_name) + 20)));
// itemInfoDate.getTwo_item_info().append(item_info.substring(item_info.indexOf(two_item_name) - 20, item_info.indexOf(two_item_name) + 20));
// itemInfoDate.getTwo_item_info().append("\n");
// }
//// itemInfoDate.getTwo_item_info().append("\n");
// if (item_info.indexOf(three_item_name) > 0) {
//// System.out.println(item_info.substring(item_info.indexOf(three_item_name) - 5, item_info.indexOf(three_item_name) + 20));
//// itemInfoDate.setThree_item_info(new StringBuffer(item_info.substring(item_info.indexOf(three_item_name) - 5, item_info.indexOf(three_item_name) + 20)));
// itemInfoDate.getThree_item_info().append(item_info.substring(item_info.indexOf(three_item_name) - 20, item_info.indexOf(three_item_name) + 20));
// itemInfoDate.getThree_item_info().append("\n");
// }
//
// }
// System.out.println(itemInfoDate);
// } catch (IOException ex) {
// ex.printStackTrace();
// } finally {
// try {
// fr.close();
// } catch (IOException ex) {
// ex.printStackTrace();
// }
// }
// }
// public void searchCodeName(String one_code_name, String two_code_name, String three_code_name) {
// CodeInfoDate codeInfoDate = new CodeInfoDate(new StringBuffer(""), new StringBuffer(""), new StringBuffer(""));
// FileReader fr = null;
// try {
// File file = new File("代码文本.txt");
// fr = new FileReader(file);
// BufferedReader bf = new BufferedReader(fr);
// char[] c = new char[124];
// int len;
// while ((len = bf.read(c)) != -1) {
// String code_info = new String(c, 0, len);
// if (code_info.indexOf(one_code_name) != -1) {
// codeInfoDate.getOne_code_info().append(code_info.substring(code_info.indexOf(one_code_name) - 5, code_info.indexOf(one_code_name) + 20));
// codeInfoDate.getOne_code_info().append("\n");
// }
// }
// System.out.println(codeInfoDate.getOne_code_info());
// } catch (IOException ex) {
// ex.printStackTrace();
// } finally {
// try {
// fr.close();
// } catch (IOException ex) {
// ex.printStackTrace();
// }
// }
// }
public int[] validateIndexNumber
(String itemName,
String dateInfo
) {
int beginIndex = dateInfo.indexOf(itemName) - 80;
int lastIndex = dateInfo.indexOf(itemName) + 80;
int dateInfoLength = dateInfo.length();
int[] validateIndex = new int[2];
if (beginIndex < 0) {
beginIndex = 80 + beginIndex;
}
validateIndex[0] = beginIndex;
if (lastIndex > dateInfoLength) {
lastIndex = dateInfoLength;
}
validateIndex[1] = lastIndex;
return validateIndex;
}
public ItemInfoDate searchItemNameLine
(String path,
String one_item_name,
String two_item_name,
String three_item_name
) {
System.
out.
println("searchItemNameLine");
// String txtPath = "d:/代码文本.txt";
try {
bf = getBufferedReader(path);
if (one_item_name == null || one_item_name.equals("")) {
one_item_name = "江大龙";
} else {
one_item_name = "名称:" + one_item_name;
}
if (two_item_name == null || two_item_name.equals("")) {
two_item_name = "江大龙";
} else {
two_item_name = "名称:" + two_item_name;
}
if (three_item_name == null || three_item_name.equals("")) {
three_item_name = "江大龙";
} else {
three_item_name = "名称:" + three_item_name;
}
int one_item_info_count = itemInfoDate.getOne_item_info_count();
int two_item_info_connt = itemInfoDate.getTwo_item_info_count();
int three_item_info_connt = itemInfoDate.getThree_item_info_count();
int i = 0;
while ((item_info = bf.readLine()) != null) {
i++;
if (item_info.indexOf(one_item_name) >= 0) {
for (int j = -3; j < 3; j++) {
// System.out.println(readTxtLine(i + j));
itemInfoDate.getOne_item_info().append(readTxtLine(i + j, path)).append("\n\r");
}
itemInfoDate.getOne_item_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
itemInfoDate.setOne_item_info_count(one_item_info_count++);
}
if (item_info.indexOf(two_item_name) >= 0) {
for (int j = -3; j < 3; j++) {
// System.out.println(readTxtLine(i + j));
itemInfoDate.getTwo_item_info().append(readTxtLine(i + j, path)).append("\n\r");
}
itemInfoDate.getTwo_item_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
itemInfoDate.setTwo_item_info_count(two_item_info_connt++);
}
if (item_info.indexOf(three_item_name) >= 0) {
for (int j = -3; j < 3; j++) {
// System.out.println(readTxtLine(i + j));
itemInfoDate.getThree_item_info().append(readTxtLine(i + j, path)).append("\n\r");
}
itemInfoDate.getThree_item_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
itemInfoDate.setThree_item_info_count(three_item_info_connt++);
}
}
System.
out.
println(itemInfoDate.
getTwo_item_info().
toString().
equals(""));
ex.printStackTrace();
} finally {
try {
// fr.close();
bf.close();
ex.printStackTrace();
}
}
return itemInfoDate;
}
try {
bf = getBufferedReader(path);
int i = 0;
while (i < lineNo) {
line = bf.readLine();
i++;
}
bf.close();
e.printStackTrace();
} finally {
}
return line;
}
// public String readTxtLine2(int lineNo) {
//
// String line = "";
// String encoding = "GBK";
// try {
// File txtFile = new File("代码文本.txt");
// InputStream in = new FileInputStream(txtFile);
// InputStreamReader read = new InputStreamReader(in, encoding);
// BufferedReader reader = new BufferedReader(read);
// int i = 0;
// while (i < lineNo) {
// line = reader.readLine();
// i++;
// }
// reader.close();
// } catch (Exception e) {
// e.printStackTrace();
// } finally{
//
// }
//
// return line;
// }
//测试方法,
// public void searchItemNameLine2(String one_item_name, String two_item_name, String three_item_name) {
//
// String line = "";
//// String encoding = "GBK";
// try {
// File txtFile = new File("代码文本.txt");
// InputStream in = new FileInputStream(txtFile);
// InputStreamReader read = new InputStreamReader(in);
// BufferedReader reader = new BufferedReader(read);
//
// String item_name_line = "";
// int i = 0;
// int j = 0;
// while ((item_name_line = reader.readLine()) != null) {
// i++;
// if ( item_name_line.indexOf("" + one_item_name) > 0){
// System.out.println(item_name_line);
// System.out.println(i);
// while ( j < i){
//// String str =
// }
// }
// }
// reader.close();
// } catch (Exception e) {
// e.printStackTrace();
// } finally{
//
// }
// }
public CodeInfoDate searchCodeNameLine
(String path,
String one_code_name,
String two_code_name,
String three_code_name
) {
String txtPath
= "d:/代码文本.txt";
if (one_code_name.equals("") || one_code_name == null) {
one_code_name = "江大龙";
} else {
one_code_name = "代码:" + one_code_name;
}
if (two_code_name.equals("") || two_code_name == null) {
two_code_name = "江大龙";
} else {
two_code_name = "代码:" + two_code_name;
}
if (three_code_name.equals("") || three_code_name == null) {
three_code_name = "江大龙";
} else {
three_code_name = "代码:" + three_code_name;
}
int one_code_info_count = codeInfoDate.getOne_code_info_count();
int two_code_info_count = codeInfoDate.getTwo_code_info_count();
int three_code_info_count = codeInfoDate.getThree_code_info_count();
int i = 0;
try {
while ((code_info = bf.readLine()) != null) {
i++;
if (code_info.indexOf(one_code_name) != -1) {
for (int j = -2; j < 4; j++) {
codeInfoDate.getOne_code_info().append(readTxtLine(i + j,path)).append("\n\r");
}
codeInfoDate.getOne_code_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
codeInfoDate.setOne_code_info_count(one_code_info_count++);
}
if (code_info.indexOf(two_code_name) != -1) {
for (int j = -2; j < 4; j++) {
codeInfoDate.getTwo_code_info().append(readTxtLine(i + j, path)).append("\n\r");
}
codeInfoDate.getTwo_code_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
codeInfoDate.setTwo_code_info_count(two_code_info_count++);
}
if (code_info.indexOf(three_code_name) != -1) {
for (int j = -2; j < 4; j++) {
codeInfoDate.getThree_code_info().append(readTxtLine(i + j,path)).append("\n\r");
}
codeInfoDate.getThree_code_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
codeInfoDate.setThree_code_info_count(three_code_info_count++);
}
}
ex.printStackTrace();
} finally {
try {
bf.close();
ex.printStackTrace();
}
}
return codeInfoDate;
}
// System.out.println("getBufferedReader执行");
try {
fileNotFoundException.printStackTrace();
} finally {
}
return bf;
}
public FuzzySearchInfo fuzzyQuery
(String item_text,
String path
) {
FuzzySearchInfo fuzzySearchInfo
= new FuzzySearchInfo
(new StringBuffer(""),
0);
// StringBuffer fuzzyInfo = new StringBuffer("");
// bf = getBufferedReader("d://代码文本.txt");
bf = getBufferedReader(path);
// int i = 0;
// System.out.println(pathInfo);
char[] c = new char[1024];
Integer[] linkLineCount
= getLinkLineCount
(item_text, path
);
Collection<Integer> linkLineSet = new HashSet<>();
// System.out.println(linkLineSet.size());
System.
out.
println("数组长度" + linkLineCount.
length);
int before = -1;
int after = 0;
if (linkLineCount.length == 1) {
linkLineSet.add(linkLineCount[0]);
} else {
if (linkLineCount.length == 2) {
linkLineSet.add(linkLineCount[1]);
} else {
for (int j = 0; j < linkLineCount.length - 1; j++) {
before++;
after++;
// System.out.println(before + " : " + after);
int dVakyeResult = linkLineCount[after] - linkLineCount[before];
if (dVakyeResult > 4) {
// System.out.println("标记" + before);
// System.out.println(linkLineCount[before]);
linkLineSet.add(linkLineCount[before]);
linkLineSet.add(linkLineCount[after]);
} else {
linkLineSet.remove(linkLineCount[before]);
}
}
}
}
// System.out.println(linkLineSet.size());
for (int j = -3; j < 3; j++) {
// System.out.println(readTxtLine(link + j));
fuzzySearchInfo.getFuzzyInfo().append(readTxtLine(link + j, path)).append("\n");
}
fuzzySearchInfo.getFuzzyInfo().append("<br><font color='red'>☠☠▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂☠☠</font>").append("\n");
}
fuzzySearchInfo.setSearchCount(linkLineSet.size());
// System.out.println(fuzzyInfo);
try {
bf.close();
ex.printStackTrace();
}
return fuzzySearchInfo;
}
Integer[] linkLineCount
= new Integer[getLinkLineLengh
(item_text, path
)];
// BufferedReader bf = getBufferedReader("d://代码文本.txt");
// System.out.println(path);
int j = 0;
int i = 0;
try {
while ((item_cache = bf.readLine()) != null) {
j++;
if (item_cache.indexOf(item_text) >= 0) {
if (i < linkLineCount.length) {
linkLineCount[i] = j;
i++;
}
}
}
// for (int cash : linkLineCount) {
// System.out.println(cash);
// }
ex.printStackTrace();
} finally {
try {
bf.close();
ex.printStackTrace();
}
}
return linkLineCount;
}
public int getLinkLineLengh
(String item_text,
String path
) {
// bf = getBufferedReader("d://代码文本.txt");
bf = getBufferedReader(path);
int i = 0;
int lineCount = 0;
try {
while ((item_cache = bf.readLine()) != null) {
i++;
if (item_cache.indexOf(item_text) >= 0) {
lineCount++;
}
}
ex.printStackTrace();
} finally {
// try {
// bf.close();
// } catch (IOException ex) {
// ex.printStackTrace();
// }
}
return lineCount;
}
/**
* 测试用
* @param name
*/
public void getLinkLine
(String name
){
int lineCount = 0;
int i = 0;
try {
while ((item_cache = bf.readLine()) != null) {
i++;
if (item_cache.indexOf(name) >= 0) {
lineCount++;
}
}
System.
out.
println(lineCount
);
ex.printStackTrace();
} finally {
try {
bf.close();
ex.printStackTrace();
}
}
}
}