Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fa3d590501fd24966729319a06522c6c99135012 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.search.regex;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 *
 * Singleton helper class used to find pattern queries in strings
 *
 */
public class PatternHelper {

	private static PatternHelper instance = null;

	private PatternHelper() {
		super();
	}

	/**
	 * Singleton
	 *
	 * @return the helper
	 */
	public static final PatternHelper getInstance() {

		synchronized (PatternHelper.class) {
			if (PatternHelper.instance == null) {
				PatternHelper.instance = new PatternHelper();
			}
		}

		return PatternHelper.instance;
	}

	/**
	 * Create a pattern
	 *
	 * @param query
	 *            the query text
	 * @param isCaseSensitive
	 *            if it is a case sensitive pattern
	 * @param isRegularExpression
	 *            if it i a regular expression pattern
	 * @return
	 *         the pattern corresponding to the query text and the parameters
	 * @throws PatternSyntaxException
	 */
	public Pattern createPattern(String query, boolean isCaseSensitive, boolean isRegularExpression) throws PatternSyntaxException {
		int options = Pattern.MULTILINE;

		if (!isRegularExpression) {
			//			query = ".*" + Pattern.quote(query); //$NON-NLS-1$
			query = Pattern.quote(query);
		}

		if (!isCaseSensitive) {
			options |= Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
		}

		Pattern pattern = Pattern.compile(query, options);
		return pattern;

	}

	// /**
	// * Check whether a string matches a certain pattern in a matcher
	// *
	// * @param m
	// * the matcher
	// * @param isRegularExpression
	// * specify if it is a regular expression pattern
	// * @return
	// * true if the text matches the pattern, else false
	// */
	// public boolean evaluate(Matcher m, boolean isRegularExpression) {
	//
	//
	// if(isRegularExpression) {
	// return m.matches();
	// } else {
	// return m.lookingAt();
	// }
	// }

}

Back to the top