2929 * ({@link #lastDocId} + 1). The caller is responsible for turning that into a
3030 * full URL.</p>
3131 */
32- public class HtmlResults extends SpanListener
32+ public class HtmlResults implements SpanListener
3333{
3434 private final Writer writer ;
3535 private final StoredFields storedFields ;
@@ -116,6 +116,25 @@ public String doclineFieldName()
116116 return this .doclineFieldName ;
117117 }
118118
119+ @ Override
120+ public void endDoc (final int spanTotal ) throws IOException
121+ {
122+ if (spanCount > 0 ) {
123+ writer .append ("</ol>\n " );
124+ }
125+ if (spanTotal == 0 ); //?
126+ else if (spanTotal == spanCount );
127+ else {
128+ writer .append ("<div class=\" span-count\" >" )
129+ .append (String .valueOf (spanCount ))
130+ .append (" / " )
131+ .append (String .valueOf (spanTotal ))
132+ .append ("</div>" );
133+ }
134+ writer .append ("</article>\n \n " );
135+ writer .flush ();
136+ }
137+
119138 public String hrefBase ()
120139 {
121140 return this .hrefBase ;
@@ -149,53 +168,16 @@ public HtmlResults hrefSearch(final String hrefSearch)
149168 return this ;
150169 }
151170
152- /** Sets the maximum number of spans emitted per document; {@code -1} = unlimited. */
153- public HtmlResults spanLimit (final int spanLimit )
154- {
155- this .spanLimit = spanLimit ;
156- return this ;
157- }
158-
159171 public int spanLimit ()
160172 {
161173 return this .spanLimit ;
162174 }
163175
164- @ Override
165- public boolean wantsMoreDocs ()
166- {
167- // docLimit < 0 means unlimited; otherwise stop once the limit is reached
168- return docLimit < 0 || docCount < docLimit ;
169- }
170-
171- @ Override
172- public void startDoc (int docId ) throws IOException
176+ /** Sets the maximum number of spans emitted per document; {@code -1} = unlimited. */
177+ public HtmlResults spanLimit (final int spanLimit )
173178 {
174- spanCount = 0 ;
175- docCount ++;
176- lastDocId = docId ;
177- doc = storedFields .document (docId );
178- content = doc .get (contentFieldName );
179- id = doc .get (ALIX_ID );
180-
181- writer .append ("<article id=\" " ).append (id )
182- .append ("\" data-docid=\" " ).append (String .valueOf (docId ))
183- .append ("\" class=\" hit\" >\n " );
184-
185- if (doclineFieldName != null ) {
186- final String docline = doc .get (doclineFieldName );
187- if (docline != null ) {
188- writer
189- .append ("<h2><a href=\" " )
190- .append (hrefBase )
191- .append (id )
192- .append (hrefExt )
193- .append (hrefSearch )
194- .append ("\" >" )
195- .append (docline )
196- .append ("</a></h2>\n " );
197- }
198- }
179+ this .spanLimit = spanLimit ;
180+ return this ;
199181 }
200182
201183 @ Override
@@ -204,10 +186,10 @@ public boolean span(OffsetsCollector collector) throws IOException
204186 if (spanLimit == 0 ) return false ;
205187 if (spanCount == 0 ) writer .append ("<ol class=\" hit spans\" >\n " );
206188 spanCount ++;
207-
189+
208190 final int termCount = collector .size ();
209191 if (termCount < 1 ) return true ;
210-
192+
211193 // Opening tag written first: no buffer needed, no prepend.
212194 writer .append ("<li class=\" hit span\" ><a href=\" " )
213195 .append (hrefBase )
@@ -217,11 +199,11 @@ public boolean span(OffsetsCollector collector) throws IOException
217199 .append ("#span" )
218200 .append (String .valueOf (collector .ord () + 1 ))
219201 .append ("\" >" );
220-
202+
221203 // Left context: locate boundary, then detag forward directly into writer.
222204 final int left = Markup .leftBoundary (content , collector .startOffset (0 ) - 1 , ctx , -1 );
223205 detagger .detag (content , left , collector .startOffset (0 ), writer );
224-
206+
225207 // Pivot terms with inter-term text between them.
226208 writer .append ("<mark class=\" hit pivot\" >" );
227209 writer .append (content , collector .startOffset (0 ), collector .endOffset (0 ));
@@ -232,7 +214,7 @@ public boolean span(OffsetsCollector collector) throws IOException
232214 writer .append (content , collector .startOffset (termOrd ), collector .endOffset (termOrd ));
233215 writer .append ("</mark>" );
234216 }
235-
217+
236218 // Right context.
237219 final int right = Markup .rightBoundary (content , collector .endOffset (termCount - 1 ), ctx , -1 );
238220 detagger .detag (content , collector .endOffset (termCount - 1 ), right , writer );
@@ -241,22 +223,40 @@ public boolean span(OffsetsCollector collector) throws IOException
241223 }
242224
243225 @ Override
244- public void endDoc ( final int spanTotal ) throws IOException
226+ public void startDoc ( int docId ) throws IOException
245227 {
246- if (spanCount > 0 ) {
247- writer .append ("</ol>\n " );
248- }
249- if (spanTotal == 0 ); //?
250- else if (spanTotal == spanCount );
251- else {
252- writer .append ("<div class=\" span-count\" >" )
253- .append (String .valueOf (spanCount ))
254- .append (" / " )
255- .append (String .valueOf (spanTotal ))
256- .append ("</div>" );
228+ spanCount = 0 ;
229+ docCount ++;
230+ lastDocId = docId ;
231+ doc = storedFields .document (docId );
232+ content = doc .get (contentFieldName );
233+ id = doc .get (ALIX_ID );
234+
235+ writer .append ("<article id=\" " ).append (id )
236+ .append ("\" data-docid=\" " ).append (String .valueOf (docId ))
237+ .append ("\" class=\" hit\" >\n " );
238+
239+ if (doclineFieldName != null ) {
240+ final String docline = doc .get (doclineFieldName );
241+ if (docline != null ) {
242+ writer
243+ .append ("<h2><a href=\" " )
244+ .append (hrefBase )
245+ .append (id )
246+ .append (hrefExt )
247+ .append (hrefSearch )
248+ .append ("\" >" )
249+ .append (docline )
250+ .append ("</a></h2>\n " );
251+ }
257252 }
258- writer .append ("</article>\n \n " );
259- writer .flush ();
253+ }
254+
255+ @ Override
256+ public boolean wantsMoreDocs ()
257+ {
258+ // docLimit < 0 means unlimited; otherwise stop once the limit is reached
259+ return docLimit < 0 || docCount < docLimit ;
260260 }
261261
262262
0 commit comments