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