| 1 | |
package org.paneris.bibliomania.metasearch.util; |
| 2 | |
|
| 3 | |
public class HackParser { |
| 4 | |
protected static class ParseException extends RuntimeException { |
| 5 | |
private int offset; |
| 6 | |
private String reason; |
| 7 | |
public ParseException(int offset, String expected) { |
| 8 | |
super(); |
| 9 | |
this.offset = offset; |
| 10 | |
this.reason = expected; |
| 11 | |
} |
| 12 | |
private static final long serialVersionUID = 1L; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
public String getMessage() { |
| 18 | |
return reason + " at " + offset; |
| 19 | |
} |
| 20 | |
} |
| 21 | |
|
| 22 | |
protected byte[] text; |
| 23 | 0 | protected int here = 0; |
| 24 | |
|
| 25 | 0 | public HackParser(byte[] text) { |
| 26 | 0 | this.text = text; |
| 27 | |
|
| 28 | 0 | } |
| 29 | |
|
| 30 | |
protected final void skipTo(byte c) { |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 0 | while (text[here++] != c); |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
protected final void skipTo(byte c, byte d) { |
| 39 | |
for (;;) { |
| 40 | 0 | byte t = text[here++]; |
| 41 | 0 | if (t == c || t == d) break; |
| 42 | 0 | } |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
public static int indexOf(byte[] text, byte[] s) { |
| 46 | 0 | byte s0 = s[0]; |
| 47 | 0 | int l = text.length - s.length; |
| 48 | 0 | for (int h = 0; h <= l; ++h) { |
| 49 | 0 | if (text[h] == s0) { |
| 50 | 0 | for (int i = 1;; ++i) { |
| 51 | 0 | if (i >= s.length) |
| 52 | 0 | return h; |
| 53 | 0 | if (text[h + i] != s[i]) |
| 54 | 0 | break; |
| 55 | |
} |
| 56 | |
} |
| 57 | |
} |
| 58 | |
|
| 59 | 0 | return -1; |
| 60 | |
} |
| 61 | |
|
| 62 | |
protected final void skipTo(byte[] s) { |
| 63 | |
|
| 64 | 0 | int startFrom = here; |
| 65 | |
for (;;) { |
| 66 | 0 | skipTo(s[0]); |
| 67 | 0 | int herePointer = here; |
| 68 | |
|
| 69 | 0 | for (int i = 1;; ++i, ++herePointer) { |
| 70 | 0 | if (i >= s.length) { |
| 71 | 0 | here = herePointer; |
| 72 | 0 | System.err.println("Found:" + new String(s)); |
| 73 | 0 | return; |
| 74 | |
} |
| 75 | 0 | if (herePointer == text.length) { |
| 76 | 0 | System.err.println(new String(text)); |
| 77 | 0 | throw new RuntimeException("String \"" + new String(s) + |
| 78 | |
"\" not found starting at " + startFrom + " and looking to " + herePointer ); |
| 79 | |
} |
| 80 | 0 | if (text[herePointer] != s[i]) |
| 81 | 0 | break; |
| 82 | |
} |
| 83 | 0 | } |
| 84 | |
} |
| 85 | |
|
| 86 | |
protected final void skipRealSpace() { |
| 87 | 0 | while (Character.isWhitespace((char)text[here])) |
| 88 | 0 | ++here; |
| 89 | 0 | } |
| 90 | |
|
| 91 | |
protected final boolean skipMaybe(byte[] s) { |
| 92 | 0 | if (here + s.length > text.length) |
| 93 | 0 | return false; |
| 94 | |
|
| 95 | 0 | for (int i = 0; i < s.length; ++i) |
| 96 | 0 | if (text[here + i] != s[i]) |
| 97 | |
|
| 98 | 0 | return false; |
| 99 | |
|
| 100 | 0 | here += s.length; |
| 101 | 0 | return true; |
| 102 | |
} |
| 103 | |
|
| 104 | 0 | protected static final byte[] nbsp = " ".getBytes(); |
| 105 | |
|
| 106 | |
protected final void skipSpace() { |
| 107 | |
do |
| 108 | 0 | skipRealSpace(); |
| 109 | 0 | while (skipMaybe(nbsp)); |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
protected final String plaintext(byte term) { |
| 113 | 0 | skipSpace(); |
| 114 | 0 | int start = here; |
| 115 | 0 | if (term == 0) |
| 116 | 0 | skipTo((byte)'<'); |
| 117 | |
else |
| 118 | 0 | skipTo((byte)'<', term); |
| 119 | |
|
| 120 | 0 | int end = here - 2; |
| 121 | 0 | while (Character.isWhitespace((char)text[end])) |
| 122 | 0 | --end; |
| 123 | |
|
| 124 | 0 | ++end; |
| 125 | |
|
| 126 | 0 | if (end <= start) |
| 127 | 0 | throw new ParseException(here, "at end"); |
| 128 | |
|
| 129 | 0 | String it = new String(text, start, end - start); |
| 130 | |
|
| 131 | 0 | while (it.endsWith(" ")) |
| 132 | 0 | it = it.substring(0, it.length() - 6).trim(); |
| 133 | |
|
| 134 | 0 | return it; |
| 135 | |
} |
| 136 | |
|
| 137 | |
protected final String plaintext() { |
| 138 | 0 | return plaintext((byte)'\0'); |
| 139 | |
} |
| 140 | |
|
| 141 | |
protected final String digits() { |
| 142 | 0 | int start = here; |
| 143 | 0 | if (!Character.isDigit((char)text[here])) |
| 144 | 0 | throw new ParseException(here, "not digit"); |
| 145 | 0 | ++here; |
| 146 | 0 | while (Character.isDigit((char)text[here])) |
| 147 | 0 | ++here; |
| 148 | 0 | return new String(text, start, here - start); |
| 149 | |
} |
| 150 | |
|
| 151 | |
protected final String quotedStringFromBack(int off) { |
| 152 | |
int start; |
| 153 | 0 | for (start = here - off; text[start] != '"'; --start); |
| 154 | 0 | skipTo((byte)'"'); |
| 155 | 0 | return new String(text, start + 1, here - start - 2); |
| 156 | |
} |
| 157 | |
} |