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 / CustomXmlParserInputWizardPage.java
CommitLineData
a0a88f65 1/*******************************************************************************
f5cc6ed1 2 * Copyright (c) 2010, 2016 Ericsson
a0a88f65
AM
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:
f6aa55e2 10 * Patrick Tasse - Initial API and implementation
a0a88f65
AM
11 *******************************************************************************/
12
2bdf0193 13package org.eclipse.tracecompass.internal.tmf.ui.parsers.wizards;
be222f56
PT
14
15import java.io.BufferedReader;
16import java.io.ByteArrayInputStream;
17import java.io.IOException;
18import java.io.InputStream;
19import java.io.InputStreamReader;
313a5081 20import java.net.URL;
be222f56 21import java.text.ParseException;
f5cc6ed1 22import java.util.AbstractMap.SimpleEntry;
be222f56 23import java.util.ArrayList;
be222f56 24import java.util.List;
f5cc6ed1 25import java.util.Map.Entry;
be222f56
PT
26
27import javax.xml.parsers.DocumentBuilder;
28import javax.xml.parsers.DocumentBuilderFactory;
29import javax.xml.parsers.ParserConfigurationException;
30
31import org.eclipse.core.resources.IFile;
32import org.eclipse.core.runtime.CoreException;
313a5081
PT
33import org.eclipse.core.runtime.FileLocator;
34import org.eclipse.core.runtime.IPath;
35import org.eclipse.core.runtime.Path;
36import org.eclipse.core.runtime.Platform;
be222f56
PT
37import org.eclipse.jface.viewers.AbstractTreeViewer;
38import org.eclipse.jface.viewers.ColumnLabelProvider;
39import org.eclipse.jface.viewers.ISelection;
40import org.eclipse.jface.viewers.ISelectionChangedListener;
41import org.eclipse.jface.viewers.IStructuredSelection;
42import org.eclipse.jface.viewers.ITreeContentProvider;
43import org.eclipse.jface.viewers.SelectionChangedEvent;
44import org.eclipse.jface.viewers.StructuredSelection;
45import org.eclipse.jface.viewers.TreeViewer;
46import org.eclipse.jface.viewers.Viewer;
47import org.eclipse.jface.wizard.WizardPage;
89151d6a 48import org.eclipse.osgi.util.NLS;
be222f56
PT
49import org.eclipse.swt.SWT;
50import org.eclipse.swt.browser.Browser;
be222f56
PT
51import org.eclipse.swt.custom.SashForm;
52import org.eclipse.swt.custom.ScrolledComposite;
53import org.eclipse.swt.custom.StyleRange;
54import org.eclipse.swt.custom.StyledText;
55import org.eclipse.swt.events.ModifyEvent;
56import org.eclipse.swt.events.ModifyListener;
57import org.eclipse.swt.events.SelectionAdapter;
58import org.eclipse.swt.events.SelectionEvent;
59import org.eclipse.swt.events.SelectionListener;
60import org.eclipse.swt.graphics.Color;
61import org.eclipse.swt.graphics.Font;
62import org.eclipse.swt.graphics.FontData;
63import org.eclipse.swt.graphics.Image;
313a5081
PT
64import org.eclipse.swt.graphics.Point;
65import org.eclipse.swt.graphics.Rectangle;
be222f56
PT
66import org.eclipse.swt.layout.FillLayout;
67import org.eclipse.swt.layout.GridData;
68import org.eclipse.swt.layout.GridLayout;
69import org.eclipse.swt.widgets.Button;
70import org.eclipse.swt.widgets.Combo;
71import org.eclipse.swt.widgets.Composite;
72import org.eclipse.swt.widgets.Display;
73import org.eclipse.swt.widgets.Group;
74import org.eclipse.swt.widgets.Label;
75import org.eclipse.swt.widgets.Shell;
76import org.eclipse.swt.widgets.Text;
2bdf0193
AM
77import org.eclipse.tracecompass.internal.tmf.ui.Activator;
78import org.eclipse.tracecompass.internal.tmf.ui.Messages;
79import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition;
f5cc6ed1 80import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag;
b2c971ec 81import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlInputAttribute;
a7418109 82import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlInputElement;
2bdf0193
AM
83import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
84import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
2bdf0193
AM
85import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
86import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
87import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
313a5081 88import org.osgi.framework.Bundle;
be222f56
PT
89import org.w3c.dom.Document;
90import org.w3c.dom.Element;
91import org.w3c.dom.NamedNodeMap;
92import org.w3c.dom.Node;
93import org.w3c.dom.NodeList;
94import org.xml.sax.EntityResolver;
95import org.xml.sax.ErrorHandler;
96import org.xml.sax.InputSource;
97import org.xml.sax.SAXException;
98import org.xml.sax.SAXParseException;
99
89151d6a
PT
100import com.google.common.base.Joiner;
101
a0a88f65
AM
102/**
103 * Input wizard page for custom XML trace parsers.
104 *
f6aa55e2 105 * @author Patrick Tasse
a0a88f65 106 */
be222f56
PT
107public class CustomXmlParserInputWizardPage extends WizardPage {
108
109 private static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
73cc4ccf
PT
110 private static final String TIMESTAMP_FORMAT_BUNDLE = "org.eclipse.tracecompass.doc.user"; //$NON-NLS-1$
111 private static final String TIMESTAMP_FORMAT_PATH = "reference/api/org/eclipse/tracecompass/tmf/core/timestamp/TmfTimestampFormat.html"; //$NON-NLS-1$
f6aa55e2
PT
112 private static final Image ELEMENT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/element_icon.gif"); //$NON-NLS-1$
113 private static final Image ADD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
114 private static final Image ADD_NEXT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addnext_button.gif"); //$NON-NLS-1$
115 private static final Image ADD_CHILD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addchild_button.gif"); //$NON-NLS-1$
116 private static final Image ADD_MANY_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addmany_button.gif"); //$NON-NLS-1$
117 private static final Image DELETE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
118 private static final Image MOVE_UP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
119 private static final Image MOVE_DOWN_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
120 private static final Image HELP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/help_button.gif"); //$NON-NLS-1$
be222f56 121 private static final Color COLOR_LIGHT_RED = new Color(Display.getDefault(), 255, 192, 192);
f6aa55e2
PT
122 private static final Color COLOR_TEXT_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
123 private static final Color COLOR_WIDGET_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
4d12b563 124 private static final Color COLOR_GRAY = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
be222f56
PT
125
126 private final ISelection selection;
127 private CustomXmlTraceDefinition definition;
c22ca172 128 private String editCategoryName;
be222f56
PT
129 private String editDefinitionName;
130 private String defaultDescription;
131 private ElementNode selectedElement;
132 private Composite container;
c22ca172 133 private Text categoryText;
be222f56
PT
134 private Text logtypeText;
135 private Text timeStampOutputFormatText;
136 private Text timeStampPreviewText;
137 private Button removeButton;
138 private Button addChildButton;
139 private Button addNextButton;
140 private Button moveUpButton;
141 private Button moveDownButton;
be222f56
PT
142 private ScrolledComposite elementScrolledComposite;
143 private TreeViewer treeViewer;
be222f56
PT
144 private Composite elementContainer;
145 private Text errorText;
146 private StyledText inputText;
147 private Font fixedFont;
148 private UpdateListener updateListener;
149 private Browser helpBrowser;
150 private Element documentElement;
151
152 // variables used recursively through element traversal
153 private String timeStampValue;
154 private String timeStampFormat;
155 private boolean timeStampFound;
156 private int logEntriesCount;
157 private boolean logEntryFound;
158
a0a88f65
AM
159 /**
160 * Constructor
161 *
162 * @param selection
163 * Selection object
164 * @param definition
165 * Trace definition
166 */
be222f56
PT
167 protected CustomXmlParserInputWizardPage(ISelection selection, CustomXmlTraceDefinition definition) {
168 super("CustomXmlParserWizardPage"); //$NON-NLS-1$
169 if (definition == null) {
170 setTitle(Messages.CustomXmlParserInputWizardPage_titleNew);
171 defaultDescription = Messages.CustomXmlParserInputWizardPage_descriptionNew;
172 } else {
173 setTitle(Messages.CustomXmlParserInputWizardPage_titleEdit);
174 defaultDescription = Messages.CustomXmlParserInputWizardPage_descriptionEdit;
175 }
176 setDescription(defaultDescription);
177 this.selection = selection;
178 this.definition = definition;
179 if (definition != null) {
c22ca172 180 this.editCategoryName = definition.categoryName;
be222f56
PT
181 this.editDefinitionName = definition.definitionName;
182 }
183 }
184
185 @Override
186 public void createControl(Composite parent) {
187 container = new Composite(parent, SWT.NULL);
188 container.setLayout(new GridLayout());
189
190 updateListener = new UpdateListener();
191
192 Composite headerComposite = new Composite(container, SWT.FILL);
193 GridLayout headerLayout = new GridLayout(5, false);
194 headerLayout.marginHeight = 0;
195 headerLayout.marginWidth = 0;
196 headerComposite.setLayout(headerLayout);
197 headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
198
c22ca172
PT
199 Label categoryLabel = new Label(headerComposite, SWT.NULL);
200 categoryLabel.setText(Messages.CustomXmlParserInputWizardPage_category);
be222f56 201
c22ca172
PT
202 categoryText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
203 categoryText.setLayoutData(new GridData(120, SWT.DEFAULT));
be222f56
PT
204
205 Label timeStampFormatLabel = new Label(headerComposite, SWT.NULL);
206 timeStampFormatLabel.setText(Messages.CustomXmlParserInputWizardPage_timestampFormat);
207
208 timeStampOutputFormatText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
209 timeStampOutputFormatText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
210 timeStampOutputFormatText.setText(DEFAULT_TIMESTAMP_FORMAT);
4d12b563
PT
211 timeStampOutputFormatText.addPaintListener(e -> {
212 if (!timeStampOutputFormatText.isFocusControl() && timeStampOutputFormatText.getText().trim().isEmpty()) {
213 e.gc.setForeground(COLOR_GRAY);
214 int borderWidth = timeStampOutputFormatText.getBorderWidth();
215 e.gc.drawText(Messages.CustomXmlParserInputWizardPage_default, borderWidth, borderWidth);
216 }
217 });
be222f56 218
313a5081
PT
219 Button timeStampFormatHelpButton = new Button(headerComposite, SWT.PUSH);
220 timeStampFormatHelpButton.setImage(HELP_IMAGE);
221 timeStampFormatHelpButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_timestampFormatHelp);
222 timeStampFormatHelpButton.addSelectionListener(new SelectionAdapter() {
be222f56
PT
223 @Override
224 public void widgetSelected(SelectionEvent e) {
313a5081
PT
225 Bundle plugin = Platform.getBundle(TIMESTAMP_FORMAT_BUNDLE);
226 IPath path = new Path(TIMESTAMP_FORMAT_PATH);
227 URL fileURL = FileLocator.find(plugin, path, null);
228 try {
229 URL pageURL = FileLocator.toFileURL(fileURL);
230 openHelpShell(pageURL.toString());
231 } catch (IOException e1) {
232 }
be222f56
PT
233 }
234 });
235
c22ca172
PT
236 Label logtypeLabel = new Label(headerComposite, SWT.NULL);
237 logtypeLabel.setText(Messages.CustomXmlParserInputWizardPage_logType);
238
239 logtypeText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
240 logtypeText.setLayoutData(new GridData(120, SWT.DEFAULT));
241 logtypeText.setFocus();
242
be222f56 243 Label timeStampPreviewLabel = new Label(headerComposite, SWT.NULL);
be222f56
PT
244 timeStampPreviewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview);
245
246 timeStampPreviewText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
247 timeStampPreviewText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
248 timeStampPreviewText.setText("*no time stamp element or attribute*"); //$NON-NLS-1$
249
250 createButtonBar();
251
252 SashForm vSash = new SashForm(container, SWT.VERTICAL);
253 vSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
4d12b563 254 vSash.setBackground(COLOR_GRAY);
be222f56
PT
255
256 SashForm hSash = new SashForm(vSash, SWT.HORIZONTAL);
257 hSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
258
f6aa55e2 259 ScrolledComposite treeScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL | SWT.H_SCROLL);
be222f56 260 treeScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
f6aa55e2 261 Composite treeContainer = new Composite(treeScrolledComposite, SWT.NONE);
be222f56
PT
262 treeContainer.setLayout(new FillLayout());
263 treeScrolledComposite.setContent(treeContainer);
264 treeScrolledComposite.setExpandHorizontal(true);
265 treeScrolledComposite.setExpandVertical(true);
266
267 treeViewer = new TreeViewer(treeContainer, SWT.SINGLE | SWT.BORDER);
268 treeViewer.setContentProvider(new InputElementTreeNodeContentProvider());
269 treeViewer.setLabelProvider(new InputElementTreeLabelProvider());
270 treeViewer.addSelectionChangedListener(new InputElementTreeSelectionChangedListener());
271 treeContainer.layout();
272
273 treeScrolledComposite
274 .setMinSize(treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
275
276 elementScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL);
277 elementScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
278 elementContainer = new Composite(elementScrolledComposite, SWT.NONE);
279 GridLayout gl = new GridLayout();
280 gl.marginHeight = 1;
281 gl.marginWidth = 0;
282 elementContainer.setLayout(gl);
283 elementScrolledComposite.setContent(elementContainer);
284 elementScrolledComposite.setExpandHorizontal(true);
285 elementScrolledComposite.setExpandVertical(true);
286
287 if (definition == null) {
288 definition = new CustomXmlTraceDefinition();
289 }
290 loadDefinition(definition);
291 treeViewer.expandAll();
292 elementContainer.layout();
293
c22ca172 294 categoryText.addModifyListener(updateListener);
be222f56
PT
295 logtypeText.addModifyListener(updateListener);
296 timeStampOutputFormatText.addModifyListener(updateListener);
297
298 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x,
299 elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
300
301 hSash.setWeights(new int[] { 1, 2 });
302
303 if (definition.rootInputElement == null) {
304 removeButton.setEnabled(false);
305 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentElement);
306 addNextButton.setEnabled(false);
307 moveUpButton.setEnabled(false);
308 moveDownButton.setEnabled(false);
309 } else { // root is selected
310 addNextButton.setEnabled(false);
311 }
312
313 Composite sashBottom = new Composite(vSash, SWT.NONE);
314 GridLayout sashBottomLayout = new GridLayout(2, false);
315 sashBottomLayout.marginHeight = 0;
316 sashBottomLayout.marginWidth = 0;
317 sashBottom.setLayout(sashBottomLayout);
318
319 Label previewLabel = new Label(sashBottom, SWT.NULL);
320 previewLabel.setText(Messages.CustomXmlParserInputWizardPage_previewInput);
321
322 errorText = new Text(sashBottom, SWT.SINGLE | SWT.READ_ONLY);
323 errorText.setBackground(COLOR_WIDGET_BACKGROUND);
324 errorText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
325 errorText.setVisible(false);
326
327 inputText = new StyledText(sashBottom, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
328 if (fixedFont == null) {
329 if (System.getProperty("os.name").contains("Windows")) { //$NON-NLS-1$ //$NON-NLS-2$
330 fixedFont = new Font(Display.getCurrent(), new FontData("Courier New", 10, SWT.NORMAL)); //$NON-NLS-1$
331 } else {
332 fixedFont = new Font(Display.getCurrent(), new FontData("Monospace", 10, SWT.NORMAL)); //$NON-NLS-1$
333 }
334 }
335 inputText.setFont(fixedFont);
336 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
337 gd.heightHint = inputText.computeSize(SWT.DEFAULT, inputText.getLineHeight() * 4).y;
338 gd.widthHint = 800;
339 inputText.setLayoutData(gd);
340 inputText.setText(getSelectionText());
30e9ac41 341 inputText.addModifyListener(e -> parseXmlInput(inputText.getText()));
be222f56
PT
342 inputText.addModifyListener(updateListener);
343
344 vSash.setWeights(new int[] { hSash.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sashBottom.computeSize(SWT.DEFAULT, SWT.DEFAULT).y });
345
346 setControl(container);
347 }
348
349 private void createButtonBar() {
350 Composite buttonBar = new Composite(container, SWT.NONE);
351 GridLayout buttonBarLayout = new GridLayout(6, false);
352 buttonBarLayout.marginHeight = 0;
353 buttonBarLayout.marginWidth = 0;
354 buttonBar.setLayout(buttonBarLayout);
355
356 removeButton = new Button(buttonBar, SWT.PUSH);
f6aa55e2 357 removeButton.setImage(DELETE_IMAGE);
be222f56
PT
358 removeButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_removeElement);
359 removeButton.addSelectionListener(new SelectionAdapter() {
360 @Override
361 public void widgetSelected(SelectionEvent e) {
362 if (treeViewer.getSelection().isEmpty() || selectedElement == null) {
363 return;
364 }
365 removeElement();
a7418109 366 CustomXmlInputElement inputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
be222f56
PT
367 if (inputElement == definition.rootInputElement) {
368 definition.rootInputElement = null;
369 } else {
a7418109 370 inputElement.getParentElement().getChildElements().remove(inputElement);
be222f56
PT
371 }
372 treeViewer.refresh();
373 validate();
374 updatePreviews();
375 removeButton.setEnabled(false);
376 if (definition.rootInputElement == null) {
377 addChildButton.setEnabled(true);
378 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentEleemnt);
379 } else {
380 addChildButton.setEnabled(false);
381 }
382 addNextButton.setEnabled(false);
383 moveUpButton.setEnabled(false);
384 moveDownButton.setEnabled(false);
385 }
386 });
387
388 addChildButton = new Button(buttonBar, SWT.PUSH);
f6aa55e2 389 addChildButton.setImage(ADD_CHILD_IMAGE);
be222f56
PT
390 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addChildElement);
391 addChildButton.addSelectionListener(new SelectionAdapter() {
392 @Override
393 public void widgetSelected(SelectionEvent e) {
f5cc6ed1 394 CustomXmlInputElement inputElement = new CustomXmlInputElement("", false, Tag.IGNORE, Tag.IGNORE.toString(), 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$
be222f56
PT
395 if (definition.rootInputElement == null) {
396 definition.rootInputElement = inputElement;
a7418109 397 inputElement.setElementName(getChildNameSuggestion(null));
be222f56
PT
398 } else if (treeViewer.getSelection().isEmpty()) {
399 return;
400 } else {
a7418109 401 CustomXmlInputElement parentInputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
be222f56 402 parentInputElement.addChild(inputElement);
a7418109 403 inputElement.setElementName(getChildNameSuggestion(parentInputElement));
be222f56
PT
404 }
405 treeViewer.refresh();
406 treeViewer.setSelection(new StructuredSelection(inputElement), true);
407 }
408 });
409
410 addNextButton = new Button(buttonBar, SWT.PUSH);
f6aa55e2 411 addNextButton.setImage(ADD_NEXT_IMAGE);
be222f56
PT
412 addNextButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addNextElement);
413 addNextButton.addSelectionListener(new SelectionAdapter() {
414 @Override
415 public void widgetSelected(SelectionEvent e) {
f5cc6ed1 416 CustomXmlInputElement inputElement = new CustomXmlInputElement("", false, Tag.IGNORE, Tag.IGNORE.toString(), 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$
be222f56
PT
417 if (definition.rootInputElement == null) {
418 definition.rootInputElement = inputElement;
a7418109 419 inputElement.setElementName(getChildNameSuggestion(null));
be222f56
PT
420 } else if (treeViewer.getSelection().isEmpty()) {
421 return;
422 } else {
a7418109 423 CustomXmlInputElement previousInputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
be222f56
PT
424 if (previousInputElement == definition.rootInputElement) {
425 return;
426 }
427 previousInputElement.addNext(inputElement);
a7418109 428 inputElement.setElementName(getChildNameSuggestion(inputElement.getParentElement()));
be222f56
PT
429 }
430 treeViewer.refresh();
431 treeViewer.setSelection(new StructuredSelection(inputElement), true);
432 }
433 });
434
f6aa55e2
PT
435 Button feelingLuckyButton = new Button(buttonBar, SWT.PUSH);
436 feelingLuckyButton.setImage(ADD_MANY_IMAGE);
be222f56
PT
437 feelingLuckyButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_feelingLucky);
438 feelingLuckyButton.addSelectionListener(new SelectionAdapter() {
439 @Override
440 public void widgetSelected(SelectionEvent e) {
a7418109 441 CustomXmlInputElement inputElement = null;
be222f56
PT
442 if (definition.rootInputElement == null) {
443 if (getChildNameSuggestion(null).length() != 0) {
f5cc6ed1 444 inputElement = new CustomXmlInputElement(getChildNameSuggestion(null), false, Tag.IGNORE, Tag.IGNORE.toString(), 0, "", null); //$NON-NLS-1$
be222f56
PT
445 definition.rootInputElement = inputElement;
446 feelingLucky(inputElement);
447 } else {
448 return;
449 }
450 } else if (treeViewer.getSelection().isEmpty()) {
451 return;
452 } else {
a7418109 453 inputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
be222f56
PT
454 feelingLucky(inputElement);
455 }
456 treeViewer.refresh();
457 treeViewer.setSelection(new StructuredSelection(inputElement), true);
458 treeViewer.expandToLevel(inputElement, AbstractTreeViewer.ALL_LEVELS);
459 }
460 });
461
462 moveUpButton = new Button(buttonBar, SWT.PUSH);
f6aa55e2 463 moveUpButton.setImage(MOVE_UP_IMAGE);
be222f56
PT
464 moveUpButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_moveUp);
465 moveUpButton.addSelectionListener(new SelectionAdapter() {
466 @Override
467 public void widgetSelected(SelectionEvent e) {
468 if (treeViewer.getSelection().isEmpty()) {
469 return;
470 }
a7418109 471 CustomXmlInputElement inputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
be222f56
PT
472 if (inputElement == definition.rootInputElement) {
473 return;
474 }
475 inputElement.moveUp();
476 treeViewer.refresh();
477 validate();
478 updatePreviews();
479 }
480 });
481
482 moveDownButton = new Button(buttonBar, SWT.PUSH);
f6aa55e2 483 moveDownButton.setImage(MOVE_DOWN_IMAGE);
be222f56
PT
484 moveDownButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_moveDown);
485 moveDownButton.addSelectionListener(new SelectionAdapter() {
486 @Override
487 public void widgetSelected(SelectionEvent e) {
488 if (treeViewer.getSelection().isEmpty()) {
489 return;
490 }
a7418109 491 CustomXmlInputElement inputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
be222f56
PT
492 if (inputElement == definition.rootInputElement) {
493 return;
494 }
495 inputElement.moveDown();
496 treeViewer.refresh();
497 validate();
498 updatePreviews();
499 }
500 });
501 }
502
a7418109 503 private void feelingLucky(CustomXmlInputElement inputElement) {
be222f56
PT
504 while (true) {
505 String attributeName = getAttributeNameSuggestion(inputElement);
506 if (attributeName.length() == 0) {
507 break;
508 }
f5cc6ed1 509 CustomXmlInputAttribute attribute = new CustomXmlInputAttribute(attributeName, Tag.OTHER, attributeName, 0, ""); //$NON-NLS-1$
be222f56
PT
510 inputElement.addAttribute(attribute);
511 }
512 while (true) {
513 String childName = getChildNameSuggestion(inputElement);
514 if (childName.length() == 0) {
515 break;
516 }
f5cc6ed1 517 CustomXmlInputElement childElement = new CustomXmlInputElement(childName, false, Tag.IGNORE, Tag.IGNORE.toString(), 0, "", null); //$NON-NLS-1$
be222f56
PT
518 inputElement.addChild(childElement);
519 feelingLucky(childElement);
520 }
521 }
522
523 private static class InputElementTreeNodeContentProvider implements ITreeContentProvider {
524
525 @Override
526 public Object[] getElements(Object inputElement) {
527 CustomXmlTraceDefinition def = (CustomXmlTraceDefinition) inputElement;
528 if (def.rootInputElement != null) {
529 return new Object[] { def.rootInputElement };
530 }
531 return new Object[0];
532 }
533
534 @Override
535 public Object[] getChildren(Object parentElement) {
a7418109
MK
536 CustomXmlInputElement inputElement = (CustomXmlInputElement) parentElement;
537 if (inputElement.getChildElements() == null) {
538 return new CustomXmlInputElement[0];
be222f56 539 }
a7418109 540 return inputElement.getChildElements().toArray();
be222f56
PT
541 }
542
543 @Override
544 public boolean hasChildren(Object element) {
a7418109
MK
545 CustomXmlInputElement inputElement = (CustomXmlInputElement) element;
546 return (inputElement.getChildElements() != null && inputElement.getChildElements().size() > 0);
be222f56
PT
547 }
548
549 @Override
550 public void dispose() {
551 }
552
553 @Override
554 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
555 }
556
557 @Override
558 public Object getParent(Object element) {
a7418109
MK
559 CustomXmlInputElement inputElement = (CustomXmlInputElement) element;
560 return inputElement.getParentElement();
be222f56
PT
561 }
562 }
563
564 private static class InputElementTreeLabelProvider extends ColumnLabelProvider {
565
566 @Override
567 public Image getImage(Object element) {
f6aa55e2 568 return ELEMENT_IMAGE;
be222f56
PT
569 }
570
571 @Override
572 public String getText(Object element) {
a7418109
MK
573 CustomXmlInputElement inputElement = (CustomXmlInputElement) element;
574 return (inputElement.getElementName().trim().length() == 0) ? "?" : inputElement.getElementName(); //$NON-NLS-1$
be222f56
PT
575 }
576 }
577
578 private class InputElementTreeSelectionChangedListener implements ISelectionChangedListener {
579 @Override
580 public void selectionChanged(SelectionChangedEvent event) {
581 if (selectedElement != null) {
582 selectedElement.dispose();
583 }
584 if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
3dca7aa5 585 IStructuredSelection sel = (IStructuredSelection) event.getSelection();
a7418109 586 CustomXmlInputElement inputElement = (CustomXmlInputElement) sel.getFirstElement();
be222f56
PT
587 selectedElement = new ElementNode(elementContainer, inputElement);
588 elementContainer.layout();
589 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x,
590 elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
591 container.layout();
592 validate();
593 updatePreviews();
594 removeButton.setEnabled(true);
595 addChildButton.setEnabled(true);
596 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addChildElement);
597 if (definition.rootInputElement == inputElement) {
598 addNextButton.setEnabled(false);
599 } else {
600 addNextButton.setEnabled(true);
601 }
602 moveUpButton.setEnabled(true);
603 moveDownButton.setEnabled(true);
604 } else {
605 removeButton.setEnabled(false);
606 if (definition.rootInputElement == null) {
607 addChildButton.setEnabled(true);
608 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentElement);
609 } else {
610 addChildButton.setEnabled(false);
611 }
612 addNextButton.setEnabled(false);
613 moveUpButton.setEnabled(false);
614 moveDownButton.setEnabled(false);
615 }
616 }
617 }
618
be222f56
PT
619 @Override
620 public void dispose() {
621 if (fixedFont != null) {
622 fixedFont.dispose();
623 fixedFont = null;
624 }
625 super.dispose();
626 }
627
628 private void loadDefinition(CustomXmlTraceDefinition def) {
c22ca172 629 categoryText.setText(def.categoryName);
be222f56 630 logtypeText.setText(def.definitionName);
4d12b563
PT
631 if (def.timeStampOutputFormat != null) {
632 timeStampOutputFormatText.setText(def.timeStampOutputFormat);
633 } else {
634 timeStampOutputFormatText.setText(""); //$NON-NLS-1$
635 }
be222f56
PT
636 treeViewer.setInput(def);
637
638 if (def.rootInputElement != null) {
639 treeViewer.setSelection(new StructuredSelection(def.rootInputElement));
640 }
641 }
642
a7418109
MK
643 private String getName(CustomXmlInputElement inputElement) {
644 String name = (inputElement.getElementName().trim().length() == 0) ? "?" : inputElement.getElementName().trim(); //$NON-NLS-1$
645 if (inputElement.getParentElement() == null) {
be222f56
PT
646 return name;
647 }
a7418109 648 return getName(inputElement.getParentElement()) + " : " + name; //$NON-NLS-1$
be222f56
PT
649 }
650
a7418109
MK
651 private String getName(CustomXmlInputAttribute inputAttribute, CustomXmlInputElement inputElement) {
652 String name = (inputAttribute.getAttributeName().trim().length() == 0) ? "?" : inputAttribute.getAttributeName().trim(); //$NON-NLS-1$
be222f56
PT
653 return getName(inputElement) + " : " + name; //$NON-NLS-1$
654 }
655
be222f56
PT
656 @Override
657 public void setVisible(boolean visible) {
658 if (visible) {
659 validate();
660 updatePreviews();
661 }
662 super.setVisible(visible);
663 }
664
a0a88f65 665 /**
f5cc6ed1 666 * Get the global list of inputs.
a0a88f65 667 *
f5cc6ed1 668 * @return The list of inputs
a0a88f65 669 */
f5cc6ed1
PT
670 public List<Entry<Tag, String>> getInputs() {
671 return getInputs(definition.rootInputElement);
be222f56
PT
672 }
673
a0a88f65 674 /**
f5cc6ed1 675 * Get the list of inputs for a given element, recursively.
a0a88f65
AM
676 *
677 * @param inputElement
678 * The element
f5cc6ed1 679 * @return The list of inputs
a0a88f65 680 */
f5cc6ed1
PT
681 public List<Entry<Tag, String>> getInputs(CustomXmlInputElement inputElement) {
682 List<Entry<Tag, String>> inputs = new ArrayList<>();
683 if (inputElement.getInputTag() != null && !inputElement.getInputTag().equals(Tag.IGNORE)) {
684 Entry<Tag, String> input = new SimpleEntry<>(inputElement.getInputTag(), inputElement.getInputName());
685 if (!inputs.contains(input)) {
686 inputs.add(input);
be222f56
PT
687 }
688 }
a7418109
MK
689 if (inputElement.getAttributes() != null) {
690 for (CustomXmlInputAttribute attribute : inputElement.getAttributes()) {
f5cc6ed1
PT
691 Entry<Tag, String> input = new SimpleEntry<>(attribute.getInputTag(), attribute.getInputName());
692 if (!inputs.contains(input)) {
693 inputs.add(input);
be222f56
PT
694 }
695 }
696 }
a7418109
MK
697 if (inputElement.getChildElements() != null) {
698 for (CustomXmlInputElement childInputElement : inputElement.getChildElements()) {
f5cc6ed1
PT
699 for (Entry<Tag, String> input : getInputs(childInputElement)) {
700 if (!inputs.contains(input)) {
701 inputs.add(input);
be222f56
PT
702 }
703 }
704 }
705 }
706 return inputs;
707 }
708
709 private void removeElement() {
710 selectedElement.dispose();
711 selectedElement = null;
712 elementContainer.layout();
713 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x,
714 elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
715 container.layout();
716 }
717
718 private String getSelectionText() {
719 InputStream inputStream = null;
720 if (this.selection instanceof IStructuredSelection) {
3dca7aa5
AM
721 Object sel = ((IStructuredSelection) this.selection).getFirstElement();
722 if (sel instanceof IFile) {
723 IFile file = (IFile) sel;
be222f56
PT
724 try {
725 inputStream = file.getContents();
726 } catch (CoreException e) {
727 return ""; //$NON-NLS-1$
728 }
729 }
730 }
731 if (inputStream != null) {
507b1336 732 try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));) {
be222f56
PT
733 StringBuilder sb = new StringBuilder();
734 String line = null;
735 while ((line = reader.readLine()) != null) {
5e1ee19a
JCK
736 sb.append(line);
737 sb.append('\n');
be222f56
PT
738 }
739 parseXmlInput(sb.toString());
be222f56
PT
740 return sb.toString();
741 } catch (IOException e) {
742 return ""; //$NON-NLS-1$
743 }
744 }
745 return ""; //$NON-NLS-1$
746 }
747
748 private void parseXmlInput(final String string) {
749 try {
750 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
751 DocumentBuilder db = dbf.newDocumentBuilder();
752
753 // The following allows xml parsing without access to the dtd
30e9ac41
MK
754 EntityResolver resolver = (publicId, systemId) -> {
755 String empty = ""; //$NON-NLS-1$
756 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
757 return new InputSource(bais);
be222f56
PT
758 };
759 db.setEntityResolver(resolver);
760
761 // The following catches xml parsing exceptions
762 db.setErrorHandler(new ErrorHandler() {
763 @Override
764 public void error(SAXParseException saxparseexception) throws SAXException {
765 }
766
767 @Override
768 public void warning(SAXParseException saxparseexception) throws SAXException {
769 }
770
771 @Override
772 public void fatalError(SAXParseException saxparseexception) throws SAXException {
773 if (string.trim().length() != 0) {
774 errorText.setText(saxparseexception.getMessage());
775 errorText.setBackground(COLOR_LIGHT_RED);
776 errorText.setVisible(true);
777 }
778 throw saxparseexception;
779 }
780 });
781
782 errorText.setVisible(false);
783 Document doc = null;
784 doc = db.parse(new ByteArrayInputStream(string.getBytes()));
785 documentElement = doc.getDocumentElement();
786 } catch (ParserConfigurationException e) {
787 Activator.getDefault().logError("Error pasing XML input string: " + string, e); //$NON-NLS-1$
788 documentElement = null;
789 } catch (SAXException e) {
790 documentElement = null;
791 } catch (IOException e) {
792 Activator.getDefault().logError("Error pasing XML input string: " + string, e); //$NON-NLS-1$
793 documentElement = null;
794 }
795 }
796
be222f56
PT
797 private void initValues() {
798 timeStampValue = null;
799 timeStampFormat = null;
800 logEntriesCount = 0;
801 logEntryFound = false;
802 }
803
3dca7aa5 804 private void updatePreviews() {
be222f56
PT
805 if (inputText == null) {
806 // early update during construction
807 return;
808 }
809 inputText.setStyleRanges(new StyleRange[] {});
810 if (selectedElement == null) {
811 return;
812 }
813
814 initValues();
815
816 selectedElement.updatePreview();
817
818 if (timeStampValue != null && timeStampFormat != null) {
819 try {
738beb68
PT
820 TmfTimestampFormat timestampFormat = new TmfTimestampFormat(timeStampFormat);
821 long timestamp = timestampFormat.parseValue(timeStampValue);
4d12b563
PT
822 if (timeStampOutputFormatText.getText().trim().isEmpty()) {
823 timestampFormat = new TmfTimestampFormat();
824 } else {
825 timestampFormat = new TmfTimestampFormat(timeStampOutputFormatText.getText().trim());
826 }
738beb68 827 timeStampPreviewText.setText(timestampFormat.format(timestamp));
be222f56
PT
828 } catch (ParseException e) {
829 timeStampPreviewText.setText("*parse exception* [" + timeStampValue + "] <> [" + timeStampFormat + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
830 } catch (IllegalArgumentException e) {
831 timeStampPreviewText.setText("*parse exception* [Illegal Argument]"); //$NON-NLS-1$
832 }
833 } else {
834 timeStampPreviewText.setText("*no matching time stamp*"); //$NON-NLS-1$
835 }
836 }
837
838 private void openHelpShell(String url) {
839 if (helpBrowser != null && !helpBrowser.isDisposed()) {
840 helpBrowser.getShell().setActive();
841 if (!helpBrowser.getUrl().equals(url)) {
842 helpBrowser.setUrl(url);
843 }
844 return;
845 }
846 final Shell helpShell = new Shell(getShell(), SWT.SHELL_TRIM);
847 helpShell.setLayout(new FillLayout());
848 helpBrowser = new Browser(helpShell, SWT.NONE);
30e9ac41 849 helpBrowser.addTitleListener(event -> helpShell.setText(event.title));
313a5081
PT
850 Rectangle r = container.getBounds();
851 Point p = container.toDisplay(r.x, r.y);
852 Rectangle trim = helpShell.computeTrim(p.x + (r.width - 750) / 2, p.y + (r.height - 400) / 2, 750, 400);
853 helpShell.setBounds(trim);
be222f56
PT
854 helpShell.open();
855 helpBrowser.setUrl(url);
856 }
857
858 private class UpdateListener implements ModifyListener, SelectionListener {
859
860 @Override
861 public void modifyText(ModifyEvent e) {
862 validate();
863 updatePreviews();
864 }
865
866 @Override
867 public void widgetDefaultSelected(SelectionEvent e) {
868 validate();
869 updatePreviews();
870 }
871
872 @Override
873 public void widgetSelected(SelectionEvent e) {
874 validate();
875 updatePreviews();
876 }
877
878 }
879
880 private class ElementNode {
a7418109 881 private final CustomXmlInputElement inputElement;
f6aa55e2 882 private final Group group;
507b1336
AM
883 private List<Attribute> attributes = new ArrayList<>();
884 private List<ElementNode> childElements = new ArrayList<>();
f6aa55e2
PT
885 private Text elementNameText;
886 private Composite tagComposite;
887 private Combo tagCombo;
888 private Label tagLabel;
889 private Text tagText;
890 private Combo actionCombo;
891 private Label previewLabel;
892 private Text previewText;
893 private Button logEntryButton;
eddf2682
PT
894 private Button eventTypeButton;
895 private Text eventTypeText;
f6aa55e2
PT
896 private Label fillerLabel;
897 private Composite addAttributeComposite;
898 private Button addAttributeButton;
899 private Label addAttributeLabel;
be222f56 900
a7418109 901 public ElementNode(Composite parent, CustomXmlInputElement inputElement) {
be222f56
PT
902 this.inputElement = inputElement;
903
904 group = new Group(parent, SWT.NONE);
905 GridLayout gl = new GridLayout(2, false);
906 gl.marginHeight = 0;
907 group.setLayout(gl);
908 group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
909 group.setText(getName(inputElement));
910
911 Label label = new Label(group, SWT.NULL);
912 label.setText(Messages.CustomXmlParserInputWizardPage_elementName);
913 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
914
915 elementNameText = new Text(group, SWT.BORDER | SWT.SINGLE);
916 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
917 gd.widthHint = 0;
918 elementNameText.setLayoutData(gd);
30e9ac41
MK
919 elementNameText.addModifyListener(e -> {
920 ElementNode.this.inputElement.setElementName(elementNameText.getText().trim());
921 group.setText(getName(ElementNode.this.inputElement));
be222f56 922 });
a7418109 923 elementNameText.setText(inputElement.getElementName());
be222f56
PT
924 elementNameText.addModifyListener(updateListener);
925
a7418109 926 if (inputElement.getParentElement() != null) {
be222f56
PT
927 previewLabel = new Label(group, SWT.NULL);
928 previewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
929 previewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview);
930
931 previewText = new Text(group, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
932 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
933 gd.widthHint = 0;
934 previewText.setLayoutData(gd);
935 previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement);
936 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
937
938 logEntryButton = new Button(group, SWT.CHECK);
939 logEntryButton.setText(Messages.CustomXmlParserInputWizardPage_logEntry);
a7418109 940 logEntryButton.setSelection(inputElement.isLogEntry());
be222f56
PT
941 logEntryButton.addSelectionListener(new SelectionListener() {
942 @Override
943 public void widgetDefaultSelected(SelectionEvent e) {
944 }
945
946 @Override
947 public void widgetSelected(SelectionEvent e) {
a7418109 948 CustomXmlInputElement parentElem = ElementNode.this.inputElement.getParentElement();
3dca7aa5 949 while (parentElem != null) {
a7418109
MK
950 parentElem.setLogEntry(false);
951 parentElem = parentElem.getParentElement();
be222f56
PT
952 }
953 }
954 });
955 logEntryButton.addSelectionListener(updateListener);
956
957 tagComposite = new Composite(group, SWT.FILL);
958 GridLayout tagLayout = new GridLayout(4, false);
959 tagLayout.marginWidth = 0;
960 tagLayout.marginHeight = 0;
961 tagComposite.setLayout(tagLayout);
962 tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
963
964 tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
f5cc6ed1
PT
965 tagCombo.setItems(new String[] {
966 Tag.IGNORE.toString(),
967 Tag.TIMESTAMP.toString(),
968 Tag.EVENT_TYPE.toString(),
969 Tag.MESSAGE.toString(),
efeeb733
GB
970 Tag.EXTRA_FIELD_NAME.toString(),
971 Tag.EXTRA_FIELD_VALUE.toString(),
f5cc6ed1 972 Tag.OTHER.toString() });
be222f56
PT
973 tagCombo.setVisibleItemCount(tagCombo.getItemCount());
974 tagCombo.addSelectionListener(new SelectionListener() {
975 @Override
976 public void widgetDefaultSelected(SelectionEvent e) {
977 }
978
979 @Override
980 public void widgetSelected(SelectionEvent e) {
981 tagText.removeModifyListener(updateListener);
982 switch (tagCombo.getSelectionIndex()) {
983 case 0: // Ignore
984 tagLabel.setVisible(false);
985 tagText.setVisible(false);
986 actionCombo.setVisible(false);
987 break;
988 case 1: // Time Stamp
989 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
990 tagLabel.setVisible(true);
991 tagText.setVisible(true);
992 tagText.addModifyListener(updateListener);
993 actionCombo.setVisible(true);
994 break;
53f17e49 995 case 2: // Event type
be222f56
PT
996 tagLabel.setVisible(false);
997 tagText.setVisible(false);
998 actionCombo.setVisible(true);
999 break;
53f17e49
GB
1000 case 3: // Message
1001 tagLabel.setVisible(false);
1002 tagText.setVisible(false);
1003 actionCombo.setVisible(true);
1004 break;
efeeb733
GB
1005 case 4: // Field names
1006 tagLabel.setVisible(false);
1007 tagText.setVisible(false);
1008 actionCombo.setVisible(false);
1009 break;
1010 case 5: // Field values
1011 tagLabel.setVisible(false);
1012 tagText.setVisible(false);
1013 actionCombo.setVisible(true);
1014 break;
1015 case 6: // Other
b97eff50 1016 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
be222f56
PT
1017 tagLabel.setVisible(true);
1018 if (tagText.getText().trim().length() == 0) {
1019 tagText.setText(elementNameText.getText().trim());
1020 }
1021 tagText.setVisible(true);
1022 tagText.addModifyListener(updateListener);
1023 actionCombo.setVisible(true);
1024 break;
1025 default:
1026 break;
1027 }
1028 tagComposite.layout();
1029 validate();
1030 updatePreviews();
1031 }
1032 });
1033
1034 tagLabel = new Label(tagComposite, SWT.NULL);
1035 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1036
1037 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
1038 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1039 gd.widthHint = 0;
1040 tagText.setLayoutData(gd);
1041
1042 actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1043 actionCombo.setItems(new String[] { Messages.CustomXmlParserInputWizardPage_set, Messages.CustomXmlParserInputWizardPage_append,
1044 Messages.CustomXmlParserInputWizardPage_appendWith });
a7418109 1045 actionCombo.select(inputElement.getInputAction());
be222f56
PT
1046 actionCombo.addSelectionListener(updateListener);
1047
f5cc6ed1 1048 if (inputElement.getInputTag().equals(Tag.IGNORE)) {
be222f56
PT
1049 tagCombo.select(0);
1050 tagLabel.setVisible(false);
1051 tagText.setVisible(false);
1052 actionCombo.setVisible(false);
f5cc6ed1 1053 } else if (inputElement.getInputTag().equals(Tag.TIMESTAMP)) {
be222f56
PT
1054 tagCombo.select(1);
1055 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
a7418109 1056 tagText.setText(inputElement.getInputFormat());
be222f56 1057 tagText.addModifyListener(updateListener);
efeeb733 1058 actionCombo.setVisible(true);
f5cc6ed1 1059 } else if (inputElement.getInputTag().equals(Tag.EVENT_TYPE)) {
be222f56
PT
1060 tagCombo.select(2);
1061 tagLabel.setVisible(false);
1062 tagText.setVisible(false);
efeeb733 1063 actionCombo.setVisible(true);
f5cc6ed1 1064 } else if (inputElement.getInputTag().equals(Tag.MESSAGE)) {
be222f56 1065 tagCombo.select(3);
53f17e49
GB
1066 tagLabel.setVisible(false);
1067 tagText.setVisible(false);
efeeb733
GB
1068 actionCombo.setVisible(true);
1069 } else if (inputElement.getInputTag().equals(Tag.EXTRA_FIELD_NAME)) {
53f17e49 1070 tagCombo.select(4);
efeeb733
GB
1071 tagLabel.setVisible(false);
1072 tagText.setVisible(false);
1073 actionCombo.setVisible(false);
1074 } else if (inputElement.getInputTag().equals(Tag.EXTRA_FIELD_VALUE)) {
1075 tagCombo.select(5);
1076 tagLabel.setVisible(false);
1077 tagText.setVisible(false);
1078 actionCombo.setVisible(true);
1079 } else {
1080 tagCombo.select(6);
b97eff50 1081 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
a7418109 1082 tagText.setText(inputElement.getInputName());
be222f56 1083 tagText.addModifyListener(updateListener);
efeeb733 1084 actionCombo.setVisible(true);
be222f56 1085 }
eddf2682
PT
1086
1087 eventTypeButton = new Button(group, SWT.CHECK);
1088 eventTypeButton.setText(Messages.CustomTxtParserInputWizardPage_eventType);
1089 eventTypeButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1090 eventTypeButton.addSelectionListener(new SelectionAdapter() {
1091 @Override
1092 public void widgetSelected(SelectionEvent e) {
1093 if (eventTypeButton.getSelection()) {
1094 eventTypeText.setEnabled(true);
1095 } else {
1096 eventTypeText.setEnabled(false);
1097 }
1098 }
1099 });
1100 eventTypeButton.addSelectionListener(updateListener);
1101
1102 eventTypeText = new Text(group, SWT.BORDER | SWT.SINGLE);
1103 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1104 gd.widthHint = 0;
1105 eventTypeText.setLayoutData(gd);
1106 if (inputElement.getEventType() != null) {
1107 eventTypeText.setText(inputElement.getEventType());
1108 eventTypeButton.setSelection(true);
1109 } else {
1110 eventTypeText.setEnabled(false);
1111 eventTypeButton.setSelection(false);
1112 }
1113 eventTypeText.addModifyListener(updateListener);
be222f56
PT
1114 }
1115
a7418109
MK
1116 if (inputElement.getAttributes() != null) {
1117 for (CustomXmlInputAttribute inputAttribute : inputElement.getAttributes()) {
be222f56
PT
1118 Attribute attribute = new Attribute(group, this, inputAttribute, attributes.size() + 1);
1119 attributes.add(attribute);
1120 }
1121 }
1122
1123 createAddButton();
1124 }
1125
1126 private void updatePreview() {
1127 Element element = getPreviewElement(inputElement);
b97eff50
PT
1128 // no preview text for document element
1129 if (inputElement.getParentElement() != null) {
be222f56
PT
1130 previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement);
1131 if (element != null) {
1132 previewText.setText(CustomXmlTrace.parseElement(element, new StringBuffer()).toString());
1133 if (logEntryButton.getSelection()) {
1134 if (!logEntryFound) {
1135 logEntryFound = true;
1136 logEntriesCount++;
1137 } else {
b97eff50
PT
1138 // remove nested log entry
1139 logEntryButton.setSelection(false);
be222f56
PT
1140 }
1141 }
f5cc6ed1 1142 if (tagCombo.getText().equals(Tag.TIMESTAMP.toString()) && logEntriesCount <= 1) {
be222f56
PT
1143 String value = previewText.getText().trim();
1144 if (value.length() != 0) {
1145 if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_SET) {
1146 timeStampValue = value;
1147 timeStampFormat = tagText.getText().trim();
1148 } else if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND) {
1149 if (timeStampValue != null) {
1150 timeStampValue += value;
1151 timeStampFormat += tagText.getText().trim();
1152 } else {
1153 timeStampValue = value;
1154 timeStampFormat = tagText.getText().trim();
1155 }
1156 } else if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
1157 if (timeStampValue != null) {
efeeb733
GB
1158 timeStampValue += CustomTraceDefinition.SEPARATOR + value;
1159 timeStampFormat += CustomTraceDefinition.SEPARATOR + tagText.getText().trim();
be222f56
PT
1160 } else {
1161 timeStampValue = value;
1162 timeStampFormat = tagText.getText().trim();
1163 }
1164 }
1165 }
1166 }
1167 }
1168 }
1169 for (Attribute attribute : attributes) {
1170 if (element != null) {
1171 String value = element.getAttribute(attribute.attributeNameText.getText().trim());
1172 if (value.length() != 0) {
1173 attribute.previewText.setText(value);
f5cc6ed1 1174 if (attribute.tagCombo.getText().equals(Tag.TIMESTAMP.toString()) && logEntriesCount <= 1) {
be222f56
PT
1175 if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_SET) {
1176 timeStampValue = value;
1177 timeStampFormat = attribute.tagText.getText().trim();
1178 } else if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND) {
1179 if (timeStampValue != null) {
1180 timeStampValue += value;
1181 timeStampFormat += attribute.tagText.getText().trim();
1182 } else {
1183 timeStampValue = value;
1184 timeStampFormat = attribute.tagText.getText().trim();
1185 }
1186 } else if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
1187 if (timeStampValue != null) {
1188 timeStampValue += " | " + value; //$NON-NLS-1$
1189 timeStampFormat += " | " + attribute.tagText.getText().trim(); //$NON-NLS-1$
1190 } else {
1191 timeStampValue = value;
1192 timeStampFormat = attribute.tagText.getText().trim();
1193 }
1194 }
1195 }
1196 } else {
1197 attribute.previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingAttribute);
1198 }
1199 } else {
1200 attribute.previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement);
1201 }
1202 }
1203 for (ElementNode child : childElements) {
1204 child.updatePreview();
1205 }
1206 if (logEntryButton != null && logEntryButton.getSelection()) {
1207 logEntryFound = false;
1208 }
1209 }
1210
1211 private void createAddButton() {
1212 fillerLabel = new Label(group, SWT.NONE);
1213
1214 addAttributeComposite = new Composite(group, SWT.NONE);
1215 addAttributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1216 GridLayout addAttributeLayout = new GridLayout(2, false);
1217 addAttributeLayout.marginHeight = 0;
1218 addAttributeLayout.marginWidth = 0;
1219 addAttributeComposite.setLayout(addAttributeLayout);
1220
1221 addAttributeButton = new Button(addAttributeComposite, SWT.PUSH);
f6aa55e2 1222 addAttributeButton.setImage(ADD_IMAGE);
be222f56
PT
1223 addAttributeButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addAttribute);
1224 addAttributeButton.addSelectionListener(new SelectionAdapter() {
1225 @Override
1226 public void widgetSelected(SelectionEvent e) {
1227 removeAddButton();
1228 String attributeName = getAttributeNameSuggestion(inputElement);
f5cc6ed1 1229 CustomXmlInputAttribute inputAttribute = new CustomXmlInputAttribute(attributeName, Tag.OTHER, attributeName, 0, ""); //$NON-NLS-1$
be222f56
PT
1230 attributes.add(new Attribute(group, ElementNode.this, inputAttribute, attributes.size() + 1));
1231 createAddButton();
1232 elementContainer.layout();
1233 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x,
1234 elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
1235 group.getParent().layout();
1236 validate();
1237 updatePreviews();
1238 }
1239 });
1240
1241 addAttributeLabel = new Label(addAttributeComposite, SWT.NULL);
1242 addAttributeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1243 addAttributeLabel.setText(Messages.CustomXmlParserInputWizardPage_newAttibute);
1244 }
1245
1246 private void removeAddButton() {
1247 fillerLabel.dispose();
1248 addAttributeComposite.dispose();
1249 }
1250
1251 private void removeAttribute(int attributeNumber) {
41b5c37f
AM
1252 int nb = attributeNumber;
1253 if (--nb < attributes.size()) {
1254 attributes.remove(nb).dispose();
1255 for (int i = nb; i < attributes.size(); i++) {
be222f56
PT
1256 attributes.get(i).setAttributeNumber(i + 1);
1257 }
1258 elementContainer.layout();
1259 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x,
1260 elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
1261 group.getParent().layout();
1262 }
1263 }
1264
1265 private void dispose() {
1266 group.dispose();
1267 }
1268
1269 private void extractInputs() {
a7418109
MK
1270 inputElement.setElementName(elementNameText.getText().trim());
1271 if (inputElement.getParentElement() != null) {
1272 inputElement.setLogEntry(logEntryButton.getSelection());
eddf2682 1273 inputElement.setEventType(eventTypeButton.getSelection() ? eventTypeText.getText().trim() : null);
f5cc6ed1
PT
1274 Tag inputTag = Tag.fromLabel(tagCombo.getText());
1275 inputElement.setInputTag(inputTag);
1276 if (inputTag.equals(Tag.OTHER)) {
a7418109 1277 inputElement.setInputName(tagText.getText().trim());
be222f56 1278 } else {
f5cc6ed1
PT
1279 inputElement.setInputName(inputTag.toString());
1280 if (inputTag.equals(Tag.TIMESTAMP)) {
a7418109 1281 inputElement.setInputFormat(tagText.getText().trim());
be222f56
PT
1282 }
1283 }
a7418109 1284 inputElement.setInputAction(actionCombo.getSelectionIndex());
be222f56 1285 }
a7418109 1286 inputElement.setAttributes(new ArrayList<CustomXmlInputAttribute>(attributes.size()));
be222f56 1287 for (int i = 0; i < attributes.size(); i++) {
a7418109
MK
1288 String inputName = null;
1289 String inputFormat = null;
be222f56 1290 Attribute attribute = attributes.get(i);
a7418109 1291 String attributeName = attribute.attributeNameText.getText().trim();
f5cc6ed1
PT
1292 Tag inputTag = Tag.fromLabel(attribute.tagCombo.getText());
1293 if (inputTag.equals(Tag.OTHER)) {
a7418109 1294 inputName = attribute.tagText.getText().trim();
be222f56 1295 } else {
f5cc6ed1
PT
1296 inputName = inputTag.toString();
1297 if (inputTag.equals(Tag.TIMESTAMP)) {
a7418109 1298 inputFormat = attribute.tagText.getText().trim();
be222f56
PT
1299 }
1300 }
a7418109 1301 int inputAction = attribute.actionCombo.getSelectionIndex();
f5cc6ed1 1302 inputElement.addAttribute(new CustomXmlInputAttribute(attributeName, inputTag, inputName, inputAction, inputFormat));
be222f56
PT
1303 }
1304 }
1305 }
1306
1307 private class Attribute {
f6aa55e2
PT
1308 private ElementNode element;
1309 private int attributeNumber;
be222f56
PT
1310
1311 // children of parent (must be disposed)
f6aa55e2
PT
1312 private Composite labelComposite;
1313 private Composite attributeComposite;
1314 private Label filler;
1315 private Composite tagComposite;
be222f56
PT
1316
1317 // children of labelComposite
f6aa55e2 1318 private Label attributeLabel;
be222f56
PT
1319
1320 // children of attributeComposite
f6aa55e2
PT
1321 private Text attributeNameText;
1322 private Text previewText;
be222f56
PT
1323
1324 // children of tagComposite
f6aa55e2
PT
1325 private Combo tagCombo;
1326 private Label tagLabel;
1327 private Text tagText;
1328 private Combo actionCombo;
be222f56 1329
a7418109 1330 public Attribute(Composite parent, ElementNode element, CustomXmlInputAttribute inputAttribute, int attributeNumber) {
be222f56
PT
1331 this.element = element;
1332 this.attributeNumber = attributeNumber;
1333
1334 labelComposite = new Composite(parent, SWT.FILL);
1335 GridLayout labelLayout = new GridLayout(2, false);
1336 labelLayout.marginWidth = 0;
1337 labelLayout.marginHeight = 0;
1338 labelComposite.setLayout(labelLayout);
1339 labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1340
1341 Button deleteButton = new Button(labelComposite, SWT.PUSH);
1342 deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
f6aa55e2 1343 deleteButton.setImage(DELETE_IMAGE);
be222f56
PT
1344 deleteButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_removeAttribute);
1345 deleteButton.addSelectionListener(new SelectionAdapter() {
1346 @Override
1347 public void widgetSelected(SelectionEvent e) {
1348 Attribute.this.element.removeAttribute(Attribute.this.attributeNumber);
1349 validate();
1350 updatePreviews();
1351 }
1352 });
1353
1354 attributeLabel = new Label(labelComposite, SWT.NULL);
1355 attributeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1356 attributeLabel.setText(Messages.CustomXmlParserInputWizardPage_attibute);
1357
1358 attributeComposite = new Composite(parent, SWT.FILL);
1359 GridLayout attributeLayout = new GridLayout(4, false);
1360 attributeLayout.marginWidth = 0;
1361 attributeLayout.marginHeight = 0;
1362 attributeComposite.setLayout(attributeLayout);
1363 attributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1364
1365 Label nameLabel = new Label(attributeComposite, SWT.NONE);
1366 nameLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
1367
1368 attributeNameText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE);
1369 attributeNameText.setLayoutData(new GridData(120, SWT.DEFAULT));
a7418109 1370 attributeNameText.setText(inputAttribute.getAttributeName());
be222f56
PT
1371 attributeNameText.addModifyListener(updateListener);
1372
1373 Label previewLabel = new Label(attributeComposite, SWT.NONE);
1374 previewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview);
1375
1376 previewText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
1377 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1378 gd.widthHint = 0;
1379 previewText.setLayoutData(gd);
1380 previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatch);
1381 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
1382
1383 filler = new Label(parent, SWT.NULL);
1384
1385 tagComposite = new Composite(parent, SWT.FILL);
1386 GridLayout tagLayout = new GridLayout(4, false);
1387 tagLayout.marginWidth = 0;
1388 tagLayout.marginHeight = 0;
1389 tagComposite.setLayout(tagLayout);
1390 tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1391
1392 tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
f5cc6ed1
PT
1393 tagCombo.setItems(new String[] {
1394 Tag.TIMESTAMP.toString(),
1395 Tag.EVENT_TYPE.toString(),
1396 Tag.MESSAGE.toString(),
efeeb733
GB
1397 Tag.EXTRA_FIELD_NAME.toString(),
1398 Tag.EXTRA_FIELD_VALUE.toString(),
f5cc6ed1 1399 Tag.OTHER.toString() });
53f17e49 1400 tagCombo.select(3); // Other
be222f56
PT
1401 tagCombo.addSelectionListener(new SelectionListener() {
1402 @Override
1403 public void widgetDefaultSelected(SelectionEvent e) {
1404 }
1405
1406 @Override
1407 public void widgetSelected(SelectionEvent e) {
1408 tagText.removeModifyListener(updateListener);
1409 switch (tagCombo.getSelectionIndex()) {
1410 case 0: // Time Stamp
1411 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
1412 tagLabel.setVisible(true);
1413 tagText.setVisible(true);
1414 tagText.addModifyListener(updateListener);
efeeb733 1415 actionCombo.setVisible(true);
be222f56 1416 break;
53f17e49 1417 case 1: // Event type
be222f56
PT
1418 tagLabel.setVisible(false);
1419 tagText.setVisible(false);
efeeb733 1420 actionCombo.setVisible(true);
be222f56 1421 break;
53f17e49
GB
1422 case 2: // Message
1423 tagLabel.setVisible(false);
1424 tagText.setVisible(false);
efeeb733
GB
1425 actionCombo.setVisible(true);
1426 break;
1427 case 3: // Field names
1428 tagLabel.setVisible(false);
1429 tagText.setVisible(false);
1430 actionCombo.setVisible(false);
1431 break;
1432 case 4: // Field values
1433 tagLabel.setVisible(false);
1434 tagText.setVisible(false);
1435 actionCombo.setVisible(true);
53f17e49 1436 break;
efeeb733 1437 case 5: // Other
b97eff50 1438 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
be222f56
PT
1439 tagLabel.setVisible(true);
1440 if (tagText.getText().trim().length() == 0) {
1441 tagText.setText(attributeNameText.getText().trim());
1442 }
1443 tagText.setVisible(true);
1444 tagText.addModifyListener(updateListener);
efeeb733 1445 actionCombo.setVisible(true);
be222f56
PT
1446 break;
1447 default:
1448 break;
1449 }
1450 tagComposite.layout();
1451 validate();
1452 updatePreviews();
1453 }
1454 });
1455
1456 tagLabel = new Label(tagComposite, SWT.NULL);
1457 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1458
1459 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
1460 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1461 gd.widthHint = 0;
1462 tagText.setLayoutData(gd);
1463 tagText.setText(attributeNameText.getText());
1464
1465 actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1466 actionCombo.setItems(new String[] { Messages.CustomXmlParserInputWizardPage_set, Messages.CustomXmlParserInputWizardPage_append,
1467 Messages.CustomXmlParserInputWizardPage_appendWith });
a7418109 1468 actionCombo.select(inputAttribute.getInputAction());
be222f56
PT
1469 actionCombo.addSelectionListener(updateListener);
1470
f5cc6ed1 1471 if (inputAttribute.getInputTag().equals(Tag.TIMESTAMP)) {
be222f56
PT
1472 tagCombo.select(0);
1473 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
a7418109 1474 tagText.setText(inputAttribute.getInputFormat());
be222f56 1475 tagText.addModifyListener(updateListener);
efeeb733 1476 actionCombo.setVisible(true);
f5cc6ed1 1477 } else if (inputAttribute.getInputTag().equals(Tag.EVENT_TYPE)) {
be222f56
PT
1478 tagCombo.select(1);
1479 tagLabel.setVisible(false);
1480 tagText.setVisible(false);
efeeb733 1481 actionCombo.setVisible(true);
f5cc6ed1 1482 } else if (inputAttribute.getInputTag().equals(Tag.MESSAGE)) {
be222f56 1483 tagCombo.select(2);
53f17e49
GB
1484 tagLabel.setVisible(false);
1485 tagText.setVisible(false);
efeeb733
GB
1486 actionCombo.setVisible(true);
1487 } else if (inputAttribute.getInputTag().equals(Tag.EXTRA_FIELD_NAME)) {
53f17e49 1488 tagCombo.select(3);
efeeb733
GB
1489 tagLabel.setVisible(false);
1490 tagText.setVisible(false);
1491 actionCombo.setVisible(false);
1492 } else if (inputAttribute.getInputTag().equals(Tag.EXTRA_FIELD_VALUE)) {
1493 tagCombo.select(4);
1494 tagLabel.setVisible(false);
1495 tagText.setVisible(false);
1496 actionCombo.setVisible(true);
1497 } else {
1498 tagCombo.select(5);
b97eff50 1499 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
a7418109 1500 tagText.setText(inputAttribute.getInputName());
be222f56 1501 tagText.addModifyListener(updateListener);
efeeb733 1502 actionCombo.setVisible(true);
be222f56
PT
1503 }
1504 }
1505
1506 private void dispose() {
1507 labelComposite.dispose();
1508 attributeComposite.dispose();
1509 filler.dispose();
1510 tagComposite.dispose();
1511 }
1512
1513 private void setAttributeNumber(int attributeNumber) {
1514 this.attributeNumber = attributeNumber;
1515 labelComposite.layout();
1516 }
1517 }
1518
a7418109
MK
1519 private Element getPreviewElement(CustomXmlInputElement inputElement) {
1520 CustomXmlInputElement currentElement = inputElement;
be222f56
PT
1521 Element element = documentElement;
1522 if (element != null) {
a7418109 1523 if (!documentElement.getNodeName().equals(definition.rootInputElement.getElementName())) {
be222f56
PT
1524 return null;
1525 }
507b1336 1526 ArrayList<String> elementNames = new ArrayList<>();
41b5c37f 1527 while (currentElement != null) {
a7418109
MK
1528 elementNames.add(currentElement.getElementName());
1529 currentElement = currentElement.getParentElement();
be222f56
PT
1530 }
1531 for (int i = elementNames.size() - 1; --i >= 0;) {
1532 NodeList childList = element.getChildNodes();
1533 element = null;
1534 for (int j = 0; j < childList.getLength(); j++) {
1535 Node child = childList.item(j);
1536 if (child instanceof Element && child.getNodeName().equals(elementNames.get(i))) {
1537 element = (Element) child;
1538 break;
1539 }
1540 }
1541 if (element == null) {
1542 break;
1543 }
1544 }
1545 if (element != null) {
1546 return element;
1547 }
1548 }
1549 return null;
1550 }
1551
a7418109 1552 private String getChildNameSuggestion(CustomXmlInputElement inputElement) {
be222f56
PT
1553 if (inputElement == null) {
1554 if (documentElement != null) {
1555 return documentElement.getNodeName();
1556 }
1557 } else {
1558 Element element = getPreviewElement(inputElement);
1559 if (element != null) {
1560 NodeList childNodes = element.getChildNodes();
1561 for (int i = 0; i < childNodes.getLength(); i++) {
1562 Node node = childNodes.item(i);
1563 if (node instanceof Element) {
1564 boolean unused = true;
a7418109
MK
1565 if (inputElement.getChildElements() != null) {
1566 for (CustomXmlInputElement child : inputElement.getChildElements()) {
1567 if (child.getElementName().equals(node.getNodeName())) {
be222f56
PT
1568 unused = false;
1569 break;
1570 }
1571 }
1572 }
1573 if (unused) {
1574 return node.getNodeName();
1575 }
1576 }
1577 }
1578 }
1579 }
1580 return ""; //$NON-NLS-1$
1581 }
1582
a7418109 1583 private String getAttributeNameSuggestion(CustomXmlInputElement inputElement) {
be222f56
PT
1584 Element element = getPreviewElement(inputElement);
1585 if (element != null) {
1586 NamedNodeMap attributeMap = element.getAttributes();
1587 for (int i = 0; i < attributeMap.getLength(); i++) {
1588 Node node = attributeMap.item(i);
1589 boolean unused = true;
a7418109
MK
1590 if (inputElement.getAttributes() != null) {
1591 for (CustomXmlInputAttribute attribute : inputElement.getAttributes()) {
1592 if (attribute.getAttributeName().equals(node.getNodeName())) {
be222f56
PT
1593 unused = false;
1594 break;
1595 }
1596 }
1597 }
1598 if (unused) {
1599 return node.getNodeName();
1600 }
1601 }
1602 }
1603 return ""; //$NON-NLS-1$
1604 }
1605
1606 private void validate() {
c22ca172 1607 definition.categoryName = categoryText.getText().trim();
be222f56
PT
1608 definition.definitionName = logtypeText.getText().trim();
1609 definition.timeStampOutputFormat = timeStampOutputFormatText.getText().trim();
1610
1611 if (selectedElement != null) {
1612 selectedElement.extractInputs();
1613 treeViewer.refresh();
1614 }
1615
89151d6a 1616 List<String> errors = new ArrayList<>();
be222f56 1617
c22ca172 1618 if (definition.categoryName.length() == 0) {
89151d6a 1619 errors.add(Messages.CustomXmlParserInputWizardPage_emptyCategoryError);
c22ca172
PT
1620 categoryText.setBackground(COLOR_LIGHT_RED);
1621 } else if (definition.definitionName.length() == 0) {
89151d6a 1622 errors.add(Messages.CustomXmlParserInputWizardPage_emptyLogTypeError);
be222f56
PT
1623 logtypeText.setBackground(COLOR_LIGHT_RED);
1624 } else {
c22ca172 1625 categoryText.setBackground(COLOR_TEXT_BACKGROUND);
be222f56 1626 logtypeText.setBackground(COLOR_TEXT_BACKGROUND);
c22ca172 1627 if (definition.categoryName.indexOf(':') != -1) {
89151d6a 1628 errors.add(Messages.CustomXmlParserInputWizardPage_invalidCategoryError);
c22ca172
PT
1629 categoryText.setBackground(COLOR_LIGHT_RED);
1630 }
1631 if (definition.definitionName.indexOf(':') != -1) {
89151d6a 1632 errors.add(Messages.CustomXmlParserInputWizardPage_invalidLogTypeError);
c22ca172
PT
1633 logtypeText.setBackground(COLOR_LIGHT_RED);
1634 }
1635 for (TraceTypeHelper helper : TmfTraceType.getTraceTypeHelpers()) {
1636 if (definition.categoryName.equals(helper.getCategoryName()) &&
1637 definition.definitionName.equals(helper.getName()) &&
1638 (editDefinitionName == null || !editDefinitionName.equals(definition.definitionName)) &&
1639 (editCategoryName == null || !editCategoryName.equals(definition.categoryName))) {
89151d6a 1640 errors.add(Messages.CustomXmlParserInputWizardPage_duplicatelogTypeError);
f6aa55e2
PT
1641 logtypeText.setBackground(COLOR_LIGHT_RED);
1642 break;
be222f56
PT
1643 }
1644 }
1645 }
1646
1647 if (definition.rootInputElement == null) {
89151d6a 1648 errors.add(Messages.CustomXmlParserInputWizardPage_noDocumentError);
be222f56
PT
1649 }
1650
1651 if (definition.rootInputElement != null) {
1652 logEntryFound = false;
1653 timeStampFound = false;
1654
89151d6a 1655 errors.addAll(validateElement(definition.rootInputElement));
be222f56 1656
a7418109
MK
1657 if ((definition.rootInputElement.getAttributes() != null && definition.rootInputElement.getAttributes().size() != 0)
1658 || (definition.rootInputElement.getChildElements() != null && definition.rootInputElement.getChildElements().size() != 0)
89151d6a 1659 || errors.size() == 0) {
be222f56 1660 if (!logEntryFound) {
89151d6a 1661 errors.add(Messages.CustomXmlParserInputWizardPage_missingLogEntryError);
be222f56
PT
1662 }
1663
4d12b563
PT
1664 if (timeStampFound && !definition.timeStampOutputFormat.isEmpty()) {
1665 try {
1666 new TmfTimestampFormat(timeStampOutputFormatText.getText().trim());
1667 timeStampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1668 } catch (IllegalArgumentException e) {
1669 errors.add(Messages.CustomXmlParserInputWizardPage_elementInvalidTimestampFmtError);
be222f56 1670 timeStampOutputFormatText.setBackground(COLOR_LIGHT_RED);
be222f56
PT
1671 }
1672 } else {
4d12b563
PT
1673 timeStampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1674 if (!timeStampFound) {
1675 timeStampPreviewText.setText(Messages.CustomXmlParserInputWizardPage_noTimestampElementOrAttribute);
1676 }
be222f56
PT
1677 }
1678 }
1679 } else {
89151d6a 1680 timeStampPreviewText.setText(Messages.CustomXmlParserInputWizardPage_noTimestampElementOrAttribute);
be222f56
PT
1681 }
1682
89151d6a 1683 if (errors.size() == 0) {
be222f56
PT
1684 setDescription(defaultDescription);
1685 setPageComplete(true);
1686 } else {
89151d6a 1687 setDescription(Joiner.on(' ').join(errors));
be222f56
PT
1688 setPageComplete(false);
1689 }
1690 }
1691
a0a88f65
AM
1692 /**
1693 * Clean up the specified XML element.
1694 *
1695 * @param inputElement
1696 * The element to clean up
1697 * @return The validated element
1698 */
a7418109 1699 public List<String> validateElement(CustomXmlInputElement inputElement) {
89151d6a 1700 List<String> errors = new ArrayList<>();
be222f56
PT
1701 ElementNode elementNode = null;
1702 if (selectedElement != null && selectedElement.inputElement.equals(inputElement)) {
1703 elementNode = selectedElement;
1704 }
1705 if (inputElement == definition.rootInputElement) {
a7418109 1706 if (inputElement.getElementName().length() == 0) {
89151d6a 1707 errors.add(Messages.CustomXmlParserInputWizardPage_missingDocumentElementError);
be222f56
PT
1708 if (elementNode != null) {
1709 elementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1710 }
1711 } else {
1712 if (elementNode != null) {
1713 elementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);
1714 }
1715 }
1716 }
1717 if (inputElement != definition.rootInputElement) {
a7418109 1718 if (inputElement.isLogEntry()) {
be222f56
PT
1719 logEntryFound = true;
1720 }
f5cc6ed1 1721 if (inputElement.getInputTag().equals(Tag.TIMESTAMP)) {
be222f56 1722 timeStampFound = true;
a7418109 1723 if (inputElement.getInputFormat().length() == 0) {
89151d6a 1724 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementMissingTimestampFmtError, getName(inputElement)));
be222f56
PT
1725 if (elementNode != null) {
1726 elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1727 }
1728 } else {
1729 try {
a7418109 1730 new TmfTimestampFormat(inputElement.getInputFormat());
be222f56
PT
1731 if (elementNode != null) {
1732 elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1733 }
1734 } catch (IllegalArgumentException e) {
89151d6a 1735 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementInvalidTimestampFmtError, getName(inputElement)));
be222f56
PT
1736 if (elementNode != null) {
1737 elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1738 }
1739 }
1740 }
a7418109 1741 } else if (inputElement.getInputName().length() == 0) {
89151d6a 1742 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementMissingInputNameError, getName(inputElement)));
be222f56
PT
1743 if (elementNode != null) {
1744 elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1745 }
f5cc6ed1
PT
1746 } else if (inputElement.getInputTag().equals(Tag.OTHER) && Tag.fromLabel(inputElement.getInputName()) != null) {
1747 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementReservedInputNameError, getName(inputElement)));
1748 if (elementNode != null) {
1749 elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1750 }
be222f56
PT
1751 } else {
1752 if (elementNode != null) {
1753 elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1754 }
1755 }
eddf2682
PT
1756 if (inputElement.getEventType() != null && inputElement.getEventType().trim().isEmpty()) {
1757 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_emptyEventTypeError, getName(inputElement)));
1758 if (elementNode != null) {
1759 elementNode.eventTypeText.setBackground(COLOR_LIGHT_RED);
1760 }
1761 } else {
1762 if (elementNode != null) {
1763 elementNode.eventTypeText.setBackground(COLOR_TEXT_BACKGROUND);
1764 }
1765 }
be222f56 1766 }
a7418109 1767 if (inputElement.getAttributes() != null) {
be222f56
PT
1768 if (elementNode != null) {
1769 for (Attribute attribute : elementNode.attributes) {
1770 attribute.attributeNameText.setBackground(COLOR_TEXT_BACKGROUND);
1771 }
1772 }
a7418109
MK
1773 for (int i = 0; i < inputElement.getAttributes().size(); i++) {
1774 CustomXmlInputAttribute attribute = inputElement.getAttributes().get(i);
be222f56 1775 boolean duplicate = false;
a7418109
MK
1776 for (int j = i + 1; j < inputElement.getAttributes().size(); j++) {
1777 CustomXmlInputAttribute otherAttribute = inputElement.getAttributes().get(j);
1778 if (otherAttribute.getAttributeName().equals(attribute.getAttributeName())) {
be222f56
PT
1779 duplicate = true;
1780 if (elementNode != null) {
1781 elementNode.attributes.get(j).attributeNameText.setBackground(COLOR_LIGHT_RED);
1782 }
1783 }
1784 }
a7418109 1785 if (attribute.getAttributeName().length() == 0) {
89151d6a 1786 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeMissingNameError, getName(inputElement)));
be222f56
PT
1787 if (elementNode != null) {
1788 elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED);
1789 }
1790 } else if (duplicate) {
89151d6a 1791 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeDuplicateNameError, getName(attribute, inputElement)));
be222f56
PT
1792 if (elementNode != null) {
1793 elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED);
1794 }
1795 }
f5cc6ed1 1796 if (attribute.getInputTag().equals(Tag.TIMESTAMP)) {
be222f56 1797 timeStampFound = true;
a7418109 1798 if (attribute.getInputFormat().length() == 0) {
89151d6a 1799 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeMissingTimestampFmtError, getName(attribute, inputElement)));
be222f56
PT
1800 if (elementNode != null) {
1801 elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1802 }
1803 } else {
1804 try {
a7418109 1805 new TmfTimestampFormat(attribute.getInputFormat());
be222f56
PT
1806 if (elementNode != null) {
1807 elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);
1808 }
1809 } catch (IllegalArgumentException e) {
89151d6a 1810 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeInvalidTimestampFmtError, getName(attribute, inputElement)));
be222f56
PT
1811 if (elementNode != null) {
1812 elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1813 }
1814 }
1815 }
a7418109 1816 } else if (attribute.getInputName().length() == 0) {
89151d6a 1817 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeMissingInputNameError, getName(attribute, inputElement)));
be222f56
PT
1818 if (elementNode != null) {
1819 elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1820 }
f5cc6ed1
PT
1821 } else if (attribute.getInputTag().equals(Tag.OTHER) && Tag.fromLabel(attribute.getInputName()) != null) {
1822 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeReservedInputNameError, getName(attribute, inputElement)));
1823 if (elementNode != null) {
1824 elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1825 }
be222f56
PT
1826 } else {
1827 if (elementNode != null) {
1828 elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);
1829 }
1830 }
1831 }
1832 }
a7418109
MK
1833 if (inputElement.getChildElements() != null) {
1834 for (CustomXmlInputElement child : inputElement.getChildElements()) {
be222f56
PT
1835 ElementNode childElementNode = null;
1836 if (selectedElement != null && selectedElement.inputElement.equals(child)) {
1837 childElementNode = selectedElement;
1838 }
1839 if (childElementNode != null) {
1840 childElementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);
1841 }
1842 }
a7418109
MK
1843 for (int i = 0; i < inputElement.getChildElements().size(); i++) {
1844 CustomXmlInputElement child = inputElement.getChildElements().get(i);
be222f56
PT
1845 ElementNode childElementNode = null;
1846 if (selectedElement != null && selectedElement.inputElement.equals(child)) {
1847 childElementNode = selectedElement;
1848 }
a7418109 1849 if (child.getElementName().length() == 0) {
89151d6a 1850 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementMissingNameError, getName(child)));
be222f56
PT
1851 if (childElementNode != null) {
1852 childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1853 }
1854 } else {
1855 boolean duplicate = false;
a7418109
MK
1856 for (int j = i + 1; j < inputElement.getChildElements().size(); j++) {
1857 CustomXmlInputElement otherChild = inputElement.getChildElements().get(j);
1858 if (otherChild.getElementName().equals(child.getElementName())) {
be222f56
PT
1859 duplicate = true;
1860 ElementNode otherChildElementNode = null;
1861 if (selectedElement != null && selectedElement.inputElement.equals(otherChild)) {
1862 otherChildElementNode = selectedElement;
1863 }
1864 if (otherChildElementNode != null) {
1865 otherChildElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1866 }
1867 }
1868 }
1869 if (duplicate) {
89151d6a 1870 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementDuplicateNameError, getName(child)));
be222f56
PT
1871 if (childElementNode != null) {
1872 childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1873 }
1874 }
1875 }
1876
89151d6a 1877 errors.addAll(validateElement(child));
be222f56
PT
1878 }
1879 }
1880 return errors;
1881 }
1882
a0a88f65
AM
1883 /**
1884 * Get the trace definition.
1885 *
1886 * @return The trace definition
1887 */
be222f56
PT
1888 public CustomXmlTraceDefinition getDefinition() {
1889 return definition;
1890 }
1891
a0a88f65
AM
1892 /**
1893 * Get the raw text input.
1894 *
1895 * @return The raw text input.
1896 */
be222f56
PT
1897 public char[] getInputText() {
1898 return inputText.getText().toCharArray();
1899 }
1900}
This page took 0.192585 seconds and 5 git commands to generate.