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
;
45 public TmfFilterMatchesNode(ITmfFilterTreeNode parent
) {
50 * @return the NOT state
52 public boolean isNot() {
60 public void setNot(boolean not
) {
65 * @return the field name
67 public String
getField() {
75 public void setField(String field
) {
80 * @return the regular expression
82 public String
getRegex() {
88 * the regular expression
90 public void setRegex(String regex
) {
93 this.fPattern
= Pattern
.compile(regex
);
94 } catch (PatternSyntaxException e
) {
100 public String
getNodeName() {
105 public boolean matches(ITmfEvent event
) {
106 if (fPattern
== null) {
110 Object value
= getFieldValue(event
, fField
);
114 String valueString
= value
.toString();
116 return fPattern
.matcher(valueString
).matches() ^ fNot
;
120 public List
<String
> getValidChildren() {
121 return new ArrayList
<String
>(0);
125 public String
toString() {
126 return fField
+ (fNot ?
" not" : "") + " matches \"" + fRegex
+ "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
130 public ITmfFilterTreeNode
clone() {
131 TmfFilterMatchesNode clone
= (TmfFilterMatchesNode
) super.clone();
132 clone
.fField
= fField
;
133 clone
.setRegex(fRegex
);
139 * the rough regex pattern
140 * @return the compliant regex
142 public static String
regexFix(String pattern
) {
143 String ret
= pattern
;
144 // if the pattern does not contain one of the expressions .* !^
145 // (at the beginning) $ (at the end), then a .* is added at the
146 // beginning and at the end of the pattern
147 if (!(ret
.indexOf(".*") >= 0 || ret
.charAt(0) == '^' || ret
.charAt(ret
.length() - 1) == '$')) { //$NON-NLS-1$
148 ret
= ".*" + ret
+ ".*"; //$NON-NLS-1$ //$NON-NLS-2$