| 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 | c383ed0 | 2015-02-26 21:35:22 +0000 | [diff] [blame] | 6 | import static de.ids_mannheim.korap.util.KrillString.*; |
| 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 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 25 | |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 26 | // Constructor for highlighting elements |
| 27 | public HighlightCombinatorElement (byte type, int number) { |
| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame] | 28 | this.type = type; |
| 29 | this.number = number; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 32 | |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 33 | // Constructor for highlighting elements, |
| 34 | // that may not be terminal, i.e. they were closed and will |
| 35 | // be reopened for overlapping issues. |
| 36 | public HighlightCombinatorElement (byte type, int number, boolean terminal) { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 37 | this.type = type; |
| 38 | this.number = number; |
| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame] | 39 | this.terminal = terminal; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 42 | |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 43 | // Constructor for textual data |
| 44 | public HighlightCombinatorElement (String characters) { |
| Nils Diewald | ff0f874 | 2015-02-26 20:42:45 +0000 | [diff] [blame] | 45 | this.type = (byte) 0; |
| 46 | this.characters = characters; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 49 | |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 50 | // Return html fragment for this combinator element |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 51 | public String toHTML (Match match, FixedBitSet level, byte[] levelCache) { |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 52 | // Opening |
| 53 | if (this.type == 1) { |
| 54 | StringBuilder sb = new StringBuilder(); |
| 55 | if (this.number == -1) { |
| 56 | sb.append("<mark>"); |
| 57 | } |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 58 | |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 59 | else if (this.number < -1) { |
| 60 | sb.append("<span xml:id=\"") |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 61 | .append(match.getPosID(match.getClassID(this.number))) |
| 62 | .append("\">"); |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 63 | } |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 64 | |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 65 | else if (this.number >= 256) { |
| 66 | sb.append("<span "); |
| 67 | if (this.number < 2048) { |
| 68 | sb.append("title=\"") |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 69 | .append(match.getAnnotationID(this.number)) |
| 70 | .append('"'); |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 71 | } |
| 72 | else { |
| 73 | Relation rel = match.getRelationID(this.number); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 74 | sb.append("xlink:title=\"").append(rel.annotation) |
| 75 | .append("\" xlink:type=\"simple\" xlink:href=\"#") |
| 76 | .append(match.getPosID(rel.ref)).append('"'); |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 77 | }; |
| 78 | sb.append('>'); |
| 79 | } |
| Nils Diewald | 52bd1cd | 2014-11-06 20:44:24 +0000 | [diff] [blame] | 80 | |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 81 | // Highlight - < 256 |
| 82 | else { |
| 83 | // Get the first free level slot |
| 84 | byte pos; |
| 85 | if (levelCache[this.number] != '\0') { |
| 86 | pos = levelCache[this.number]; |
| 87 | } |
| 88 | else { |
| 89 | pos = (byte) level.nextSetBit(0); |
| 90 | level.clear(pos); |
| 91 | levelCache[this.number] = pos; |
| 92 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 93 | sb.append("<mark class=\"class-").append(this.number) |
| 94 | .append(" level-").append(pos).append("\">"); |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 95 | }; |
| 96 | return sb.toString(); |
| 97 | } |
| 98 | // Closing |
| 99 | else if (this.type == 2) { |
| 100 | if (this.number < -1 || this.number >= 256) |
| 101 | return "</span>"; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 102 | |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 103 | if (this.number == -1) |
| 104 | return "</mark>"; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 105 | |
| Nils Diewald | dcd5ab1 | 2015-02-20 02:59:09 +0000 | [diff] [blame] | 106 | if (this.terminal) |
| 107 | level.set((int) levelCache[this.number]); |
| 108 | return "</mark>"; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 109 | }; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 110 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 111 | // HTML encode primary data |
| 112 | return escapeHTML(this.characters); |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 115 | |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 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 | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 118 | if (this.type == 1) { |
| 119 | StringBuilder sb = new StringBuilder(); |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 120 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 121 | // Match |
| 122 | if (this.number == -1) { |
| 123 | sb.append("["); |
| 124 | } |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 125 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 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; |
| Nils Diewald | 79f6c4d | 2014-09-17 17:34:01 +0000 | [diff] [blame] | 157 | }; |
| 158 | }; |