source: trunk/src_apollo/org/apollo/util/ExpediteeFileTextSearch.java@ 315

Last change on this file since 315 was 315, checked in by bjn8, 16 years ago

Apollo spin-off added

File size: 3.6 KB
Line 
1package org.apollo.util;
2
3import java.io.BufferedReader;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7import java.util.LinkedList;
8import java.util.List;
9
10/**
11 * Performs text item searches in expeditee files.
12 *
13 * @author Brook Novak
14 *
15 */
16public class ExpediteeFileTextSearch {
17
18 private ExpediteeFileTextSearch() {}
19
20 /**
21 * Peforms a perfix search.
22 *
23 * @param framePath
24 * The path of the frame file to search. Must not be null.
25 *
26 * @param textPrefixes
27 * The prefixes of the text to match against. Must not be null or empty.
28 *
29 * @return
30 * A list of results. Never null.
31 *
32 * @throws FileNotFoundException
33 * If the frame path does not exist.
34 *
35 * @throws IOException
36 * If a read operation failed.
37 *
38 * @throws NullPointerException
39 * If framePath or textPrefixes is null.
40 *
41 * @throws IllegalArgumentException
42 * If textPrefixes is empty.
43 */
44 public static List<TextItemSearchResult> prefixSearch(String framePath, String[] textPrefixes)
45 throws FileNotFoundException, IOException {
46
47 if (framePath == null)
48 throw new NullPointerException("framePath");
49 else if (textPrefixes == null)
50 throw new NullPointerException("textPrefix");
51 else if (textPrefixes.length == 0)
52 throw new IllegalArgumentException("textPrefix");
53
54 for (String prefix : textPrefixes)
55 if (prefix == null) throw new NullPointerException("textPrefix");
56
57 List<TextItemSearchResult> results = new LinkedList<TextItemSearchResult>();
58
59 BufferedReader reader = null;
60
61 try
62 {
63 reader = new BufferedReader(new FileReader(framePath));
64
65 String line = null;
66
67 String strippedTextualLine = null; // not null if match
68 String framelinkLine = null;
69 LinkedList<String> currentDataLines = new LinkedList<String>();
70
71 while (((line = reader.readLine()) != null)) {
72
73 if (line.length() <= 2) continue;
74
75 if (line.startsWith("S")) {
76
77 if (strippedTextualLine != null) {
78 results.add(createResult(strippedTextualLine, framelinkLine, currentDataLines));
79 }
80
81 // Reset - next item to parse
82 currentDataLines.clear();
83 strippedTextualLine = null;
84 framelinkLine = null;
85
86 } else if (line.startsWith("T ")) { // leave space - important
87
88 strippedTextualLine = line.substring(2);
89 boolean matches = false;
90 for (String prefix : textPrefixes) { // check for match
91 if (strippedTextualLine.startsWith(prefix)) {
92 matches = true;
93 break;
94 }
95 }
96
97 if (!matches) strippedTextualLine = null;
98
99 } else if (line.startsWith("D ")) { // leave space - important
100
101 currentDataLines.add(line);
102
103 } else if (line.startsWith("F ")) { // leave space - important
104
105 framelinkLine = line;
106
107 }
108
109 }
110
111 // Add last item
112 if (strippedTextualLine != null) {
113 results.add(createResult(strippedTextualLine, framelinkLine, currentDataLines));
114 }
115
116 } finally {
117 if (reader != null) {
118 reader.close();
119 }
120 }
121
122 return results;
123 }
124
125
126 private static TextItemSearchResult createResult(
127 String strippedTextualLine,
128 String framelinkLine,
129 LinkedList<String> dataLines) {
130
131 assert(strippedTextualLine != null);
132 assert(dataLines != null);
133
134
135 TextItemSearchResult result = new TextItemSearchResult();
136
137 result.text = strippedTextualLine;
138 result.explink = (framelinkLine != null) ? framelinkLine.substring(2) : null; // not stripped
139 result.data = new LinkedList<String>();
140 for (String dataLine : dataLines) {
141 result.data.add(dataLine.substring(2)); // data lines not stripped
142 }
143
144 return result;
145
146 }
147
148}
Note: See TracBrowser for help on using the repository browser.