blob: 55837b826672bfe256b77d4ef4872d37341a6051 [file] [log] [blame]
Nils Diewaldff0f8742015-02-26 20:42:45 +00001package de.ids_mannheim.korap.response.match;
Nils Diewald79f6c4d2014-09-17 17:34:01 +00002
3import org.apache.lucene.util.FixedBitSet;
Nils Diewald392bcf32015-02-26 20:01:17 +00004import de.ids_mannheim.korap.response.Match;
Nils Diewaldff0f8742015-02-26 20:42:45 +00005import de.ids_mannheim.korap.response.match.Relation;
Nils Diewaldc383ed02015-02-26 21:35:22 +00006import static de.ids_mannheim.korap.util.KrillString.*;
Nils Diewald79f6c4d2014-09-17 17:34:01 +00007import java.util.*;
8import java.io.*;
9
10/*
11 Class for elements with highlighting information
12*/
13public 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 Diewaldbb33da22015-03-04 16:24:25 +000025
Nils Diewald79f6c4d2014-09-17 17:34:01 +000026 // Constructor for highlighting elements
27 public HighlightCombinatorElement (byte type, int number) {
Nils Diewaldff0f8742015-02-26 20:42:45 +000028 this.type = type;
29 this.number = number;
Nils Diewald79f6c4d2014-09-17 17:34:01 +000030 };
31
Nils Diewaldbb33da22015-03-04 16:24:25 +000032
Nils Diewald79f6c4d2014-09-17 17:34:01 +000033 // 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 Diewaldbb33da22015-03-04 16:24:25 +000037 this.type = type;
38 this.number = number;
Nils Diewaldff0f8742015-02-26 20:42:45 +000039 this.terminal = terminal;
Nils Diewald79f6c4d2014-09-17 17:34:01 +000040 };
41
Nils Diewaldbb33da22015-03-04 16:24:25 +000042
Nils Diewald79f6c4d2014-09-17 17:34:01 +000043 // Constructor for textual data
44 public HighlightCombinatorElement (String characters) {
Nils Diewaldff0f8742015-02-26 20:42:45 +000045 this.type = (byte) 0;
46 this.characters = characters;
Nils Diewald79f6c4d2014-09-17 17:34:01 +000047 };
48
Nils Diewaldbb33da22015-03-04 16:24:25 +000049
Nils Diewald79f6c4d2014-09-17 17:34:01 +000050 // Return html fragment for this combinator element
Nils Diewaldbb33da22015-03-04 16:24:25 +000051 public String toHTML (Match match, FixedBitSet level, byte[] levelCache) {
Nils Diewalddcd5ab12015-02-20 02:59:09 +000052 // Opening
53 if (this.type == 1) {
54 StringBuilder sb = new StringBuilder();
55 if (this.number == -1) {
56 sb.append("<mark>");
57 }
Nils Diewald79f6c4d2014-09-17 17:34:01 +000058
Nils Diewalddcd5ab12015-02-20 02:59:09 +000059 else if (this.number < -1) {
60 sb.append("<span xml:id=\"")
Nils Diewaldbb33da22015-03-04 16:24:25 +000061 .append(match.getPosID(match.getClassID(this.number)))
62 .append("\">");
Nils Diewalddcd5ab12015-02-20 02:59:09 +000063 }
Nils Diewaldbb33da22015-03-04 16:24:25 +000064
Nils Diewalddcd5ab12015-02-20 02:59:09 +000065 else if (this.number >= 256) {
66 sb.append("<span ");
67 if (this.number < 2048) {
68 sb.append("title=\"")
Nils Diewaldbb33da22015-03-04 16:24:25 +000069 .append(match.getAnnotationID(this.number))
70 .append('"');
Nils Diewalddcd5ab12015-02-20 02:59:09 +000071 }
72 else {
73 Relation rel = match.getRelationID(this.number);
Nils Diewaldbb33da22015-03-04 16:24:25 +000074 sb.append("xlink:title=\"").append(rel.annotation)
75 .append("\" xlink:type=\"simple\" xlink:href=\"#")
76 .append(match.getPosID(rel.ref)).append('"');
Nils Diewalddcd5ab12015-02-20 02:59:09 +000077 };
78 sb.append('>');
79 }
Nils Diewald52bd1cd2014-11-06 20:44:24 +000080
Nils Diewalddcd5ab12015-02-20 02:59:09 +000081 // 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 Diewaldbb33da22015-03-04 16:24:25 +000093 sb.append("<mark class=\"class-").append(this.number)
94 .append(" level-").append(pos).append("\">");
Nils Diewalddcd5ab12015-02-20 02:59:09 +000095 };
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 Diewaldbb33da22015-03-04 16:24:25 +0000102
Nils Diewalddcd5ab12015-02-20 02:59:09 +0000103 if (this.number == -1)
104 return "</mark>";
Nils Diewaldbb33da22015-03-04 16:24:25 +0000105
Nils Diewalddcd5ab12015-02-20 02:59:09 +0000106 if (this.terminal)
107 level.set((int) levelCache[this.number]);
108 return "</mark>";
Nils Diewaldbb33da22015-03-04 16:24:25 +0000109 };
Nils Diewald79f6c4d2014-09-17 17:34:01 +0000110
Nils Diewaldbb33da22015-03-04 16:24:25 +0000111 // HTML encode primary data
112 return escapeHTML(this.characters);
Nils Diewald79f6c4d2014-09-17 17:34:01 +0000113 };
114
Nils Diewaldbb33da22015-03-04 16:24:25 +0000115
Nils Diewald79f6c4d2014-09-17 17:34:01 +0000116 // Return bracket fragment for this combinator element
Nils Diewald392bcf32015-02-26 20:01:17 +0000117 public String toBrackets (Match match) {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000118 if (this.type == 1) {
119 StringBuilder sb = new StringBuilder();
Nils Diewald79f6c4d2014-09-17 17:34:01 +0000120
Nils Diewaldbb33da22015-03-04 16:24:25 +0000121 // Match
122 if (this.number == -1) {
123 sb.append("[");
124 }
Nils Diewald79f6c4d2014-09-17 17:34:01 +0000125
Nils Diewaldbb33da22015-03-04 16:24:25 +0000126 // 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 Diewald79f6c4d2014-09-17 17:34:01 +0000157 };
158};