×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Jiang BigLong
Added: Oct 27, 2016 1:16 PM
Views: 2107
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package chann.mxd.shopping.service;
  7.  
  8. import chann.mxd.shopping.domain.CodeInfoDate;
  9. import chann.mxd.shopping.domain.FuzzySearchInfo;
  10. import chann.mxd.shopping.domain.ItemInfoDate;
  11. import java.io.BufferedReader;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileReader;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.InputStreamReader;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Collection;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26.  
  27. /**
  28.  *
  29.  * @author ml123
  30.  */
  31. public class ItemCodeNameServic {
  32.  
  33. //      public void searchItemName(String one_item_name, String two_item_name, String three_item_name) {
  34. //              ItemInfoDate itemInfoDate = new ItemInfoDate(new StringBuffer(""), new StringBuffer(""), new StringBuffer(""));
  35. //              FileReader fr = null;
  36. //              try {
  37. //                      File file = new File("代码文本.txt");
  38. //                      fr = new FileReader(file);
  39. //                      BufferedReader bf = new BufferedReader(fr);
  40. //
  41. //                      if (one_item_name == null || one_item_name.equals("")) {
  42. //                              one_item_name = "江大龙";
  43. //                      } else {
  44. //                              one_item_name = "名称:" + one_item_name;
  45. //                      }
  46. //
  47. //                      if (two_item_name == null || two_item_name.equals("")) {
  48. //                              two_item_name = "江大龙";
  49. //                      } else {
  50. //                              two_item_name = "名称:" + two_item_name;
  51. //                      }
  52. //
  53. //                      if (three_item_name == null || three_item_name.equals("")) {
  54. //                              three_item_name = "江大龙";
  55. //                      } else {
  56. //                              three_item_name = "名称:" + three_item_name;
  57. //                      }
  58. //
  59. //                      char[] c = new char[1024 * 20];
  60. //                      int len;
  61. //                      while ((len = bf.read(c)) != -1) {
  62. //
  63. //                              String item_info = new String(c, 0, len);
  64. //
  65. //                              if (item_info.indexOf(one_item_name) > 0) {
  66. ////                                            System.out.println(item_info.substring(item_info.indexOf(one_item_name) - 5, item_info.indexOf(one_item_name) + 20));
  67. ////                                            itemInfoDate.setOne_item_info(new StringBuffer("").append(item_info.substring(item_info.indexOf(one_item_name) - 5, item_info.indexOf(one_item_name) + 20)));
  68. //
  69. ////                                            System.out.println(one_item_name);
  70. //                                      int[] validateIndex = validateIndexNumber(one_item_name, item_info);
  71. //                                      itemInfoDate.getOne_item_info().append((item_info.substring(validateIndex[0], validateIndex[1])));
  72. //                                      itemInfoDate.getOne_item_info().append("\n");
  73. //                              }
  74. //
  75. //                              if (item_info.indexOf(two_item_name) > 0) {
  76. ////                                    System.out.println(item_info.substring(item_info.indexOf(two_item_name) - 5, item_info.indexOf(two_item_name) + 20));
  77. ////                                    itemInfoDate.setTwo_item_info(new StringBuffer(item_info.substring(item_info.indexOf(two_item_name) - 5, item_info.indexOf(two_item_name) + 20)));
  78. //                                      itemInfoDate.getTwo_item_info().append(item_info.substring(item_info.indexOf(two_item_name) - 20, item_info.indexOf(two_item_name) + 20));
  79. //                                      itemInfoDate.getTwo_item_info().append("\n");
  80. //                              }
  81. ////                            itemInfoDate.getTwo_item_info().append("\n");
  82. //                              if (item_info.indexOf(three_item_name) > 0) {
  83. ////                                    System.out.println(item_info.substring(item_info.indexOf(three_item_name) - 5, item_info.indexOf(three_item_name) + 20));
  84. ////                                    itemInfoDate.setThree_item_info(new StringBuffer(item_info.substring(item_info.indexOf(three_item_name) - 5, item_info.indexOf(three_item_name) + 20)));
  85. //                                      itemInfoDate.getThree_item_info().append(item_info.substring(item_info.indexOf(three_item_name) - 20, item_info.indexOf(three_item_name) + 20));
  86. //                                      itemInfoDate.getThree_item_info().append("\n");
  87. //                              }
  88. //
  89. //                      }
  90. //                      System.out.println(itemInfoDate);
  91. //              } catch (IOException ex) {
  92. //                      ex.printStackTrace();
  93. //              } finally {
  94. //                      try {
  95. //                              fr.close();
  96. //                      } catch (IOException ex) {
  97. //                              ex.printStackTrace();
  98. //                      }
  99. //              }
  100. //      }
  101. //      public void searchCodeName(String one_code_name, String two_code_name, String three_code_name) {
  102. //              CodeInfoDate codeInfoDate = new CodeInfoDate(new StringBuffer(""), new StringBuffer(""), new StringBuffer(""));
  103. //              FileReader fr = null;
  104. //              try {
  105. //                      File file = new File("代码文本.txt");
  106. //                      fr = new FileReader(file);
  107. //                      BufferedReader bf = new BufferedReader(fr);
  108. //                      char[] c = new char[124];
  109. //                      int len;
  110. //                      while ((len = bf.read(c)) != -1) {
  111. //                              String code_info = new String(c, 0, len);
  112. //                              if (code_info.indexOf(one_code_name) != -1) {
  113. //                                      codeInfoDate.getOne_code_info().append(code_info.substring(code_info.indexOf(one_code_name) - 5, code_info.indexOf(one_code_name) + 20));
  114. //                                      codeInfoDate.getOne_code_info().append("\n");
  115. //                              }
  116. //                      }
  117. //                      System.out.println(codeInfoDate.getOne_code_info());
  118. //              } catch (IOException ex) {
  119. //                      ex.printStackTrace();
  120. //              } finally {
  121. //                      try {
  122. //                              fr.close();
  123. //                      } catch (IOException ex) {
  124. //                              ex.printStackTrace();
  125. //                      }
  126. //              }
  127. //      }
  128.         public int[] validateIndexNumber(String itemName, String dateInfo) {
  129.                 int beginIndex = dateInfo.indexOf(itemName) - 80;
  130.                 int lastIndex = dateInfo.indexOf(itemName) + 80;
  131.                 int dateInfoLength = dateInfo.length();
  132.  
  133.                 int[] validateIndex = new int[2];
  134.  
  135.                 if (beginIndex < 0) {
  136.                         beginIndex = 80 + beginIndex;
  137.                 }
  138.                 validateIndex[0] = beginIndex;
  139.                 if (lastIndex > dateInfoLength) {
  140.                         lastIndex = dateInfoLength;
  141.                 }
  142.                 validateIndex[1] = lastIndex;
  143.  
  144.                 return validateIndex;
  145.  
  146.         }
  147.  
  148.         public ItemInfoDate searchItemNameLine(String path, String one_item_name, String two_item_name, String three_item_name) {
  149.                 System.out.println("searchItemNameLine");
  150.                 ItemInfoDate itemInfoDate = new ItemInfoDate(new StringBuffer(""), new StringBuffer(""), new StringBuffer(""), 1, 1, 1);
  151. //              String txtPath = "d:/代码文本.txt";
  152.                 BufferedReader bf = null;
  153.                 try {
  154.  
  155.                         bf = getBufferedReader(path);
  156.  
  157.                         if (one_item_name == null || one_item_name.equals("")) {
  158.                                 one_item_name = "江大龙";
  159.                         } else {
  160.                                 one_item_name = "名称:" + one_item_name;
  161.                         }
  162.  
  163.                         if (two_item_name == null || two_item_name.equals("")) {
  164.                                 two_item_name = "江大龙";
  165.                         } else {
  166.                                 two_item_name = "名称:" + two_item_name;
  167.                         }
  168.  
  169.                         if (three_item_name == null || three_item_name.equals("")) {
  170.                                 three_item_name = "江大龙";
  171.                         } else {
  172.                                 three_item_name = "名称:" + three_item_name;
  173.                         }
  174.                        
  175.                         int one_item_info_count = itemInfoDate.getOne_item_info_count();
  176.                         int two_item_info_connt = itemInfoDate.getTwo_item_info_count();
  177.                         int three_item_info_connt = itemInfoDate.getThree_item_info_count();
  178.                         int i = 0;
  179.                         String item_info;
  180.                         while ((item_info = bf.readLine()) != null) {
  181.                                 i++;
  182.                                 if (item_info.indexOf(one_item_name) >= 0) {
  183.                                         for (int j = -3; j < 3; j++) {
  184. //                                              System.out.println(readTxtLine(i + j));
  185.                                                 itemInfoDate.getOne_item_info().append(readTxtLine(i + j, path)).append("\n\r");
  186.  
  187.                                         }
  188.                                         itemInfoDate.getOne_item_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
  189.                                        
  190.                                         itemInfoDate.setOne_item_info_count(one_item_info_count++);
  191.                                 }
  192.  
  193.                                 if (item_info.indexOf(two_item_name) >= 0) {
  194.  
  195.                                         for (int j = -3; j < 3; j++) {
  196. //                                              System.out.println(readTxtLine(i + j));
  197.                                                 itemInfoDate.getTwo_item_info().append(readTxtLine(i + j, path)).append("\n\r");
  198.                                         }
  199.                                         itemInfoDate.getTwo_item_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
  200.                                        
  201.                                         itemInfoDate.setTwo_item_info_count(two_item_info_connt++);
  202.                                 }
  203.  
  204.                                 if (item_info.indexOf(three_item_name) >= 0) {
  205.                                         for (int j = -3; j < 3; j++) {
  206. //                                              System.out.println(readTxtLine(i + j));
  207.                                                 itemInfoDate.getThree_item_info().append(readTxtLine(i + j, path)).append("\n\r");
  208.                                         }
  209.                                         itemInfoDate.getThree_item_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
  210.                                         itemInfoDate.setThree_item_info_count(three_item_info_connt++);
  211.                                 }
  212.                         }
  213.  
  214.                         System.out.println(itemInfoDate.getTwo_item_info().toString().equals(""));
  215.  
  216.                 } catch (IOException ex) {
  217.                         ex.printStackTrace();
  218.                 } finally {
  219.                         try {
  220. //                              fr.close();
  221.                                 bf.close();
  222.                         } catch (IOException ex) {
  223.                                 ex.printStackTrace();
  224.                         }
  225.  
  226.                 }
  227.                 return itemInfoDate;
  228.         }
  229.  
  230.         public String readTxtLine(int lineNo, String path) {
  231.  
  232.                 String line = "";
  233.                 String encoding = "GBK";
  234.                 BufferedReader bf = null;
  235.                 try {
  236.                         bf = getBufferedReader(path);
  237.                         int i = 0;
  238.                         while (i < lineNo) {
  239.                                 line = bf.readLine();
  240.                                 i++;
  241.                         }
  242.                         bf.close();
  243.                 } catch (Exception e) {
  244.                         e.printStackTrace();
  245.                 } finally {
  246.  
  247.                 }
  248.  
  249.                 return line;
  250.         }
  251.  
  252. //      public  String readTxtLine2(int lineNo) {
  253. //
  254. //              String line = "";
  255. //              String encoding = "GBK";
  256. //              try {
  257. //                      File txtFile = new File("代码文本.txt");
  258. //                      InputStream in = new FileInputStream(txtFile);
  259. //                      InputStreamReader read = new InputStreamReader(in, encoding);
  260. //                      BufferedReader reader = new BufferedReader(read);
  261. //                      int i = 0;
  262. //                      while (i < lineNo) {
  263. //                              line = reader.readLine();
  264. //                              i++;
  265. //                      }
  266. //                      reader.close();
  267. //              } catch (Exception e) {
  268. //                      e.printStackTrace();
  269. //              } finally{
  270. //                     
  271. //              }
  272. //
  273. //              return line;
  274. //      }
  275.         //测试方法,
  276. //      public void searchItemNameLine2(String one_item_name, String two_item_name, String three_item_name) {
  277. //
  278. //              String line = "";
  279. ////            String encoding = "GBK";
  280. //              try {
  281. //                      File txtFile = new File("代码文本.txt");
  282. //                      InputStream in = new FileInputStream(txtFile);
  283. //                      InputStreamReader read = new InputStreamReader(in);
  284. //                      BufferedReader reader = new BufferedReader(read);
  285. //                     
  286. //                      String item_name_line = "";
  287. //                      int i = 0;
  288. //                      int j = 0;
  289. //                      while ((item_name_line = reader.readLine()) != null) {
  290. //                              i++;
  291. //                              if ( item_name_line.indexOf("" + one_item_name) > 0){
  292. //                                      System.out.println(item_name_line);
  293. //                                      System.out.println(i);
  294. //                                      while ( j < i){
  295. ////                                            String str =
  296. //                                      }
  297. //                              }
  298. //                      }
  299. //                      reader.close();
  300. //              } catch (Exception e) {
  301. //                      e.printStackTrace();
  302. //              } finally{
  303. //                     
  304. //              }
  305. //      }
  306.         public CodeInfoDate searchCodeNameLine(String path, String one_code_name, String two_code_name, String three_code_name) {
  307.                 CodeInfoDate codeInfoDate = new CodeInfoDate(new StringBuffer(""), new StringBuffer(""), new StringBuffer("") , 1, 1, 1);
  308.                 String txtPath = "d:/代码文本.txt";
  309.  
  310.                 if (one_code_name.equals("") || one_code_name == null) {
  311.                         one_code_name = "江大龙";
  312.                 } else {
  313.                         one_code_name = "代码:" + one_code_name;
  314.                 }
  315.  
  316.                 if (two_code_name.equals("") || two_code_name == null) {
  317.                         two_code_name = "江大龙";
  318.                 } else {
  319.                         two_code_name = "代码:" + two_code_name;
  320.                 }
  321.  
  322.                 if (three_code_name.equals("") || three_code_name == null) {
  323.                         three_code_name = "江大龙";
  324.                 } else {
  325.                         three_code_name = "代码:" + three_code_name;
  326.                 }
  327.  
  328.                 BufferedReader bf = getBufferedReader(path);
  329.                
  330.                 int one_code_info_count = codeInfoDate.getOne_code_info_count();
  331.                 int two_code_info_count = codeInfoDate.getTwo_code_info_count();
  332.                 int three_code_info_count = codeInfoDate.getThree_code_info_count();
  333.                
  334.                 String code_info;
  335.                 int i = 0;
  336.                 try {
  337.                         while ((code_info = bf.readLine()) != null) {
  338.                                 i++;
  339.                                 if (code_info.indexOf(one_code_name) != -1) {
  340.                                         for (int j = -2; j < 4; j++) {
  341.                                                 codeInfoDate.getOne_code_info().append(readTxtLine(i + j,path)).append("\n\r");
  342.                                         }
  343.                                         codeInfoDate.getOne_code_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
  344.                                         codeInfoDate.setOne_code_info_count(one_code_info_count++);
  345.                                 }
  346.  
  347.                                 if (code_info.indexOf(two_code_name) != -1) {
  348.                                         for (int j = -2; j < 4; j++) {
  349.                                                 codeInfoDate.getTwo_code_info().append(readTxtLine(i + j, path)).append("\n\r");
  350.                                         }
  351.                                         codeInfoDate.getTwo_code_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
  352.                                         codeInfoDate.setTwo_code_info_count(two_code_info_count++);
  353.                                 }
  354.  
  355.                                 if (code_info.indexOf(three_code_name) != -1) {
  356.                                         for (int j = -2; j < 4; j++) {
  357.                                                 codeInfoDate.getThree_code_info().append(readTxtLine(i + j,path)).append("\n\r");
  358.                                         }
  359.                                         codeInfoDate.getThree_code_info().append("<font color='red'>▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂</font>").append("\n");
  360.                                         codeInfoDate.setThree_code_info_count(three_code_info_count++);
  361.                                 }
  362.  
  363.                         }
  364.                 } catch (IOException ex) {
  365.                         ex.printStackTrace();
  366.                 } finally {
  367.                         try {
  368.                                 bf.close();
  369.                         } catch (IOException ex) {
  370.                                 ex.printStackTrace();
  371.                         }
  372.                 }
  373.                 return codeInfoDate;
  374.         }
  375.  
  376.         public BufferedReader getBufferedReader(String txtPath) {
  377. //              System.out.println("getBufferedReader执行");
  378.                 BufferedReader bf = null;
  379.                 try {
  380.                         File file = new File(txtPath);
  381.                         FileReader fr = new FileReader(file);
  382.                         bf = new BufferedReader(fr);
  383.                 } catch (FileNotFoundException fileNotFoundException) {
  384.                         fileNotFoundException.printStackTrace();
  385.                 } finally {
  386.                 }
  387.                 return bf;
  388.         }
  389.  
  390.         public FuzzySearchInfo fuzzyQuery(String item_text, String path) {
  391.  
  392.                 FuzzySearchInfo fuzzySearchInfo = new FuzzySearchInfo(new StringBuffer(""), 0);
  393.                
  394. //              StringBuffer fuzzyInfo = new StringBuffer("");
  395.                 BufferedReader bf = null;
  396.  
  397.                 String pathInfo = path;
  398. //              bf = getBufferedReader("d://代码文本.txt");
  399.                 bf = getBufferedReader(path);
  400. //              int i = 0;
  401. //              System.out.println(pathInfo);
  402.                 String cache_line;
  403.                 char[] c = new char[1024];
  404.                 Integer[] linkLineCount = getLinkLineCount(item_text, path);
  405.  
  406.                 Collection<Integer> linkLineSet = new HashSet<>();
  407. //              System.out.println(linkLineSet.size());
  408.                 System.out.println("数组长度" + linkLineCount.length);
  409.                 int before = -1;
  410.                 int after = 0;
  411.                 if (linkLineCount.length == 1) {
  412.                         linkLineSet.add(linkLineCount[0]);
  413.                 } else {
  414.                         if (linkLineCount.length == 2) {
  415.                                 linkLineSet.add(linkLineCount[1]);
  416.                         } else {
  417.                                 for (int j = 0; j < linkLineCount.length - 1; j++) {
  418.                                         before++;
  419.                                         after++;
  420. //                      System.out.println(before + " : " + after);
  421.                                         int dVakyeResult = linkLineCount[after] - linkLineCount[before];
  422.                                         if (dVakyeResult > 4) {
  423. //                                      System.out.println("标记" + before);
  424. //                                      System.out.println(linkLineCount[before]);
  425.                                                 linkLineSet.add(linkLineCount[before]);
  426.                                                 linkLineSet.add(linkLineCount[after]);
  427.                                         } else {
  428.                                                 linkLineSet.remove(linkLineCount[before]);
  429.                                         }
  430.  
  431.                                 }
  432.                         }
  433.  
  434.                 }
  435. //              System.out.println(linkLineSet.size());
  436.                 for (Integer link : linkLineSet) {
  437.                         for (int j = -3; j < 3; j++) {
  438. //                              System.out.println(readTxtLine(link + j));
  439.                                 fuzzySearchInfo.getFuzzyInfo().append(readTxtLine(link + j, path)).append("\n");
  440.                         }
  441.                         fuzzySearchInfo.getFuzzyInfo().append("<br><font color='red'>☠☠▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸▸☠☠☠☠◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂◂☠☠</font>").append("\n");
  442.                 }
  443.                 fuzzySearchInfo.setSearchCount(linkLineSet.size());
  444. //              System.out.println(fuzzyInfo);
  445.                 try {
  446.                         bf.close();
  447.                 } catch (IOException ex) {
  448.                         ex.printStackTrace();
  449.                 }
  450.                 return fuzzySearchInfo;
  451.         }
  452.  
  453.         public Integer[] getLinkLineCount(String item_text, String path) {
  454.  
  455.                 Integer[] linkLineCount = new Integer[getLinkLineLengh(item_text, path)];
  456. //              BufferedReader bf = getBufferedReader("d://代码文本.txt");
  457.                 BufferedReader bf = getBufferedReader(path);
  458. //              System.out.println(path);
  459.                 String item_cache;
  460.  
  461.                 int j = 0;
  462.                 int i = 0;
  463.  
  464.                 try {
  465.                         while ((item_cache = bf.readLine()) != null) {
  466.                                 j++;
  467.                                 if (item_cache.indexOf(item_text) >= 0) {
  468.  
  469.                                         if (i < linkLineCount.length) {
  470.                                                 linkLineCount[i] = j;
  471.                                                 i++;
  472.                                         }
  473.                                 }
  474.                         }
  475. //                      for (int cash : linkLineCount) {
  476. //                              System.out.println(cash);
  477. //                      }
  478.  
  479.                 } catch (IOException ex) {
  480.                         ex.printStackTrace();
  481.                 } finally {
  482.                         try {
  483.                                 bf.close();
  484.                         } catch (IOException ex) {
  485.                                 ex.printStackTrace();
  486.                         }
  487.                 }
  488.  
  489.                 return linkLineCount;
  490.         }
  491.  
  492.         public int getLinkLineLengh(String item_text, String path) {
  493.                 BufferedReader bf = null;
  494. //              bf = getBufferedReader("d://代码文本.txt");
  495.                 bf = getBufferedReader(path);
  496.  
  497.                 String item_cache;
  498.                 int i = 0;
  499.                 int lineCount = 0;
  500.                 try {
  501.                         while ((item_cache = bf.readLine()) != null) {
  502.                                 i++;
  503.                                 if (item_cache.indexOf(item_text) >= 0) {
  504.                                         lineCount++;
  505.                                 }
  506.                         }
  507.                 } catch (IOException ex) {
  508.                         ex.printStackTrace();
  509.                 } finally {
  510. //                      try {
  511. //                              bf.close();
  512. //                      } catch (IOException ex) {
  513. //                              ex.printStackTrace();
  514. //                      }
  515.                 }
  516.  
  517.                 return lineCount;
  518.         }
  519.        
  520.         /**
  521.          * 测试用
  522.          * @param name
  523.          */
  524.         public void getLinkLine(String name){
  525.                 BufferedReader bf = getBufferedReader("d://代码文本.txt");
  526.                 int lineCount = 0;
  527.                 String item_cache;
  528.                 int i = 0;
  529.                 try {
  530.                         while ((item_cache = bf.readLine()) != null) {
  531.                                 i++;
  532.                                 if (item_cache.indexOf(name) >= 0) {
  533.                                         lineCount++;
  534.                                 }
  535.                         }
  536.                         System.out.println(lineCount);
  537.                 } catch (IOException ex) {
  538.                         ex.printStackTrace();
  539.                 } finally {
  540.                         try {
  541.                                 bf.close();
  542.                         } catch (IOException ex) {
  543.                                 ex.printStackTrace();
  544.                         }
  545.                 }
  546.                
  547.         }
  548. }
  549.