0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
407N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
1138N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A */
1138N/A
0N/Apackage org.opensolaris.opengrok.search.context;
1138N/A
1138N/Aimport java.util.Locale;
1138N/A
0N/A/**
0N/A * Base class for matching a line against terms
0N/A *
0N/A */
0N/Apublic abstract class LineMatcher {
0N/A public static String tagBegin, tagEnd;
0N/A public static final int NOT_MATCHED = 0;
0N/A public static final int MATCHED = 1;
1190N/A public static final int WAIT = 2;
1138N/A
1138N/A /**
1138N/A * Tells whether the matching should be done in a case insensitive manner.
1138N/A */
1138N/A private final boolean caseInsensitive;
1138N/A
1138N/A /**
1138N/A * Create a {@code LineMatcher} instance.
1138N/A *
1138N/A * @param caseInsensitive if {@code true}, matching should not consider
1138N/A * case differences significant
1138N/A */
1138N/A LineMatcher(boolean caseInsensitive) {
1138N/A this.caseInsensitive = caseInsensitive;
1138N/A }
1138N/A
1138N/A /**
1138N/A * Check if two strings are equal. If this is a case insensitive matcher,
1138N/A * the check will return true if the only difference between the strings
1138N/A * is difference in case.
1138N/A */
1138N/A boolean equal(String s1, String s2) {
1138N/A return compareStrings(s1, s2) == 0;
1138N/A }
1138N/A
1138N/A /**
1138N/A * Compare two strings and return -1, 0 or 1 if the first string is
1138N/A * lexicographically smaller than, equal to or greater than the second
1138N/A * string. If this is a case insensitive matcher, case differences will
1138N/A * be ignored.
1138N/A */
1138N/A int compareStrings(String s1, String s2) {
1138N/A if (s1 == null) {
1138N/A return s2 == null ? 0 : -1;
1138N/A } else if (caseInsensitive) {
1138N/A return s1.compareToIgnoreCase(s2);
1138N/A } else {
1138N/A return s1.compareTo(s2);
1138N/A }
1138N/A }
1138N/A
1138N/A /**
1138N/A * Normalize a string token for comparison with other string tokens. That
1138N/A * is, convert to lower case if this is a case insensitive matcher.
1138N/A * Otherwise, return the string itself.
1138N/A */
1138N/A String normalizeString(String s) {
1138N/A if (s == null) {
1138N/A return null;
1138N/A } else if (caseInsensitive) {
1138N/A return s.toLowerCase(Locale.getDefault());
1138N/A } else {
1138N/A return s;
1138N/A }
1138N/A }
1138N/A
0N/A public abstract int match(String line);
0N/A}