Commit | Line | Data |
---|---|---|
be222f56 PT |
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$ | |
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; | |
92 | try { | |
93 | this.fPattern = Pattern.compile(regex); | |
94 | } catch (PatternSyntaxException e) { | |
95 | this.fPattern = null; | |
96 | } | |
97 | } | |
98 | ||
99 | @Override | |
100 | public String getNodeName() { | |
101 | return NODE_NAME; | |
102 | } | |
103 | ||
104 | @Override | |
105 | public boolean matches(ITmfEvent event) { | |
be222f56 PT |
106 | if (fPattern == null) { |
107 | return false ^ fNot; | |
108 | } | |
109 | ||
110 | Object value = getFieldValue(event, fField); | |
111 | if (value == null) { | |
112 | return false ^ fNot; | |
113 | } | |
114 | String valueString = value.toString(); | |
115 | ||
116 | return fPattern.matcher(valueString).matches() ^ fNot; | |
41b5c37f AM |
117 | } |
118 | ||
119 | @Override | |
120 | public List<String> getValidChildren() { | |
121 | return new ArrayList<String>(0); | |
122 | } | |
123 | ||
124 | @Override | |
125 | public String toString() { | |
126 | return fField + (fNot ? " not" : "") + " matches \"" + fRegex + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ | |
127 | } | |
128 | ||
129 | @Override | |
130 | public ITmfFilterTreeNode clone() { | |
131 | TmfFilterMatchesNode clone = (TmfFilterMatchesNode) super.clone(); | |
132 | clone.fField = fField; | |
133 | clone.setRegex(fRegex); | |
134 | return clone; | |
135 | } | |
136 | ||
137 | /** | |
138 | * @param pattern | |
139 | * the rough regex pattern | |
140 | * @return the compliant regex | |
141 | */ | |
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$ | |
149 | } | |
150 | return ret; | |
151 | } | |
be222f56 | 152 | } |