xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/search/TermEscaperBase.java (revision 5d9f3aa0ca3da3a714233f987fa732f62c0965f6)
1*a5cf78b2SChris Fraire /*
2*a5cf78b2SChris Fraire  * CDDL HEADER START
3*a5cf78b2SChris Fraire  *
4*a5cf78b2SChris Fraire  * The contents of this file are subject to the terms of the
5*a5cf78b2SChris Fraire  * Common Development and Distribution License (the "License").
6*a5cf78b2SChris Fraire  * You may not use this file except in compliance with the License.
7*a5cf78b2SChris Fraire  *
8*a5cf78b2SChris Fraire  * See LICENSE.txt included in this distribution for the specific
9*a5cf78b2SChris Fraire  * language governing permissions and limitations under the License.
10*a5cf78b2SChris Fraire  *
11*a5cf78b2SChris Fraire  * When distributing Covered Code, include this CDDL HEADER in each
12*a5cf78b2SChris Fraire  * file and include the License file at LICENSE.txt.
13*a5cf78b2SChris Fraire  * If applicable, add the following below this CDDL HEADER, with the
14*a5cf78b2SChris Fraire  * fields enclosed by brackets "[]" replaced with your own identifying
15*a5cf78b2SChris Fraire  * information: Portions Copyright [yyyy] [name of copyright owner]
16*a5cf78b2SChris Fraire  *
17*a5cf78b2SChris Fraire  * CDDL HEADER END
18*a5cf78b2SChris Fraire  */
19*a5cf78b2SChris Fraire 
20*a5cf78b2SChris Fraire /*
21*a5cf78b2SChris Fraire  * Copyright (c) 2018, Chris Fraire <cfraire@me.com>.
22*a5cf78b2SChris Fraire  */
23*a5cf78b2SChris Fraire package org.opengrok.indexer.search;
24*a5cf78b2SChris Fraire 
25*a5cf78b2SChris Fraire import java.io.IOException;
26*a5cf78b2SChris Fraire 
27*a5cf78b2SChris Fraire /**
28*a5cf78b2SChris Fraire  * Represents an abstract base class for OpenGrok query-building term-
29*a5cf78b2SChris Fraire  * transformers.
30*a5cf78b2SChris Fraire  */
31*a5cf78b2SChris Fraire abstract class TermEscaperBase {
32*a5cf78b2SChris Fraire 
33*a5cf78b2SChris Fraire     private StringBuilder out;
34*a5cf78b2SChris Fraire 
35*a5cf78b2SChris Fraire     /**
36*a5cf78b2SChris Fraire      * "Runs the scanner [as documented by JFlex].
37*a5cf78b2SChris Fraire      * <p>[The method] can be used to get the next token from the input."
38*a5cf78b2SChris Fraire      * <p>"Consume[s] input until one of the expressions in the specification
39*a5cf78b2SChris Fraire      * is matched or an error occurs."
40*a5cf78b2SChris Fraire      * @return a value returned by the lexer specification if defined or the
41*a5cf78b2SChris Fraire      * {@code EOF} value upon reading end-of-file
42*a5cf78b2SChris Fraire      * @throws IOException if an error occurs reading the input
43*a5cf78b2SChris Fraire      */
yylex()44*a5cf78b2SChris Fraire     abstract boolean yylex() throws IOException;
45*a5cf78b2SChris Fraire 
46*a5cf78b2SChris Fraire     /**
47*a5cf78b2SChris Fraire      * @param out the target to append
48*a5cf78b2SChris Fraire      */
setOut(StringBuilder out)49*a5cf78b2SChris Fraire     void setOut(StringBuilder out) {
50*a5cf78b2SChris Fraire         this.out = out;
51*a5cf78b2SChris Fraire     }
52*a5cf78b2SChris Fraire 
appendOut(char c)53*a5cf78b2SChris Fraire     void appendOut(char c) {
54*a5cf78b2SChris Fraire         out.append(c);
55*a5cf78b2SChris Fraire     }
56*a5cf78b2SChris Fraire 
appendOut(String s)57*a5cf78b2SChris Fraire     void appendOut(String s) {
58*a5cf78b2SChris Fraire         out.append(s);
59*a5cf78b2SChris Fraire     }
60*a5cf78b2SChris Fraire 
61*a5cf78b2SChris Fraire     /**
62*a5cf78b2SChris Fraire      * Call {@link #yylex()} until {@code false}, which consumes all input so
63*a5cf78b2SChris Fraire      * that the argument to {@link #setOut(StringBuilder)} contains the entire
64*a5cf78b2SChris Fraire      * transformation.
65*a5cf78b2SChris Fraire      */
consume()66*a5cf78b2SChris Fraire     void consume() {
67*a5cf78b2SChris Fraire         try {
68*a5cf78b2SChris Fraire             while (yylex()) {
69*a5cf78b2SChris Fraire                 //noinspection UnnecessaryContinue
70*a5cf78b2SChris Fraire                 continue;
71*a5cf78b2SChris Fraire             }
72*a5cf78b2SChris Fraire         } catch (IOException ex) {
73*a5cf78b2SChris Fraire             // cannot get here with StringBuilder operations
74*a5cf78b2SChris Fraire         }
75*a5cf78b2SChris Fraire     }
76*a5cf78b2SChris Fraire }
77