f574f08499f944078c37442c4abb7f6341c158be
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / parsers / wizards / CustomXmlParserInputWizardPage.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 java.io.BufferedReader;
16 import java.io.ByteArrayInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
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.List;
25 import java.util.Map.Entry;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
30
31 import org.eclipse.core.resources.IFile;
32 import org.eclipse.core.runtime.CoreException;
33 import org.eclipse.core.runtime.FileLocator;
34 import org.eclipse.core.runtime.IPath;
35 import org.eclipse.core.runtime.Path;
36 import org.eclipse.core.runtime.Platform;
37 import org.eclipse.jface.viewers.AbstractTreeViewer;
38 import org.eclipse.jface.viewers.ColumnLabelProvider;
39 import org.eclipse.jface.viewers.ISelection;
40 import org.eclipse.jface.viewers.ISelectionChangedListener;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.jface.viewers.ITreeContentProvider;
43 import org.eclipse.jface.viewers.SelectionChangedEvent;
44 import org.eclipse.jface.viewers.StructuredSelection;
45 import org.eclipse.jface.viewers.TreeViewer;
46 import org.eclipse.jface.viewers.Viewer;
47 import org.eclipse.jface.wizard.WizardPage;
48 import org.eclipse.osgi.util.NLS;
49 import org.eclipse.swt.SWT;
50 import org.eclipse.swt.browser.Browser;
51 import org.eclipse.swt.custom.SashForm;
52 import org.eclipse.swt.custom.ScrolledComposite;
53 import org.eclipse.swt.custom.StyleRange;
54 import org.eclipse.swt.custom.StyledText;
55 import org.eclipse.swt.events.ModifyEvent;
56 import org.eclipse.swt.events.ModifyListener;
57 import org.eclipse.swt.events.SelectionAdapter;
58 import org.eclipse.swt.events.SelectionEvent;
59 import org.eclipse.swt.events.SelectionListener;
60 import org.eclipse.swt.graphics.Color;
61 import org.eclipse.swt.graphics.Font;
62 import org.eclipse.swt.graphics.FontData;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.graphics.Point;
65 import org.eclipse.swt.graphics.Rectangle;
66 import org.eclipse.swt.layout.FillLayout;
67 import org.eclipse.swt.layout.GridData;
68 import org.eclipse.swt.layout.GridLayout;
69 import org.eclipse.swt.widgets.Button;
70 import org.eclipse.swt.widgets.Combo;
71 import org.eclipse.swt.widgets.Composite;
72 import org.eclipse.swt.widgets.Display;
73 import org.eclipse.swt.widgets.Group;
74 import org.eclipse.swt.widgets.Label;
75 import org.eclipse.swt.widgets.Shell;
76 import org.eclipse.swt.widgets.Text;
77 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
78 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
79 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition;
80 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag;
81 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlInputAttribute;
82 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlInputElement;
83 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
84 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
85 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
86 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
87 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
88 import org.osgi.framework.Bundle;
89 import org.w3c.dom.Document;
90 import org.w3c.dom.Element;
91 import org.w3c.dom.NamedNodeMap;
92 import org.w3c.dom.Node;
93 import org.w3c.dom.NodeList;
94 import org.xml.sax.EntityResolver;
95 import org.xml.sax.ErrorHandler;
96 import org.xml.sax.InputSource;
97 import org.xml.sax.SAXException;
98 import org.xml.sax.SAXParseException;
99
100 import com.google.common.base.Joiner;
101
102 /**
103 * Input wizard page for custom XML trace parsers.
104 *
105 * @author Patrick Tasse
106 */
107 public class CustomXmlParserInputWizardPage extends WizardPage {
108
109 private static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
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$
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$
121 private static final Color COLOR_LIGHT_RED = new Color(Display.getDefault(), 255, 192, 192);
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);
124 private static final Color COLOR_GRAY = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
125
126 private final ISelection selection;
127 private CustomXmlTraceDefinition definition;
128 private String editCategoryName;
129 private String editDefinitionName;
130 private String defaultDescription;
131 private ElementNode selectedElement;
132 private Composite container;
133 private Text categoryText;
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;
142 private ScrolledComposite elementScrolledComposite;
143 private TreeViewer treeViewer;
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
159 /**
160 * Constructor
161 *
162 * @param selection
163 * Selection object
164 * @param definition
165 * Trace definition
166 */
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) {
180 this.editCategoryName = definition.categoryName;
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
199 Label categoryLabel = new Label(headerComposite, SWT.NULL);
200 categoryLabel.setText(Messages.CustomXmlParserInputWizardPage_category);
201
202 categoryText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
203 categoryText.setLayoutData(new GridData(120, SWT.DEFAULT));
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);
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 });
218
219 Button timeStampFormatHelpButton = new Button(headerComposite, SWT.PUSH);
220 timeStampFormatHelpButton.setImage(HELP_IMAGE);
221 timeStampFormatHelpButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_timestampFormatHelp);
222 timeStampFormatHelpButton.addSelectionListener(new SelectionAdapter() {
223 @Override
224 public void widgetSelected(SelectionEvent e) {
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 }
233 }
234 });
235
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
243 Label timeStampPreviewLabel = new Label(headerComposite, SWT.NULL);
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));
254 vSash.setBackground(COLOR_GRAY);
255
256 SashForm hSash = new SashForm(vSash, SWT.HORIZONTAL);
257 hSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
258
259 ScrolledComposite treeScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL | SWT.H_SCROLL);
260 treeScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
261 Composite treeContainer = new Composite(treeScrolledComposite, SWT.NONE);
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
294 categoryText.addModifyListener(updateListener);
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());
341 inputText.addModifyListener(e -> parseXmlInput(inputText.getText()));
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);
357 removeButton.setImage(DELETE_IMAGE);
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();
366 CustomXmlInputElement inputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
367 if (inputElement == definition.rootInputElement) {
368 definition.rootInputElement = null;
369 } else {
370 inputElement.getParentElement().getChildElements().remove(inputElement);
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);
389 addChildButton.setImage(ADD_CHILD_IMAGE);
390 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addChildElement);
391 addChildButton.addSelectionListener(new SelectionAdapter() {
392 @Override
393 public void widgetSelected(SelectionEvent e) {
394 CustomXmlInputElement inputElement = new CustomXmlInputElement("", false, Tag.IGNORE, Tag.IGNORE.toString(), 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$
395 if (definition.rootInputElement == null) {
396 definition.rootInputElement = inputElement;
397 inputElement.setElementName(getChildNameSuggestion(null));
398 } else if (treeViewer.getSelection().isEmpty()) {
399 return;
400 } else {
401 CustomXmlInputElement parentInputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
402 parentInputElement.addChild(inputElement);
403 inputElement.setElementName(getChildNameSuggestion(parentInputElement));
404 }
405 treeViewer.refresh();
406 treeViewer.setSelection(new StructuredSelection(inputElement), true);
407 }
408 });
409
410 addNextButton = new Button(buttonBar, SWT.PUSH);
411 addNextButton.setImage(ADD_NEXT_IMAGE);
412 addNextButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addNextElement);
413 addNextButton.addSelectionListener(new SelectionAdapter() {
414 @Override
415 public void widgetSelected(SelectionEvent e) {
416 CustomXmlInputElement inputElement = new CustomXmlInputElement("", false, Tag.IGNORE, Tag.IGNORE.toString(), 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$
417 if (definition.rootInputElement == null) {
418 definition.rootInputElement = inputElement;
419 inputElement.setElementName(getChildNameSuggestion(null));
420 } else if (treeViewer.getSelection().isEmpty()) {
421 return;
422 } else {
423 CustomXmlInputElement previousInputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
424 if (previousInputElement == definition.rootInputElement) {
425 return;
426 }
427 previousInputElement.addNext(inputElement);
428 inputElement.setElementName(getChildNameSuggestion(inputElement.getParentElement()));
429 }
430 treeViewer.refresh();
431 treeViewer.setSelection(new StructuredSelection(inputElement), true);
432 }
433 });
434
435 Button feelingLuckyButton = new Button(buttonBar, SWT.PUSH);
436 feelingLuckyButton.setImage(ADD_MANY_IMAGE);
437 feelingLuckyButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_feelingLucky);
438 feelingLuckyButton.addSelectionListener(new SelectionAdapter() {
439 @Override
440 public void widgetSelected(SelectionEvent e) {
441 CustomXmlInputElement inputElement = null;
442 if (definition.rootInputElement == null) {
443 if (getChildNameSuggestion(null).length() != 0) {
444 inputElement = new CustomXmlInputElement(getChildNameSuggestion(null), false, Tag.IGNORE, Tag.IGNORE.toString(), 0, "", null); //$NON-NLS-1$
445 definition.rootInputElement = inputElement;
446 feelingLucky(inputElement);
447 } else {
448 return;
449 }
450 } else if (treeViewer.getSelection().isEmpty()) {
451 return;
452 } else {
453 inputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
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);
463 moveUpButton.setImage(MOVE_UP_IMAGE);
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 }
471 CustomXmlInputElement inputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
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);
483 moveDownButton.setImage(MOVE_DOWN_IMAGE);
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 }
491 CustomXmlInputElement inputElement = (CustomXmlInputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
492 if (inputElement == definition.rootInputElement) {
493 return;
494 }
495 inputElement.moveDown();
496 treeViewer.refresh();
497 validate();
498 updatePreviews();
499 }
500 });
501 }
502
503 private void feelingLucky(CustomXmlInputElement inputElement) {
504 while (true) {
505 String attributeName = getAttributeNameSuggestion(inputElement);
506 if (attributeName.length() == 0) {
507 break;
508 }
509 CustomXmlInputAttribute attribute = new CustomXmlInputAttribute(attributeName, Tag.OTHER, attributeName, 0, ""); //$NON-NLS-1$
510 inputElement.addAttribute(attribute);
511 }
512 while (true) {
513 String childName = getChildNameSuggestion(inputElement);
514 if (childName.length() == 0) {
515 break;
516 }
517 CustomXmlInputElement childElement = new CustomXmlInputElement(childName, false, Tag.IGNORE, Tag.IGNORE.toString(), 0, "", null); //$NON-NLS-1$
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) {
536 CustomXmlInputElement inputElement = (CustomXmlInputElement) parentElement;
537 if (inputElement.getChildElements() == null) {
538 return new CustomXmlInputElement[0];
539 }
540 return inputElement.getChildElements().toArray();
541 }
542
543 @Override
544 public boolean hasChildren(Object element) {
545 CustomXmlInputElement inputElement = (CustomXmlInputElement) element;
546 return (inputElement.getChildElements() != null && inputElement.getChildElements().size() > 0);
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) {
559 CustomXmlInputElement inputElement = (CustomXmlInputElement) element;
560 return inputElement.getParentElement();
561 }
562 }
563
564 private static class InputElementTreeLabelProvider extends ColumnLabelProvider {
565
566 @Override
567 public Image getImage(Object element) {
568 return ELEMENT_IMAGE;
569 }
570
571 @Override
572 public String getText(Object element) {
573 CustomXmlInputElement inputElement = (CustomXmlInputElement) element;
574 return (inputElement.getElementName().trim().length() == 0) ? "?" : inputElement.getElementName(); //$NON-NLS-1$
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) {
585 IStructuredSelection sel = (IStructuredSelection) event.getSelection();
586 CustomXmlInputElement inputElement = (CustomXmlInputElement) sel.getFirstElement();
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
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) {
629 categoryText.setText(def.categoryName);
630 logtypeText.setText(def.definitionName);
631 if (def.timeStampOutputFormat != null) {
632 timeStampOutputFormatText.setText(def.timeStampOutputFormat);
633 } else {
634 timeStampOutputFormatText.setText(""); //$NON-NLS-1$
635 }
636 treeViewer.setInput(def);
637
638 if (def.rootInputElement != null) {
639 treeViewer.setSelection(new StructuredSelection(def.rootInputElement));
640 }
641 }
642
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) {
646 return name;
647 }
648 return getName(inputElement.getParentElement()) + " : " + name; //$NON-NLS-1$
649 }
650
651 private String getName(CustomXmlInputAttribute inputAttribute, CustomXmlInputElement inputElement) {
652 String name = (inputAttribute.getAttributeName().trim().length() == 0) ? "?" : inputAttribute.getAttributeName().trim(); //$NON-NLS-1$
653 return getName(inputElement) + " : " + name; //$NON-NLS-1$
654 }
655
656 @Override
657 public void setVisible(boolean visible) {
658 if (visible) {
659 validate();
660 updatePreviews();
661 }
662 super.setVisible(visible);
663 }
664
665 /**
666 * Get the global list of inputs.
667 *
668 * @return The list of inputs
669 */
670 public List<Entry<Tag, String>> getInputs() {
671 return getInputs(definition.rootInputElement);
672 }
673
674 /**
675 * Get the list of inputs for a given element, recursively.
676 *
677 * @param inputElement
678 * The element
679 * @return The list of inputs
680 */
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);
687 }
688 }
689 if (inputElement.getAttributes() != null) {
690 for (CustomXmlInputAttribute attribute : inputElement.getAttributes()) {
691 Entry<Tag, String> input = new SimpleEntry<>(attribute.getInputTag(), attribute.getInputName());
692 if (!inputs.contains(input)) {
693 inputs.add(input);
694 }
695 }
696 }
697 if (inputElement.getChildElements() != null) {
698 for (CustomXmlInputElement childInputElement : inputElement.getChildElements()) {
699 for (Entry<Tag, String> input : getInputs(childInputElement)) {
700 if (!inputs.contains(input)) {
701 inputs.add(input);
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) {
721 Object sel = ((IStructuredSelection) this.selection).getFirstElement();
722 if (sel instanceof IFile) {
723 IFile file = (IFile) sel;
724 try {
725 inputStream = file.getContents();
726 } catch (CoreException e) {
727 return ""; //$NON-NLS-1$
728 }
729 }
730 }
731 if (inputStream != null) {
732 try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));) {
733 StringBuilder sb = new StringBuilder();
734 String line = null;
735 while ((line = reader.readLine()) != null) {
736 sb.append(line);
737 sb.append('\n');
738 }
739 parseXmlInput(sb.toString());
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
754 EntityResolver resolver = (publicId, systemId) -> {
755 String empty = ""; //$NON-NLS-1$
756 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
757 return new InputSource(bais);
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
797 private void initValues() {
798 timeStampValue = null;
799 timeStampFormat = null;
800 logEntriesCount = 0;
801 logEntryFound = false;
802 }
803
804 private void updatePreviews() {
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 {
820 TmfTimestampFormat timestampFormat = new TmfTimestampFormat(timeStampFormat);
821 long timestamp = timestampFormat.parseValue(timeStampValue);
822 if (timeStampOutputFormatText.getText().trim().isEmpty()) {
823 timestampFormat = new TmfTimestampFormat();
824 } else {
825 timestampFormat = new TmfTimestampFormat(timeStampOutputFormatText.getText().trim());
826 }
827 timeStampPreviewText.setText(timestampFormat.format(timestamp));
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);
849 helpBrowser.addTitleListener(event -> helpShell.setText(event.title));
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);
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 {
881 private final CustomXmlInputElement inputElement;
882 private final Group group;
883 private List<Attribute> attributes = new ArrayList<>();
884 private List<ElementNode> childElements = new ArrayList<>();
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;
894 private Button eventTypeButton;
895 private Text eventTypeText;
896 private Label fillerLabel;
897 private Composite addAttributeComposite;
898 private Button addAttributeButton;
899 private Label addAttributeLabel;
900
901 public ElementNode(Composite parent, CustomXmlInputElement inputElement) {
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);
919 elementNameText.addModifyListener(e -> {
920 ElementNode.this.inputElement.setElementName(elementNameText.getText().trim());
921 group.setText(getName(ElementNode.this.inputElement));
922 });
923 elementNameText.setText(inputElement.getElementName());
924 elementNameText.addModifyListener(updateListener);
925
926 if (inputElement.getParentElement() != null) {
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);
940 logEntryButton.setSelection(inputElement.isLogEntry());
941 logEntryButton.addSelectionListener(new SelectionListener() {
942 @Override
943 public void widgetDefaultSelected(SelectionEvent e) {
944 }
945
946 @Override
947 public void widgetSelected(SelectionEvent e) {
948 CustomXmlInputElement parentElem = ElementNode.this.inputElement.getParentElement();
949 while (parentElem != null) {
950 parentElem.setLogEntry(false);
951 parentElem = parentElem.getParentElement();
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);
965 tagCombo.setItems(new String[] {
966 Tag.IGNORE.toString(),
967 Tag.TIMESTAMP.toString(),
968 Tag.EVENT_TYPE.toString(),
969 Tag.MESSAGE.toString(),
970 Tag.OTHER.toString() });
971 tagCombo.setVisibleItemCount(tagCombo.getItemCount());
972 tagCombo.addSelectionListener(new SelectionListener() {
973 @Override
974 public void widgetDefaultSelected(SelectionEvent e) {
975 }
976
977 @Override
978 public void widgetSelected(SelectionEvent e) {
979 tagText.removeModifyListener(updateListener);
980 switch (tagCombo.getSelectionIndex()) {
981 case 0: // Ignore
982 tagLabel.setVisible(false);
983 tagText.setVisible(false);
984 actionCombo.setVisible(false);
985 break;
986 case 1: // Time Stamp
987 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
988 tagLabel.setVisible(true);
989 tagText.setVisible(true);
990 tagText.addModifyListener(updateListener);
991 actionCombo.setVisible(true);
992 break;
993 case 2: // Event type
994 tagLabel.setVisible(false);
995 tagText.setVisible(false);
996 actionCombo.setVisible(true);
997 break;
998 case 3: // Message
999 tagLabel.setVisible(false);
1000 tagText.setVisible(false);
1001 actionCombo.setVisible(true);
1002 break;
1003 case 4: // Other
1004 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
1005 tagLabel.setVisible(true);
1006 if (tagText.getText().trim().length() == 0) {
1007 tagText.setText(elementNameText.getText().trim());
1008 }
1009 tagText.setVisible(true);
1010 tagText.addModifyListener(updateListener);
1011 actionCombo.setVisible(true);
1012 break;
1013 default:
1014 break;
1015 }
1016 tagComposite.layout();
1017 validate();
1018 updatePreviews();
1019 }
1020 });
1021
1022 tagLabel = new Label(tagComposite, SWT.NULL);
1023 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1024
1025 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
1026 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1027 gd.widthHint = 0;
1028 tagText.setLayoutData(gd);
1029
1030 actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1031 actionCombo.setItems(new String[] { Messages.CustomXmlParserInputWizardPage_set, Messages.CustomXmlParserInputWizardPage_append,
1032 Messages.CustomXmlParserInputWizardPage_appendWith });
1033 actionCombo.select(inputElement.getInputAction());
1034 actionCombo.addSelectionListener(updateListener);
1035
1036 if (inputElement.getInputTag().equals(Tag.IGNORE)) {
1037 tagCombo.select(0);
1038 tagLabel.setVisible(false);
1039 tagText.setVisible(false);
1040 actionCombo.setVisible(false);
1041 } else if (inputElement.getInputTag().equals(Tag.TIMESTAMP)) {
1042 tagCombo.select(1);
1043 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
1044 tagText.setText(inputElement.getInputFormat());
1045 tagText.addModifyListener(updateListener);
1046 } else if (inputElement.getInputTag().equals(Tag.EVENT_TYPE)) {
1047 tagCombo.select(2);
1048 tagLabel.setVisible(false);
1049 tagText.setVisible(false);
1050 } else if (inputElement.getInputTag().equals(Tag.MESSAGE)) {
1051 tagCombo.select(3);
1052 tagLabel.setVisible(false);
1053 tagText.setVisible(false);
1054 } else {
1055 tagCombo.select(4);
1056 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
1057 tagText.setText(inputElement.getInputName());
1058 tagText.addModifyListener(updateListener);
1059 }
1060
1061 eventTypeButton = new Button(group, SWT.CHECK);
1062 eventTypeButton.setText(Messages.CustomTxtParserInputWizardPage_eventType);
1063 eventTypeButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1064 eventTypeButton.addSelectionListener(new SelectionAdapter() {
1065 @Override
1066 public void widgetSelected(SelectionEvent e) {
1067 if (eventTypeButton.getSelection()) {
1068 eventTypeText.setEnabled(true);
1069 } else {
1070 eventTypeText.setEnabled(false);
1071 }
1072 }
1073 });
1074 eventTypeButton.addSelectionListener(updateListener);
1075
1076 eventTypeText = new Text(group, SWT.BORDER | SWT.SINGLE);
1077 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1078 gd.widthHint = 0;
1079 eventTypeText.setLayoutData(gd);
1080 if (inputElement.getEventType() != null) {
1081 eventTypeText.setText(inputElement.getEventType());
1082 eventTypeButton.setSelection(true);
1083 } else {
1084 eventTypeText.setEnabled(false);
1085 eventTypeButton.setSelection(false);
1086 }
1087 eventTypeText.addModifyListener(updateListener);
1088 }
1089
1090 if (inputElement.getAttributes() != null) {
1091 for (CustomXmlInputAttribute inputAttribute : inputElement.getAttributes()) {
1092 Attribute attribute = new Attribute(group, this, inputAttribute, attributes.size() + 1);
1093 attributes.add(attribute);
1094 }
1095 }
1096
1097 createAddButton();
1098 }
1099
1100 private void updatePreview() {
1101 Element element = getPreviewElement(inputElement);
1102 // no preview text for document element
1103 if (inputElement.getParentElement() != null) {
1104 previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement);
1105 if (element != null) {
1106 previewText.setText(CustomXmlTrace.parseElement(element, new StringBuffer()).toString());
1107 if (logEntryButton.getSelection()) {
1108 if (!logEntryFound) {
1109 logEntryFound = true;
1110 logEntriesCount++;
1111 } else {
1112 // remove nested log entry
1113 logEntryButton.setSelection(false);
1114 }
1115 }
1116 if (tagCombo.getText().equals(Tag.TIMESTAMP.toString()) && logEntriesCount <= 1) {
1117 String value = previewText.getText().trim();
1118 if (value.length() != 0) {
1119 if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_SET) {
1120 timeStampValue = value;
1121 timeStampFormat = tagText.getText().trim();
1122 } else if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND) {
1123 if (timeStampValue != null) {
1124 timeStampValue += value;
1125 timeStampFormat += tagText.getText().trim();
1126 } else {
1127 timeStampValue = value;
1128 timeStampFormat = tagText.getText().trim();
1129 }
1130 } else if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
1131 if (timeStampValue != null) {
1132 timeStampValue += " | " + value; //$NON-NLS-1$
1133 timeStampFormat += " | " + tagText.getText().trim(); //$NON-NLS-1$
1134 } else {
1135 timeStampValue = value;
1136 timeStampFormat = tagText.getText().trim();
1137 }
1138 }
1139 }
1140 }
1141 }
1142 }
1143 for (Attribute attribute : attributes) {
1144 if (element != null) {
1145 String value = element.getAttribute(attribute.attributeNameText.getText().trim());
1146 if (value.length() != 0) {
1147 attribute.previewText.setText(value);
1148 if (attribute.tagCombo.getText().equals(Tag.TIMESTAMP.toString()) && logEntriesCount <= 1) {
1149 if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_SET) {
1150 timeStampValue = value;
1151 timeStampFormat = attribute.tagText.getText().trim();
1152 } else if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND) {
1153 if (timeStampValue != null) {
1154 timeStampValue += value;
1155 timeStampFormat += attribute.tagText.getText().trim();
1156 } else {
1157 timeStampValue = value;
1158 timeStampFormat = attribute.tagText.getText().trim();
1159 }
1160 } else if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
1161 if (timeStampValue != null) {
1162 timeStampValue += " | " + value; //$NON-NLS-1$
1163 timeStampFormat += " | " + attribute.tagText.getText().trim(); //$NON-NLS-1$
1164 } else {
1165 timeStampValue = value;
1166 timeStampFormat = attribute.tagText.getText().trim();
1167 }
1168 }
1169 }
1170 } else {
1171 attribute.previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingAttribute);
1172 }
1173 } else {
1174 attribute.previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement);
1175 }
1176 }
1177 for (ElementNode child : childElements) {
1178 child.updatePreview();
1179 }
1180 if (logEntryButton != null && logEntryButton.getSelection()) {
1181 logEntryFound = false;
1182 }
1183 }
1184
1185 private void createAddButton() {
1186 fillerLabel = new Label(group, SWT.NONE);
1187
1188 addAttributeComposite = new Composite(group, SWT.NONE);
1189 addAttributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1190 GridLayout addAttributeLayout = new GridLayout(2, false);
1191 addAttributeLayout.marginHeight = 0;
1192 addAttributeLayout.marginWidth = 0;
1193 addAttributeComposite.setLayout(addAttributeLayout);
1194
1195 addAttributeButton = new Button(addAttributeComposite, SWT.PUSH);
1196 addAttributeButton.setImage(ADD_IMAGE);
1197 addAttributeButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addAttribute);
1198 addAttributeButton.addSelectionListener(new SelectionAdapter() {
1199 @Override
1200 public void widgetSelected(SelectionEvent e) {
1201 removeAddButton();
1202 String attributeName = getAttributeNameSuggestion(inputElement);
1203 CustomXmlInputAttribute inputAttribute = new CustomXmlInputAttribute(attributeName, Tag.OTHER, attributeName, 0, ""); //$NON-NLS-1$
1204 attributes.add(new Attribute(group, ElementNode.this, inputAttribute, attributes.size() + 1));
1205 createAddButton();
1206 elementContainer.layout();
1207 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x,
1208 elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1);
1209 group.getParent().layout();
1210 validate();
1211 updatePreviews();
1212 }
1213 });
1214
1215 addAttributeLabel = new Label(addAttributeComposite, SWT.NULL);
1216 addAttributeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1217 addAttributeLabel.setText(Messages.CustomXmlParserInputWizardPage_newAttibute);
1218 }
1219
1220 private void removeAddButton() {
1221 fillerLabel.dispose();
1222 addAttributeComposite.dispose();
1223 }
1224
1225 private void removeAttribute(int attributeNumber) {
1226 int nb = attributeNumber;
1227 if (--nb < attributes.size()) {
1228 attributes.remove(nb).dispose();
1229 for (int i = nb; i < attributes.size(); i++) {
1230 attributes.get(i).setAttributeNumber(i + 1);
1231 }
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 }
1237 }
1238
1239 private void dispose() {
1240 group.dispose();
1241 }
1242
1243 private void extractInputs() {
1244 inputElement.setElementName(elementNameText.getText().trim());
1245 if (inputElement.getParentElement() != null) {
1246 inputElement.setLogEntry(logEntryButton.getSelection());
1247 inputElement.setEventType(eventTypeButton.getSelection() ? eventTypeText.getText().trim() : null);
1248 Tag inputTag = Tag.fromLabel(tagCombo.getText());
1249 inputElement.setInputTag(inputTag);
1250 if (inputTag.equals(Tag.OTHER)) {
1251 inputElement.setInputName(tagText.getText().trim());
1252 } else {
1253 inputElement.setInputName(inputTag.toString());
1254 if (inputTag.equals(Tag.TIMESTAMP)) {
1255 inputElement.setInputFormat(tagText.getText().trim());
1256 }
1257 }
1258 inputElement.setInputAction(actionCombo.getSelectionIndex());
1259 }
1260 inputElement.setAttributes(new ArrayList<CustomXmlInputAttribute>(attributes.size()));
1261 for (int i = 0; i < attributes.size(); i++) {
1262 String inputName = null;
1263 String inputFormat = null;
1264 Attribute attribute = attributes.get(i);
1265 String attributeName = attribute.attributeNameText.getText().trim();
1266 Tag inputTag = Tag.fromLabel(attribute.tagCombo.getText());
1267 if (inputTag.equals(Tag.OTHER)) {
1268 inputName = attribute.tagText.getText().trim();
1269 } else {
1270 inputName = inputTag.toString();
1271 if (inputTag.equals(Tag.TIMESTAMP)) {
1272 inputFormat = attribute.tagText.getText().trim();
1273 }
1274 }
1275 int inputAction = attribute.actionCombo.getSelectionIndex();
1276 inputElement.addAttribute(new CustomXmlInputAttribute(attributeName, inputTag, inputName, inputAction, inputFormat));
1277 }
1278 }
1279 }
1280
1281 private class Attribute {
1282 private ElementNode element;
1283 private int attributeNumber;
1284
1285 // children of parent (must be disposed)
1286 private Composite labelComposite;
1287 private Composite attributeComposite;
1288 private Label filler;
1289 private Composite tagComposite;
1290
1291 // children of labelComposite
1292 private Label attributeLabel;
1293
1294 // children of attributeComposite
1295 private Text attributeNameText;
1296 private Text previewText;
1297
1298 // children of tagComposite
1299 private Combo tagCombo;
1300 private Label tagLabel;
1301 private Text tagText;
1302 private Combo actionCombo;
1303
1304 public Attribute(Composite parent, ElementNode element, CustomXmlInputAttribute inputAttribute, int attributeNumber) {
1305 this.element = element;
1306 this.attributeNumber = attributeNumber;
1307
1308 labelComposite = new Composite(parent, SWT.FILL);
1309 GridLayout labelLayout = new GridLayout(2, false);
1310 labelLayout.marginWidth = 0;
1311 labelLayout.marginHeight = 0;
1312 labelComposite.setLayout(labelLayout);
1313 labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1314
1315 Button deleteButton = new Button(labelComposite, SWT.PUSH);
1316 deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1317 deleteButton.setImage(DELETE_IMAGE);
1318 deleteButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_removeAttribute);
1319 deleteButton.addSelectionListener(new SelectionAdapter() {
1320 @Override
1321 public void widgetSelected(SelectionEvent e) {
1322 Attribute.this.element.removeAttribute(Attribute.this.attributeNumber);
1323 validate();
1324 updatePreviews();
1325 }
1326 });
1327
1328 attributeLabel = new Label(labelComposite, SWT.NULL);
1329 attributeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1330 attributeLabel.setText(Messages.CustomXmlParserInputWizardPage_attibute);
1331
1332 attributeComposite = new Composite(parent, SWT.FILL);
1333 GridLayout attributeLayout = new GridLayout(4, false);
1334 attributeLayout.marginWidth = 0;
1335 attributeLayout.marginHeight = 0;
1336 attributeComposite.setLayout(attributeLayout);
1337 attributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1338
1339 Label nameLabel = new Label(attributeComposite, SWT.NONE);
1340 nameLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
1341
1342 attributeNameText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE);
1343 attributeNameText.setLayoutData(new GridData(120, SWT.DEFAULT));
1344 attributeNameText.setText(inputAttribute.getAttributeName());
1345 attributeNameText.addModifyListener(updateListener);
1346
1347 Label previewLabel = new Label(attributeComposite, SWT.NONE);
1348 previewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview);
1349
1350 previewText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
1351 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1352 gd.widthHint = 0;
1353 previewText.setLayoutData(gd);
1354 previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatch);
1355 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
1356
1357 filler = new Label(parent, SWT.NULL);
1358
1359 tagComposite = new Composite(parent, SWT.FILL);
1360 GridLayout tagLayout = new GridLayout(4, false);
1361 tagLayout.marginWidth = 0;
1362 tagLayout.marginHeight = 0;
1363 tagComposite.setLayout(tagLayout);
1364 tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1365
1366 tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1367 tagCombo.setItems(new String[] {
1368 Tag.TIMESTAMP.toString(),
1369 Tag.EVENT_TYPE.toString(),
1370 Tag.MESSAGE.toString(),
1371 Tag.OTHER.toString() });
1372 tagCombo.select(3); // Other
1373 tagCombo.addSelectionListener(new SelectionListener() {
1374 @Override
1375 public void widgetDefaultSelected(SelectionEvent e) {
1376 }
1377
1378 @Override
1379 public void widgetSelected(SelectionEvent e) {
1380 tagText.removeModifyListener(updateListener);
1381 switch (tagCombo.getSelectionIndex()) {
1382 case 0: // Time Stamp
1383 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
1384 tagLabel.setVisible(true);
1385 tagText.setVisible(true);
1386 tagText.addModifyListener(updateListener);
1387 break;
1388 case 1: // Event type
1389 tagLabel.setVisible(false);
1390 tagText.setVisible(false);
1391 break;
1392 case 2: // Message
1393 tagLabel.setVisible(false);
1394 tagText.setVisible(false);
1395 break;
1396 case 3: // Other
1397 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
1398 tagLabel.setVisible(true);
1399 if (tagText.getText().trim().length() == 0) {
1400 tagText.setText(attributeNameText.getText().trim());
1401 }
1402 tagText.setVisible(true);
1403 tagText.addModifyListener(updateListener);
1404 break;
1405 default:
1406 break;
1407 }
1408 tagComposite.layout();
1409 validate();
1410 updatePreviews();
1411 }
1412 });
1413
1414 tagLabel = new Label(tagComposite, SWT.NULL);
1415 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1416
1417 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
1418 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1419 gd.widthHint = 0;
1420 tagText.setLayoutData(gd);
1421 tagText.setText(attributeNameText.getText());
1422
1423 actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1424 actionCombo.setItems(new String[] { Messages.CustomXmlParserInputWizardPage_set, Messages.CustomXmlParserInputWizardPage_append,
1425 Messages.CustomXmlParserInputWizardPage_appendWith });
1426 actionCombo.select(inputAttribute.getInputAction());
1427 actionCombo.addSelectionListener(updateListener);
1428
1429 if (inputAttribute.getInputTag().equals(Tag.TIMESTAMP)) {
1430 tagCombo.select(0);
1431 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
1432 tagText.setText(inputAttribute.getInputFormat());
1433 tagText.addModifyListener(updateListener);
1434 } else if (inputAttribute.getInputTag().equals(Tag.EVENT_TYPE)) {
1435 tagCombo.select(1);
1436 tagLabel.setVisible(false);
1437 tagText.setVisible(false);
1438 } else if (inputAttribute.getInputTag().equals(Tag.MESSAGE)) {
1439 tagCombo.select(2);
1440 tagLabel.setVisible(false);
1441 tagText.setVisible(false);
1442 } else {
1443 tagCombo.select(3);
1444 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
1445 tagText.setText(inputAttribute.getInputName());
1446 tagText.addModifyListener(updateListener);
1447 }
1448 }
1449
1450 private void dispose() {
1451 labelComposite.dispose();
1452 attributeComposite.dispose();
1453 filler.dispose();
1454 tagComposite.dispose();
1455 }
1456
1457 private void setAttributeNumber(int attributeNumber) {
1458 this.attributeNumber = attributeNumber;
1459 labelComposite.layout();
1460 }
1461 }
1462
1463 private Element getPreviewElement(CustomXmlInputElement inputElement) {
1464 CustomXmlInputElement currentElement = inputElement;
1465 Element element = documentElement;
1466 if (element != null) {
1467 if (!documentElement.getNodeName().equals(definition.rootInputElement.getElementName())) {
1468 return null;
1469 }
1470 ArrayList<String> elementNames = new ArrayList<>();
1471 while (currentElement != null) {
1472 elementNames.add(currentElement.getElementName());
1473 currentElement = currentElement.getParentElement();
1474 }
1475 for (int i = elementNames.size() - 1; --i >= 0;) {
1476 NodeList childList = element.getChildNodes();
1477 element = null;
1478 for (int j = 0; j < childList.getLength(); j++) {
1479 Node child = childList.item(j);
1480 if (child instanceof Element && child.getNodeName().equals(elementNames.get(i))) {
1481 element = (Element) child;
1482 break;
1483 }
1484 }
1485 if (element == null) {
1486 break;
1487 }
1488 }
1489 if (element != null) {
1490 return element;
1491 }
1492 }
1493 return null;
1494 }
1495
1496 private String getChildNameSuggestion(CustomXmlInputElement inputElement) {
1497 if (inputElement == null) {
1498 if (documentElement != null) {
1499 return documentElement.getNodeName();
1500 }
1501 } else {
1502 Element element = getPreviewElement(inputElement);
1503 if (element != null) {
1504 NodeList childNodes = element.getChildNodes();
1505 for (int i = 0; i < childNodes.getLength(); i++) {
1506 Node node = childNodes.item(i);
1507 if (node instanceof Element) {
1508 boolean unused = true;
1509 if (inputElement.getChildElements() != null) {
1510 for (CustomXmlInputElement child : inputElement.getChildElements()) {
1511 if (child.getElementName().equals(node.getNodeName())) {
1512 unused = false;
1513 break;
1514 }
1515 }
1516 }
1517 if (unused) {
1518 return node.getNodeName();
1519 }
1520 }
1521 }
1522 }
1523 }
1524 return ""; //$NON-NLS-1$
1525 }
1526
1527 private String getAttributeNameSuggestion(CustomXmlInputElement inputElement) {
1528 Element element = getPreviewElement(inputElement);
1529 if (element != null) {
1530 NamedNodeMap attributeMap = element.getAttributes();
1531 for (int i = 0; i < attributeMap.getLength(); i++) {
1532 Node node = attributeMap.item(i);
1533 boolean unused = true;
1534 if (inputElement.getAttributes() != null) {
1535 for (CustomXmlInputAttribute attribute : inputElement.getAttributes()) {
1536 if (attribute.getAttributeName().equals(node.getNodeName())) {
1537 unused = false;
1538 break;
1539 }
1540 }
1541 }
1542 if (unused) {
1543 return node.getNodeName();
1544 }
1545 }
1546 }
1547 return ""; //$NON-NLS-1$
1548 }
1549
1550 private void validate() {
1551 definition.categoryName = categoryText.getText().trim();
1552 definition.definitionName = logtypeText.getText().trim();
1553 definition.timeStampOutputFormat = timeStampOutputFormatText.getText().trim();
1554
1555 if (selectedElement != null) {
1556 selectedElement.extractInputs();
1557 treeViewer.refresh();
1558 }
1559
1560 List<String> errors = new ArrayList<>();
1561
1562 if (definition.categoryName.length() == 0) {
1563 errors.add(Messages.CustomXmlParserInputWizardPage_emptyCategoryError);
1564 categoryText.setBackground(COLOR_LIGHT_RED);
1565 } else if (definition.definitionName.length() == 0) {
1566 errors.add(Messages.CustomXmlParserInputWizardPage_emptyLogTypeError);
1567 logtypeText.setBackground(COLOR_LIGHT_RED);
1568 } else {
1569 categoryText.setBackground(COLOR_TEXT_BACKGROUND);
1570 logtypeText.setBackground(COLOR_TEXT_BACKGROUND);
1571 if (definition.categoryName.indexOf(':') != -1) {
1572 errors.add(Messages.CustomXmlParserInputWizardPage_invalidCategoryError);
1573 categoryText.setBackground(COLOR_LIGHT_RED);
1574 }
1575 if (definition.definitionName.indexOf(':') != -1) {
1576 errors.add(Messages.CustomXmlParserInputWizardPage_invalidLogTypeError);
1577 logtypeText.setBackground(COLOR_LIGHT_RED);
1578 }
1579 for (TraceTypeHelper helper : TmfTraceType.getTraceTypeHelpers()) {
1580 if (definition.categoryName.equals(helper.getCategoryName()) &&
1581 definition.definitionName.equals(helper.getName()) &&
1582 (editDefinitionName == null || !editDefinitionName.equals(definition.definitionName)) &&
1583 (editCategoryName == null || !editCategoryName.equals(definition.categoryName))) {
1584 errors.add(Messages.CustomXmlParserInputWizardPage_duplicatelogTypeError);
1585 logtypeText.setBackground(COLOR_LIGHT_RED);
1586 break;
1587 }
1588 }
1589 }
1590
1591 if (definition.rootInputElement == null) {
1592 errors.add(Messages.CustomXmlParserInputWizardPage_noDocumentError);
1593 }
1594
1595 if (definition.rootInputElement != null) {
1596 logEntryFound = false;
1597 timeStampFound = false;
1598
1599 errors.addAll(validateElement(definition.rootInputElement));
1600
1601 if ((definition.rootInputElement.getAttributes() != null && definition.rootInputElement.getAttributes().size() != 0)
1602 || (definition.rootInputElement.getChildElements() != null && definition.rootInputElement.getChildElements().size() != 0)
1603 || errors.size() == 0) {
1604 if (!logEntryFound) {
1605 errors.add(Messages.CustomXmlParserInputWizardPage_missingLogEntryError);
1606 }
1607
1608 if (timeStampFound && !definition.timeStampOutputFormat.isEmpty()) {
1609 try {
1610 new TmfTimestampFormat(timeStampOutputFormatText.getText().trim());
1611 timeStampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1612 } catch (IllegalArgumentException e) {
1613 errors.add(Messages.CustomXmlParserInputWizardPage_elementInvalidTimestampFmtError);
1614 timeStampOutputFormatText.setBackground(COLOR_LIGHT_RED);
1615 }
1616 } else {
1617 timeStampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1618 if (!timeStampFound) {
1619 timeStampPreviewText.setText(Messages.CustomXmlParserInputWizardPage_noTimestampElementOrAttribute);
1620 }
1621 }
1622 }
1623 } else {
1624 timeStampPreviewText.setText(Messages.CustomXmlParserInputWizardPage_noTimestampElementOrAttribute);
1625 }
1626
1627 if (errors.size() == 0) {
1628 setDescription(defaultDescription);
1629 setPageComplete(true);
1630 } else {
1631 setDescription(Joiner.on(' ').join(errors));
1632 setPageComplete(false);
1633 }
1634 }
1635
1636 /**
1637 * Clean up the specified XML element.
1638 *
1639 * @param inputElement
1640 * The element to clean up
1641 * @return The validated element
1642 */
1643 public List<String> validateElement(CustomXmlInputElement inputElement) {
1644 List<String> errors = new ArrayList<>();
1645 ElementNode elementNode = null;
1646 if (selectedElement != null && selectedElement.inputElement.equals(inputElement)) {
1647 elementNode = selectedElement;
1648 }
1649 if (inputElement == definition.rootInputElement) {
1650 if (inputElement.getElementName().length() == 0) {
1651 errors.add(Messages.CustomXmlParserInputWizardPage_missingDocumentElementError);
1652 if (elementNode != null) {
1653 elementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1654 }
1655 } else {
1656 if (elementNode != null) {
1657 elementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);
1658 }
1659 }
1660 }
1661 if (inputElement != definition.rootInputElement) {
1662 if (inputElement.isLogEntry()) {
1663 logEntryFound = true;
1664 }
1665 if (inputElement.getInputTag().equals(Tag.TIMESTAMP)) {
1666 timeStampFound = true;
1667 if (inputElement.getInputFormat().length() == 0) {
1668 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementMissingTimestampFmtError, getName(inputElement)));
1669 if (elementNode != null) {
1670 elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1671 }
1672 } else {
1673 try {
1674 new TmfTimestampFormat(inputElement.getInputFormat());
1675 if (elementNode != null) {
1676 elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1677 }
1678 } catch (IllegalArgumentException e) {
1679 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementInvalidTimestampFmtError, getName(inputElement)));
1680 if (elementNode != null) {
1681 elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1682 }
1683 }
1684 }
1685 } else if (inputElement.getInputName().length() == 0) {
1686 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementMissingInputNameError, getName(inputElement)));
1687 if (elementNode != null) {
1688 elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1689 }
1690 } else if (inputElement.getInputTag().equals(Tag.OTHER) && Tag.fromLabel(inputElement.getInputName()) != null) {
1691 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementReservedInputNameError, getName(inputElement)));
1692 if (elementNode != null) {
1693 elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1694 }
1695 } else {
1696 if (elementNode != null) {
1697 elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1698 }
1699 }
1700 if (inputElement.getEventType() != null && inputElement.getEventType().trim().isEmpty()) {
1701 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_emptyEventTypeError, getName(inputElement)));
1702 if (elementNode != null) {
1703 elementNode.eventTypeText.setBackground(COLOR_LIGHT_RED);
1704 }
1705 } else {
1706 if (elementNode != null) {
1707 elementNode.eventTypeText.setBackground(COLOR_TEXT_BACKGROUND);
1708 }
1709 }
1710 }
1711 if (inputElement.getAttributes() != null) {
1712 if (elementNode != null) {
1713 for (Attribute attribute : elementNode.attributes) {
1714 attribute.attributeNameText.setBackground(COLOR_TEXT_BACKGROUND);
1715 }
1716 }
1717 for (int i = 0; i < inputElement.getAttributes().size(); i++) {
1718 CustomXmlInputAttribute attribute = inputElement.getAttributes().get(i);
1719 boolean duplicate = false;
1720 for (int j = i + 1; j < inputElement.getAttributes().size(); j++) {
1721 CustomXmlInputAttribute otherAttribute = inputElement.getAttributes().get(j);
1722 if (otherAttribute.getAttributeName().equals(attribute.getAttributeName())) {
1723 duplicate = true;
1724 if (elementNode != null) {
1725 elementNode.attributes.get(j).attributeNameText.setBackground(COLOR_LIGHT_RED);
1726 }
1727 }
1728 }
1729 if (attribute.getAttributeName().length() == 0) {
1730 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeMissingNameError, getName(inputElement)));
1731 if (elementNode != null) {
1732 elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED);
1733 }
1734 } else if (duplicate) {
1735 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeDuplicateNameError, getName(attribute, inputElement)));
1736 if (elementNode != null) {
1737 elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED);
1738 }
1739 }
1740 if (attribute.getInputTag().equals(Tag.TIMESTAMP)) {
1741 timeStampFound = true;
1742 if (attribute.getInputFormat().length() == 0) {
1743 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeMissingTimestampFmtError, getName(attribute, inputElement)));
1744 if (elementNode != null) {
1745 elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1746 }
1747 } else {
1748 try {
1749 new TmfTimestampFormat(attribute.getInputFormat());
1750 if (elementNode != null) {
1751 elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);
1752 }
1753 } catch (IllegalArgumentException e) {
1754 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeInvalidTimestampFmtError, getName(attribute, inputElement)));
1755 if (elementNode != null) {
1756 elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1757 }
1758 }
1759 }
1760 } else if (attribute.getInputName().length() == 0) {
1761 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeMissingInputNameError, getName(attribute, inputElement)));
1762 if (elementNode != null) {
1763 elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1764 }
1765 } else if (attribute.getInputTag().equals(Tag.OTHER) && Tag.fromLabel(attribute.getInputName()) != null) {
1766 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_attributeReservedInputNameError, getName(attribute, inputElement)));
1767 if (elementNode != null) {
1768 elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1769 }
1770 } else {
1771 if (elementNode != null) {
1772 elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);
1773 }
1774 }
1775 }
1776 }
1777 if (inputElement.getChildElements() != null) {
1778 for (CustomXmlInputElement child : inputElement.getChildElements()) {
1779 ElementNode childElementNode = null;
1780 if (selectedElement != null && selectedElement.inputElement.equals(child)) {
1781 childElementNode = selectedElement;
1782 }
1783 if (childElementNode != null) {
1784 childElementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);
1785 }
1786 }
1787 for (int i = 0; i < inputElement.getChildElements().size(); i++) {
1788 CustomXmlInputElement child = inputElement.getChildElements().get(i);
1789 ElementNode childElementNode = null;
1790 if (selectedElement != null && selectedElement.inputElement.equals(child)) {
1791 childElementNode = selectedElement;
1792 }
1793 if (child.getElementName().length() == 0) {
1794 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementMissingNameError, getName(child)));
1795 if (childElementNode != null) {
1796 childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1797 }
1798 } else {
1799 boolean duplicate = false;
1800 for (int j = i + 1; j < inputElement.getChildElements().size(); j++) {
1801 CustomXmlInputElement otherChild = inputElement.getChildElements().get(j);
1802 if (otherChild.getElementName().equals(child.getElementName())) {
1803 duplicate = true;
1804 ElementNode otherChildElementNode = null;
1805 if (selectedElement != null && selectedElement.inputElement.equals(otherChild)) {
1806 otherChildElementNode = selectedElement;
1807 }
1808 if (otherChildElementNode != null) {
1809 otherChildElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1810 }
1811 }
1812 }
1813 if (duplicate) {
1814 errors.add(NLS.bind(Messages.CustomXmlParserInputWizardPage_elementDuplicateNameError, getName(child)));
1815 if (childElementNode != null) {
1816 childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1817 }
1818 }
1819 }
1820
1821 errors.addAll(validateElement(child));
1822 }
1823 }
1824 return errors;
1825 }
1826
1827 /**
1828 * Get the trace definition.
1829 *
1830 * @return The trace definition
1831 */
1832 public CustomXmlTraceDefinition getDefinition() {
1833 return definition;
1834 }
1835
1836 /**
1837 * Get the raw text input.
1838 *
1839 * @return The raw text input.
1840 */
1841 public char[] getInputText() {
1842 return inputText.getText().toCharArray();
1843 }
1844 }
This page took 0.088758 seconds and 4 git commands to generate.