| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame^] | 1 | package de.ids_mannheim.korap.response.match; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 2 | |
| 3 | import org.apache.lucene.util.FixedBitSet; |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 4 | import de.ids_mannheim.korap.response.Match; |
| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame^] | 5 | import de.ids_mannheim.korap.response.match.Relation; |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 6 | import static de.ids_mannheim.korap.util.KorapString.*; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 7 | import java.util.*; |
| 8 | import java.io.*; |
| 9 | |
| 10 | /* |
| 11 | Class for elements with highlighting information |
| 12 | */ |
| 13 | public class HighlightCombinatorElement { |
| 14 | |
| 15 | // Type 0: Textual data |
| 16 | // Type 1: Opening |
| 17 | // Type 2: Closing |
| 18 | public byte type; |
| 19 | |
| 20 | public int number = 0; |
| 21 | |
| 22 | public String characters; |
| 23 | public boolean terminal = true; |
| 24 | |
| 25 | // Constructor for highlighting elements |
| 26 | public HighlightCombinatorElement (byte type, int number) { |
| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame^] | 27 | this.type = type; |
| 28 | this.number = number; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | // Constructor for highlighting elements, |
| 32 | // that may not be terminal, i.e. they were closed and will |
| 33 | // be reopened for overlapping issues. |
| 34 | public HighlightCombinatorElement (byte type, int number, boolean terminal) { |
| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame^] | 35 | this.type = type; |
| 36 | this.number = number; |
| 37 | this.terminal = terminal; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // Constructor for textual data |
| 41 | public HighlightCombinatorElement (String characters) { |
| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame^] | 42 | this.type = (byte) 0; |
| 43 | this.characters = characters; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | // Return html fragment for this combinator element |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 47 | public String toHTML (Match match, FixedBitSet level, byte[] levelCache) { |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 48 | // Opening |
| 49 | if (this.type == 1) { |
| 50 | StringBuilder sb = new StringBuilder(); |
| 51 | if (this.number == -1) { |
| 52 | sb.append("<mark>"); |
| 53 | } |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 54 | |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 55 | else if (this.number < -1) { |
| 56 | sb.append("<span xml:id=\"") |
| 57 | .append(match.getPosID(match.getClassID(this.number))) |
| 58 | .append("\">"); |
| 59 | } |
| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame^] | 60 | |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 61 | else if (this.number >= 256) { |
| 62 | sb.append("<span "); |
| 63 | if (this.number < 2048) { |
| 64 | sb.append("title=\"") |
| 65 | .append(match.getAnnotationID(this.number)) |
| 66 | .append('"'); |
| 67 | } |
| 68 | else { |
| 69 | Relation rel = match.getRelationID(this.number); |
| 70 | sb.append("xlink:title=\"") |
| 71 | .append(rel.annotation) |
| 72 | .append("\" xlink:type=\"simple\" xlink:href=\"#") |
| 73 | .append(match.getPosID(rel.ref)) |
| 74 | .append('"'); |
| 75 | }; |
| 76 | sb.append('>'); |
| 77 | } |
| Nils Diewald | 52bd1cd | 2014-11-06 20:44:24 +0000 | [diff] [blame] | 78 | |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 79 | // Highlight - < 256 |
| 80 | else { |
| 81 | // Get the first free level slot |
| 82 | byte pos; |
| 83 | if (levelCache[this.number] != '\0') { |
| 84 | pos = levelCache[this.number]; |
| 85 | } |
| 86 | else { |
| 87 | pos = (byte) level.nextSetBit(0); |
| 88 | level.clear(pos); |
| 89 | levelCache[this.number] = pos; |
| 90 | }; |
| 91 | sb.append("<mark class=\"class-") |
| 92 | .append(this.number) |
| 93 | .append(" level-") |
| 94 | .append(pos) |
| 95 | .append("\">"); |
| 96 | }; |
| 97 | return sb.toString(); |
| 98 | } |
| 99 | // Closing |
| 100 | else if (this.type == 2) { |
| 101 | if (this.number < -1 || this.number >= 256) |
| 102 | return "</span>"; |
| 103 | |
| 104 | if (this.number == -1) |
| 105 | return "</mark>"; |
| 106 | |
| 107 | if (this.terminal) |
| 108 | level.set((int) levelCache[this.number]); |
| 109 | return "</mark>"; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | // HTML encode primary data |
| Nils Diewald | fe6a365 | 2015-02-05 20:34:27 +0000 | [diff] [blame] | 113 | return escapeHTML(this.characters); |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | // Return bracket fragment for this combinator element |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 117 | public String toBrackets (Match match) { |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 118 | if (this.type == 1) { |
| 119 | StringBuilder sb = new StringBuilder(); |
| 120 | |
| 121 | // Match |
| 122 | if (this.number == -1) { |
| 123 | sb.append("["); |
| 124 | } |
| 125 | |
| 126 | // Identifier |
| 127 | else if (this.number < -1) { |
| 128 | sb.append("{#"); |
| 129 | sb.append(match.getClassID(this.number)); |
| 130 | sb.append(':'); |
| 131 | } |
| 132 | |
| 133 | // Highlight, Relation, Span |
| 134 | else { |
| 135 | sb.append("{"); |
| 136 | if (this.number >= 256) { |
| 137 | if (this.number < 2048) |
| 138 | sb.append(match.getAnnotationID(this.number)); |
| 139 | else { |
| 140 | Relation rel = match.getRelationID(this.number); |
| 141 | sb.append(rel.annotation); |
| 142 | sb.append('>').append(rel.ref); |
| 143 | }; |
| 144 | sb.append(':'); |
| 145 | } |
| 146 | else if (this.number != 0) |
| 147 | sb.append(this.number).append(':'); |
| 148 | }; |
| 149 | return sb.toString(); |
| 150 | } |
| 151 | else if (this.type == 2) { |
| 152 | if (this.number == -1) |
| 153 | return "]"; |
| 154 | return "}"; |
| 155 | }; |
| 156 | return this.characters; |
| 157 | }; |
| 158 | }; |