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

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

Added y-pixel information to audio model.
Added Position-info retreival in quick text search util.
Added mutable primitive type
Linked track layout views now position tracks according to position in frame...a long coming modification.

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