source: trunk/src/org/apollo/util/ExpediteeFileTextSearch.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 4.4 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
10import org.expeditee.core.Point;
11
12/**
13 * Performs text item searches in expeditee files.
14 *
15 * @author Brook Novak
16 *
17 */
18public class ExpediteeFileTextSearch {
19
20 private ExpediteeFileTextSearch() {}
21
22 /**
23 * Peforms a perfix search.
24 *
25 * @param framePath
26 * The path of the frame file to search. Must not be null.
27 *
28 * @param textPrefixes
29 * The prefixes of the text to match against. Must not be null or empty.
30 *
31 * @return
32 * A list of results. Never null.
33 *
34 * @throws FileNotFoundException
35 * If the frame path does not exist.
36 *
37 * @throws IOException
38 * If a read operation failed.
39 *
40 * @throws NullPointerException
41 * If framePath or textPrefixes is null.
42 *
43 * @throws IllegalArgumentException
44 * If textPrefixes is empty.
45 */
46 public static List<TextItemSearchResult> prefixSearch(String framePath, String[] textPrefixes)
47 throws FileNotFoundException, IOException {
48
49 if (framePath == null)
50 throw new NullPointerException("framePath");
51 else if (textPrefixes == null)
52 throw new NullPointerException("textPrefix");
53 else if (textPrefixes.length == 0)
54 throw new IllegalArgumentException("textPrefix");
55
56 for (String prefix : textPrefixes)
57 if (prefix == null) throw new NullPointerException("textPrefix");
58
59 List<TextItemSearchResult> results = new LinkedList<TextItemSearchResult>();
60
61 BufferedReader reader = null;
62
63 try
64 {
65 reader = new BufferedReader(new FileReader(framePath));
66
67 String line = null;
68
69 String strippedTextualLine = null; // not null if match
70 String framelinkLine = null;
71 LinkedList<String> currentDataLines = new LinkedList<String>();
72 String positionLine = null;
73
74 while (((line = reader.readLine()) != null)) {
75
76 if (line.length() <= 2) continue;
77
78 if (line.startsWith("S")) {
79
80 if (strippedTextualLine != null) {
81 results.add(createResult(strippedTextualLine, framelinkLine, currentDataLines, positionLine));
82 }
83
84 // Reset - next item to parse
85 currentDataLines.clear();
86 strippedTextualLine = null;
87 framelinkLine = null;
88 positionLine = null;
89
90 } else if (line.startsWith("T ")) { // leave space - important
91
92 strippedTextualLine = line.substring(2);
93 boolean matches = false;
94 for (String prefix : textPrefixes) { // check for match
95 if (strippedTextualLine.startsWith(prefix)) {
96 matches = true;
97 break;
98 }
99 }
100
101 if (!matches) strippedTextualLine = null;
102
103 } else if (line.startsWith("D ")) { // leave space - important
104
105 currentDataLines.add(line);
106
107 } else if (line.startsWith("F ")) { // leave space - important
108
109 framelinkLine = line;
110
111 } else if (line.startsWith("P ")) {
112 positionLine = line;
113 }
114
115 }
116
117 // Add last item
118 if (strippedTextualLine != null) {
119 results.add(createResult(strippedTextualLine, framelinkLine, currentDataLines, positionLine));
120 }
121
122 } finally {
123 if (reader != null) {
124 reader.close();
125 }
126 }
127
128 return results;
129 }
130
131
132 private static TextItemSearchResult createResult(
133 String strippedTextualLine,
134 String framelinkLine,
135 LinkedList<String> dataLines,
136 String positionLine) {
137
138 if (strippedTextualLine == null || dataLines == null || positionLine == null) {
139 System.err.println("FAILED TO READ ITEM IN EXPEDITEE FILE");
140 return null;
141 }
142
143
144 TextItemSearchResult result = new TextItemSearchResult();
145
146 result.text = strippedTextualLine;
147 result.explink = (framelinkLine != null) ? framelinkLine.substring(2) : null; // not stripped
148 result.data = new LinkedList<String>();
149 for (String dataLine : dataLines) {
150 result.data.add(dataLine.substring(2)); // data lines not stripped
151 }
152
153 // Parse position information
154 if (positionLine.length() <= 2) return null;
155 String tmp = positionLine.substring(2);
156 tmp = tmp.trim();
157 int yStrIndex = tmp.indexOf(' ');
158 if (yStrIndex == -1) return null;
159
160 int x;
161 int y;
162
163 try {
164 x = Integer.parseInt(tmp.substring(0, yStrIndex).trim());
165 y = Integer.parseInt(tmp.substring(yStrIndex).trim());
166 } catch (NumberFormatException e) {
167 e.printStackTrace();
168 return null;
169 }
170
171 result.position = new Point(x, y);
172
173 return result;
174
175 }
176
177}
Note: See TracBrowser for help on using the repository browser.