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
.TmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfNoSuchFieldException
;
24 public class TmfFilterMatchesNode
extends TmfFilterTreeNode
{
26 public static final String NODE_NAME
= "MATCHES"; //$NON-NLS-1$
27 public static final String NOT_ATTR
= "not"; //$NON-NLS-1$
28 public static final String FIELD_ATTR
= "field"; //$NON-NLS-1$
29 public static final String REGEX_ATTR
= "regex"; //$NON-NLS-1$
31 private boolean fNot
= false;
32 private String fField
;
33 private String fRegex
;
34 private Pattern fPattern
;
36 public TmfFilterMatchesNode(ITmfFilterTreeNode parent
) {
40 public boolean isNot() {
44 public void setNot(boolean not
) {
48 public String
getField() {
52 public void setField(String field
) {
56 public String
getRegex() {
60 public void setRegex(String regex
) {
63 this.fPattern
= Pattern
.compile(regex
);
64 } catch (PatternSyntaxException e
) {
70 public String
getNodeName() {
75 public boolean matches(TmfEvent event
) {
76 if (fPattern
== null) {
80 Object value
= event
.getContent().getField(fField
);
84 String valueString
= value
.toString();
85 return fPattern
.matcher(valueString
).matches() ^ fNot
;
86 } catch (TmfNoSuchFieldException e
) {
92 public List
<String
> getValidChildren() {
93 return new ArrayList
<String
>(0);
97 public String
toString() {
98 return fField
+ (fNot ?
" not" : "") + " matches \"" + fRegex
+ "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
102 public ITmfFilterTreeNode
clone() {
103 TmfFilterMatchesNode clone
= (TmfFilterMatchesNode
) super.clone();
104 clone
.fField
= new String(fField
);
105 clone
.setRegex(new String(fRegex
));
109 public static String
regexFix(String pattern
) {
110 // if the pattern does not contain one of the expressions .* !^
111 // (at the beginning) $ (at the end), then a .* is added at the
112 // beginning and at the end of the pattern
113 if (!(pattern
.indexOf(".*") >= 0 || pattern
.charAt(0) == '^' || pattern
.charAt(pattern
.length() - 1) == '$')) { //$NON-NLS-1$
114 pattern
= ".*" + pattern
+ ".*"; //$NON-NLS-1$ //$NON-NLS-2$