PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/FileSearchSys/src/com/FSS/File/FilesGatherer.java

http://wadamolyfproject.googlecode.com/
Java | 202 lines | 176 code | 18 blank | 8 comment | 29 complexity | b1d08f50268bce42869ed14748095f12 MD5 | raw file
  1. package com.FSS.File;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.ListIterator;
  9. import java.util.Observable;
  10. import java.util.StringTokenizer;
  11. import java.util.Timer;
  12. import java.util.Vector;
  13. import org.apache.poi.hwpf.HWPFDocument;
  14. import org.apache.poi.hwpf.usermodel.Paragraph;
  15. import org.apache.poi.hwpf.usermodel.Range;
  16. import com.FSS.FileList.DefaultList;
  17. import com.FSS.FileList.FileList;
  18. import com.FSS.config.Config;
  19. import com.FSS.util.FileListUtil;
  20. import com.FSS.util.FilesChangeMonitor;
  21. public class FilesGatherer extends Observable {
  22. //
  23. private FileList allIFiles;
  24. private FileList formalFiles;
  25. private FileList inFormalFiles;
  26. //
  27. public FilesGatherer() {
  28. super();
  29. File file = new File(Config.DATA_LOCATION);
  30. if (file.exists())
  31. allIFiles = FileListUtil.loadFromDisk(file.getAbsolutePath());
  32. else
  33. allIFiles = new DefaultList();
  34. file = new File(Config.getValue("FORMAL"));
  35. if (file.exists()) {
  36. formalFiles = FileListUtil.loadFromDisk(file.getAbsolutePath());
  37. } else
  38. formalFiles = new DefaultList();
  39. file = new File(Config.getValue("INFORMAL"));
  40. if (file.exists()) {
  41. inFormalFiles= FileListUtil.loadFromDisk(file.getAbsolutePath());
  42. } else
  43. inFormalFiles = new DefaultList();
  44. }
  45. public FileList getFormalFiles() {
  46. return formalFiles;
  47. }
  48. public FileList getInFormalFiles() {
  49. return inFormalFiles;
  50. }
  51. public void gatherFiles(Vector<String> fileNames) {
  52. for (String name : fileNames) {
  53. IFile ifile = new DefaultFile();
  54. ifile.setName(name);
  55. this.setIFile(ifile);
  56. allIFiles.add(ifile);
  57. if (ifile.getType() == 0)
  58. formalFiles.add(ifile);
  59. else
  60. inFormalFiles.add(ifile);
  61. }
  62. setChanged();
  63. super.notifyObservers();
  64. }
  65. public void removeIFiles(Vector<String> fileNames) {
  66. for (String name : fileNames) {
  67. ListIterator<IFile> itr = allIFiles.listIterator();
  68. while (itr.hasNext()) {
  69. IFile ifile = itr.next();
  70. if (ifile.getName().equals(name)) {
  71. itr.remove();
  72. break;
  73. }
  74. }
  75. itr = formalFiles.listIterator();
  76. while (itr.hasNext()) {
  77. IFile ifile = itr.next();
  78. if (ifile.getName().equals(name)) {
  79. itr.remove();
  80. break;
  81. }
  82. }
  83. itr = inFormalFiles.listIterator();
  84. while (itr.hasNext()) {
  85. IFile ifile = itr.next();
  86. if (ifile.getName().equals(name)) {
  87. itr.remove();
  88. break;
  89. }
  90. }
  91. }
  92. setChanged();
  93. super.notifyObservers();
  94. }
  95. private void setIFile(IFile f) {
  96. f.setForm(".doc");
  97. File file = new File(Config.DOC_LOCATION + "\\" + f.getName());
  98. long time = file.lastModified();
  99. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  100. String tsForm = formatter.format(new Date(time));
  101. int year = Integer.valueOf(tsForm.substring(0, 4));
  102. f.setYear(year);
  103. int month = Integer.valueOf(tsForm.substring(5, 7));
  104. f.setMonth(month);
  105. HWPFDocument doc = null;
  106. try {
  107. doc = new HWPFDocument(new FileInputStream(Config.DOC_LOCATION
  108. + "\\" + f.getName()));
  109. } catch (FileNotFoundException e) {
  110. // TODO Auto-generated catch block
  111. e.printStackTrace();
  112. } catch (IOException e) {
  113. // TODO Auto-generated catch block
  114. e.printStackTrace();
  115. System.out.println(f.getName());
  116. }
  117. Range r = doc.getRange(); // ??word?????
  118. int lenParagraph = r.numParagraphs();// ?????
  119. // Paragraph p;
  120. String firstP = null;
  121. StringBuffer secondP = new StringBuffer();// ????????;
  122. int count = 0;
  123. for (int x = 0; x < lenParagraph; x++) {
  124. Paragraph p = r.getParagraph(x);
  125. String text = p.text();
  126. if (text.trim().length() == 0) {
  127. if (count >= 2)
  128. break;
  129. continue;
  130. }
  131. else {
  132. count++;
  133. if (count == 1)
  134. firstP = text;
  135. else {
  136. secondP.append(text.substring(0, text.length() - 1));
  137. if (text.trim().matches(".*[??][\\s]*"))
  138. break;
  139. }
  140. }
  141. }
  142. try {
  143. f.setDepartment(firstP.substring(firstP.indexOf('?'), firstP
  144. .indexOf('?')));
  145. int num = Integer.valueOf(firstP.substring(firstP.indexOf('?') + 1,
  146. firstP.indexOf('?')));
  147. f.setNum(num);
  148. // ??????????
  149. f.setTitle(secondP.toString().replace("[ |?]*", "")
  150. .replace(" ", ""));
  151. } catch (Exception e) {
  152. // ?????
  153. f.setType(1);
  154. f.setTitle(f.getName());
  155. f.setDepartment("???");
  156. }
  157. // ?????
  158. String line = null;
  159. for (int x = 0; x < lenParagraph; x++) {
  160. Paragraph p = r.getParagraph(x);
  161. String text = p.text();
  162. if (text.matches(".*???.*\\s")) {
  163. line = text;
  164. break;
  165. }
  166. }
  167. if (line != null) {
  168. StringTokenizer st = new StringTokenizer(line, " ?");
  169. String temp = null;
  170. st.nextToken();
  171. while (st.hasMoreTokens() && (temp = st.nextToken()) != null) {
  172. f.addKeyWord(temp.trim());
  173. }
  174. }
  175. }
  176. public FileList getAllFiles() {
  177. return allIFiles;
  178. }
  179. public static void main(String args[]) {
  180. FilesGatherer fg = new FilesGatherer();
  181. Timer t = new Timer();
  182. t.schedule(new FilesChangeMonitor(fg), 1000, 5000);
  183. }
  184. }