8d2c35c87597b8badc53cbd0f8b284f2e96fbe15
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / 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.core.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.core.event.ITmfEvent;
21
22 /**
23 * Filter node for the regex match
24 *
25 * @version 1.0
26 * @author Patrick Tasse
27 */
28 @SuppressWarnings("javadoc")
29 public class TmfFilterMatchesNode extends TmfFilterTreeNode {
30
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$
35
36 private boolean fNot = false;
37 private String fField;
38 private String fRegex;
39 private Pattern fPattern;
40
41 /**
42 * @param parent the parent node
43 */
44 public TmfFilterMatchesNode(ITmfFilterTreeNode parent) {
45 super(parent);
46 }
47
48 /**
49 * @return the NOT state
50 */
51 public boolean isNot() {
52 return fNot;
53 }
54
55 /**
56 * @param not the NOT state
57 */
58 public void setNot(boolean not) {
59 this.fNot = not;
60 }
61
62 /**
63 * @return the field name
64 */
65 public String getField() {
66 return fField;
67 }
68
69 /**
70 * @param field the field name
71 */
72 public void setField(String field) {
73 this.fField = field;
74 }
75
76 /**
77 * @return the regular expression
78 */
79 public String getRegex() {
80 return fRegex;
81 }
82
83 /**
84 * @param regex the regular expression
85 */
86 public void setRegex(String regex) {
87 this.fRegex = regex;
88 try {
89 this.fPattern = Pattern.compile(regex);
90 } catch (PatternSyntaxException e) {
91 this.fPattern = null;
92 }
93 }
94
95 @Override
96 public String getNodeName() {
97 return NODE_NAME;
98 }
99
100 @Override
101 public boolean matches(ITmfEvent event) {
102 if (fPattern == null) {
103 return false ^ fNot;
104 }
105
106 Object value = getFieldValue(event, fField);
107 if (value == null) {
108 return false ^ fNot;
109 }
110 String valueString = value.toString();
111
112 return fPattern.matcher(valueString).matches() ^ fNot;
113 }
114
115 @Override
116 public List<String> getValidChildren() {
117 return new ArrayList<String>(0);
118 }
119
120 @Override
121 public String toString() {
122 return fField + (fNot ? " not" : "") + " matches \"" + fRegex + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
123 }
124
125 @Override
126 public ITmfFilterTreeNode clone() {
127 TmfFilterMatchesNode clone = (TmfFilterMatchesNode) super.clone();
128 clone.fField = fField;
129 clone.setRegex(fRegex);
130 return clone;
131 }
132
133 /**
134 * @param pattern the rough regex pattern
135 * @return the compliant regex
136 */
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$
143 }
144 return pattern;
145 }
146 }
This page took 0.041058 seconds and 5 git commands to generate.