Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / filter / model / TmfFilterMatchesNode.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.filter.model;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.regex.Pattern;
18 import java.util.regex.PatternSyntaxException;
19
20 import org.eclipse.linuxtools.tmf.event.TmfEvent;
21 import org.eclipse.linuxtools.tmf.event.TmfNoSuchFieldException;
22
23
24 public class TmfFilterMatchesNode extends TmfFilterTreeNode {
25
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$
30
31 private boolean fNot = false;
32 private String fField;
33 private String fRegex;
34 private Pattern fPattern;
35
36 public TmfFilterMatchesNode(ITmfFilterTreeNode parent) {
37 super(parent);
38 }
39
40 public boolean isNot() {
41 return fNot;
42 }
43
44 public void setNot(boolean not) {
45 this.fNot = not;
46 }
47
48 public String getField() {
49 return fField;
50 }
51
52 public void setField(String field) {
53 this.fField = field;
54 }
55
56 public String getRegex() {
57 return fRegex;
58 }
59
60 public void setRegex(String regex) {
61 this.fRegex = regex;
62 try {
63 this.fPattern = Pattern.compile(regex);
64 } catch (PatternSyntaxException e) {
65 this.fPattern = null;
66 }
67 }
68
69 @Override
70 public String getNodeName() {
71 return NODE_NAME;
72 }
73
74 @Override
75 public boolean matches(TmfEvent event) {
76 if (fPattern == null) {
77 return false ^ fNot;
78 }
79 try {
80 Object value = event.getContent().getField(fField);
81 if (value == null) {
82 return false ^ fNot;
83 }
84 String valueString = value.toString();
85 return fPattern.matcher(valueString).matches() ^ fNot;
86 } catch (TmfNoSuchFieldException e) {
87 return false ^ fNot;
88 }
89 }
90
91 @Override
92 public List<String> getValidChildren() {
93 return new ArrayList<String>(0);
94 }
95
96 @Override
97 public String toString() {
98 return fField + (fNot ? " not" : "") + " matches \"" + fRegex + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
99 }
100
101 @Override
102 public ITmfFilterTreeNode clone() {
103 TmfFilterMatchesNode clone = (TmfFilterMatchesNode) super.clone();
104 clone.fField = new String(fField);
105 clone.setRegex(new String(fRegex));
106 return clone;
107 }
108
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$
115 }
116 return pattern;
117 }
118 }
This page took 0.036207 seconds and 5 git commands to generate.