Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2010, 2013 Ericsson |
be222f56 PT |
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$ | |
41b5c37f AM |
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 | |
43 | * the parent node | |
44 | */ | |
45 | public TmfFilterMatchesNode(ITmfFilterTreeNode parent) { | |
46 | super(parent); | |
47 | } | |
48 | ||
49 | /** | |
50 | * @return the NOT state | |
51 | */ | |
52 | public boolean isNot() { | |
53 | return fNot; | |
54 | } | |
55 | ||
56 | /** | |
57 | * @param not | |
58 | * the NOT state | |
59 | */ | |
60 | public void setNot(boolean not) { | |
61 | this.fNot = not; | |
62 | } | |
63 | ||
64 | /** | |
65 | * @return the field name | |
66 | */ | |
67 | public String getField() { | |
68 | return fField; | |
69 | } | |
70 | ||
71 | /** | |
72 | * @param field | |
73 | * the field name | |
74 | */ | |
75 | public void setField(String field) { | |
76 | this.fField = field; | |
77 | } | |
78 | ||
79 | /** | |
80 | * @return the regular expression | |
81 | */ | |
82 | public String getRegex() { | |
83 | return fRegex; | |
84 | } | |
85 | ||
86 | /** | |
87 | * @param regex | |
88 | * the regular expression | |
89 | */ | |
90 | public void setRegex(String regex) { | |
91 | this.fRegex = regex; | |
ef906471 XR |
92 | if (regex != null) { |
93 | try { | |
94 | this.fPattern = Pattern.compile(regex, Pattern.DOTALL); | |
95 | } catch (PatternSyntaxException e) { | |
96 | this.fPattern = null; | |
97 | } | |
41b5c37f AM |
98 | } |
99 | } | |
100 | ||
101 | @Override | |
102 | public String getNodeName() { | |
103 | return NODE_NAME; | |
104 | } | |
105 | ||
106 | @Override | |
107 | public boolean matches(ITmfEvent event) { | |
be222f56 PT |
108 | if (fPattern == null) { |
109 | return false ^ fNot; | |
110 | } | |
111 | ||
112 | Object value = getFieldValue(event, fField); | |
113 | if (value == null) { | |
114 | return false ^ fNot; | |
115 | } | |
116 | String valueString = value.toString(); | |
117 | ||
118 | return fPattern.matcher(valueString).matches() ^ fNot; | |
41b5c37f AM |
119 | } |
120 | ||
121 | @Override | |
122 | public List<String> getValidChildren() { | |
a4524c1b | 123 | return new ArrayList<>(0); |
41b5c37f AM |
124 | } |
125 | ||
126 | @Override | |
127 | public String toString() { | |
128 | return fField + (fNot ? " not" : "") + " matches \"" + fRegex + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ | |
129 | } | |
130 | ||
131 | @Override | |
132 | public ITmfFilterTreeNode clone() { | |
133 | TmfFilterMatchesNode clone = (TmfFilterMatchesNode) super.clone(); | |
134 | clone.fField = fField; | |
135 | clone.setRegex(fRegex); | |
136 | return clone; | |
137 | } | |
138 | ||
139 | /** | |
140 | * @param pattern | |
141 | * the rough regex pattern | |
142 | * @return the compliant regex | |
143 | */ | |
144 | public static String regexFix(String pattern) { | |
145 | String ret = pattern; | |
146 | // if the pattern does not contain one of the expressions .* !^ | |
147 | // (at the beginning) $ (at the end), then a .* is added at the | |
148 | // beginning and at the end of the pattern | |
149 | if (!(ret.indexOf(".*") >= 0 || ret.charAt(0) == '^' || ret.charAt(ret.length() - 1) == '$')) { //$NON-NLS-1$ | |
150 | ret = ".*" + ret + ".*"; //$NON-NLS-1$ //$NON-NLS-2$ | |
151 | } | |
152 | return ret; | |
153 | } | |
be222f56 | 154 | } |