1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
17 import java
.util
.regex
.Pattern
;
18 import java
.util
.regex
.PatternSyntaxException
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 * Filter node for the regex match
26 * @author Patrick Tasse
28 @SuppressWarnings("javadoc")
29 public class TmfFilterMatchesNode
extends TmfFilterTreeNode
{
31 public static final String NODE_NAME
= "MATCHES"; //$NON-NLS-1$
32 public static final String NOT_ATTR
= "not"; //$NON-NLS-1$
33 public static final String FIELD_ATTR
= "field"; //$NON-NLS-1$
34 public static final String REGEX_ATTR
= "regex"; //$NON-NLS-1$
36 private boolean fNot
= false;
37 private String fField
;
38 private String fRegex
;
39 private Pattern fPattern
;
42 * @param parent the parent node
44 public TmfFilterMatchesNode(ITmfFilterTreeNode parent
) {
49 * @return the NOT state
51 public boolean isNot() {
56 * @param not the NOT state
58 public void setNot(boolean not
) {
63 * @return the field name
65 public String
getField() {
70 * @param field the field name
72 public void setField(String field
) {
77 * @return the regular expression
79 public String
getRegex() {
84 * @param regex the regular expression
86 public void setRegex(String regex
) {
89 this.fPattern
= Pattern
.compile(regex
);
90 } catch (PatternSyntaxException e
) {
96 public String
getNodeName() {
101 public boolean matches(ITmfEvent event
) {
102 if (fPattern
== null) {
106 Object value
= getFieldValue(event
, fField
);
110 String valueString
= value
.toString();
112 return fPattern
.matcher(valueString
).matches() ^ fNot
;
116 public List
<String
> getValidChildren() {
117 return new ArrayList
<String
>(0);
121 public String
toString() {
122 return fField
+ (fNot ?
" not" : "") + " matches \"" + fRegex
+ "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
126 public ITmfFilterTreeNode
clone() {
127 TmfFilterMatchesNode clone
= (TmfFilterMatchesNode
) super.clone();
128 clone
.fField
= fField
;
129 clone
.setRegex(fRegex
);
134 * @param pattern the rough regex pattern
135 * @return the compliant regex
137 public static String
regexFix(String pattern
) {
138 // if the pattern does not contain one of the expressions .* !^
139 // (at the beginning) $ (at the end), then a .* is added at the
140 // beginning and at the end of the pattern
141 if (!(pattern
.indexOf(".*") >= 0 || pattern
.charAt(0) == '^' || pattern
.charAt(pattern
.length() - 1) == '$')) { //$NON-NLS-1$
142 pattern
= ".*" + pattern
+ ".*"; //$NON-NLS-1$ //$NON-NLS-2$