tmf: bug 494698 Add per-event fields to custom parsers
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / parsers / wizards / CustomTxtParserInputWizardPage.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 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.tracecompass.internal.tmf.ui.parsers.wizards;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.net.URL;
21 import java.text.ParseException;
22 import java.util.AbstractMap.SimpleEntry;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Scanner;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 import java.util.regex.PatternSyntaxException;
34
35 import org.eclipse.core.resources.IFile;
36 import org.eclipse.core.runtime.CoreException;
37 import org.eclipse.core.runtime.FileLocator;
38 import org.eclipse.core.runtime.IPath;
39 import org.eclipse.core.runtime.Path;
40 import org.eclipse.core.runtime.Platform;
41 import org.eclipse.jface.viewers.ColumnLabelProvider;
42 import org.eclipse.jface.viewers.ISelection;
43 import org.eclipse.jface.viewers.ISelectionChangedListener;
44 import org.eclipse.jface.viewers.IStructuredSelection;
45 import org.eclipse.jface.viewers.ITreeContentProvider;
46 import org.eclipse.jface.viewers.SelectionChangedEvent;
47 import org.eclipse.jface.viewers.StructuredSelection;
48 import org.eclipse.jface.viewers.TreeViewer;
49 import org.eclipse.jface.viewers.Viewer;
50 import org.eclipse.jface.wizard.WizardPage;
51 import org.eclipse.osgi.util.NLS;
52 import org.eclipse.swt.SWT;
53 import org.eclipse.swt.browser.Browser;
54 import org.eclipse.swt.browser.TitleEvent;
55 import org.eclipse.swt.browser.TitleListener;
56 import org.eclipse.swt.custom.SashForm;
57 import org.eclipse.swt.custom.ScrolledComposite;
58 import org.eclipse.swt.custom.StyleRange;
59 import org.eclipse.swt.custom.StyledText;
60 import org.eclipse.swt.events.ModifyEvent;
61 import org.eclipse.swt.events.ModifyListener;
62 import org.eclipse.swt.events.SelectionAdapter;
63 import org.eclipse.swt.events.SelectionEvent;
64 import org.eclipse.swt.events.SelectionListener;
65 import org.eclipse.swt.events.VerifyEvent;
66 import org.eclipse.swt.events.VerifyListener;
67 import org.eclipse.swt.graphics.Color;
68 import org.eclipse.swt.graphics.Font;
69 import org.eclipse.swt.graphics.FontData;
70 import org.eclipse.swt.graphics.Image;
71 import org.eclipse.swt.graphics.Point;
72 import org.eclipse.swt.graphics.Rectangle;
73 import org.eclipse.swt.layout.FillLayout;
74 import org.eclipse.swt.layout.GridData;
75 import org.eclipse.swt.layout.GridLayout;
76 import org.eclipse.swt.widgets.Button;
77 import org.eclipse.swt.widgets.Combo;
78 import org.eclipse.swt.widgets.Composite;
79 import org.eclipse.swt.widgets.Display;
80 import org.eclipse.swt.widgets.Group;
81 import org.eclipse.swt.widgets.Label;
82 import org.eclipse.swt.widgets.Shell;
83 import org.eclipse.swt.widgets.Text;
84 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
85 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
86 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition;
87 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag;
88 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
89 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.Cardinality;
90 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputData;
91 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputLine;
92 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
93 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
94 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
95 import org.osgi.framework.Bundle;
96
97 /**
98 * Input wizard page for custom text parsers.
99 *
100 * @author Patrick Tasse
101 */
102 public class CustomTxtParserInputWizardPage extends WizardPage {
103
104 private static final String DEFAULT_REGEX = "\\s*(.*\\S)"; //$NON-NLS-1$
105 private static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
106 private static final String TIMESTAMP_FORMAT_BUNDLE = "org.eclipse.tracecompass.doc.user"; //$NON-NLS-1$
107 private static final String TIMESTAMP_FORMAT_PATH = "reference/api/org/eclipse/tracecompass/tmf/core/timestamp/TmfTimestampFormat.html"; //$NON-NLS-1$
108 private static final String PATTERN_URL = "http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum"; //$NON-NLS-1$
109 private static final Image LINE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/line_icon.gif"); //$NON-NLS-1$
110 private static final Image ADD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
111 private static final Image ADD_NEXT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addnext_button.gif"); //$NON-NLS-1$
112 private static final Image ADD_CHILD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addchild_button.gif"); //$NON-NLS-1$
113 private static final Image DELETE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
114 private static final Image MOVE_UP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
115 private static final Image MOVE_DOWN_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
116 private static final Image HELP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/help_button.gif"); //$NON-NLS-1$
117 private static final Color COLOR_BLACK = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
118 private static final Color COLOR_LIGHT_GREEN = new Color(Display.getDefault(), 192, 255, 192);
119 private static final Color COLOR_GREEN = Display.getDefault().getSystemColor(SWT.COLOR_GREEN);
120 private static final Color COLOR_LIGHT_YELLOW = new Color(Display.getDefault(), 255, 255, 192);
121 private static final Color COLOR_YELLOW = Display.getDefault().getSystemColor(SWT.COLOR_YELLOW);
122 private static final Color COLOR_LIGHT_MAGENTA = new Color(Display.getDefault(), 255, 192, 255);
123 private static final Color COLOR_MAGENTA = Display.getDefault().getSystemColor(SWT.COLOR_MAGENTA);
124 private static final Color COLOR_LIGHT_RED = new Color(Display.getDefault(), 255, 192, 192);
125 private static final Color COLOR_TEXT_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
126 private static final Color COLOR_WIDGET_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
127 private static final Color COLOR_GRAY = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
128
129 private final ISelection selection;
130 private CustomTxtTraceDefinition definition;
131 private String editCategoryName;
132 private String editDefinitionName;
133 private String defaultDescription;
134 private Line selectedLine;
135 private Composite container;
136 private Text categoryText;
137 private Text logtypeText;
138 private Text timestampOutputFormatText;
139 private Text timestampPreviewText;
140 private ScrolledComposite lineScrolledComposite;
141 private TreeViewer treeViewer;
142 private Composite lineContainer;
143 private StyledText inputText;
144 private Font fixedFont;
145 private UpdateListener updateListener;
146 private Browser helpBrowser;
147
148 // variables used recursively through line traversal
149 private String timeStampFormat;
150 private boolean timestampFound;
151
152 /**
153 * Constructor
154 *
155 * @param selection
156 * The Selection object
157 * @param definition
158 * The trace definition
159 */
160 protected CustomTxtParserInputWizardPage(ISelection selection,
161 CustomTxtTraceDefinition definition) {
162 super("CustomParserWizardPage"); //$NON-NLS-1$
163 if (definition == null) {
164 setTitle(Messages.CustomTxtParserInputWizardPage_titleNew);
165 defaultDescription = Messages.CustomTxtParserInputWizardPage_descriptionNew;
166 } else {
167 setTitle(Messages.CustomTxtParserInputWizardPage_titleEdit);
168 defaultDescription = Messages.CustomTxtParserInputWizardPage_desccriptionEdit;
169 }
170 setDescription(defaultDescription);
171 this.selection = selection;
172 this.definition = definition;
173 if (definition != null) {
174 this.editCategoryName = definition.categoryName;
175 this.editDefinitionName = definition.definitionName;
176 }
177 }
178
179 @Override
180 public void createControl(Composite parent) {
181 container = new Composite(parent, SWT.NULL);
182 container.setLayout(new GridLayout());
183
184 updateListener = new UpdateListener();
185
186 Composite headerComposite = new Composite(container, SWT.FILL);
187 GridLayout headerLayout = new GridLayout(5, false);
188 headerLayout.marginHeight = 0;
189 headerLayout.marginWidth = 0;
190 headerComposite.setLayout(headerLayout);
191 headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
192
193 Label categoryLabel = new Label(headerComposite, SWT.NULL);
194 categoryLabel.setText(Messages.CustomTxtParserInputWizardPage_category);
195
196 categoryText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
197 categoryText.setLayoutData(new GridData(120, SWT.DEFAULT));
198
199 Label timestampFormatLabel = new Label(headerComposite, SWT.NULL);
200 timestampFormatLabel.setText(Messages.CustomTxtParserInputWizardPage_timestampFormat);
201
202 timestampOutputFormatText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
203 timestampOutputFormatText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
204 timestampOutputFormatText.setText(DEFAULT_TIMESTAMP_FORMAT);
205 timestampOutputFormatText.addPaintListener(e -> {
206 if (!timestampOutputFormatText.isFocusControl() && timestampOutputFormatText.getText().trim().isEmpty()) {
207 e.gc.setForeground(COLOR_GRAY);
208 int borderWidth = timestampOutputFormatText.getBorderWidth();
209 e.gc.drawText(Messages.CustomTxtParserInputWizardPage_default, borderWidth, borderWidth);
210 }
211 });
212
213 Button timeStampFormatHelpButton = new Button(headerComposite, SWT.PUSH);
214 timeStampFormatHelpButton.setImage(HELP_IMAGE);
215 timeStampFormatHelpButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_timestampFormatHelp);
216 timeStampFormatHelpButton.addSelectionListener(new SelectionAdapter() {
217 @Override
218 public void widgetSelected(SelectionEvent e) {
219 Bundle plugin = Platform.getBundle(TIMESTAMP_FORMAT_BUNDLE);
220 IPath path = new Path(TIMESTAMP_FORMAT_PATH);
221 URL fileURL = FileLocator.find(plugin, path, null);
222 try {
223 URL pageURL = FileLocator.toFileURL(fileURL);
224 openHelpShell(pageURL.toString());
225 } catch (IOException e1) {
226 }
227 }
228 });
229
230 Label logtypeLabel = new Label(headerComposite, SWT.NULL);
231 logtypeLabel.setText(Messages.CustomTxtParserInputWizardPage_logType);
232
233 logtypeText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
234 logtypeText.setLayoutData(new GridData(120, SWT.DEFAULT));
235 logtypeText.setFocus();
236
237 Label timestampPreviewLabel = new Label(headerComposite, SWT.NULL);
238 timestampPreviewLabel.setText(Messages.CustomTxtParserInputWizardPage_preview);
239
240 timestampPreviewText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
241 timestampPreviewText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
242 timestampPreviewText.setText(Messages.CustomTxtParserInputWizardPage_noMatchingTimestamp);
243
244 Composite buttonBar = new Composite(container, SWT.NONE);
245 GridLayout buttonBarLayout = new GridLayout(5, false);
246 buttonBarLayout.marginHeight = 0;
247 buttonBarLayout.marginWidth = 0;
248 buttonBar.setLayout(buttonBarLayout);
249
250 Button removeButton = new Button(buttonBar, SWT.PUSH);
251 removeButton.setImage(DELETE_IMAGE);
252 removeButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_removeLine);
253 removeButton.addSelectionListener(new SelectionAdapter() {
254 @Override
255 public void widgetSelected(SelectionEvent e) {
256 if (treeViewer.getSelection().isEmpty() || selectedLine == null) {
257 return;
258 }
259 removeLine();
260 InputLine inputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
261 if (inputLine.parentInput == null) {
262 definition.inputs.remove(inputLine);
263 } else {
264 int index = inputLine.parentInput.childrenInputs.indexOf(inputLine);
265 if (index > 0) {
266 inputLine.parentInput.childrenInputs.get(index - 1).nextInput = inputLine.nextInput;
267 }
268 inputLine.parentInput.childrenInputs.remove(inputLine);
269 }
270 treeViewer.refresh();
271 validate();
272 updatePreviews();
273 }
274 });
275 Button addNextButton = new Button(buttonBar, SWT.PUSH);
276 addNextButton.setImage(ADD_NEXT_IMAGE);
277 addNextButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_addNextLine);
278 addNextButton.addSelectionListener(new SelectionAdapter() {
279 @Override
280 public void widgetSelected(SelectionEvent e) {
281 InputLine inputLine = new InputLine(Cardinality.ZERO_OR_MORE, "", null); //$NON-NLS-1$
282 if (((List<?>) treeViewer.getInput()).size() == 0) {
283 definition.inputs.add(inputLine);
284 } else if (treeViewer.getSelection().isEmpty()) {
285 return;
286 } else {
287 InputLine previousInputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
288 if (previousInputLine.parentInput == null) {
289 for (int i = 0; i < definition.inputs.size(); i++) {
290 if (definition.inputs.get(i).equals(previousInputLine)) {
291 definition.inputs.add(i + 1, inputLine);
292 }
293 }
294 } else {
295 previousInputLine.addNext(inputLine);
296 }
297 }
298 treeViewer.refresh();
299 treeViewer.setSelection(new StructuredSelection(inputLine), true);
300 }
301 });
302 Button addChildButton = new Button(buttonBar, SWT.PUSH);
303 addChildButton.setImage(ADD_CHILD_IMAGE);
304 addChildButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_addChildLine);
305 addChildButton.addSelectionListener(new SelectionAdapter() {
306 @Override
307 public void widgetSelected(SelectionEvent e) {
308 InputLine inputLine = new InputLine(Cardinality.ZERO_OR_MORE, "", null); //$NON-NLS-1$
309 if (((List<?>) treeViewer.getInput()).size() == 0) {
310 definition.inputs.add(inputLine);
311 } else if (treeViewer.getSelection().isEmpty()) {
312 return;
313 } else {
314 InputLine parentInputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
315 parentInputLine.addChild(inputLine);
316 }
317 treeViewer.refresh();
318 treeViewer.setSelection(new StructuredSelection(inputLine), true);
319 }
320 });
321 Button moveUpButton = new Button(buttonBar, SWT.PUSH);
322 moveUpButton.setImage(MOVE_UP_IMAGE);
323 moveUpButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_moveUp);
324 moveUpButton.addSelectionListener(new SelectionAdapter() {
325 @Override
326 public void widgetSelected(SelectionEvent e) {
327 if (treeViewer.getSelection().isEmpty()) {
328 return;
329 }
330 InputLine inputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
331 if (inputLine.parentInput == null) {
332 for (int i = 1; i < definition.inputs.size(); i++) {
333 if (definition.inputs.get(i).equals(inputLine)) {
334 definition.inputs.add(i - 1, definition.inputs.remove(i));
335 break;
336 }
337 }
338 } else {
339 inputLine.moveUp();
340 }
341 treeViewer.refresh();
342 validate();
343 updatePreviews();
344 }
345 });
346 Button moveDownButton = new Button(buttonBar, SWT.PUSH);
347 moveDownButton.setImage(MOVE_DOWN_IMAGE);
348 moveDownButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_moveDown);
349 moveDownButton.addSelectionListener(new SelectionAdapter() {
350 @Override
351 public void widgetSelected(SelectionEvent e) {
352 if (treeViewer.getSelection().isEmpty()) {
353 return;
354 }
355 InputLine inputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
356 if (inputLine.parentInput == null) {
357 for (int i = 0; i < definition.inputs.size() - 1; i++) {
358 if (definition.inputs.get(i).equals(inputLine)) {
359 definition.inputs.add(i + 1, definition.inputs.remove(i));
360 break;
361 }
362 }
363 } else {
364 inputLine.moveDown();
365 }
366 treeViewer.refresh();
367 validate();
368 updatePreviews();
369 }
370 });
371
372 SashForm vSash = new SashForm(container, SWT.VERTICAL);
373 vSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
374 vSash.setBackground(COLOR_GRAY);
375
376 SashForm hSash = new SashForm(vSash, SWT.HORIZONTAL);
377 hSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
378
379 ScrolledComposite treeScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL | SWT.H_SCROLL);
380 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
381 gd.heightHint = 200;
382 gd.widthHint = 200;
383 treeScrolledComposite.setLayoutData(gd);
384 Composite treeContainer = new Composite(treeScrolledComposite, SWT.NONE);
385 treeContainer.setLayout(new FillLayout());
386 treeScrolledComposite.setContent(treeContainer);
387 treeScrolledComposite.setExpandHorizontal(true);
388 treeScrolledComposite.setExpandVertical(true);
389
390 treeViewer = new TreeViewer(treeContainer, SWT.SINGLE | SWT.BORDER);
391 treeViewer.setContentProvider(new InputLineTreeNodeContentProvider());
392 treeViewer.setLabelProvider(new InputLineTreeLabelProvider());
393 treeViewer.addSelectionChangedListener(new InputLineTreeSelectionChangedListener());
394 treeContainer.layout();
395
396 treeScrolledComposite.setMinSize(treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
397
398 lineScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL);
399 lineScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
400 lineContainer = new Composite(lineScrolledComposite, SWT.NONE);
401 GridLayout linesLayout = new GridLayout();
402 linesLayout.marginHeight = 1;
403 linesLayout.marginWidth = 0;
404 lineContainer.setLayout(linesLayout);
405 lineScrolledComposite.setContent(lineContainer);
406 lineScrolledComposite.setExpandHorizontal(true);
407 lineScrolledComposite.setExpandVertical(true);
408
409 if (definition == null) {
410 definition = new CustomTxtTraceDefinition();
411 definition.inputs.add(new InputLine(Cardinality.ZERO_OR_MORE, DEFAULT_REGEX,
412 Arrays.asList(new InputData(Tag.MESSAGE, CustomTraceDefinition.ACTION_SET))));
413 }
414 loadDefinition(definition);
415 treeViewer.expandAll();
416 lineContainer.layout();
417
418 categoryText.addModifyListener(updateListener);
419 logtypeText.addModifyListener(updateListener);
420 timestampOutputFormatText.addModifyListener(updateListener);
421
422 lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
423
424 hSash.setWeights(new int[] { 1, 2 });
425
426 Composite sashBottom = new Composite(vSash, SWT.NONE);
427 GridLayout sashBottomLayout = new GridLayout(3, false);
428 sashBottomLayout.marginHeight = 0;
429 sashBottomLayout.marginWidth = 0;
430 sashBottom.setLayout(sashBottomLayout);
431
432 Label previewLabel = new Label(sashBottom, SWT.NULL);
433 previewLabel.setText(Messages.CustomTxtParserInputWizardPage_previewInput);
434 previewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
435
436 Button highlightAllButton = new Button(sashBottom, SWT.PUSH);
437 highlightAllButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
438 highlightAllButton.setText(Messages.CustomTxtParserInputWizardPage_highlightAll);
439 highlightAllButton.addSelectionListener(new SelectionAdapter() {
440 @Override
441 public void widgetSelected(SelectionEvent e) {
442 updatePreviews(true);
443 }
444 });
445
446 Button legendButton = new Button(sashBottom, SWT.PUSH);
447 legendButton.setImage(HELP_IMAGE);
448 legendButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_previewLegend);
449 legendButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
450 legendButton.addSelectionListener(new SelectionAdapter() {
451 @Override
452 public void widgetSelected(SelectionEvent e) {
453 openLegend();
454 }
455 });
456
457 inputText = new StyledText(sashBottom, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
458 if (fixedFont == null) {
459 if (System.getProperty("os.name").contains("Windows")) { //$NON-NLS-1$ //$NON-NLS-2$
460 fixedFont = new Font(Display.getCurrent(), new FontData("Courier New", 10, SWT.NORMAL)); //$NON-NLS-1$
461 } else {
462 fixedFont = new Font(Display.getCurrent(), new FontData("Monospace", 10, SWT.NORMAL)); //$NON-NLS-1$
463 }
464 }
465 inputText.setFont(fixedFont);
466 gd = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
467 gd.heightHint = inputText.computeSize(SWT.DEFAULT, inputText.getLineHeight() * 4).y;
468 gd.widthHint = 800;
469 inputText.setLayoutData(gd);
470 inputText.setText(getSelectionText());
471 inputText.addModifyListener(updateListener);
472
473 vSash.setWeights(new int[] { hSash.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sashBottom.computeSize(SWT.DEFAULT, SWT.DEFAULT).y });
474
475 setControl(container);
476
477 validate();
478 updatePreviews();
479 }
480
481 private static class InputLineTreeNodeContentProvider implements ITreeContentProvider {
482
483 @Override
484 public Object[] getElements(Object inputElement) {
485 return ((List<?>) inputElement).toArray();
486 }
487
488 @Override
489 public Object[] getChildren(Object parentElement) {
490 InputLine inputLine = (InputLine) parentElement;
491 if (inputLine.childrenInputs == null) {
492 return new InputLine[0];
493 }
494 return inputLine.childrenInputs.toArray();
495 }
496
497 @Override
498 public boolean hasChildren(Object element) {
499 InputLine inputLine = (InputLine) element;
500 return (inputLine.childrenInputs != null && inputLine.childrenInputs.size() > 0);
501 }
502
503 @Override
504 public void dispose() {
505 }
506
507 @Override
508 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
509 }
510
511 @Override
512 public Object getParent(Object element) {
513 InputLine inputLine = (InputLine) element;
514 return inputLine.parentInput;
515 }
516 }
517
518 private class InputLineTreeLabelProvider extends ColumnLabelProvider {
519
520 @Override
521 public Image getImage(Object element) {
522 return LINE_IMAGE;
523 }
524
525 @Override
526 public String getText(Object element) {
527 InputLine inputLine = (InputLine) element;
528 if (inputLine.parentInput == null) {
529 return "Root Line " + getName(inputLine) + " " + inputLine.cardinality.toString() + " : " + inputLine.getRegex(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
530 }
531 return "Line " + getName(inputLine) + " " + inputLine.cardinality.toString() + " : " + inputLine.getRegex(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
532 }
533 }
534
535 private class InputLineTreeSelectionChangedListener implements ISelectionChangedListener {
536 @Override
537 public void selectionChanged(SelectionChangedEvent event) {
538 if (selectedLine != null) {
539 selectedLine.dispose();
540 }
541 if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
542 IStructuredSelection sel = (IStructuredSelection) event.getSelection();
543 InputLine inputLine = (InputLine) sel.getFirstElement();
544 selectedLine = new Line(lineContainer, getName(inputLine), inputLine);
545 lineContainer.layout();
546 lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
547 container.layout();
548 validate();
549 updatePreviews();
550 }
551 }
552 }
553
554 @Override
555 public void dispose() {
556 if (fixedFont != null) {
557 fixedFont.dispose();
558 fixedFont = null;
559 }
560 super.dispose();
561 }
562
563 private void loadDefinition(CustomTxtTraceDefinition def) {
564 categoryText.setText(def.categoryName);
565 logtypeText.setText(def.definitionName);
566 if (def.timeStampOutputFormat != null) {
567 timestampOutputFormatText.setText(def.timeStampOutputFormat);
568 } else {
569 timestampOutputFormatText.setText(""); //$NON-NLS-1$
570 }
571 treeViewer.setInput(def.inputs);
572 if (def.inputs.size() > 0) {
573 InputLine inputLine = def.inputs.get(0);
574 treeViewer.setSelection(new StructuredSelection(inputLine));
575 }
576 }
577
578 private String getName(InputLine inputLine) {
579 if (inputLine.parentInput == null) {
580 return Integer.toString(definition.inputs.indexOf(inputLine) + 1);
581 }
582 return getName(inputLine.parentInput) + "." + Integer.toString(inputLine.parentInput.childrenInputs.indexOf(inputLine) + 1); //$NON-NLS-1$
583 }
584
585 /**
586 * Get the global list of inputs.
587 *
588 * @return The list of inputs
589 */
590 public List<Entry<Tag, String>> getInputs() {
591 List<Entry<Tag, String>> inputs = new ArrayList<>();
592 for (InputLine inputLine : definition.inputs) {
593 for (Entry<Tag, String> input : getInputs(inputLine)) {
594 if (!inputs.contains(input)) {
595 inputs.add(input);
596 }
597 }
598 }
599 return inputs;
600 }
601
602 /**
603 * Get the list of inputs for the given input line, recursively.
604 *
605 * @param inputLine
606 * The input line
607 * @return The list of inputs
608 */
609 private List<Entry<Tag, String>> getInputs(InputLine inputLine) {
610 List<Entry<Tag, String>> inputs = new ArrayList<>();
611 if (inputLine.columns != null) {
612 for (InputData inputData : inputLine.columns) {
613 Entry<Tag, String> input = new SimpleEntry<>(inputData.tag, inputData.name);
614 if (!inputs.contains(input)) {
615 inputs.add(input);
616 }
617 }
618 }
619 if (inputLine.childrenInputs != null) {
620 for (InputLine childInputLine : inputLine.childrenInputs) {
621 for (Entry<Tag, String> input : getInputs(childInputLine)) {
622 if (!inputs.contains(input)) {
623 inputs.add(input);
624 }
625 }
626 }
627 }
628 return inputs;
629 }
630
631 private void removeLine() {
632 selectedLine.dispose();
633 selectedLine = null;
634 lineContainer.layout();
635 lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
636 container.layout();
637 }
638
639 private String getSelectionText() {
640 if (this.selection instanceof IStructuredSelection) {
641 Object sel = ((IStructuredSelection) this.selection).getFirstElement();
642 if (sel instanceof IFile) {
643 IFile file = (IFile) sel;
644 BufferedReader reader = null;
645 try {
646 reader = new BufferedReader(new InputStreamReader(file.getContents()));
647 StringBuilder sb = new StringBuilder();
648 String line = null;
649 while ((line = reader.readLine()) != null) {
650 sb.append(line);
651 sb.append('\n');
652 }
653 return sb.toString();
654 } catch (CoreException e) {
655 return ""; //$NON-NLS-1$
656 } catch (IOException e) {
657 return ""; //$NON-NLS-1$
658 } finally {
659 if (reader != null) {
660 try {
661 reader.close();
662 } catch (IOException e) {
663 }
664 }
665 }
666 }
667 }
668 return ""; //$NON-NLS-1$
669 }
670
671 private void updatePreviews() {
672 updatePreviews(false);
673 }
674
675 private void updatePreviews(boolean updateAll) {
676 if (inputText == null) {
677 // early update during construction
678 return;
679 }
680 inputText.setStyleRanges(new StyleRange[] {});
681
682 try (Scanner scanner = new Scanner(inputText.getText());) {
683 scanner.useDelimiter("\n"); //$NON-NLS-1$
684
685 int rawPos = 0;
686 // skip starting delimiters
687 String skip = scanner.findWithinHorizon("\\A\n+", 0); //$NON-NLS-1$
688 if (skip != null) {
689 rawPos += skip.length();
690 }
691
692 timeStampFormat = null;
693 if (selectedLine != null) {
694 for (InputGroup input : selectedLine.inputs) {
695 input.previewText.setText(Messages.CustomTxtParserInputWizardPage_noMathcingLine);
696 }
697 }
698
699 Map<Object, String> data = new HashMap<>();
700 int rootLineMatches = 0;
701 String firstEntryTimeStamp = null;
702 String firstEntryTimeStampInputFormat = null;
703 String line = null;
704 boolean lineIsNull = true; // needed because of JDT bug with continue at label
705 event: while (scanner.hasNext() || !lineIsNull) {
706 if (rootLineMatches > 0 && !updateAll) {
707 break;
708 }
709 if (line == null) {
710 line = scanner.next();
711 lineIsNull = false;
712 }
713 int length = line.length();
714 String log = line.replaceAll("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
715 for (InputLine rootInputLine : definition.inputs) {
716 Pattern pattern;
717 try {
718 pattern = rootInputLine.getPattern();
719 } catch (PatternSyntaxException e) {
720 continue;
721 }
722 Matcher matcher = pattern.matcher(log);
723 if (matcher.matches()) {
724 rootLineMatches++;
725 inputText.setStyleRange(new StyleRange(rawPos, length,
726 COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC));
727 data = new HashMap<>();
728 timeStampFormat = null;
729 updatePreviewLine(rootInputLine, matcher, data, rawPos, rootLineMatches);
730 if (rootLineMatches == 1) {
731 firstEntryTimeStamp = data.get(Tag.TIMESTAMP);
732 firstEntryTimeStampInputFormat = timeStampFormat;
733 }
734 HashMap<InputLine, Integer> countMap = new HashMap<>();
735 InputLine currentInput = null;
736 if (rootInputLine.childrenInputs != null && rootInputLine.childrenInputs.size() > 0) {
737 currentInput = rootInputLine.childrenInputs.get(0);
738 countMap.put(currentInput, 0);
739 }
740 rawPos += length + 1; // +1 for \n
741 while (scanner.hasNext()) {
742 line = scanner.next();
743 length = line.length();
744 log = line.replaceAll("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
745 boolean processed = false;
746 if (currentInput == null) {
747 for (InputLine input : definition.inputs) {
748 try {
749 matcher = input.getPattern().matcher(log);
750 } catch (PatternSyntaxException e) {
751 continue;
752 }
753 if (matcher.matches()) {
754 continue event;
755 }
756 }
757 } else {
758 if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMinCount()) {
759 List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
760 if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
761 for (InputLine input : definition.inputs) {
762 try {
763 matcher = input.getPattern().matcher(log);
764 } catch (PatternSyntaxException e) {
765 continue;
766 }
767 if (matcher.matches()) {
768 continue event;
769 }
770 }
771 }
772 for (InputLine input : nextInputs) {
773 try {
774 matcher = input.getPattern().matcher(log);
775 } catch (PatternSyntaxException e) {
776 continue;
777 }
778 if (matcher.matches()) {
779 inputText.setStyleRange(new StyleRange(rawPos, length,
780 COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC));
781 currentInput = input;
782 updatePreviewLine(currentInput, matcher, data, rawPos, rootLineMatches);
783 if (countMap.get(currentInput) == null) {
784 countMap.put(currentInput, 1);
785 } else {
786 countMap.put(currentInput, checkNotNull(countMap.get(currentInput)) + 1);
787 }
788 Iterator<InputLine> iter = countMap.keySet().iterator();
789 while (iter.hasNext()) {
790 InputLine inputLine = iter.next();
791 if (inputLine.level > currentInput.level) {
792 iter.remove();
793 }
794 }
795 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
796 currentInput = currentInput.childrenInputs.get(0);
797 countMap.put(currentInput, 0);
798 } else {
799 if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMaxCount()) {
800 if (currentInput.getNextInputs(countMap).size() > 0) {
801 currentInput = currentInput.getNextInputs(countMap).get(0);
802 if (countMap.get(currentInput) == null) {
803 countMap.put(currentInput, 0);
804 }
805 iter = countMap.keySet().iterator();
806 while (iter.hasNext()) {
807 InputLine inputLine = iter.next();
808 if (inputLine.level > currentInput.level) {
809 iter.remove();
810 }
811 }
812 } else {
813 currentInput = null;
814 }
815 }
816 }
817 processed = true;
818 break;
819 }
820 }
821 }
822 if (!processed && currentInput != null) {
823 matcher = null;
824 try {
825 matcher = currentInput.getPattern().matcher(log);
826 } catch (PatternSyntaxException e) {
827 }
828 if (matcher != null && matcher.matches()) {
829 inputText.setStyleRange(new StyleRange(rawPos, length,
830 COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC));
831 updatePreviewLine(currentInput, matcher, data, rawPos, rootLineMatches);
832 countMap.put(currentInput, checkNotNull(countMap.get(currentInput)) + 1);
833 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
834 currentInput = currentInput.childrenInputs.get(0);
835 countMap.put(currentInput, 0);
836 } else {
837 if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMaxCount()) {
838 if (currentInput.getNextInputs(countMap).size() > 0) {
839 currentInput = currentInput.getNextInputs(countMap).get(0);
840 if (countMap.get(currentInput) == null) {
841 countMap.put(currentInput, 0);
842 }
843 Iterator<InputLine> iter = countMap.keySet().iterator();
844 while (iter.hasNext()) {
845 InputLine inputLine = iter.next();
846 if (inputLine.level > currentInput.level) {
847 iter.remove();
848 }
849 }
850 } else {
851 currentInput = null;
852 }
853 }
854 }
855 }
856 }
857 }
858 rawPos += length + 1; // +1 for \n
859 }
860
861 break;
862 }
863 }
864 rawPos += length + 1; // +1 for \n
865 line = null;
866 lineIsNull = true;
867 }
868
869 if (rootLineMatches == 1) {
870 firstEntryTimeStamp = data.get(Tag.TIMESTAMP);
871 firstEntryTimeStampInputFormat = timeStampFormat;
872 }
873 if (firstEntryTimeStamp == null) {
874 timestampPreviewText.setText(Messages.CustomTxtParserInputWizardPage_noTimestampGroup);
875 if (selectedLine != null) {
876 for (InputGroup group : selectedLine.inputs) {
877 if (group.tagCombo.getText().equals(Tag.TIMESTAMP.toString())) {
878 timestampPreviewText.setText(Messages.CustomTxtParserInputWizardPage_noMatchingTimestamp);
879 break;
880 }
881 }
882 }
883 } else {
884 try {
885 TmfTimestampFormat timestampFormat = new TmfTimestampFormat(firstEntryTimeStampInputFormat);
886 long timestamp = timestampFormat.parseValue(firstEntryTimeStamp);
887 if (timestampOutputFormatText.getText().trim().isEmpty()) {
888 timestampFormat = new TmfTimestampFormat();
889 } else {
890 timestampFormat = new TmfTimestampFormat(timestampOutputFormatText.getText().trim());
891 }
892 timestampPreviewText.setText(timestampFormat.format(timestamp));
893 } catch (ParseException e) {
894 timestampPreviewText.setText("*parse exception* [" + firstEntryTimeStamp + "] <> [" + firstEntryTimeStampInputFormat + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
895 } catch (IllegalArgumentException e) {
896 timestampPreviewText.setText("*parse exception* [Illegal Argument: " + e.getMessage() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
897 }
898
899 }
900 }
901 }
902
903 private void updatePreviewLine(InputLine line, Matcher matcher, Map<Object, String> data, int rawPos, int rootLineMatches) {
904 for (int i = 0; i < line.columns.size(); i++) {
905 InputData input = line.columns.get(i);
906 if (i < matcher.groupCount() && matcher.group(i + 1) != null) {
907 if (line.parentInput == null) {
908 inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i + 1), matcher.end(i + 1) - matcher.start(i + 1),
909 COLOR_BLACK, COLOR_GREEN, SWT.BOLD));
910 } else {
911 inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i + 1), matcher.end(i + 1) - matcher.start(i + 1),
912 COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD));
913 }
914 String value = matcher.group(i + 1).trim();
915 if (selectedLine != null && selectedLine.inputLine.equals(line) && rootLineMatches == 1 &&
916 selectedLine.inputs.get(i).previewText.getText().equals(Messages.CustomTxtParserInputWizardPage_noMatchingLine)) {
917 selectedLine.inputs.get(i).previewText.setText(value);
918 }
919 if (value.length() == 0) {
920 continue;
921 }
922 Object key = (input.tag.equals(Tag.OTHER) ? input.name : input.tag);
923 if (input.action == CustomTraceDefinition.ACTION_SET) {
924 data.put(key, value);
925 if (input.tag.equals(Tag.TIMESTAMP)) {
926 timeStampFormat = input.format;
927 }
928 } else if (input.action == CustomTraceDefinition.ACTION_APPEND) {
929 String s = data.get(key);
930 if (s != null) {
931 data.put(key, s + value);
932 } else {
933 data.put(key, value);
934 }
935 if (input.tag.equals(Tag.TIMESTAMP)) {
936 if (timeStampFormat != null) {
937 timeStampFormat += input.format;
938 } else {
939 timeStampFormat = input.format;
940 }
941 }
942 } else if (input.action == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
943 String s = data.get(key);
944 if (s != null) {
945 data.put(key, s + CustomTraceDefinition.SEPARATOR + value);
946 } else {
947 data.put(key, value);
948 }
949 if (input.tag.equals(Tag.TIMESTAMP)) {
950 if (timeStampFormat != null) {
951 timeStampFormat += CustomTraceDefinition.SEPARATOR + input.format;
952 } else {
953 timeStampFormat = input.format;
954 }
955 }
956 }
957 } else {
958 if (selectedLine != null && selectedLine.inputLine.equals(line) && rootLineMatches == 1) {
959 if (selectedLine.inputs.get(i).previewText.getText().equals(Messages.CustomTxtParserInputWizardPage_noMatchingLine)) {
960 selectedLine.inputs.get(i).previewText.setText(Messages.CustomTxtParserInputWizardPage_noMatchingGroup);
961 }
962 }
963 }
964 }
965 // highlight the matching groups that have no corresponponding input
966 for (int i = line.columns.size(); i < matcher.groupCount(); i++) {
967 if (matcher.group(i + 1) != null) {
968 if (line.parentInput == null) {
969 inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i + 1), matcher.end(i + 1) - matcher.start(i + 1),
970 COLOR_BLACK, COLOR_MAGENTA));
971 } else {
972 inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i + 1), matcher.end(i + 1) - matcher.start(i + 1),
973 COLOR_BLACK, COLOR_LIGHT_MAGENTA));
974 }
975 }
976 }
977 }
978
979 private void openHelpShell(String url) {
980 if (helpBrowser != null && !helpBrowser.isDisposed()) {
981 helpBrowser.getShell().setActive();
982 if (!helpBrowser.getUrl().equals(url)) {
983 helpBrowser.setUrl(url);
984 }
985 return;
986 }
987 final Shell helpShell = new Shell(getShell(), SWT.SHELL_TRIM);
988 helpShell.setLayout(new FillLayout());
989 helpBrowser = new Browser(helpShell, SWT.NONE);
990 helpBrowser.addTitleListener(new TitleListener() {
991 @Override
992 public void changed(TitleEvent event) {
993 helpShell.setText(event.title);
994 }
995 });
996 Rectangle r = container.getBounds();
997 Point p = container.toDisplay(r.x, r.y);
998 Rectangle trim = helpShell.computeTrim(p.x + (r.width - 750) / 2, p.y + (r.height - 400) / 2, 750, 400);
999 helpShell.setBounds(trim);
1000 helpShell.open();
1001 helpBrowser.setUrl(url);
1002 }
1003
1004 private void openLegend() {
1005 final String cg = Messages.CustomTxtParserInputWizardPage_capturedGroup;
1006 final String ucg = Messages.CustomTxtParserInputWizardPage_unidentifiedCaptureGroup;
1007 final String ut = Messages.CustomTxtParserInputWizardPage_uncapturedText;
1008 int line1start = 0;
1009 String line1 = Messages.CustomTxtParserInputWizardPage_nonMatchingLine;
1010 int line2start = line1start + line1.length();
1011 String line2 = Messages.CustomTxtParserInputWizardPage_matchingRootLine + ' ' + cg + ' ' + ucg + ' ' + ut + " \n"; //$NON-NLS-1$
1012 int line3start = line2start + line2.length();
1013 String line3 = Messages.CustomTxtParserInputWizardPage_matchingOtherLine + ' ' + cg + ' ' + ucg + ' ' + ut + " \n"; //$NON-NLS-1$
1014 int line4start = line3start + line3.length();
1015 String line4 = Messages.CustomTxtParserInputWizardPage_matchingOtherLine + ' ' + cg + ' ' + ucg + ' ' + ut + " \n"; //$NON-NLS-1$
1016 int line5start = line4start + line4.length();
1017 String line5 = Messages.CustomTxtParserInputWizardPage_nonMatchingLine;
1018 int line6start = line5start + line5.length();
1019 String line6 = Messages.CustomTxtParserInputWizardPage_matchingRootLine + cg + ' ' + ucg + ' ' + ut + " \n"; //$NON-NLS-1$
1020
1021 final Shell legendShell = new Shell(getShell(), SWT.DIALOG_TRIM);
1022 legendShell.setLayout(new FillLayout());
1023 StyledText legendText = new StyledText(legendShell, SWT.MULTI);
1024 legendText.setFont(fixedFont);
1025 legendText.setText(line1 + line2 + line3 + line4 + line5 + line6);
1026 legendText.setStyleRange(new StyleRange(line2start, line2.length(), COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC));
1027 legendText.setStyleRange(new StyleRange(line3start, line3.length(), COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC));
1028 legendText.setStyleRange(new StyleRange(line4start, line4.length(), COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC));
1029 legendText.setStyleRange(new StyleRange(line6start, line6.length(), COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC));
1030 legendText.setStyleRange(new StyleRange(line2start + line2.indexOf(cg), cg.length(), COLOR_BLACK, COLOR_GREEN, SWT.BOLD));
1031 legendText.setStyleRange(new StyleRange(line2start + line2.indexOf(ucg), ucg.length(), COLOR_BLACK, COLOR_MAGENTA));
1032 legendText.setStyleRange(new StyleRange(line3start + line3.indexOf(cg), cg.length(), COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD));
1033 legendText.setStyleRange(new StyleRange(line3start + line3.indexOf(ucg), ucg.length(), COLOR_BLACK, COLOR_LIGHT_MAGENTA));
1034 legendText.setStyleRange(new StyleRange(line4start + line4.indexOf(cg), cg.length(), COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD));
1035 legendText.setStyleRange(new StyleRange(line4start + line4.indexOf(ucg), ucg.length(), COLOR_BLACK, COLOR_LIGHT_MAGENTA));
1036 legendText.setStyleRange(new StyleRange(line6start + line6.indexOf(cg), cg.length(), COLOR_BLACK, COLOR_GREEN, SWT.BOLD));
1037 legendText.setStyleRange(new StyleRange(line6start + line6.indexOf(ucg), ucg.length(), COLOR_BLACK, COLOR_MAGENTA));
1038 legendShell.setText(Messages.CustomTxtParserInputWizardPage_previewLegend);
1039 legendShell.pack();
1040 legendShell.open();
1041 }
1042
1043 private class UpdateListener implements ModifyListener, SelectionListener {
1044
1045 @Override
1046 public void modifyText(ModifyEvent e) {
1047 validate();
1048 updatePreviews();
1049 }
1050
1051 @Override
1052 public void widgetDefaultSelected(SelectionEvent e) {
1053 validate();
1054 updatePreviews();
1055 }
1056
1057 @Override
1058 public void widgetSelected(SelectionEvent e) {
1059 validate();
1060 updatePreviews();
1061 }
1062
1063 }
1064
1065 private class Line {
1066 private static final String INFINITY_STRING = "\u221E"; //$NON-NLS-1$
1067 private InputLine inputLine;
1068 private Group group;
1069 private Composite labelComposite;
1070 private Text regexText;
1071 private Composite cardinalityContainer;
1072 private Combo cardinalityCombo;
1073 private Label cardinalityMinLabel;
1074 private Text cardinalityMinText;
1075 private Label cardinalityMaxLabel;
1076 private Text cardinalityMaxText;
1077 private Button infiniteButton;
1078 private Button eventTypeButton;
1079 private Text eventTypeText;
1080 private List<InputGroup> inputs = new ArrayList<>();
1081 private Button addGroupButton;
1082 private Label addGroupLabel;
1083
1084 public Line(Composite parent, String name, InputLine inputLine) {
1085 this.inputLine = inputLine;
1086
1087 group = new Group(parent, SWT.NONE);
1088 group.setText(name);
1089 group.setLayout(new GridLayout(2, false));
1090 group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1091
1092 labelComposite = new Composite(group, SWT.FILL);
1093 GridLayout labelLayout = new GridLayout(1, false);
1094 labelLayout.marginWidth = 0;
1095 labelLayout.marginHeight = 0;
1096 labelComposite.setLayout(labelLayout);
1097 labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1098
1099 Label label = new Label(labelComposite, SWT.NULL);
1100 label.setText(Messages.CustomTxtParserInputWizardPage_regularExpression);
1101
1102 Composite regexContainer = new Composite(group, SWT.NONE);
1103 GridLayout regexLayout = new GridLayout(2, false);
1104 regexLayout.marginHeight = 0;
1105 regexLayout.marginWidth = 0;
1106 regexContainer.setLayout(regexLayout);
1107 regexContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1108
1109 regexText = new Text(regexContainer, SWT.BORDER | SWT.SINGLE);
1110 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1111 gd.widthHint = 0;
1112 regexText.setLayoutData(gd);
1113 regexText.setText(inputLine.getRegex());
1114 regexText.addModifyListener(updateListener);
1115
1116 Button regexHelpButton = new Button(regexContainer, SWT.PUSH);
1117 regexHelpButton.setImage(HELP_IMAGE);
1118 regexHelpButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_regularExpressionHelp);
1119 regexHelpButton.addSelectionListener(new SelectionAdapter() {
1120 @Override
1121 public void widgetSelected(SelectionEvent e) {
1122 openHelpShell(PATTERN_URL);
1123 }
1124 });
1125
1126 label = new Label(group, SWT.NONE);
1127 label.setText(Messages.CustomTxtParserInputWizardPage_cardinality);
1128 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1129
1130 cardinalityContainer = new Composite(group, SWT.NONE);
1131 GridLayout cardinalityLayout = new GridLayout(6, false);
1132 cardinalityLayout.marginHeight = 0;
1133 cardinalityLayout.marginWidth = 0;
1134 cardinalityContainer.setLayout(cardinalityLayout);
1135 cardinalityContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1136
1137 cardinalityCombo = new Combo(cardinalityContainer, SWT.DROP_DOWN | SWT.READ_ONLY);
1138 cardinalityCombo.setItems(new String[] {
1139 Cardinality.ZERO_OR_MORE.toString(),
1140 Cardinality.ONE_OR_MORE.toString(),
1141 Cardinality.ZERO_OR_ONE.toString(),
1142 Cardinality.ONE.toString(), "(?,?)" }); //$NON-NLS-1$
1143 cardinalityCombo.addSelectionListener(new SelectionListener() {
1144 @Override
1145 public void widgetDefaultSelected(SelectionEvent e) {
1146 }
1147
1148 @Override
1149 public void widgetSelected(SelectionEvent e) {
1150 switch (cardinalityCombo.getSelectionIndex()) {
1151 case 4: // (?,?)
1152 cardinalityMinLabel.setVisible(true);
1153 cardinalityMinText.setVisible(true);
1154 cardinalityMaxLabel.setVisible(true);
1155 cardinalityMaxText.setVisible(true);
1156 infiniteButton.setVisible(true);
1157 break;
1158 default:
1159 cardinalityMinLabel.setVisible(false);
1160 cardinalityMinText.setVisible(false);
1161 cardinalityMaxLabel.setVisible(false);
1162 cardinalityMaxText.setVisible(false);
1163 infiniteButton.setVisible(false);
1164 break;
1165 }
1166 cardinalityContainer.layout();
1167 validate();
1168 updatePreviews();
1169 }
1170 });
1171
1172 cardinalityMinLabel = new Label(cardinalityContainer, SWT.NONE);
1173 cardinalityMinLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1174 cardinalityMinLabel.setText(Messages.CustomTxtParserInputWizardPage_min);
1175 cardinalityMinLabel.setVisible(false);
1176
1177 cardinalityMinText = new Text(cardinalityContainer, SWT.BORDER | SWT.SINGLE);
1178 gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
1179 gd.widthHint = 20;
1180 cardinalityMinText.setLayoutData(gd);
1181 cardinalityMinText.setVisible(false);
1182
1183 cardinalityMaxLabel = new Label(cardinalityContainer, SWT.NONE);
1184 cardinalityMaxLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1185 cardinalityMaxLabel.setText(Messages.CustomTxtParserInputWizardPage_max);
1186 cardinalityMaxLabel.setVisible(false);
1187
1188 cardinalityMaxText = new Text(cardinalityContainer, SWT.BORDER | SWT.SINGLE);
1189 gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
1190 gd.widthHint = 20;
1191 cardinalityMaxText.setLayoutData(gd);
1192 cardinalityMaxText.setVisible(false);
1193
1194 infiniteButton = new Button(cardinalityContainer, SWT.PUSH);
1195 infiniteButton.setText(INFINITY_STRING);
1196 infiniteButton.setVisible(false);
1197 infiniteButton.addSelectionListener(new SelectionAdapter() {
1198 @Override
1199 public void widgetSelected(SelectionEvent e) {
1200 cardinalityMaxText.setText(INFINITY_STRING);
1201 }
1202 });
1203
1204 if (inputLine.cardinality.equals(Cardinality.ZERO_OR_MORE)) {
1205 cardinalityCombo.select(0);
1206 } else if (inputLine.cardinality.equals(Cardinality.ONE_OR_MORE)) {
1207 cardinalityCombo.select(1);
1208 } else if (inputLine.cardinality.equals(Cardinality.ZERO_OR_ONE)) {
1209 cardinalityCombo.select(2);
1210 } else if (inputLine.cardinality.equals(Cardinality.ONE)) {
1211 cardinalityCombo.select(3);
1212 } else {
1213 cardinalityCombo.select(4);
1214 cardinalityMinLabel.setVisible(true);
1215 cardinalityMinText.setVisible(true);
1216 if (inputLine.getMinCount() >= 0) {
1217 cardinalityMinText.setText(Integer.toString(inputLine.getMinCount()));
1218 }
1219 cardinalityMaxLabel.setVisible(true);
1220 cardinalityMaxText.setVisible(true);
1221 if (inputLine.getMaxCount() == Cardinality.INF) {
1222 cardinalityMaxText.setText(INFINITY_STRING);
1223 } else if (inputLine.getMaxCount() >= 0) {
1224 cardinalityMaxText.setText(Integer.toString(inputLine.getMaxCount()));
1225 }
1226 infiniteButton.setVisible(true);
1227 }
1228
1229 VerifyListener digitsListener = new VerifyListener() {
1230 @Override
1231 public void verifyText(VerifyEvent e) {
1232 if (e.text.equals(INFINITY_STRING)) {
1233 e.doit = e.widget == cardinalityMaxText && e.start == 0 && e.end == ((Text) e.widget).getText().length();
1234 } else {
1235 if (((Text) e.widget).getText().equals(INFINITY_STRING)) {
1236 e.doit = e.start == 0 && e.end == ((Text) e.widget).getText().length();
1237 }
1238 for (int i = 0; i < e.text.length(); i++) {
1239 if (!Character.isDigit(e.text.charAt(i))) {
1240 e.doit = false;
1241 break;
1242 }
1243 }
1244 }
1245 }
1246 };
1247
1248 cardinalityMinText.addModifyListener(updateListener);
1249 cardinalityMaxText.addModifyListener(updateListener);
1250 cardinalityMinText.addVerifyListener(digitsListener);
1251 cardinalityMaxText.addVerifyListener(digitsListener);
1252
1253 eventTypeButton = new Button(group, SWT.CHECK);
1254 eventTypeButton.setText(Messages.CustomTxtParserInputWizardPage_eventType);
1255 eventTypeButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1256 eventTypeButton.addSelectionListener(new SelectionAdapter() {
1257 @Override
1258 public void widgetSelected(SelectionEvent e) {
1259 if (eventTypeButton.getSelection()) {
1260 eventTypeText.setEnabled(true);
1261 } else {
1262 eventTypeText.setEnabled(false);
1263 }
1264 }
1265 });
1266 eventTypeButton.addSelectionListener(updateListener);
1267
1268 eventTypeText = new Text(group, SWT.BORDER | SWT.SINGLE);
1269 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1270 gd.widthHint = 0;
1271 eventTypeText.setLayoutData(gd);
1272 if (inputLine.eventType != null) {
1273 eventTypeText.setText(inputLine.eventType);
1274 eventTypeButton.setSelection(true);
1275 } else {
1276 eventTypeText.setEnabled(false);
1277 eventTypeButton.setSelection(false);
1278 }
1279 eventTypeText.addModifyListener(updateListener);
1280
1281 if (inputLine.columns != null) {
1282 for (InputData inputData : inputLine.columns) {
1283 InputGroup inputGroup = new InputGroup(group, this, inputs.size() + 1);
1284 if (inputData.tag.equals(Tag.TIMESTAMP)) {
1285 inputGroup.tagCombo.select(0);
1286 inputGroup.tagText.setText(inputData.format);
1287 inputGroup.tagLabel.setText(Messages.CustomTxtParserInputWizardPage_format);
1288 inputGroup.tagLabel.setVisible(true);
1289 inputGroup.tagText.setVisible(true);
1290 inputGroup.tagText.addModifyListener(updateListener);
1291 inputGroup.actionCombo.setVisible(true);
1292 } else if (inputData.tag.equals(Tag.EVENT_TYPE)) {
1293 inputGroup.tagCombo.select(1);
1294 inputGroup.actionCombo.setVisible(true);
1295 } else if (inputData.tag.equals(Tag.MESSAGE)) {
1296 inputGroup.tagCombo.select(2);
1297 inputGroup.actionCombo.setVisible(true);
1298 } else if (inputData.tag.equals(Tag.EXTRA_FIELD_NAME)) {
1299 inputGroup.tagCombo.select(3);
1300 inputGroup.actionCombo.setVisible(false);
1301 } else if (inputData.tag.equals(Tag.EXTRA_FIELD_VALUE)) {
1302 inputGroup.tagCombo.select(4);
1303 inputGroup.actionCombo.setVisible(true);
1304 } else {
1305 inputGroup.tagCombo.select(5);
1306 inputGroup.tagText.setText(inputData.name);
1307 inputGroup.tagLabel.setText(Messages.CustomTxtParserInputWizardPage_name);
1308 inputGroup.tagLabel.setVisible(true);
1309 inputGroup.tagText.setVisible(true);
1310 inputGroup.tagText.addModifyListener(updateListener);
1311 inputGroup.actionCombo.setVisible(true);
1312 }
1313 inputGroup.actionCombo.select(inputData.action);
1314 inputs.add(inputGroup);
1315 }
1316 }
1317
1318 createAddGroupButton();
1319 }
1320
1321 private void createAddGroupButton() {
1322 addGroupButton = new Button(group, SWT.PUSH);
1323 addGroupButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1324 addGroupButton.setImage(ADD_IMAGE);
1325 addGroupButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_addGroup);
1326 addGroupButton.addSelectionListener(new SelectionAdapter() {
1327 @Override
1328 public void widgetSelected(SelectionEvent e) {
1329 removeAddGroupButton();
1330 inputs.add(new InputGroup(group, Line.this, inputs.size() + 1));
1331 createAddGroupButton();
1332 lineContainer.layout();
1333 lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
1334 group.getParent().layout();
1335 validate();
1336 updatePreviews();
1337 }
1338 });
1339
1340 addGroupLabel = new Label(group, SWT.NULL);
1341 addGroupLabel.setText(Messages.CustomTxtParserInputWizardPage_newGroup);
1342 }
1343
1344 private void removeAddGroupButton() {
1345 addGroupButton.dispose();
1346 addGroupLabel.dispose();
1347 }
1348
1349 private void removeInput(int inputNumber) {
1350 int nb = inputNumber;
1351 if (--nb < inputs.size()) {
1352 inputs.remove(nb).dispose();
1353 for (int i = nb; i < inputs.size(); i++) {
1354 inputs.get(i).setInputNumber(i + 1);
1355 }
1356 lineContainer.layout();
1357 lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
1358 group.getParent().layout();
1359 }
1360 }
1361
1362 private void dispose() {
1363 group.dispose();
1364 }
1365
1366 private void extractInputs() {
1367 inputLine.setRegex(selectedLine.regexText.getText());
1368 inputLine.eventType = selectedLine.eventTypeButton.getSelection() ? selectedLine.eventTypeText.getText().trim() : null;
1369 switch (cardinalityCombo.getSelectionIndex()) {
1370 case 0:
1371 inputLine.cardinality = Cardinality.ZERO_OR_MORE;
1372 break;
1373 case 1:
1374 inputLine.cardinality = Cardinality.ONE_OR_MORE;
1375 break;
1376 case 2:
1377 inputLine.cardinality = Cardinality.ZERO_OR_ONE;
1378 break;
1379 case 3:
1380 inputLine.cardinality = Cardinality.ONE;
1381 break;
1382 case 4: // (?,?)
1383 int min,
1384 max;
1385 try {
1386 min = Integer.parseInt(cardinalityMinText.getText());
1387 } catch (NumberFormatException e) {
1388 min = -1;
1389 }
1390 try {
1391 if (cardinalityMaxText.getText().equals(INFINITY_STRING)) {
1392 max = Cardinality.INF;
1393 } else {
1394 max = Integer.parseInt(cardinalityMaxText.getText());
1395 }
1396 } catch (NumberFormatException e) {
1397 max = -1;
1398 }
1399 inputLine.cardinality = new Cardinality(min, max);
1400 break;
1401 default:
1402 inputLine.cardinality = Cardinality.ZERO_OR_MORE;
1403 break;
1404 }
1405 inputLine.columns = new ArrayList<>(inputs.size());
1406 for (int i = 0; i < inputs.size(); i++) {
1407 InputGroup grp = inputs.get(i);
1408 InputData inputData = new InputData();
1409 inputData.tag = Tag.fromLabel(grp.tagCombo.getText());
1410 if (inputData.tag.equals(Tag.OTHER)) {
1411 inputData.name = grp.tagText.getText().trim();
1412 } else {
1413 inputData.name = inputData.tag.toString();
1414 if (inputData.tag.equals(Tag.TIMESTAMP)) {
1415 inputData.format = grp.tagText.getText().trim();
1416 }
1417 }
1418 inputData.action = grp.actionCombo.getSelectionIndex();
1419 inputLine.columns.add(inputData);
1420 }
1421 }
1422 }
1423
1424 private class InputGroup {
1425 private Line line;
1426 private int inputNumber;
1427
1428 // children of parent (must be disposed)
1429 private Composite labelComposite;
1430 private Composite tagComposite;
1431 private Label previewLabel;
1432 private Text previewText;
1433
1434 // children of labelComposite
1435 private Label inputLabel;
1436
1437 // children of tagComposite
1438 private Combo tagCombo;
1439 private Label tagLabel;
1440 private Text tagText;
1441 private Combo actionCombo;
1442
1443 public InputGroup(Composite parent, Line line, int inputNumber) {
1444 this.line = line;
1445 this.inputNumber = inputNumber;
1446
1447 labelComposite = new Composite(parent, SWT.FILL);
1448 GridLayout labelLayout = new GridLayout(2, false);
1449 labelLayout.marginWidth = 0;
1450 labelLayout.marginHeight = 0;
1451 labelComposite.setLayout(labelLayout);
1452 labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1453
1454 Button deleteButton = new Button(labelComposite, SWT.PUSH);
1455 deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1456 deleteButton.setImage(DELETE_IMAGE);
1457 deleteButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_removeGroup);
1458 deleteButton.addSelectionListener(new SelectionAdapter() {
1459 @Override
1460 public void widgetSelected(SelectionEvent e) {
1461 InputGroup.this.line.removeInput(InputGroup.this.inputNumber);
1462 validate();
1463 updatePreviews();
1464 }
1465 });
1466
1467 inputLabel = new Label(labelComposite, SWT.NULL);
1468 inputLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1469 inputLabel.setText(NLS.bind(Messages.CustomTxtParserInputWizardPage_group, inputNumber));
1470
1471 tagComposite = new Composite(parent, SWT.FILL);
1472 GridLayout tagLayout = new GridLayout(4, false);
1473 tagLayout.marginWidth = 0;
1474 tagLayout.marginHeight = 0;
1475 tagComposite.setLayout(tagLayout);
1476 tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1477
1478 tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1479 tagCombo.setItems(new String[] {
1480 Tag.TIMESTAMP.toString(),
1481 Tag.EVENT_TYPE.toString(),
1482 Tag.MESSAGE.toString(),
1483 Tag.EXTRA_FIELD_NAME.toString(),
1484 Tag.EXTRA_FIELD_VALUE.toString(),
1485 Tag.OTHER.toString()});
1486 tagCombo.select(2);
1487 tagCombo.addSelectionListener(new SelectionListener() {
1488 @Override
1489 public void widgetDefaultSelected(SelectionEvent e) {
1490 }
1491
1492 @Override
1493 public void widgetSelected(SelectionEvent e) {
1494 tagText.removeModifyListener(updateListener);
1495 switch (tagCombo.getSelectionIndex()) {
1496 case 0: // Time Stamp
1497 tagLabel.setText(Messages.CustomTxtParserInputWizardPage_format);
1498 tagLabel.setVisible(true);
1499 tagText.setVisible(true);
1500 tagText.addModifyListener(updateListener);
1501 actionCombo.setVisible(true);
1502 break;
1503 case 1: // Event type
1504 tagLabel.setVisible(false);
1505 tagText.setVisible(false);
1506 actionCombo.setVisible(true);
1507 break;
1508 case 2: // Message
1509 tagLabel.setVisible(false);
1510 tagText.setVisible(false);
1511 actionCombo.setVisible(true);
1512 break;
1513 case 3: // Field name
1514 tagLabel.setVisible(false);
1515 tagText.setVisible(false);
1516 actionCombo.setVisible(false);
1517 break;
1518 case 4: // Field type
1519 tagLabel.setVisible(false);
1520 tagText.setVisible(false);
1521 actionCombo.setVisible(true);
1522 break;
1523 case 5: // Other
1524 tagLabel.setText(Messages.CustomTxtParserInputWizardPage_name);
1525 tagLabel.setVisible(true);
1526 tagText.setVisible(true);
1527 tagText.addModifyListener(updateListener);
1528 actionCombo.setVisible(true);
1529 break;
1530 default:
1531 break;
1532 }
1533 tagComposite.layout();
1534 validate();
1535 updatePreviews();
1536 }
1537 });
1538
1539 tagLabel = new Label(tagComposite, SWT.NULL);
1540 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1541 tagLabel.setVisible(false);
1542
1543 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
1544 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1545 gd.widthHint = 0;
1546 tagText.setLayoutData(gd);
1547 tagText.setVisible(false);
1548
1549 actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1550 actionCombo.setItems(new String[] { Messages.CustomTxtParserInputWizardPage_set, Messages.CustomTxtParserInputWizardPage_append, Messages.CustomTxtParserInputWizardPage_appendWith });
1551 actionCombo.select(0);
1552 actionCombo.addSelectionListener(updateListener);
1553
1554 previewLabel = new Label(parent, SWT.NULL);
1555 previewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1556 previewLabel.setText(Messages.CustomTxtParserInputWizardPage_preview);
1557
1558 previewText = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
1559 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1560 gd.widthHint = 0;
1561 previewText.setLayoutData(gd);
1562 previewText.setText(Messages.CustomTxtParserInputWizardPage_noMatch);
1563 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
1564 }
1565
1566 private void dispose() {
1567 labelComposite.dispose();
1568 tagComposite.dispose();
1569 previewLabel.dispose();
1570 previewText.dispose();
1571 }
1572
1573 private void setInputNumber(int inputNumber) {
1574 this.inputNumber = inputNumber;
1575 inputLabel.setText(NLS.bind(Messages.CustomTxtParserInputWizardPage_group, inputNumber));
1576 labelComposite.layout();
1577 }
1578 }
1579
1580 private void validate() {
1581
1582 definition.categoryName = categoryText.getText().trim();
1583 definition.definitionName = logtypeText.getText().trim();
1584 definition.timeStampOutputFormat = timestampOutputFormatText.getText().trim();
1585
1586 if (selectedLine != null) {
1587 selectedLine.extractInputs();
1588 treeViewer.refresh();
1589 }
1590
1591 StringBuffer errors = new StringBuffer();
1592
1593 if (definition.categoryName.length() == 0) {
1594 errors.append("Enter a category for the new trace type. "); //$NON-NLS-1$
1595 categoryText.setBackground(COLOR_LIGHT_RED);
1596 } else if (definition.definitionName.length() == 0) {
1597 errors.append("Enter a name for the new trace type. "); //$NON-NLS-1$
1598 logtypeText.setBackground(COLOR_LIGHT_RED);
1599 } else {
1600 categoryText.setBackground(COLOR_TEXT_BACKGROUND);
1601 logtypeText.setBackground(COLOR_TEXT_BACKGROUND);
1602 if (definition.categoryName.indexOf(':') != -1) {
1603 errors.append("Invalid character ':' in category. "); //$NON-NLS-1$
1604 categoryText.setBackground(COLOR_LIGHT_RED);
1605 }
1606 if (definition.definitionName.indexOf(':') != -1) {
1607 errors.append("Invalid character ':' in trace type. "); //$NON-NLS-1$
1608 logtypeText.setBackground(COLOR_LIGHT_RED);
1609 }
1610 for (TraceTypeHelper helper : TmfTraceType.getTraceTypeHelpers()) {
1611 if (definition.categoryName.equals(helper.getCategoryName()) &&
1612 definition.definitionName.equals(helper.getName()) &&
1613 (editDefinitionName == null || !editDefinitionName.equals(definition.definitionName)) &&
1614 (editCategoryName == null || !editCategoryName.equals(definition.categoryName))) {
1615 errors.append("The trace type name already exists. "); //$NON-NLS-1$
1616 logtypeText.setBackground(COLOR_LIGHT_RED);
1617 break;
1618 }
1619 }
1620 }
1621
1622 timestampFound = false;
1623 for (int i = 0; i < definition.inputs.size(); i++) {
1624
1625 InputLine inputLine = definition.inputs.get(i);
1626 String name = Integer.toString(i + 1);
1627 errors.append(validateLine(inputLine, name));
1628 }
1629 if (timestampFound && !definition.timeStampOutputFormat.isEmpty()) {
1630 try {
1631 new TmfTimestampFormat(definition.timeStampOutputFormat);
1632 timestampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1633 } catch (IllegalArgumentException e) {
1634 errors.append("Enter a valid output format for the Time Stamp field [" + e.getMessage() + "]."); //$NON-NLS-1$ //$NON-NLS-2$
1635 timestampOutputFormatText.setBackground(COLOR_LIGHT_RED);
1636 }
1637 } else {
1638 timestampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1639 }
1640
1641 if (errors.length() == 0) {
1642 setDescription(defaultDescription);
1643 setPageComplete(true);
1644 } else {
1645 setDescription(errors.toString());
1646 setPageComplete(false);
1647 }
1648 }
1649
1650 /**
1651 * Validate an input line.
1652 *
1653 * @param inputLine
1654 * The line to clean up
1655 * @param name
1656 * The name of the line
1657 * @return The cleaned up line
1658 */
1659 public StringBuffer validateLine(InputLine inputLine, String name) {
1660 StringBuffer errors = new StringBuffer();
1661 Line line = null;
1662 if (selectedLine != null && selectedLine.inputLine.equals(inputLine)) {
1663 line = selectedLine;
1664 }
1665 try {
1666 Pattern.compile(inputLine.getRegex());
1667 if (line != null) {
1668 line.regexText.setBackground(COLOR_TEXT_BACKGROUND);
1669 }
1670 } catch (PatternSyntaxException e) {
1671 errors.append("Enter a valid regular expression (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$
1672 if (line != null) {
1673 line.regexText.setBackground(COLOR_LIGHT_RED);
1674 }
1675 }
1676 if (inputLine.getMinCount() == -1) {
1677 errors.append("Enter a minimum value for cardinality (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$
1678 if (line != null) {
1679 line.cardinalityMinText.setBackground(COLOR_LIGHT_RED);
1680 }
1681 } else {
1682 if (line != null) {
1683 line.cardinalityMinText.setBackground(COLOR_TEXT_BACKGROUND);
1684 }
1685 }
1686 if (inputLine.getMaxCount() == -1) {
1687 errors.append("Enter a maximum value for cardinality (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$
1688 if (line != null) {
1689 line.cardinalityMaxText.setBackground(COLOR_LIGHT_RED);
1690 }
1691 } else if (inputLine.getMinCount() > inputLine.getMaxCount()) {
1692 errors.append("Enter correct (min <= max) values for cardinality (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$
1693 if (line != null) {
1694 line.cardinalityMinText.setBackground(COLOR_LIGHT_RED);
1695 }
1696 if (line != null) {
1697 line.cardinalityMaxText.setBackground(COLOR_LIGHT_RED);
1698 }
1699 } else {
1700 if (line != null) {
1701 line.cardinalityMaxText.setBackground(COLOR_TEXT_BACKGROUND);
1702 }
1703 }
1704 if (inputLine.eventType != null && inputLine.eventType.trim().isEmpty()) {
1705 errors.append("Enter the event type (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$
1706 if (line != null) {
1707 line.eventTypeText.setBackground(COLOR_LIGHT_RED);
1708 }
1709 } else {
1710 if (line != null) {
1711 line.eventTypeText.setBackground(COLOR_TEXT_BACKGROUND);
1712 }
1713 }
1714 for (int i = 0; inputLine.columns != null && i < inputLine.columns.size(); i++) {
1715 InputData inputData = inputLine.columns.get(i);
1716 InputGroup group = null;
1717 if (line != null) {
1718 group = line.inputs.get(i);
1719 }
1720 if (inputData.tag.equals(Tag.TIMESTAMP)) {
1721 timestampFound = true;
1722 if (inputData.format.length() == 0) {
1723 errors.append("Enter the input format for the Time Stamp (Line " + name + " Group " + (i + 1) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1724 if (group != null) {
1725 group.tagText.setBackground(COLOR_LIGHT_RED);
1726 }
1727 } else {
1728 try {
1729 new TmfTimestampFormat(inputData.format);
1730 if (group != null) {
1731 group.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1732 }
1733 } catch (IllegalArgumentException e) {
1734 errors.append("Enter a valid input format for the Time Stamp (Line " + name + " Group " + (i + 1) + ") [" + e.getMessage() + "]. "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
1735 if (group != null) {
1736 group.tagText.setBackground(COLOR_LIGHT_RED);
1737 }
1738 }
1739 }
1740 } else if (inputData.tag.equals(Tag.OTHER)) {
1741 if (inputData.name.isEmpty()) {
1742 errors.append("Enter a name for the data group (Line " + name + " Group " + (i + 1) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1743 if (group != null) {
1744 group.tagText.setBackground(COLOR_LIGHT_RED);
1745 }
1746 } else if (Tag.fromLabel(inputData.name) != null) {
1747 errors.append("Cannot use reserved name for the data group (Line " + name + " Group " + (i + 1) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1748 if (group != null) {
1749 group.tagText.setBackground(COLOR_LIGHT_RED);
1750 }
1751 } else {
1752 if (group != null) {
1753 group.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1754 }
1755 }
1756 } else {
1757 if (group != null) {
1758 group.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1759 }
1760 }
1761 }
1762 for (int i = 0; inputLine.childrenInputs != null && i < inputLine.childrenInputs.size(); i++) {
1763 errors.append(validateLine(inputLine.childrenInputs.get(i), name + "." + (i + 1))); //$NON-NLS-1$
1764 }
1765 return errors;
1766 }
1767
1768 /**
1769 * Get the trace definition.
1770 *
1771 * @return The trace definition
1772 */
1773 public CustomTxtTraceDefinition getDefinition() {
1774 return definition;
1775 }
1776
1777 /**
1778 * Get the raw text of the input.
1779 *
1780 * @return The raw input text
1781 */
1782 public char[] getInputText() {
1783 return inputText.getText().toCharArray();
1784 }
1785 }
This page took 0.073995 seconds and 5 git commands to generate.