[315] | 1 | package org.apollo.util;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.io.FileNotFoundException;
|
---|
| 5 | import java.io.FileReader;
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.util.LinkedList;
|
---|
| 8 | import java.util.List;
|
---|
| 9 |
|
---|
[1102] | 10 | import org.expeditee.core.Point;
|
---|
| 11 |
|
---|
[315] | 12 | /**
|
---|
| 13 | * Performs text item searches in expeditee files.
|
---|
| 14 | *
|
---|
| 15 | * @author Brook Novak
|
---|
| 16 | *
|
---|
| 17 | */
|
---|
| 18 | public 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>();
|
---|
[349] | 72 | String positionLine = null;
|
---|
[315] | 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) {
|
---|
[349] | 81 | results.add(createResult(strippedTextualLine, framelinkLine, currentDataLines, positionLine));
|
---|
[315] | 82 | }
|
---|
| 83 |
|
---|
| 84 | // Reset - next item to parse
|
---|
| 85 | currentDataLines.clear();
|
---|
| 86 | strippedTextualLine = null;
|
---|
| 87 | framelinkLine = null;
|
---|
[349] | 88 | positionLine = null;
|
---|
[315] | 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 |
|
---|
[349] | 111 | } else if (line.startsWith("P ")) {
|
---|
| 112 | positionLine = line;
|
---|
[315] | 113 | }
|
---|
| 114 |
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | // Add last item
|
---|
| 118 | if (strippedTextualLine != null) {
|
---|
[349] | 119 | results.add(createResult(strippedTextualLine, framelinkLine, currentDataLines, positionLine));
|
---|
[315] | 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,
|
---|
[349] | 135 | LinkedList<String> dataLines,
|
---|
| 136 | String positionLine) {
|
---|
[315] | 137 |
|
---|
[349] | 138 | if (strippedTextualLine == null || dataLines == null || positionLine == null) {
|
---|
| 139 | System.err.println("FAILED TO READ ITEM IN EXPEDITEE FILE");
|
---|
| 140 | return null;
|
---|
| 141 | }
|
---|
[315] | 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 |
|
---|
[349] | 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 |
|
---|
[315] | 173 | return result;
|
---|
| 174 |
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | }
|
---|