1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
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
10 * Patrick Tassé - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.wizards
;
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
.text
.ParseException
;
21 import java
.text
.SimpleDateFormat
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Date
;
24 import java
.util
.List
;
26 import javax
.xml
.parsers
.DocumentBuilder
;
27 import javax
.xml
.parsers
.DocumentBuilderFactory
;
28 import javax
.xml
.parsers
.ParserConfigurationException
;
30 import org
.eclipse
.core
.resources
.IFile
;
31 import org
.eclipse
.core
.runtime
.CoreException
;
32 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
33 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
34 import org
.eclipse
.jface
.viewers
.ISelection
;
35 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
36 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
37 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
38 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
39 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
40 import org
.eclipse
.jface
.viewers
.TreeViewer
;
41 import org
.eclipse
.jface
.viewers
.Viewer
;
42 import org
.eclipse
.jface
.wizard
.WizardPage
;
43 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
44 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
45 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTraceDefinition
;
46 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
47 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
48 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputAttribute
;
49 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputElement
;
50 import org
.eclipse
.swt
.SWT
;
51 import org
.eclipse
.swt
.browser
.Browser
;
52 import org
.eclipse
.swt
.browser
.TitleEvent
;
53 import org
.eclipse
.swt
.browser
.TitleListener
;
54 import org
.eclipse
.swt
.custom
.SashForm
;
55 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
56 import org
.eclipse
.swt
.custom
.StyleRange
;
57 import org
.eclipse
.swt
.custom
.StyledText
;
58 import org
.eclipse
.swt
.events
.ModifyEvent
;
59 import org
.eclipse
.swt
.events
.ModifyListener
;
60 import org
.eclipse
.swt
.events
.SelectionAdapter
;
61 import org
.eclipse
.swt
.events
.SelectionEvent
;
62 import org
.eclipse
.swt
.events
.SelectionListener
;
63 import org
.eclipse
.swt
.graphics
.Color
;
64 import org
.eclipse
.swt
.graphics
.Font
;
65 import org
.eclipse
.swt
.graphics
.FontData
;
66 import org
.eclipse
.swt
.graphics
.Image
;
67 import org
.eclipse
.swt
.layout
.FillLayout
;
68 import org
.eclipse
.swt
.layout
.GridData
;
69 import org
.eclipse
.swt
.layout
.GridLayout
;
70 import org
.eclipse
.swt
.widgets
.Button
;
71 import org
.eclipse
.swt
.widgets
.Combo
;
72 import org
.eclipse
.swt
.widgets
.Composite
;
73 import org
.eclipse
.swt
.widgets
.Display
;
74 import org
.eclipse
.swt
.widgets
.Group
;
75 import org
.eclipse
.swt
.widgets
.Label
;
76 import org
.eclipse
.swt
.widgets
.Shell
;
77 import org
.eclipse
.swt
.widgets
.Text
;
78 import org
.w3c
.dom
.Document
;
79 import org
.w3c
.dom
.Element
;
80 import org
.w3c
.dom
.NamedNodeMap
;
81 import org
.w3c
.dom
.Node
;
82 import org
.w3c
.dom
.NodeList
;
83 import org
.xml
.sax
.EntityResolver
;
84 import org
.xml
.sax
.ErrorHandler
;
85 import org
.xml
.sax
.InputSource
;
86 import org
.xml
.sax
.SAXException
;
87 import org
.xml
.sax
.SAXParseException
;
90 * Input wizard page for custom XML trace parsers.
92 * @author Patrick Tassé
94 public class CustomXmlParserInputWizardPage
extends WizardPage
{
96 private static final String DEFAULT_TIMESTAMP_FORMAT
= "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
97 private static final String SIMPLE_DATE_FORMAT_URL
= "http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#skip-navbar_top"; //$NON-NLS-1$
98 private static final Image elementImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/element_icon.gif"); //$NON-NLS-1$
99 private static final Image addImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
100 private static final Image addNextImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/addnext_button.gif"); //$NON-NLS-1$
101 private static final Image addChildImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/addchild_button.gif"); //$NON-NLS-1$
102 private static final Image addManyImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/addmany_button.gif"); //$NON-NLS-1$
103 private static final Image deleteImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
104 private static final Image moveUpImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
105 private static final Image moveDownImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
106 private static final Image helpImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/help_button.gif"); //$NON-NLS-1$
107 private static final Color COLOR_LIGHT_RED
= new Color(Display
.getDefault(), 255, 192, 192);
108 private static final Color COLOR_TEXT_BACKGROUND
= Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
);
109 private static final Color COLOR_WIDGET_BACKGROUND
= Display
.getCurrent().getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
);
111 private final ISelection selection
;
112 private CustomXmlTraceDefinition definition
;
113 private String editDefinitionName
;
114 private String defaultDescription
;
115 private ElementNode selectedElement
;
116 private Composite container
;
117 private Text logtypeText
;
118 private Text timeStampOutputFormatText
;
119 private Text timeStampPreviewText
;
120 private Button removeButton
;
121 private Button addChildButton
;
122 private Button addNextButton
;
123 private Button moveUpButton
;
124 private Button moveDownButton
;
125 private Button feelingLuckyButton
;
126 private ScrolledComposite treeScrolledComposite
;
127 private ScrolledComposite elementScrolledComposite
;
128 private TreeViewer treeViewer
;
129 private Composite treeContainer
;
130 private Composite elementContainer
;
131 private Text errorText
;
132 private StyledText inputText
;
133 private Font fixedFont
;
134 private UpdateListener updateListener
;
135 private Browser helpBrowser
;
136 private Element documentElement
;
138 // variables used recursively through element traversal
139 private String timeStampValue
;
140 private String timeStampFormat
;
141 private boolean timeStampFound
;
142 private int logEntriesCount
;
143 private boolean logEntryFound
;
153 protected CustomXmlParserInputWizardPage(ISelection selection
, CustomXmlTraceDefinition definition
) {
154 super("CustomXmlParserWizardPage"); //$NON-NLS-1$
155 if (definition
== null) {
156 setTitle(Messages
.CustomXmlParserInputWizardPage_titleNew
);
157 defaultDescription
= Messages
.CustomXmlParserInputWizardPage_descriptionNew
;
159 setTitle(Messages
.CustomXmlParserInputWizardPage_titleEdit
);
160 defaultDescription
= Messages
.CustomXmlParserInputWizardPage_descriptionEdit
;
162 setDescription(defaultDescription
);
163 this.selection
= selection
;
164 this.definition
= definition
;
165 if (definition
!= null) {
166 this.editDefinitionName
= definition
.definitionName
;
171 public void createControl(Composite parent
) {
172 container
= new Composite(parent
, SWT
.NULL
);
173 container
.setLayout(new GridLayout());
175 updateListener
= new UpdateListener();
177 Composite headerComposite
= new Composite(container
, SWT
.FILL
);
178 GridLayout headerLayout
= new GridLayout(5, false);
179 headerLayout
.marginHeight
= 0;
180 headerLayout
.marginWidth
= 0;
181 headerComposite
.setLayout(headerLayout
);
182 headerComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
184 Label logtypeLabel
= new Label(headerComposite
, SWT
.NULL
);
185 logtypeLabel
.setText(Messages
.CustomXmlParserInputWizardPage_logType
);
187 logtypeText
= new Text(headerComposite
, SWT
.BORDER
| SWT
.SINGLE
);
188 logtypeText
.setLayoutData(new GridData(120, SWT
.DEFAULT
));
190 Label timeStampFormatLabel
= new Label(headerComposite
, SWT
.NULL
);
191 timeStampFormatLabel
.setText(Messages
.CustomXmlParserInputWizardPage_timestampFormat
);
193 timeStampOutputFormatText
= new Text(headerComposite
, SWT
.BORDER
| SWT
.SINGLE
);
194 timeStampOutputFormatText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
195 timeStampOutputFormatText
.setText(DEFAULT_TIMESTAMP_FORMAT
);
197 Button dateFormatHelpButton
= new Button(headerComposite
, SWT
.PUSH
);
198 dateFormatHelpButton
.setImage(helpImage
);
199 dateFormatHelpButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_dateFormatHelp
);
200 dateFormatHelpButton
.addSelectionListener(new SelectionAdapter() {
202 public void widgetSelected(SelectionEvent e
) {
203 openHelpShell(SIMPLE_DATE_FORMAT_URL
);
207 Label timeStampPreviewLabel
= new Label(headerComposite
, SWT
.NULL
);
208 timeStampPreviewLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 3, 1));
209 timeStampPreviewLabel
.setText(Messages
.CustomXmlParserInputWizardPage_preview
);
211 timeStampPreviewText
= new Text(headerComposite
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.READ_ONLY
);
212 timeStampPreviewText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 1));
213 timeStampPreviewText
.setText("*no time stamp element or attribute*"); //$NON-NLS-1$
217 SashForm vSash
= new SashForm(container
, SWT
.VERTICAL
);
218 vSash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
219 vSash
.setBackground(vSash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
221 SashForm hSash
= new SashForm(vSash
, SWT
.HORIZONTAL
);
222 hSash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
224 treeScrolledComposite
= new ScrolledComposite(hSash
, SWT
.V_SCROLL
| SWT
.H_SCROLL
);
225 treeScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
226 treeContainer
= new Composite(treeScrolledComposite
, SWT
.NONE
);
227 treeContainer
.setLayout(new FillLayout());
228 treeScrolledComposite
.setContent(treeContainer
);
229 treeScrolledComposite
.setExpandHorizontal(true);
230 treeScrolledComposite
.setExpandVertical(true);
232 treeViewer
= new TreeViewer(treeContainer
, SWT
.SINGLE
| SWT
.BORDER
);
233 treeViewer
.setContentProvider(new InputElementTreeNodeContentProvider());
234 treeViewer
.setLabelProvider(new InputElementTreeLabelProvider());
235 treeViewer
.addSelectionChangedListener(new InputElementTreeSelectionChangedListener());
236 treeContainer
.layout();
238 treeScrolledComposite
239 .setMinSize(treeContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, treeContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
);
241 elementScrolledComposite
= new ScrolledComposite(hSash
, SWT
.V_SCROLL
);
242 elementScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
243 elementContainer
= new Composite(elementScrolledComposite
, SWT
.NONE
);
244 GridLayout gl
= new GridLayout();
247 elementContainer
.setLayout(gl
);
248 elementScrolledComposite
.setContent(elementContainer
);
249 elementScrolledComposite
.setExpandHorizontal(true);
250 elementScrolledComposite
.setExpandVertical(true);
252 if (definition
== null) {
253 definition
= new CustomXmlTraceDefinition();
255 loadDefinition(definition
);
256 treeViewer
.expandAll();
257 elementContainer
.layout();
259 logtypeText
.addModifyListener(updateListener
);
260 timeStampOutputFormatText
.addModifyListener(updateListener
);
262 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
,
263 elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
- 1);
265 hSash
.setWeights(new int[] { 1, 2 });
267 if (definition
.rootInputElement
== null) {
268 removeButton
.setEnabled(false);
269 addChildButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_addDocumentElement
);
270 addNextButton
.setEnabled(false);
271 moveUpButton
.setEnabled(false);
272 moveDownButton
.setEnabled(false);
273 } else { // root is selected
274 addNextButton
.setEnabled(false);
277 Composite sashBottom
= new Composite(vSash
, SWT
.NONE
);
278 GridLayout sashBottomLayout
= new GridLayout(2, false);
279 sashBottomLayout
.marginHeight
= 0;
280 sashBottomLayout
.marginWidth
= 0;
281 sashBottom
.setLayout(sashBottomLayout
);
283 Label previewLabel
= new Label(sashBottom
, SWT
.NULL
);
284 previewLabel
.setText(Messages
.CustomXmlParserInputWizardPage_previewInput
);
286 errorText
= new Text(sashBottom
, SWT
.SINGLE
| SWT
.READ_ONLY
);
287 errorText
.setBackground(COLOR_WIDGET_BACKGROUND
);
288 errorText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
289 errorText
.setVisible(false);
291 inputText
= new StyledText(sashBottom
, SWT
.MULTI
| SWT
.V_SCROLL
| SWT
.H_SCROLL
);
292 if (fixedFont
== null) {
293 if (System
.getProperty("os.name").contains("Windows")) { //$NON-NLS-1$ //$NON-NLS-2$
294 fixedFont
= new Font(Display
.getCurrent(), new FontData("Courier New", 10, SWT
.NORMAL
)); //$NON-NLS-1$
296 fixedFont
= new Font(Display
.getCurrent(), new FontData("Monospace", 10, SWT
.NORMAL
)); //$NON-NLS-1$
299 inputText
.setFont(fixedFont
);
300 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true, 2, 1);
301 gd
.heightHint
= inputText
.computeSize(SWT
.DEFAULT
, inputText
.getLineHeight() * 4).y
;
303 inputText
.setLayoutData(gd
);
304 inputText
.setText(getSelectionText());
305 inputText
.addModifyListener(new ModifyListener() {
307 public void modifyText(ModifyEvent e
) {
308 parseXmlInput(inputText
.getText());
311 inputText
.addModifyListener(updateListener
);
313 vSash
.setWeights(new int[] { hSash
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sashBottom
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
315 setControl(container
);
318 private void createButtonBar() {
319 Composite buttonBar
= new Composite(container
, SWT
.NONE
);
320 GridLayout buttonBarLayout
= new GridLayout(6, false);
321 buttonBarLayout
.marginHeight
= 0;
322 buttonBarLayout
.marginWidth
= 0;
323 buttonBar
.setLayout(buttonBarLayout
);
325 removeButton
= new Button(buttonBar
, SWT
.PUSH
);
326 removeButton
.setImage(deleteImage
);
327 removeButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_removeElement
);
328 removeButton
.addSelectionListener(new SelectionAdapter() {
330 public void widgetSelected(SelectionEvent e
) {
331 if (treeViewer
.getSelection().isEmpty() || selectedElement
== null) {
335 InputElement inputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
336 if (inputElement
== definition
.rootInputElement
) {
337 definition
.rootInputElement
= null;
339 inputElement
.parentElement
.childElements
.remove(inputElement
);
341 treeViewer
.refresh();
344 removeButton
.setEnabled(false);
345 if (definition
.rootInputElement
== null) {
346 addChildButton
.setEnabled(true);
347 addChildButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_addDocumentEleemnt
);
349 addChildButton
.setEnabled(false);
351 addNextButton
.setEnabled(false);
352 moveUpButton
.setEnabled(false);
353 moveDownButton
.setEnabled(false);
357 addChildButton
= new Button(buttonBar
, SWT
.PUSH
);
358 addChildButton
.setImage(addChildImage
);
359 addChildButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_addChildElement
);
360 addChildButton
.addSelectionListener(new SelectionAdapter() {
362 public void widgetSelected(SelectionEvent e
) {
363 InputElement inputElement
= new InputElement("", false, CustomXmlTraceDefinition
.TAG_IGNORE
, 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$
364 if (definition
.rootInputElement
== null) {
365 definition
.rootInputElement
= inputElement
;
366 inputElement
.elementName
= getChildNameSuggestion(null);
367 } else if (treeViewer
.getSelection().isEmpty()) {
370 InputElement parentInputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
371 parentInputElement
.addChild(inputElement
);
372 inputElement
.elementName
= getChildNameSuggestion(parentInputElement
);
374 treeViewer
.refresh();
375 treeViewer
.setSelection(new StructuredSelection(inputElement
), true);
379 addNextButton
= new Button(buttonBar
, SWT
.PUSH
);
380 addNextButton
.setImage(addNextImage
);
381 addNextButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_addNextElement
);
382 addNextButton
.addSelectionListener(new SelectionAdapter() {
384 public void widgetSelected(SelectionEvent e
) {
385 InputElement inputElement
= new InputElement("", false, CustomXmlTraceDefinition
.TAG_IGNORE
, 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$
386 if (definition
.rootInputElement
== null) {
387 definition
.rootInputElement
= inputElement
;
388 inputElement
.elementName
= getChildNameSuggestion(null);
389 } else if (treeViewer
.getSelection().isEmpty()) {
392 InputElement previousInputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
393 if (previousInputElement
== definition
.rootInputElement
) {
396 previousInputElement
.addNext(inputElement
);
397 inputElement
.elementName
= getChildNameSuggestion(inputElement
.parentElement
);
399 treeViewer
.refresh();
400 treeViewer
.setSelection(new StructuredSelection(inputElement
), true);
404 feelingLuckyButton
= new Button(buttonBar
, SWT
.PUSH
);
405 feelingLuckyButton
.setImage(addManyImage
);
406 feelingLuckyButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_feelingLucky
);
407 feelingLuckyButton
.addSelectionListener(new SelectionAdapter() {
409 public void widgetSelected(SelectionEvent e
) {
410 InputElement inputElement
= null;
411 if (definition
.rootInputElement
== null) {
412 if (getChildNameSuggestion(null).length() != 0) {
413 inputElement
= new InputElement(getChildNameSuggestion(null), false, CustomXmlTraceDefinition
.TAG_IGNORE
, 0, "", null); //$NON-NLS-1$
414 definition
.rootInputElement
= inputElement
;
415 feelingLucky(inputElement
);
419 } else if (treeViewer
.getSelection().isEmpty()) {
422 inputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
423 feelingLucky(inputElement
);
425 treeViewer
.refresh();
426 treeViewer
.setSelection(new StructuredSelection(inputElement
), true);
427 treeViewer
.expandToLevel(inputElement
, AbstractTreeViewer
.ALL_LEVELS
);
431 moveUpButton
= new Button(buttonBar
, SWT
.PUSH
);
432 moveUpButton
.setImage(moveUpImage
);
433 moveUpButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_moveUp
);
434 moveUpButton
.addSelectionListener(new SelectionAdapter() {
436 public void widgetSelected(SelectionEvent e
) {
437 if (treeViewer
.getSelection().isEmpty()) {
440 InputElement inputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
441 if (inputElement
== definition
.rootInputElement
) {
444 inputElement
.moveUp();
445 treeViewer
.refresh();
451 moveDownButton
= new Button(buttonBar
, SWT
.PUSH
);
452 moveDownButton
.setImage(moveDownImage
);
453 moveDownButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_moveDown
);
454 moveDownButton
.addSelectionListener(new SelectionAdapter() {
456 public void widgetSelected(SelectionEvent e
) {
457 if (treeViewer
.getSelection().isEmpty()) {
460 InputElement inputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
461 if (inputElement
== definition
.rootInputElement
) {
464 inputElement
.moveDown();
465 treeViewer
.refresh();
472 private void feelingLucky(InputElement inputElement
) {
474 String attributeName
= getAttributeNameSuggestion(inputElement
);
475 if (attributeName
.length() == 0) {
478 InputAttribute attribute
= new InputAttribute(attributeName
, attributeName
, 0, ""); //$NON-NLS-1$
479 inputElement
.addAttribute(attribute
);
482 String childName
= getChildNameSuggestion(inputElement
);
483 if (childName
.length() == 0) {
486 InputElement childElement
= new InputElement(childName
, false, CustomXmlTraceDefinition
.TAG_IGNORE
, 0, "", null); //$NON-NLS-1$
487 inputElement
.addChild(childElement
);
488 feelingLucky(childElement
);
492 private static class InputElementTreeNodeContentProvider
implements ITreeContentProvider
{
495 public Object
[] getElements(Object inputElement
) {
496 CustomXmlTraceDefinition def
= (CustomXmlTraceDefinition
) inputElement
;
497 if (def
.rootInputElement
!= null) {
498 return new Object
[] { def
.rootInputElement
};
500 return new Object
[0];
504 public Object
[] getChildren(Object parentElement
) {
505 InputElement inputElement
= (InputElement
) parentElement
;
506 if (inputElement
.childElements
== null) {
507 return new InputElement
[0];
509 return inputElement
.childElements
.toArray();
513 public boolean hasChildren(Object element
) {
514 InputElement inputElement
= (InputElement
) element
;
515 return (inputElement
.childElements
!= null && inputElement
.childElements
.size() > 0);
519 public void dispose() {
523 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
527 public Object
getParent(Object element
) {
528 InputElement inputElement
= (InputElement
) element
;
529 return inputElement
.parentElement
;
533 private static class InputElementTreeLabelProvider
extends ColumnLabelProvider
{
536 public Image
getImage(Object element
) {
541 public String
getText(Object element
) {
542 InputElement inputElement
= (InputElement
) element
;
543 return (inputElement
.elementName
.trim().length() == 0) ?
"?" : inputElement
.elementName
; //$NON-NLS-1$
547 private class InputElementTreeSelectionChangedListener
implements ISelectionChangedListener
{
549 public void selectionChanged(SelectionChangedEvent event
) {
550 if (selectedElement
!= null) {
551 selectedElement
.dispose();
553 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
554 IStructuredSelection sel
= (IStructuredSelection
) event
.getSelection();
555 InputElement inputElement
= (InputElement
) sel
.getFirstElement();
556 selectedElement
= new ElementNode(elementContainer
, inputElement
);
557 elementContainer
.layout();
558 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
,
559 elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
- 1);
563 removeButton
.setEnabled(true);
564 addChildButton
.setEnabled(true);
565 addChildButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_addChildElement
);
566 if (definition
.rootInputElement
== inputElement
) {
567 addNextButton
.setEnabled(false);
569 addNextButton
.setEnabled(true);
571 moveUpButton
.setEnabled(true);
572 moveDownButton
.setEnabled(true);
574 removeButton
.setEnabled(false);
575 if (definition
.rootInputElement
== null) {
576 addChildButton
.setEnabled(true);
577 addChildButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_addDocumentElement
);
579 addChildButton
.setEnabled(false);
581 addNextButton
.setEnabled(false);
582 moveUpButton
.setEnabled(false);
583 moveDownButton
.setEnabled(false);
589 public void dispose() {
590 if (fixedFont
!= null) {
597 private void loadDefinition(CustomXmlTraceDefinition def
) {
598 logtypeText
.setText(def
.definitionName
);
599 timeStampOutputFormatText
.setText(def
.timeStampOutputFormat
);
600 treeViewer
.setInput(def
);
602 if (def
.rootInputElement
!= null) {
603 treeViewer
.setSelection(new StructuredSelection(def
.rootInputElement
));
607 private String
getName(InputElement inputElement
) {
608 String name
= (inputElement
.elementName
.trim().length() == 0) ?
"?" : inputElement
.elementName
.trim(); //$NON-NLS-1$
609 if (inputElement
.parentElement
== null) {
612 return getName(inputElement
.parentElement
) + " : " + name
; //$NON-NLS-1$
615 private String
getName(InputAttribute inputAttribute
, InputElement inputElement
) {
616 String name
= (inputAttribute
.attributeName
.trim().length() == 0) ?
"?" : inputAttribute
.attributeName
.trim(); //$NON-NLS-1$
617 return getName(inputElement
) + " : " + name
; //$NON-NLS-1$
621 public void setVisible(boolean visible
) {
626 super.setVisible(visible
);
630 * Get the global list of input names.
632 * @return The list of input names
634 public List
<String
> getInputNames() {
635 return getInputNames(definition
.rootInputElement
);
639 * Get the list of input names for a given element.
641 * @param inputElement
643 * @return The input names for this element
645 public List
<String
> getInputNames(InputElement inputElement
) {
646 List
<String
> inputs
= new ArrayList
<String
>();
647 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
648 String inputName
= inputElement
.inputName
;
649 if (!inputs
.contains(inputName
)) {
650 inputs
.add(inputName
);
653 if (inputElement
.attributes
!= null) {
654 for (InputAttribute attribute
: inputElement
.attributes
) {
655 String inputName
= attribute
.inputName
;
656 if (!inputs
.contains(inputName
)) {
657 inputs
.add(inputName
);
661 if (inputElement
.childElements
!= null) {
662 for (InputElement childInputElement
: inputElement
.childElements
) {
663 for (String inputName
: getInputNames(childInputElement
)) {
664 if (!inputs
.contains(inputName
)) {
665 inputs
.add(inputName
);
673 private void removeElement() {
674 selectedElement
.dispose();
675 selectedElement
= null;
676 elementContainer
.layout();
677 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
,
678 elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
- 1);
682 private String
getSelectionText() {
683 InputStream inputStream
= null;
684 if (this.selection
instanceof IStructuredSelection
) {
685 Object sel
= ((IStructuredSelection
) this.selection
).getFirstElement();
686 if (sel
instanceof IFile
) {
687 IFile file
= (IFile
) sel
;
689 inputStream
= file
.getContents();
690 } catch (CoreException e
) {
691 return ""; //$NON-NLS-1$
695 if (inputStream
!= null) {
696 BufferedReader reader
= null;
698 reader
= new BufferedReader(new InputStreamReader(inputStream
));
699 StringBuilder sb
= new StringBuilder();
701 while ((line
= reader
.readLine()) != null) {
702 sb
.append(line
+ "\n"); //$NON-NLS-1$
704 parseXmlInput(sb
.toString());
706 return sb
.toString();
707 } catch (IOException e
) {
708 return ""; //$NON-NLS-1$
711 return ""; //$NON-NLS-1$
714 private void parseXmlInput(final String string
) {
716 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
717 DocumentBuilder db
= dbf
.newDocumentBuilder();
719 // The following allows xml parsing without access to the dtd
720 EntityResolver resolver
= new EntityResolver() {
722 public InputSource
resolveEntity(String publicId
, String systemId
) {
723 String empty
= ""; //$NON-NLS-1$
724 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
725 return new InputSource(bais
);
728 db
.setEntityResolver(resolver
);
730 // The following catches xml parsing exceptions
731 db
.setErrorHandler(new ErrorHandler() {
733 public void error(SAXParseException saxparseexception
) throws SAXException
{
737 public void warning(SAXParseException saxparseexception
) throws SAXException
{
741 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
742 if (string
.trim().length() != 0) {
743 errorText
.setText(saxparseexception
.getMessage());
744 errorText
.setBackground(COLOR_LIGHT_RED
);
745 errorText
.setVisible(true);
747 throw saxparseexception
;
751 errorText
.setVisible(false);
753 doc
= db
.parse(new ByteArrayInputStream(string
.getBytes()));
754 documentElement
= doc
.getDocumentElement();
755 } catch (ParserConfigurationException e
) {
756 Activator
.getDefault().logError("Error pasing XML input string: " + string
, e
); //$NON-NLS-1$
757 documentElement
= null;
758 } catch (SAXException e
) {
759 documentElement
= null;
760 } catch (IOException e
) {
761 Activator
.getDefault().logError("Error pasing XML input string: " + string
, e
); //$NON-NLS-1$
762 documentElement
= null;
766 private void initValues() {
767 timeStampValue
= null;
768 timeStampFormat
= null;
770 logEntryFound
= false;
773 private void updatePreviews() {
774 if (inputText
== null) {
775 // early update during construction
778 inputText
.setStyleRanges(new StyleRange
[] {});
779 if (selectedElement
== null) {
785 selectedElement
.updatePreview();
787 if (timeStampValue
!= null && timeStampFormat
!= null) {
789 SimpleDateFormat dateFormat
= new SimpleDateFormat(timeStampFormat
);
790 Date date
= dateFormat
.parse(timeStampValue
);
791 dateFormat
= new SimpleDateFormat(timeStampOutputFormatText
.getText().trim());
792 timeStampPreviewText
.setText(dateFormat
.format(date
));
793 } catch (ParseException e
) {
794 timeStampPreviewText
.setText("*parse exception* [" + timeStampValue
+ "] <> [" + timeStampFormat
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
795 } catch (IllegalArgumentException e
) {
796 timeStampPreviewText
.setText("*parse exception* [Illegal Argument]"); //$NON-NLS-1$
799 timeStampPreviewText
.setText("*no matching time stamp*"); //$NON-NLS-1$
803 private void openHelpShell(String url
) {
804 if (helpBrowser
!= null && !helpBrowser
.isDisposed()) {
805 helpBrowser
.getShell().setActive();
806 if (!helpBrowser
.getUrl().equals(url
)) {
807 helpBrowser
.setUrl(url
);
811 final Shell helpShell
= new Shell(getShell(), SWT
.SHELL_TRIM
);
812 helpShell
.setLayout(new FillLayout());
813 helpBrowser
= new Browser(helpShell
, SWT
.NONE
);
814 helpBrowser
.addTitleListener(new TitleListener() {
816 public void changed(TitleEvent event
) {
817 helpShell
.setText(event
.title
);
820 helpBrowser
.setBounds(0, 0, 600, 400);
823 helpBrowser
.setUrl(url
);
826 private class UpdateListener
implements ModifyListener
, SelectionListener
{
829 public void modifyText(ModifyEvent e
) {
835 public void widgetDefaultSelected(SelectionEvent e
) {
841 public void widgetSelected(SelectionEvent e
) {
848 private class ElementNode
{
849 final InputElement inputElement
;
851 List
<Attribute
> attributes
= new ArrayList
<Attribute
>();
852 List
<ElementNode
> childElements
= new ArrayList
<ElementNode
>();
853 Text elementNameText
;
854 Composite tagComposite
;
861 Button logEntryButton
;
863 Composite addAttributeComposite
;
864 Button addAttributeButton
;
865 Label addAttributeLabel
;
867 public ElementNode(Composite parent
, InputElement inputElement
) {
868 this.inputElement
= inputElement
;
870 group
= new Group(parent
, SWT
.NONE
);
871 GridLayout gl
= new GridLayout(2, false);
874 group
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
875 group
.setText(getName(inputElement
));
877 Label label
= new Label(group
, SWT
.NULL
);
878 label
.setText(Messages
.CustomXmlParserInputWizardPage_elementName
);
879 label
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
881 elementNameText
= new Text(group
, SWT
.BORDER
| SWT
.SINGLE
);
882 GridData gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
884 elementNameText
.setLayoutData(gd
);
885 elementNameText
.addModifyListener(new ModifyListener() {
887 public void modifyText(ModifyEvent e
) {
888 ElementNode
.this.inputElement
.elementName
= elementNameText
.getText().trim();
889 group
.setText(getName(ElementNode
.this.inputElement
));
892 elementNameText
.setText(inputElement
.elementName
);
893 elementNameText
.addModifyListener(updateListener
);
895 if (inputElement
.parentElement
!= null) {
896 previewLabel
= new Label(group
, SWT
.NULL
);
897 previewLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
898 previewLabel
.setText(Messages
.CustomXmlParserInputWizardPage_preview
);
900 previewText
= new Text(group
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.READ_ONLY
);
901 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
903 previewText
.setLayoutData(gd
);
904 previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatchingElement
);
905 previewText
.setBackground(COLOR_WIDGET_BACKGROUND
);
907 logEntryButton
= new Button(group
, SWT
.CHECK
);
908 logEntryButton
.setText(Messages
.CustomXmlParserInputWizardPage_logEntry
);
909 logEntryButton
.setSelection(inputElement
.logEntry
);
910 logEntryButton
.addSelectionListener(new SelectionListener() {
912 public void widgetDefaultSelected(SelectionEvent e
) {
916 public void widgetSelected(SelectionEvent e
) {
917 InputElement parentElem
= ElementNode
.this.inputElement
.parentElement
;
918 while (parentElem
!= null) {
919 parentElem
.logEntry
= false;
920 parentElem
= parentElem
.parentElement
;
924 logEntryButton
.addSelectionListener(updateListener
);
926 tagComposite
= new Composite(group
, SWT
.FILL
);
927 GridLayout tagLayout
= new GridLayout(4, false);
928 tagLayout
.marginWidth
= 0;
929 tagLayout
.marginHeight
= 0;
930 tagComposite
.setLayout(tagLayout
);
931 tagComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
933 tagCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
934 tagCombo
.setItems(new String
[] { CustomXmlTraceDefinition
.TAG_IGNORE
, CustomTraceDefinition
.TAG_TIMESTAMP
,
935 CustomTraceDefinition
.TAG_MESSAGE
, CustomTraceDefinition
.TAG_OTHER
});
936 tagCombo
.setVisibleItemCount(tagCombo
.getItemCount());
937 tagCombo
.addSelectionListener(new SelectionListener() {
939 public void widgetDefaultSelected(SelectionEvent e
) {
943 public void widgetSelected(SelectionEvent e
) {
944 tagText
.removeModifyListener(updateListener
);
945 switch (tagCombo
.getSelectionIndex()) {
947 tagLabel
.setVisible(false);
948 tagText
.setVisible(false);
949 actionCombo
.setVisible(false);
951 case 1: // Time Stamp
952 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_format
);
953 tagLabel
.setVisible(true);
954 tagText
.setVisible(true);
955 tagText
.addModifyListener(updateListener
);
956 actionCombo
.setVisible(true);
959 tagLabel
.setVisible(false);
960 tagText
.setVisible(false);
961 actionCombo
.setVisible(true);
964 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_tagName
);
965 tagLabel
.setVisible(true);
966 if (tagText
.getText().trim().length() == 0) {
967 tagText
.setText(elementNameText
.getText().trim());
969 tagText
.setVisible(true);
970 tagText
.addModifyListener(updateListener
);
971 actionCombo
.setVisible(true);
976 tagComposite
.layout();
982 tagLabel
= new Label(tagComposite
, SWT
.NULL
);
983 tagLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
985 tagText
= new Text(tagComposite
, SWT
.BORDER
| SWT
.SINGLE
);
986 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
988 tagText
.setLayoutData(gd
);
990 actionCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
991 actionCombo
.setItems(new String
[] { Messages
.CustomXmlParserInputWizardPage_set
, Messages
.CustomXmlParserInputWizardPage_append
,
992 Messages
.CustomXmlParserInputWizardPage_appendWith
});
993 actionCombo
.select(inputElement
.inputAction
);
994 actionCombo
.addSelectionListener(updateListener
);
996 if (inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
998 tagLabel
.setVisible(false);
999 tagText
.setVisible(false);
1000 actionCombo
.setVisible(false);
1001 } else if (inputElement
.inputName
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1003 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_format
);
1004 tagText
.setText(inputElement
.inputFormat
);
1005 tagText
.addModifyListener(updateListener
);
1006 } else if (inputElement
.inputName
.equals(CustomTraceDefinition
.TAG_MESSAGE
)) {
1008 tagLabel
.setVisible(false);
1009 tagText
.setVisible(false);
1012 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_tagName
);
1013 tagText
.setText(inputElement
.inputName
);
1014 tagText
.addModifyListener(updateListener
);
1018 if (inputElement
.attributes
!= null) {
1019 for (InputAttribute inputAttribute
: inputElement
.attributes
) {
1020 Attribute attribute
= new Attribute(group
, this, inputAttribute
, attributes
.size() + 1);
1021 attributes
.add(attribute
);
1028 private void updatePreview() {
1029 Element element
= getPreviewElement(inputElement
);
1030 if (inputElement
.parentElement
!= null) { // no preview text for
1032 previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatchingElement
);
1033 if (element
!= null) {
1034 previewText
.setText(CustomXmlTrace
.parseElement(element
, new StringBuffer()).toString());
1035 if (logEntryButton
.getSelection()) {
1036 if (!logEntryFound
) {
1037 logEntryFound
= true;
1040 logEntryButton
.setSelection(false); // remove nested
1044 if (tagCombo
.getText().equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && logEntriesCount
<= 1) {
1045 String value
= previewText
.getText().trim();
1046 if (value
.length() != 0) {
1047 if (actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_SET
) {
1048 timeStampValue
= value
;
1049 timeStampFormat
= tagText
.getText().trim();
1050 } else if (actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_APPEND
) {
1051 if (timeStampValue
!= null) {
1052 timeStampValue
+= value
;
1053 timeStampFormat
+= tagText
.getText().trim();
1055 timeStampValue
= value
;
1056 timeStampFormat
= tagText
.getText().trim();
1058 } else if (actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_APPEND_WITH_SEPARATOR
) {
1059 if (timeStampValue
!= null) {
1060 timeStampValue
+= " | " + value
; //$NON-NLS-1$
1061 timeStampFormat
+= " | " + tagText
.getText().trim(); //$NON-NLS-1$
1063 timeStampValue
= value
;
1064 timeStampFormat
= tagText
.getText().trim();
1071 for (Attribute attribute
: attributes
) {
1072 if (element
!= null) {
1073 String value
= element
.getAttribute(attribute
.attributeNameText
.getText().trim());
1074 if (value
.length() != 0) {
1075 attribute
.previewText
.setText(value
);
1076 if (attribute
.tagCombo
.getText().equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && logEntriesCount
<= 1) {
1077 if (attribute
.actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_SET
) {
1078 timeStampValue
= value
;
1079 timeStampFormat
= attribute
.tagText
.getText().trim();
1080 } else if (attribute
.actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_APPEND
) {
1081 if (timeStampValue
!= null) {
1082 timeStampValue
+= value
;
1083 timeStampFormat
+= attribute
.tagText
.getText().trim();
1085 timeStampValue
= value
;
1086 timeStampFormat
= attribute
.tagText
.getText().trim();
1088 } else if (attribute
.actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_APPEND_WITH_SEPARATOR
) {
1089 if (timeStampValue
!= null) {
1090 timeStampValue
+= " | " + value
; //$NON-NLS-1$
1091 timeStampFormat
+= " | " + attribute
.tagText
.getText().trim(); //$NON-NLS-1$
1093 timeStampValue
= value
;
1094 timeStampFormat
= attribute
.tagText
.getText().trim();
1099 attribute
.previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatchingAttribute
);
1102 attribute
.previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatchingElement
);
1105 for (ElementNode child
: childElements
) {
1106 child
.updatePreview();
1108 if (logEntryButton
!= null && logEntryButton
.getSelection()) {
1109 logEntryFound
= false;
1113 private void createAddButton() {
1114 fillerLabel
= new Label(group
, SWT
.NONE
);
1116 addAttributeComposite
= new Composite(group
, SWT
.NONE
);
1117 addAttributeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1118 GridLayout addAttributeLayout
= new GridLayout(2, false);
1119 addAttributeLayout
.marginHeight
= 0;
1120 addAttributeLayout
.marginWidth
= 0;
1121 addAttributeComposite
.setLayout(addAttributeLayout
);
1123 addAttributeButton
= new Button(addAttributeComposite
, SWT
.PUSH
);
1124 addAttributeButton
.setImage(addImage
);
1125 addAttributeButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_addAttribute
);
1126 addAttributeButton
.addSelectionListener(new SelectionAdapter() {
1128 public void widgetSelected(SelectionEvent e
) {
1130 String attributeName
= getAttributeNameSuggestion(inputElement
);
1131 InputAttribute inputAttribute
= new InputAttribute(attributeName
, attributeName
, 0, ""); //$NON-NLS-1$
1132 attributes
.add(new Attribute(group
, ElementNode
.this, inputAttribute
, attributes
.size() + 1));
1134 elementContainer
.layout();
1135 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
,
1136 elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
- 1);
1137 group
.getParent().layout();
1143 addAttributeLabel
= new Label(addAttributeComposite
, SWT
.NULL
);
1144 addAttributeLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1145 addAttributeLabel
.setText(Messages
.CustomXmlParserInputWizardPage_newAttibute
);
1148 private void removeAddButton() {
1149 fillerLabel
.dispose();
1150 addAttributeComposite
.dispose();
1153 private void removeAttribute(int attributeNumber
) {
1154 int nb
= attributeNumber
;
1155 if (--nb
< attributes
.size()) {
1156 attributes
.remove(nb
).dispose();
1157 for (int i
= nb
; i
< attributes
.size(); i
++) {
1158 attributes
.get(i
).setAttributeNumber(i
+ 1);
1160 elementContainer
.layout();
1161 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
,
1162 elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
- 1);
1163 group
.getParent().layout();
1167 private void dispose() {
1171 private void extractInputs() {
1172 inputElement
.elementName
= elementNameText
.getText().trim();
1173 if (inputElement
.parentElement
!= null) {
1174 inputElement
.logEntry
= logEntryButton
.getSelection();
1175 if (tagCombo
.getText().equals(CustomTraceDefinition
.TAG_OTHER
)) {
1176 inputElement
.inputName
= tagText
.getText().trim();
1178 inputElement
.inputName
= tagCombo
.getText();
1179 if (tagCombo
.getText().equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1180 inputElement
.inputFormat
= tagText
.getText().trim();
1183 inputElement
.inputAction
= actionCombo
.getSelectionIndex();
1185 inputElement
.attributes
= new ArrayList
<InputAttribute
>(attributes
.size());
1186 for (int i
= 0; i
< attributes
.size(); i
++) {
1187 Attribute attribute
= attributes
.get(i
);
1188 InputAttribute inputAttribute
= new InputAttribute();
1189 inputAttribute
.attributeName
= attribute
.attributeNameText
.getText().trim();
1190 if (attribute
.tagCombo
.getText().equals(CustomTraceDefinition
.TAG_OTHER
)) {
1191 inputAttribute
.inputName
= attribute
.tagText
.getText().trim();
1193 inputAttribute
.inputName
= attribute
.tagCombo
.getText();
1194 if (attribute
.tagCombo
.getText().equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1195 inputAttribute
.inputFormat
= attribute
.tagText
.getText().trim();
1198 inputAttribute
.inputAction
= attribute
.actionCombo
.getSelectionIndex();
1199 inputElement
.addAttribute(inputAttribute
);
1204 private class Attribute
{
1205 ElementNode element
;
1206 int attributeNumber
;
1208 // children of parent (must be disposed)
1209 Composite labelComposite
;
1210 Composite attributeComposite
;
1212 Composite tagComposite
;
1214 // children of labelComposite
1215 Label attributeLabel
;
1217 // children of attributeComposite
1218 Text attributeNameText
;
1221 // children of tagComposite
1227 public Attribute(Composite parent
, ElementNode element
, InputAttribute inputAttribute
, int attributeNumber
) {
1228 this.element
= element
;
1229 this.attributeNumber
= attributeNumber
;
1231 labelComposite
= new Composite(parent
, SWT
.FILL
);
1232 GridLayout labelLayout
= new GridLayout(2, false);
1233 labelLayout
.marginWidth
= 0;
1234 labelLayout
.marginHeight
= 0;
1235 labelComposite
.setLayout(labelLayout
);
1236 labelComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1238 Button deleteButton
= new Button(labelComposite
, SWT
.PUSH
);
1239 deleteButton
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1240 deleteButton
.setImage(deleteImage
);
1241 deleteButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_removeAttribute
);
1242 deleteButton
.addSelectionListener(new SelectionAdapter() {
1244 public void widgetSelected(SelectionEvent e
) {
1245 Attribute
.this.element
.removeAttribute(Attribute
.this.attributeNumber
);
1251 attributeLabel
= new Label(labelComposite
, SWT
.NULL
);
1252 attributeLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1253 attributeLabel
.setText(Messages
.CustomXmlParserInputWizardPage_attibute
);
1255 attributeComposite
= new Composite(parent
, SWT
.FILL
);
1256 GridLayout attributeLayout
= new GridLayout(4, false);
1257 attributeLayout
.marginWidth
= 0;
1258 attributeLayout
.marginHeight
= 0;
1259 attributeComposite
.setLayout(attributeLayout
);
1260 attributeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1262 Label nameLabel
= new Label(attributeComposite
, SWT
.NONE
);
1263 nameLabel
.setText(Messages
.CustomXmlParserInputWizardPage_name
);
1265 attributeNameText
= new Text(attributeComposite
, SWT
.BORDER
| SWT
.SINGLE
);
1266 attributeNameText
.setLayoutData(new GridData(120, SWT
.DEFAULT
));
1267 attributeNameText
.setText(inputAttribute
.attributeName
);
1268 attributeNameText
.addModifyListener(updateListener
);
1270 Label previewLabel
= new Label(attributeComposite
, SWT
.NONE
);
1271 previewLabel
.setText(Messages
.CustomXmlParserInputWizardPage_preview
);
1273 previewText
= new Text(attributeComposite
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.READ_ONLY
);
1274 GridData gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
1276 previewText
.setLayoutData(gd
);
1277 previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatch
);
1278 previewText
.setBackground(COLOR_WIDGET_BACKGROUND
);
1280 filler
= new Label(parent
, SWT
.NULL
);
1282 tagComposite
= new Composite(parent
, SWT
.FILL
);
1283 GridLayout tagLayout
= new GridLayout(4, false);
1284 tagLayout
.marginWidth
= 0;
1285 tagLayout
.marginHeight
= 0;
1286 tagComposite
.setLayout(tagLayout
);
1287 tagComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1289 tagCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
1290 tagCombo
.setItems(new String
[] { CustomTraceDefinition
.TAG_TIMESTAMP
, CustomTraceDefinition
.TAG_MESSAGE
,
1291 CustomTraceDefinition
.TAG_OTHER
});
1292 tagCombo
.select(2); // Other
1293 tagCombo
.addSelectionListener(new SelectionListener() {
1295 public void widgetDefaultSelected(SelectionEvent e
) {
1299 public void widgetSelected(SelectionEvent e
) {
1300 tagText
.removeModifyListener(updateListener
);
1301 switch (tagCombo
.getSelectionIndex()) {
1302 case 0: // Time Stamp
1303 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_format
);
1304 tagLabel
.setVisible(true);
1305 tagText
.setVisible(true);
1306 tagText
.addModifyListener(updateListener
);
1309 tagLabel
.setVisible(false);
1310 tagText
.setVisible(false);
1313 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_tagName
);
1314 tagLabel
.setVisible(true);
1315 if (tagText
.getText().trim().length() == 0) {
1316 tagText
.setText(attributeNameText
.getText().trim());
1318 tagText
.setVisible(true);
1319 tagText
.addModifyListener(updateListener
);
1324 tagComposite
.layout();
1330 tagLabel
= new Label(tagComposite
, SWT
.NULL
);
1331 tagLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1333 tagText
= new Text(tagComposite
, SWT
.BORDER
| SWT
.SINGLE
);
1334 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
1336 tagText
.setLayoutData(gd
);
1337 tagText
.setText(attributeNameText
.getText());
1339 actionCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
1340 actionCombo
.setItems(new String
[] { Messages
.CustomXmlParserInputWizardPage_set
, Messages
.CustomXmlParserInputWizardPage_append
,
1341 Messages
.CustomXmlParserInputWizardPage_appendWith
});
1342 actionCombo
.select(inputAttribute
.inputAction
);
1343 actionCombo
.addSelectionListener(updateListener
);
1345 if (inputAttribute
.inputName
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1347 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_format
);
1348 tagText
.setText(inputAttribute
.inputFormat
);
1349 tagText
.addModifyListener(updateListener
);
1350 } else if (inputAttribute
.inputName
.equals(CustomTraceDefinition
.TAG_MESSAGE
)) {
1352 tagLabel
.setVisible(false);
1353 tagText
.setVisible(false);
1356 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_tagName
);
1357 tagText
.setText(inputAttribute
.inputName
);
1358 tagText
.addModifyListener(updateListener
);
1362 private void dispose() {
1363 labelComposite
.dispose();
1364 attributeComposite
.dispose();
1366 tagComposite
.dispose();
1369 private void setAttributeNumber(int attributeNumber
) {
1370 this.attributeNumber
= attributeNumber
;
1371 labelComposite
.layout();
1375 private Element
getPreviewElement(InputElement inputElement
) {
1376 InputElement currentElement
= inputElement
;
1377 Element element
= documentElement
;
1378 if (element
!= null) {
1379 if (!documentElement
.getNodeName().equals(definition
.rootInputElement
.elementName
)) {
1382 ArrayList
<String
> elementNames
= new ArrayList
<String
>();
1383 while (currentElement
!= null) {
1384 elementNames
.add(currentElement
.elementName
);
1385 currentElement
= currentElement
.parentElement
;
1387 for (int i
= elementNames
.size() - 1; --i
>= 0;) {
1388 NodeList childList
= element
.getChildNodes();
1390 for (int j
= 0; j
< childList
.getLength(); j
++) {
1391 Node child
= childList
.item(j
);
1392 if (child
instanceof Element
&& child
.getNodeName().equals(elementNames
.get(i
))) {
1393 element
= (Element
) child
;
1397 if (element
== null) {
1401 if (element
!= null) {
1408 private String
getChildNameSuggestion(InputElement inputElement
) {
1409 if (inputElement
== null) {
1410 if (documentElement
!= null) {
1411 return documentElement
.getNodeName();
1414 Element element
= getPreviewElement(inputElement
);
1415 if (element
!= null) {
1416 NodeList childNodes
= element
.getChildNodes();
1417 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
1418 Node node
= childNodes
.item(i
);
1419 if (node
instanceof Element
) {
1420 boolean unused
= true;
1421 if (inputElement
.childElements
!= null) {
1422 for (InputElement child
: inputElement
.childElements
) {
1423 if (child
.elementName
.equals(node
.getNodeName())) {
1430 return node
.getNodeName();
1436 return ""; //$NON-NLS-1$
1439 private String
getAttributeNameSuggestion(InputElement inputElement
) {
1440 Element element
= getPreviewElement(inputElement
);
1441 if (element
!= null) {
1442 NamedNodeMap attributeMap
= element
.getAttributes();
1443 for (int i
= 0; i
< attributeMap
.getLength(); i
++) {
1444 Node node
= attributeMap
.item(i
);
1445 boolean unused
= true;
1446 if (inputElement
.attributes
!= null) {
1447 for (InputAttribute attribute
: inputElement
.attributes
) {
1448 if (attribute
.attributeName
.equals(node
.getNodeName())) {
1455 return node
.getNodeName();
1459 return ""; //$NON-NLS-1$
1462 private void validate() {
1463 definition
.definitionName
= logtypeText
.getText().trim();
1464 definition
.timeStampOutputFormat
= timeStampOutputFormatText
.getText().trim();
1466 if (selectedElement
!= null) {
1467 selectedElement
.extractInputs();
1468 treeViewer
.refresh();
1471 StringBuffer errors
= new StringBuffer();
1473 if (definition
.definitionName
.length() == 0) {
1474 errors
.append(Messages
.CustomXmlParserInputWizardPage_emptyLogTypeError
);
1475 logtypeText
.setBackground(COLOR_LIGHT_RED
);
1477 logtypeText
.setBackground(COLOR_TEXT_BACKGROUND
);
1478 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
1479 if (definition
.definitionName
.equals(def
.definitionName
)) {
1480 if (editDefinitionName
== null || !editDefinitionName
.equals(definition
.definitionName
)) {
1481 errors
.append(Messages
.CustomXmlParserInputWizardPage_duplicatelogTypeError
);
1482 logtypeText
.setBackground(COLOR_LIGHT_RED
);
1489 if (definition
.rootInputElement
== null) {
1490 errors
.append(Messages
.CustomXmlParserInputWizardPage_noDocumentError
);
1493 if (definition
.rootInputElement
!= null) {
1494 logEntryFound
= false;
1495 timeStampFound
= false;
1497 errors
.append(validateElement(definition
.rootInputElement
));
1499 if ((definition
.rootInputElement
.attributes
!= null && definition
.rootInputElement
.attributes
.size() != 0)
1500 || (definition
.rootInputElement
.childElements
!= null && definition
.rootInputElement
.childElements
.size() != 0)
1501 || errors
.length() == 0) {
1502 if (!logEntryFound
) {
1503 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingLogEntryError
);
1506 if (timeStampFound
) {
1507 if (timeStampOutputFormatText
.getText().trim().length() == 0) {
1508 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingTimestampFmtError
);
1509 timeStampOutputFormatText
.setBackground(COLOR_LIGHT_RED
);
1512 new SimpleDateFormat(timeStampOutputFormatText
.getText().trim());
1513 timeStampOutputFormatText
.setBackground(COLOR_TEXT_BACKGROUND
);
1514 } catch (IllegalArgumentException e
) {
1515 errors
.append(Messages
.CustomXmlParserInputWizardPage_invalidTimestampFmtError
);
1516 timeStampOutputFormatText
.setBackground(COLOR_LIGHT_RED
);
1520 timeStampPreviewText
.setText(Messages
.CustomXmlParserInputWizardPage_notimestamporAttributeError
);
1524 timeStampPreviewText
.setText(Messages
.CustomXmlParserInputWizardPage_notimestamporAttributeError
);
1527 if (errors
.length() == 0) {
1528 setDescription(defaultDescription
);
1529 setPageComplete(true);
1531 setDescription(errors
.toString());
1532 setPageComplete(false);
1537 * Clean up the specified XML element.
1539 * @param inputElement
1540 * The element to clean up
1541 * @return The validated element
1543 public StringBuffer
validateElement(InputElement inputElement
) {
1544 StringBuffer errors
= new StringBuffer();
1545 ElementNode elementNode
= null;
1546 if (selectedElement
!= null && selectedElement
.inputElement
.equals(inputElement
)) {
1547 elementNode
= selectedElement
;
1549 if (inputElement
== definition
.rootInputElement
) {
1550 if (inputElement
.elementName
.length() == 0) {
1551 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingDocumentElementError
);
1552 if (elementNode
!= null) {
1553 elementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1556 if (elementNode
!= null) {
1557 elementNode
.elementNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1561 if (inputElement
!= definition
.rootInputElement
) {
1562 if (inputElement
.logEntry
) {
1563 logEntryFound
= true;
1565 if (inputElement
.inputName
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1566 timeStampFound
= true;
1567 if (inputElement
.inputFormat
.length() == 0) {
1568 errors
.append(Messages
.CustomXmlParserInputWizardPage_timestampFormatPrompt
1569 + " (" + Messages
.CustomXmlParserInputWizardPage_timestampElementPrompt
+ " " + getName(inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1570 if (elementNode
!= null) {
1571 elementNode
.tagText
.setBackground(COLOR_LIGHT_RED
);
1575 new SimpleDateFormat(inputElement
.inputFormat
);
1576 if (elementNode
!= null) {
1577 elementNode
.tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1579 } catch (IllegalArgumentException e
) {
1580 errors
.append(Messages
.CustomXmlParserInputWizardPage_invalidTimestampFmtError
1581 + " (" + Messages
.CustomXmlParserInputWizardPage_timestampElementPrompt
+ " " + getName(inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1582 if (elementNode
!= null) {
1583 elementNode
.tagText
.setBackground(COLOR_LIGHT_RED
);
1587 } else if (inputElement
.inputName
.length() == 0) {
1588 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingInputElementNameError
);
1589 if (elementNode
!= null) {
1590 elementNode
.tagText
.setBackground(COLOR_LIGHT_RED
);
1593 if (elementNode
!= null) {
1594 elementNode
.tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1598 if (inputElement
.attributes
!= null) {
1599 if (elementNode
!= null) {
1600 for (Attribute attribute
: elementNode
.attributes
) {
1601 attribute
.attributeNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1604 for (int i
= 0; i
< inputElement
.attributes
.size(); i
++) {
1605 InputAttribute attribute
= inputElement
.attributes
.get(i
);
1606 boolean duplicate
= false;
1607 for (int j
= i
+ 1; j
< inputElement
.attributes
.size(); j
++) {
1608 InputAttribute otherAttribute
= inputElement
.attributes
.get(j
);
1609 if (otherAttribute
.attributeName
.equals(attribute
.attributeName
)) {
1611 if (elementNode
!= null) {
1612 elementNode
.attributes
.get(j
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1616 if (attribute
.attributeName
.length() == 0) {
1617 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingAttribute
1618 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(inputElement
) + ": ?). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1619 if (elementNode
!= null) {
1620 elementNode
.attributes
.get(i
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1622 } else if (duplicate
) {
1623 errors
.append(Messages
.CustomXmlParserInputWizardPage_duplicateAttributeError
1624 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(attribute
, inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1625 if (elementNode
!= null) {
1626 elementNode
.attributes
.get(i
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1629 if (attribute
.inputName
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1630 timeStampFound
= true;
1631 if (attribute
.inputFormat
.length() == 0) {
1632 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingTimestampInFmtError
1633 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(attribute
, inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1634 if (elementNode
!= null) {
1635 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_LIGHT_RED
);
1639 new SimpleDateFormat(attribute
.inputFormat
);
1640 if (elementNode
!= null) {
1641 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1643 } catch (IllegalArgumentException e
) {
1644 errors
.append(Messages
.CustomXmlParserInputWizardPage_invalidTimestampInFmtError
1645 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(attribute
, inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1646 if (elementNode
!= null) {
1647 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_LIGHT_RED
);
1651 } else if (attribute
.inputName
.length() == 0) {
1652 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingDataGroupNameError
1653 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(attribute
, inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1654 if (elementNode
!= null) {
1655 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_LIGHT_RED
);
1658 if (elementNode
!= null) {
1659 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1664 if (inputElement
.childElements
!= null) {
1665 for (InputElement child
: inputElement
.childElements
) {
1666 ElementNode childElementNode
= null;
1667 if (selectedElement
!= null && selectedElement
.inputElement
.equals(child
)) {
1668 childElementNode
= selectedElement
;
1670 if (childElementNode
!= null) {
1671 childElementNode
.elementNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1674 for (int i
= 0; i
< inputElement
.childElements
.size(); i
++) {
1675 InputElement child
= inputElement
.childElements
.get(i
);
1676 ElementNode childElementNode
= null;
1677 if (selectedElement
!= null && selectedElement
.inputElement
.equals(child
)) {
1678 childElementNode
= selectedElement
;
1680 if (child
.elementName
.length() == 0) {
1681 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingElementNameError
1682 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(child
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1683 if (childElementNode
!= null) {
1684 childElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1687 boolean duplicate
= false;
1688 for (int j
= i
+ 1; j
< inputElement
.childElements
.size(); j
++) {
1689 InputElement otherChild
= inputElement
.childElements
.get(j
);
1690 if (otherChild
.elementName
.equals(child
.elementName
)) {
1692 ElementNode otherChildElementNode
= null;
1693 if (selectedElement
!= null && selectedElement
.inputElement
.equals(otherChild
)) {
1694 otherChildElementNode
= selectedElement
;
1696 if (otherChildElementNode
!= null) {
1697 otherChildElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1702 errors
.append(Messages
.CustomXmlParserInputWizardPage_duplicateElementNameError
1703 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(child
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1704 if (childElementNode
!= null) {
1705 childElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1710 errors
.append(validateElement(child
));
1717 * Get the trace definition.
1719 * @return The trace definition
1721 public CustomXmlTraceDefinition
getDefinition() {
1726 * Get the raw text input.
1728 * @return The raw text input.
1730 public char[] getInputText() {
1731 return inputText
.getText().toCharArray();