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 selection
= (IStructuredSelection
) event
.getSelection();
555 InputElement inputElement
= (InputElement
) selection
.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);
591 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
594 public void dispose() {
595 if (fixedFont
!= null) {
602 private void loadDefinition(CustomXmlTraceDefinition def
) {
603 logtypeText
.setText(def
.definitionName
);
604 timeStampOutputFormatText
.setText(def
.timeStampOutputFormat
);
605 treeViewer
.setInput(def
);
607 if (def
.rootInputElement
!= null) {
608 treeViewer
.setSelection(new StructuredSelection(def
.rootInputElement
));
612 private String
getName(InputElement inputElement
) {
613 String name
= (inputElement
.elementName
.trim().length() == 0) ?
"?" : inputElement
.elementName
.trim(); //$NON-NLS-1$
614 if (inputElement
.parentElement
== null) {
617 return getName(inputElement
.parentElement
) + " : " + name
; //$NON-NLS-1$
620 private String
getName(InputAttribute inputAttribute
, InputElement inputElement
) {
621 String name
= (inputAttribute
.attributeName
.trim().length() == 0) ?
"?" : inputAttribute
.attributeName
.trim(); //$NON-NLS-1$
622 return getName(inputElement
) + " : " + name
; //$NON-NLS-1$
628 * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
631 public void setVisible(boolean visible
) {
636 super.setVisible(visible
);
640 * Get the global list of input names.
642 * @return The list of input names
644 public List
<String
> getInputNames() {
645 return getInputNames(definition
.rootInputElement
);
649 * Get the list of input names for a given element.
651 * @param inputElement
653 * @return The input names for this element
655 public List
<String
> getInputNames(InputElement inputElement
) {
656 List
<String
> inputs
= new ArrayList
<String
>();
657 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
658 String inputName
= inputElement
.inputName
;
659 if (!inputs
.contains(inputName
)) {
660 inputs
.add(inputName
);
663 if (inputElement
.attributes
!= null) {
664 for (InputAttribute attribute
: inputElement
.attributes
) {
665 String inputName
= attribute
.inputName
;
666 if (!inputs
.contains(inputName
)) {
667 inputs
.add(inputName
);
671 if (inputElement
.childElements
!= null) {
672 for (InputElement childInputElement
: inputElement
.childElements
) {
673 for (String inputName
: getInputNames(childInputElement
)) {
674 if (!inputs
.contains(inputName
)) {
675 inputs
.add(inputName
);
683 private void removeElement() {
684 selectedElement
.dispose();
685 selectedElement
= null;
686 elementContainer
.layout();
687 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
,
688 elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
- 1);
692 private String
getSelectionText() {
693 InputStream inputStream
= null;
694 if (this.selection
instanceof IStructuredSelection
) {
695 Object selection
= ((IStructuredSelection
) this.selection
).getFirstElement();
696 if (selection
instanceof IFile
) {
697 IFile file
= (IFile
) selection
;
699 inputStream
= file
.getContents();
700 } catch (CoreException e
) {
701 return ""; //$NON-NLS-1$
705 if (inputStream
!= null) {
706 BufferedReader reader
= null;
708 reader
= new BufferedReader(new InputStreamReader(inputStream
));
709 StringBuilder sb
= new StringBuilder();
711 while ((line
= reader
.readLine()) != null) {
712 sb
.append(line
+ "\n"); //$NON-NLS-1$
714 parseXmlInput(sb
.toString());
716 return sb
.toString();
717 } catch (IOException e
) {
718 return ""; //$NON-NLS-1$
721 return ""; //$NON-NLS-1$
724 private void parseXmlInput(final String string
) {
726 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
727 DocumentBuilder db
= dbf
.newDocumentBuilder();
729 // The following allows xml parsing without access to the dtd
730 EntityResolver resolver
= new EntityResolver() {
732 public InputSource
resolveEntity(String publicId
, String systemId
) {
733 String empty
= ""; //$NON-NLS-1$
734 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
735 return new InputSource(bais
);
738 db
.setEntityResolver(resolver
);
740 // The following catches xml parsing exceptions
741 db
.setErrorHandler(new ErrorHandler() {
743 public void error(SAXParseException saxparseexception
) throws SAXException
{
747 public void warning(SAXParseException saxparseexception
) throws SAXException
{
751 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
752 if (string
.trim().length() != 0) {
753 errorText
.setText(saxparseexception
.getMessage());
754 errorText
.setBackground(COLOR_LIGHT_RED
);
755 errorText
.setVisible(true);
757 throw saxparseexception
;
761 errorText
.setVisible(false);
763 doc
= db
.parse(new ByteArrayInputStream(string
.getBytes()));
764 documentElement
= doc
.getDocumentElement();
765 } catch (ParserConfigurationException e
) {
766 Activator
.getDefault().logError("Error pasing XML input string: " + string
, e
); //$NON-NLS-1$
767 documentElement
= null;
768 } catch (SAXException e
) {
769 documentElement
= null;
770 } catch (IOException e
) {
771 Activator
.getDefault().logError("Error pasing XML input string: " + string
, e
); //$NON-NLS-1$
772 documentElement
= null;
776 private void updatePreviews() {
777 updatePreviews(false);
780 private void initValues() {
781 timeStampValue
= null;
782 timeStampFormat
= null;
784 logEntryFound
= false;
787 private void updatePreviews(boolean updateAll
) {
788 if (inputText
== null) {
789 // early update during construction
792 inputText
.setStyleRanges(new StyleRange
[] {});
793 if (selectedElement
== null) {
799 selectedElement
.updatePreview();
801 if (timeStampValue
!= null && timeStampFormat
!= null) {
803 SimpleDateFormat dateFormat
= new SimpleDateFormat(timeStampFormat
);
804 Date date
= dateFormat
.parse(timeStampValue
);
805 dateFormat
= new SimpleDateFormat(timeStampOutputFormatText
.getText().trim());
806 timeStampPreviewText
.setText(dateFormat
.format(date
));
807 } catch (ParseException e
) {
808 timeStampPreviewText
.setText("*parse exception* [" + timeStampValue
+ "] <> [" + timeStampFormat
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
809 } catch (IllegalArgumentException e
) {
810 timeStampPreviewText
.setText("*parse exception* [Illegal Argument]"); //$NON-NLS-1$
813 timeStampPreviewText
.setText("*no matching time stamp*"); //$NON-NLS-1$
817 private void openHelpShell(String url
) {
818 if (helpBrowser
!= null && !helpBrowser
.isDisposed()) {
819 helpBrowser
.getShell().setActive();
820 if (!helpBrowser
.getUrl().equals(url
)) {
821 helpBrowser
.setUrl(url
);
825 final Shell helpShell
= new Shell(getShell(), SWT
.SHELL_TRIM
);
826 helpShell
.setLayout(new FillLayout());
827 helpBrowser
= new Browser(helpShell
, SWT
.NONE
);
828 helpBrowser
.addTitleListener(new TitleListener() {
830 public void changed(TitleEvent event
) {
831 helpShell
.setText(event
.title
);
834 helpBrowser
.setBounds(0, 0, 600, 400);
837 helpBrowser
.setUrl(url
);
840 private class UpdateListener
implements ModifyListener
, SelectionListener
{
843 public void modifyText(ModifyEvent e
) {
849 public void widgetDefaultSelected(SelectionEvent e
) {
855 public void widgetSelected(SelectionEvent e
) {
862 private class ElementNode
{
863 final InputElement inputElement
;
865 List
<Attribute
> attributes
= new ArrayList
<Attribute
>();
866 List
<ElementNode
> childElements
= new ArrayList
<ElementNode
>();
867 Text elementNameText
;
868 Composite tagComposite
;
875 Button logEntryButton
;
877 Composite addAttributeComposite
;
878 Button addAttributeButton
;
879 Label addAttributeLabel
;
881 public ElementNode(Composite parent
, InputElement inputElement
) {
882 this.inputElement
= inputElement
;
884 group
= new Group(parent
, SWT
.NONE
);
885 GridLayout gl
= new GridLayout(2, false);
888 group
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
889 group
.setText(getName(inputElement
));
891 Label label
= new Label(group
, SWT
.NULL
);
892 label
.setText(Messages
.CustomXmlParserInputWizardPage_elementName
);
893 label
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
895 elementNameText
= new Text(group
, SWT
.BORDER
| SWT
.SINGLE
);
896 GridData gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
898 elementNameText
.setLayoutData(gd
);
899 elementNameText
.addModifyListener(new ModifyListener() {
901 public void modifyText(ModifyEvent e
) {
902 ElementNode
.this.inputElement
.elementName
= elementNameText
.getText().trim();
903 group
.setText(getName(ElementNode
.this.inputElement
));
906 elementNameText
.setText(inputElement
.elementName
);
907 elementNameText
.addModifyListener(updateListener
);
909 if (inputElement
.parentElement
!= null) {
910 previewLabel
= new Label(group
, SWT
.NULL
);
911 previewLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
912 previewLabel
.setText(Messages
.CustomXmlParserInputWizardPage_preview
);
914 previewText
= new Text(group
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.READ_ONLY
);
915 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
917 previewText
.setLayoutData(gd
);
918 previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatchingElement
);
919 previewText
.setBackground(COLOR_WIDGET_BACKGROUND
);
921 logEntryButton
= new Button(group
, SWT
.CHECK
);
922 logEntryButton
.setText(Messages
.CustomXmlParserInputWizardPage_logEntry
);
923 logEntryButton
.setSelection(inputElement
.logEntry
);
924 logEntryButton
.addSelectionListener(new SelectionListener() {
926 public void widgetDefaultSelected(SelectionEvent e
) {
930 public void widgetSelected(SelectionEvent e
) {
931 InputElement parent
= ElementNode
.this.inputElement
.parentElement
;
932 while (parent
!= null) {
933 parent
.logEntry
= false;
934 parent
= parent
.parentElement
;
938 logEntryButton
.addSelectionListener(updateListener
);
940 tagComposite
= new Composite(group
, SWT
.FILL
);
941 GridLayout tagLayout
= new GridLayout(4, false);
942 tagLayout
.marginWidth
= 0;
943 tagLayout
.marginHeight
= 0;
944 tagComposite
.setLayout(tagLayout
);
945 tagComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
947 tagCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
948 tagCombo
.setItems(new String
[] { CustomXmlTraceDefinition
.TAG_IGNORE
, CustomTraceDefinition
.TAG_TIMESTAMP
,
949 CustomTraceDefinition
.TAG_MESSAGE
, CustomTraceDefinition
.TAG_OTHER
});
950 tagCombo
.setVisibleItemCount(tagCombo
.getItemCount());
951 tagCombo
.addSelectionListener(new SelectionListener() {
953 public void widgetDefaultSelected(SelectionEvent e
) {
957 public void widgetSelected(SelectionEvent e
) {
958 tagText
.removeModifyListener(updateListener
);
959 switch (tagCombo
.getSelectionIndex()) {
961 tagLabel
.setVisible(false);
962 tagText
.setVisible(false);
963 actionCombo
.setVisible(false);
965 case 1: // Time Stamp
966 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_format
);
967 tagLabel
.setVisible(true);
968 tagText
.setVisible(true);
969 tagText
.addModifyListener(updateListener
);
970 actionCombo
.setVisible(true);
973 tagLabel
.setVisible(false);
974 tagText
.setVisible(false);
975 actionCombo
.setVisible(true);
978 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_tagName
);
979 tagLabel
.setVisible(true);
980 if (tagText
.getText().trim().length() == 0) {
981 tagText
.setText(elementNameText
.getText().trim());
983 tagText
.setVisible(true);
984 tagText
.addModifyListener(updateListener
);
985 actionCombo
.setVisible(true);
990 tagComposite
.layout();
996 tagLabel
= new Label(tagComposite
, SWT
.NULL
);
997 tagLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
999 tagText
= new Text(tagComposite
, SWT
.BORDER
| SWT
.SINGLE
);
1000 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
1002 tagText
.setLayoutData(gd
);
1004 actionCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
1005 actionCombo
.setItems(new String
[] { Messages
.CustomXmlParserInputWizardPage_set
, Messages
.CustomXmlParserInputWizardPage_append
,
1006 Messages
.CustomXmlParserInputWizardPage_appendWith
});
1007 actionCombo
.select(inputElement
.inputAction
);
1008 actionCombo
.addSelectionListener(updateListener
);
1010 if (inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
1012 tagLabel
.setVisible(false);
1013 tagText
.setVisible(false);
1014 actionCombo
.setVisible(false);
1015 } else if (inputElement
.inputName
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1017 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_format
);
1018 tagText
.setText(inputElement
.inputFormat
);
1019 tagText
.addModifyListener(updateListener
);
1020 } else if (inputElement
.inputName
.equals(CustomTraceDefinition
.TAG_MESSAGE
)) {
1022 tagLabel
.setVisible(false);
1023 tagText
.setVisible(false);
1026 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_tagName
);
1027 tagText
.setText(inputElement
.inputName
);
1028 tagText
.addModifyListener(updateListener
);
1032 if (inputElement
.attributes
!= null) {
1033 for (InputAttribute inputAttribute
: inputElement
.attributes
) {
1034 Attribute attribute
= new Attribute(group
, this, inputAttribute
, attributes
.size() + 1);
1035 attributes
.add(attribute
);
1042 private void updatePreview() {
1043 Element element
= getPreviewElement(inputElement
);
1044 if (inputElement
.parentElement
!= null) { // no preview text for
1046 previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatchingElement
);
1047 if (element
!= null) {
1048 previewText
.setText(CustomXmlTrace
.parseElement(element
, new StringBuffer()).toString());
1049 if (logEntryButton
.getSelection()) {
1050 if (!logEntryFound
) {
1051 logEntryFound
= true;
1054 logEntryButton
.setSelection(false); // remove nested
1058 if (tagCombo
.getText().equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && logEntriesCount
<= 1) {
1059 String value
= previewText
.getText().trim();
1060 if (value
.length() != 0) {
1061 if (actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_SET
) {
1062 timeStampValue
= value
;
1063 timeStampFormat
= tagText
.getText().trim();
1064 } else if (actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_APPEND
) {
1065 if (timeStampValue
!= null) {
1066 timeStampValue
+= value
;
1067 timeStampFormat
+= tagText
.getText().trim();
1069 timeStampValue
= value
;
1070 timeStampFormat
= tagText
.getText().trim();
1072 } else if (actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_APPEND_WITH_SEPARATOR
) {
1073 if (timeStampValue
!= null) {
1074 timeStampValue
+= " | " + value
; //$NON-NLS-1$
1075 timeStampFormat
+= " | " + tagText
.getText().trim(); //$NON-NLS-1$
1077 timeStampValue
= value
;
1078 timeStampFormat
= tagText
.getText().trim();
1085 for (Attribute attribute
: attributes
) {
1086 if (element
!= null) {
1087 String value
= element
.getAttribute(attribute
.attributeNameText
.getText().trim());
1088 if (value
.length() != 0) {
1089 attribute
.previewText
.setText(value
);
1090 if (attribute
.tagCombo
.getText().equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && logEntriesCount
<= 1) {
1091 if (attribute
.actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_SET
) {
1092 timeStampValue
= value
;
1093 timeStampFormat
= attribute
.tagText
.getText().trim();
1094 } else if (attribute
.actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_APPEND
) {
1095 if (timeStampValue
!= null) {
1096 timeStampValue
+= value
;
1097 timeStampFormat
+= attribute
.tagText
.getText().trim();
1099 timeStampValue
= value
;
1100 timeStampFormat
= attribute
.tagText
.getText().trim();
1102 } else if (attribute
.actionCombo
.getSelectionIndex() == CustomTraceDefinition
.ACTION_APPEND_WITH_SEPARATOR
) {
1103 if (timeStampValue
!= null) {
1104 timeStampValue
+= " | " + value
; //$NON-NLS-1$
1105 timeStampFormat
+= " | " + attribute
.tagText
.getText().trim(); //$NON-NLS-1$
1107 timeStampValue
= value
;
1108 timeStampFormat
= attribute
.tagText
.getText().trim();
1113 attribute
.previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatchingAttribute
);
1116 attribute
.previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatchingElement
);
1119 for (ElementNode child
: childElements
) {
1120 child
.updatePreview();
1122 if (logEntryButton
!= null && logEntryButton
.getSelection()) {
1123 logEntryFound
= false;
1127 private void createAddButton() {
1128 fillerLabel
= new Label(group
, SWT
.NONE
);
1130 addAttributeComposite
= new Composite(group
, SWT
.NONE
);
1131 addAttributeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1132 GridLayout addAttributeLayout
= new GridLayout(2, false);
1133 addAttributeLayout
.marginHeight
= 0;
1134 addAttributeLayout
.marginWidth
= 0;
1135 addAttributeComposite
.setLayout(addAttributeLayout
);
1137 addAttributeButton
= new Button(addAttributeComposite
, SWT
.PUSH
);
1138 addAttributeButton
.setImage(addImage
);
1139 addAttributeButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_addAttribute
);
1140 addAttributeButton
.addSelectionListener(new SelectionAdapter() {
1142 public void widgetSelected(SelectionEvent e
) {
1144 String attributeName
= getAttributeNameSuggestion(inputElement
);
1145 InputAttribute inputAttribute
= new InputAttribute(attributeName
, attributeName
, 0, ""); //$NON-NLS-1$
1146 attributes
.add(new Attribute(group
, ElementNode
.this, inputAttribute
, attributes
.size() + 1));
1148 elementContainer
.layout();
1149 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
,
1150 elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
- 1);
1151 group
.getParent().layout();
1157 addAttributeLabel
= new Label(addAttributeComposite
, SWT
.NULL
);
1158 addAttributeLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1159 addAttributeLabel
.setText(Messages
.CustomXmlParserInputWizardPage_newAttibute
);
1162 private void removeAddButton() {
1163 fillerLabel
.dispose();
1164 addAttributeComposite
.dispose();
1167 private void removeAttribute(int attributeNumber
) {
1168 if (--attributeNumber
< attributes
.size()) {
1169 attributes
.remove(attributeNumber
).dispose();
1170 for (int i
= attributeNumber
; i
< attributes
.size(); i
++) {
1171 attributes
.get(i
).setAttributeNumber(i
+ 1);
1173 elementContainer
.layout();
1174 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
,
1175 elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
- 1);
1176 group
.getParent().layout();
1180 private void dispose() {
1184 private void extractInputs() {
1185 inputElement
.elementName
= elementNameText
.getText().trim();
1186 if (inputElement
.parentElement
!= null) {
1187 inputElement
.logEntry
= logEntryButton
.getSelection();
1188 if (tagCombo
.getText().equals(CustomTraceDefinition
.TAG_OTHER
)) {
1189 inputElement
.inputName
= tagText
.getText().trim();
1191 inputElement
.inputName
= tagCombo
.getText();
1192 if (tagCombo
.getText().equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1193 inputElement
.inputFormat
= tagText
.getText().trim();
1196 inputElement
.inputAction
= actionCombo
.getSelectionIndex();
1198 inputElement
.attributes
= new ArrayList
<InputAttribute
>(attributes
.size());
1199 for (int i
= 0; i
< attributes
.size(); i
++) {
1200 Attribute attribute
= attributes
.get(i
);
1201 InputAttribute inputAttribute
= new InputAttribute();
1202 inputAttribute
.attributeName
= attribute
.attributeNameText
.getText().trim();
1203 if (attribute
.tagCombo
.getText().equals(CustomTraceDefinition
.TAG_OTHER
)) {
1204 inputAttribute
.inputName
= attribute
.tagText
.getText().trim();
1206 inputAttribute
.inputName
= attribute
.tagCombo
.getText();
1207 if (attribute
.tagCombo
.getText().equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1208 inputAttribute
.inputFormat
= attribute
.tagText
.getText().trim();
1211 inputAttribute
.inputAction
= attribute
.actionCombo
.getSelectionIndex();
1212 inputElement
.addAttribute(inputAttribute
);
1217 private class Attribute
{
1218 ElementNode element
;
1219 int attributeNumber
;
1221 // children of parent (must be disposed)
1222 Composite labelComposite
;
1223 Composite attributeComposite
;
1225 Composite tagComposite
;
1227 // children of labelComposite
1228 Label attributeLabel
;
1230 // children of attributeComposite
1231 Text attributeNameText
;
1234 // children of tagComposite
1240 public Attribute(Composite parent
, ElementNode element
, InputAttribute inputAttribute
, int attributeNumber
) {
1241 this.element
= element
;
1242 this.attributeNumber
= attributeNumber
;
1244 labelComposite
= new Composite(parent
, SWT
.FILL
);
1245 GridLayout labelLayout
= new GridLayout(2, false);
1246 labelLayout
.marginWidth
= 0;
1247 labelLayout
.marginHeight
= 0;
1248 labelComposite
.setLayout(labelLayout
);
1249 labelComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1251 Button deleteButton
= new Button(labelComposite
, SWT
.PUSH
);
1252 deleteButton
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1253 deleteButton
.setImage(deleteImage
);
1254 deleteButton
.setToolTipText(Messages
.CustomXmlParserInputWizardPage_removeAttribute
);
1255 deleteButton
.addSelectionListener(new SelectionAdapter() {
1257 public void widgetSelected(SelectionEvent e
) {
1258 Attribute
.this.element
.removeAttribute(Attribute
.this.attributeNumber
);
1264 attributeLabel
= new Label(labelComposite
, SWT
.NULL
);
1265 attributeLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1266 attributeLabel
.setText(Messages
.CustomXmlParserInputWizardPage_attibute
);
1268 attributeComposite
= new Composite(parent
, SWT
.FILL
);
1269 GridLayout attributeLayout
= new GridLayout(4, false);
1270 attributeLayout
.marginWidth
= 0;
1271 attributeLayout
.marginHeight
= 0;
1272 attributeComposite
.setLayout(attributeLayout
);
1273 attributeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1275 Label nameLabel
= new Label(attributeComposite
, SWT
.NONE
);
1276 nameLabel
.setText(Messages
.CustomXmlParserInputWizardPage_name
);
1278 attributeNameText
= new Text(attributeComposite
, SWT
.BORDER
| SWT
.SINGLE
);
1279 attributeNameText
.setLayoutData(new GridData(120, SWT
.DEFAULT
));
1280 attributeNameText
.setText(inputAttribute
.attributeName
);
1281 attributeNameText
.addModifyListener(updateListener
);
1283 Label previewLabel
= new Label(attributeComposite
, SWT
.NONE
);
1284 previewLabel
.setText(Messages
.CustomXmlParserInputWizardPage_preview
);
1286 previewText
= new Text(attributeComposite
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.READ_ONLY
);
1287 GridData gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
1289 previewText
.setLayoutData(gd
);
1290 previewText
.setText(Messages
.CustomXmlParserInputWizardPage_noMatch
);
1291 previewText
.setBackground(COLOR_WIDGET_BACKGROUND
);
1293 filler
= new Label(parent
, SWT
.NULL
);
1295 tagComposite
= new Composite(parent
, SWT
.FILL
);
1296 GridLayout tagLayout
= new GridLayout(4, false);
1297 tagLayout
.marginWidth
= 0;
1298 tagLayout
.marginHeight
= 0;
1299 tagComposite
.setLayout(tagLayout
);
1300 tagComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1302 tagCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
1303 tagCombo
.setItems(new String
[] { CustomTraceDefinition
.TAG_TIMESTAMP
, CustomTraceDefinition
.TAG_MESSAGE
,
1304 CustomTraceDefinition
.TAG_OTHER
});
1305 tagCombo
.select(2); // Other
1306 tagCombo
.addSelectionListener(new SelectionListener() {
1308 public void widgetDefaultSelected(SelectionEvent e
) {
1312 public void widgetSelected(SelectionEvent e
) {
1313 tagText
.removeModifyListener(updateListener
);
1314 switch (tagCombo
.getSelectionIndex()) {
1315 case 0: // Time Stamp
1316 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_format
);
1317 tagLabel
.setVisible(true);
1318 tagText
.setVisible(true);
1319 tagText
.addModifyListener(updateListener
);
1322 tagLabel
.setVisible(false);
1323 tagText
.setVisible(false);
1326 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_tagName
);
1327 tagLabel
.setVisible(true);
1328 if (tagText
.getText().trim().length() == 0) {
1329 tagText
.setText(attributeNameText
.getText().trim());
1331 tagText
.setVisible(true);
1332 tagText
.addModifyListener(updateListener
);
1337 tagComposite
.layout();
1343 tagLabel
= new Label(tagComposite
, SWT
.NULL
);
1344 tagLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1346 tagText
= new Text(tagComposite
, SWT
.BORDER
| SWT
.SINGLE
);
1347 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
1349 tagText
.setLayoutData(gd
);
1350 tagText
.setText(attributeNameText
.getText());
1352 actionCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
1353 actionCombo
.setItems(new String
[] { Messages
.CustomXmlParserInputWizardPage_set
, Messages
.CustomXmlParserInputWizardPage_append
,
1354 Messages
.CustomXmlParserInputWizardPage_appendWith
});
1355 actionCombo
.select(inputAttribute
.inputAction
);
1356 actionCombo
.addSelectionListener(updateListener
);
1358 if (inputAttribute
.inputName
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1360 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_format
);
1361 tagText
.setText(inputAttribute
.inputFormat
);
1362 tagText
.addModifyListener(updateListener
);
1363 } else if (inputAttribute
.inputName
.equals(CustomTraceDefinition
.TAG_MESSAGE
)) {
1365 tagLabel
.setVisible(false);
1366 tagText
.setVisible(false);
1369 tagLabel
.setText(Messages
.CustomXmlParserInputWizardPage_tagName
);
1370 tagText
.setText(inputAttribute
.inputName
);
1371 tagText
.addModifyListener(updateListener
);
1375 private void dispose() {
1376 labelComposite
.dispose();
1377 attributeComposite
.dispose();
1379 tagComposite
.dispose();
1382 private void setAttributeNumber(int attributeNumber
) {
1383 this.attributeNumber
= attributeNumber
;
1384 labelComposite
.layout();
1388 private Element
getPreviewElement(InputElement inputElement
) {
1389 Element element
= documentElement
;
1390 if (element
!= null) {
1391 if (!documentElement
.getNodeName().equals(definition
.rootInputElement
.elementName
)) {
1394 ArrayList
<String
> elementNames
= new ArrayList
<String
>();
1395 while (inputElement
!= null) {
1396 elementNames
.add(inputElement
.elementName
);
1397 inputElement
= inputElement
.parentElement
;
1399 for (int i
= elementNames
.size() - 1; --i
>= 0;) {
1400 NodeList childList
= element
.getChildNodes();
1402 for (int j
= 0; j
< childList
.getLength(); j
++) {
1403 Node child
= childList
.item(j
);
1404 if (child
instanceof Element
&& child
.getNodeName().equals(elementNames
.get(i
))) {
1405 element
= (Element
) child
;
1409 if (element
== null) {
1413 if (element
!= null) {
1420 private String
getChildNameSuggestion(InputElement inputElement
) {
1421 if (inputElement
== null) {
1422 if (documentElement
!= null) {
1423 return documentElement
.getNodeName();
1426 Element element
= getPreviewElement(inputElement
);
1427 if (element
!= null) {
1428 NodeList childNodes
= element
.getChildNodes();
1429 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
1430 Node node
= childNodes
.item(i
);
1431 if (node
instanceof Element
) {
1432 boolean unused
= true;
1433 if (inputElement
.childElements
!= null) {
1434 for (InputElement child
: inputElement
.childElements
) {
1435 if (child
.elementName
.equals(node
.getNodeName())) {
1442 return node
.getNodeName();
1448 return ""; //$NON-NLS-1$
1451 private String
getAttributeNameSuggestion(InputElement inputElement
) {
1452 Element element
= getPreviewElement(inputElement
);
1453 if (element
!= null) {
1454 NamedNodeMap attributeMap
= element
.getAttributes();
1455 for (int i
= 0; i
< attributeMap
.getLength(); i
++) {
1456 Node node
= attributeMap
.item(i
);
1457 boolean unused
= true;
1458 if (inputElement
.attributes
!= null) {
1459 for (InputAttribute attribute
: inputElement
.attributes
) {
1460 if (attribute
.attributeName
.equals(node
.getNodeName())) {
1467 return node
.getNodeName();
1471 return ""; //$NON-NLS-1$
1474 private void validate() {
1475 definition
.definitionName
= logtypeText
.getText().trim();
1476 definition
.timeStampOutputFormat
= timeStampOutputFormatText
.getText().trim();
1478 if (selectedElement
!= null) {
1479 selectedElement
.extractInputs();
1480 treeViewer
.refresh();
1483 StringBuffer errors
= new StringBuffer();
1485 if (definition
.definitionName
.length() == 0) {
1486 errors
.append(Messages
.CustomXmlParserInputWizardPage_emptyLogTypeError
);
1487 logtypeText
.setBackground(COLOR_LIGHT_RED
);
1489 logtypeText
.setBackground(COLOR_TEXT_BACKGROUND
);
1490 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
1491 if (definition
.definitionName
.equals(def
.definitionName
)) {
1492 if (editDefinitionName
== null || !editDefinitionName
.equals(definition
.definitionName
)) {
1493 errors
.append(Messages
.CustomXmlParserInputWizardPage_duplicatelogTypeError
);
1494 logtypeText
.setBackground(COLOR_LIGHT_RED
);
1501 if (definition
.rootInputElement
== null) {
1502 errors
.append(Messages
.CustomXmlParserInputWizardPage_noDocumentError
);
1505 if (definition
.rootInputElement
!= null) {
1506 logEntryFound
= false;
1507 timeStampFound
= false;
1509 errors
.append(validateElement(definition
.rootInputElement
));
1511 if ((definition
.rootInputElement
.attributes
!= null && definition
.rootInputElement
.attributes
.size() != 0)
1512 || (definition
.rootInputElement
.childElements
!= null && definition
.rootInputElement
.childElements
.size() != 0)
1513 || errors
.length() == 0) {
1514 if (!logEntryFound
) {
1515 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingLogEntryError
);
1518 if (timeStampFound
) {
1519 if (timeStampOutputFormatText
.getText().trim().length() == 0) {
1520 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingTimestampFmtError
);
1521 timeStampOutputFormatText
.setBackground(COLOR_LIGHT_RED
);
1524 new SimpleDateFormat(timeStampOutputFormatText
.getText().trim());
1525 timeStampOutputFormatText
.setBackground(COLOR_TEXT_BACKGROUND
);
1526 } catch (IllegalArgumentException e
) {
1527 errors
.append(Messages
.CustomXmlParserInputWizardPage_invalidTimestampFmtError
);
1528 timeStampOutputFormatText
.setBackground(COLOR_LIGHT_RED
);
1532 timeStampPreviewText
.setText(Messages
.CustomXmlParserInputWizardPage_notimestamporAttributeError
);
1536 timeStampPreviewText
.setText(Messages
.CustomXmlParserInputWizardPage_notimestamporAttributeError
);
1539 if (errors
.length() == 0) {
1540 setDescription(defaultDescription
);
1541 setPageComplete(true);
1543 setDescription(errors
.toString());
1544 setPageComplete(false);
1549 * Clean up the specified XML element.
1551 * @param inputElement
1552 * The element to clean up
1553 * @return The validated element
1555 public StringBuffer
validateElement(InputElement inputElement
) {
1556 StringBuffer errors
= new StringBuffer();
1557 ElementNode elementNode
= null;
1558 if (selectedElement
!= null && selectedElement
.inputElement
.equals(inputElement
)) {
1559 elementNode
= selectedElement
;
1561 if (inputElement
== definition
.rootInputElement
) {
1562 if (inputElement
.elementName
.length() == 0) {
1563 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingDocumentElementError
);
1564 if (elementNode
!= null) {
1565 elementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1568 if (elementNode
!= null) {
1569 elementNode
.elementNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1573 if (inputElement
!= definition
.rootInputElement
) {
1574 if (inputElement
.logEntry
) {
1575 logEntryFound
= true;
1577 if (inputElement
.inputName
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1578 timeStampFound
= true;
1579 if (inputElement
.inputFormat
.length() == 0) {
1580 errors
.append(Messages
.CustomXmlParserInputWizardPage_timestampFormatPrompt
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 new SimpleDateFormat(inputElement
.inputFormat
);
1588 if (elementNode
!= null) {
1589 elementNode
.tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1591 } catch (IllegalArgumentException e
) {
1592 errors
.append(Messages
.CustomXmlParserInputWizardPage_invalidTimestampFmtError
1593 + " (" + Messages
.CustomXmlParserInputWizardPage_timestampElementPrompt
+ " " + getName(inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1594 if (elementNode
!= null) {
1595 elementNode
.tagText
.setBackground(COLOR_LIGHT_RED
);
1599 } else if (inputElement
.inputName
.length() == 0) {
1600 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingInputElementNameError
);
1601 if (elementNode
!= null) {
1602 elementNode
.tagText
.setBackground(COLOR_LIGHT_RED
);
1605 if (elementNode
!= null) {
1606 elementNode
.tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1610 if (inputElement
.attributes
!= null) {
1611 if (elementNode
!= null) {
1612 for (Attribute attribute
: elementNode
.attributes
) {
1613 attribute
.attributeNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1616 for (int i
= 0; i
< inputElement
.attributes
.size(); i
++) {
1617 InputAttribute attribute
= inputElement
.attributes
.get(i
);
1618 boolean duplicate
= false;
1619 for (int j
= i
+ 1; j
< inputElement
.attributes
.size(); j
++) {
1620 InputAttribute otherAttribute
= inputElement
.attributes
.get(j
);
1621 if (otherAttribute
.attributeName
.equals(attribute
.attributeName
)) {
1623 if (elementNode
!= null) {
1624 elementNode
.attributes
.get(j
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1628 if (attribute
.attributeName
.length() == 0) {
1629 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingAttribute
1630 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(inputElement
) + ": ?). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1631 if (elementNode
!= null) {
1632 elementNode
.attributes
.get(i
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1634 } else if (duplicate
) {
1635 errors
.append(Messages
.CustomXmlParserInputWizardPage_duplicateAttributeError
1636 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(attribute
, inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1637 if (elementNode
!= null) {
1638 elementNode
.attributes
.get(i
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1641 if (attribute
.inputName
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
)) {
1642 timeStampFound
= true;
1643 if (attribute
.inputFormat
.length() == 0) {
1644 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingTimestampInFmtError
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 new SimpleDateFormat(attribute
.inputFormat
);
1652 if (elementNode
!= null) {
1653 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1655 } catch (IllegalArgumentException e
) {
1656 errors
.append(Messages
.CustomXmlParserInputWizardPage_invalidTimestampInFmtError
1657 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(attribute
, inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1658 if (elementNode
!= null) {
1659 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_LIGHT_RED
);
1663 } else if (attribute
.inputName
.length() == 0) {
1664 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingDataGroupNameError
1665 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(attribute
, inputElement
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1666 if (elementNode
!= null) {
1667 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_LIGHT_RED
);
1670 if (elementNode
!= null) {
1671 elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1676 if (inputElement
.childElements
!= null) {
1677 for (InputElement child
: inputElement
.childElements
) {
1678 ElementNode childElementNode
= null;
1679 if (selectedElement
!= null && selectedElement
.inputElement
.equals(child
)) {
1680 childElementNode
= selectedElement
;
1682 if (childElementNode
!= null) {
1683 childElementNode
.elementNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1686 for (int i
= 0; i
< inputElement
.childElements
.size(); i
++) {
1687 InputElement child
= inputElement
.childElements
.get(i
);
1688 ElementNode childElementNode
= null;
1689 if (selectedElement
!= null && selectedElement
.inputElement
.equals(child
)) {
1690 childElementNode
= selectedElement
;
1692 if (child
.elementName
.length() == 0) {
1693 errors
.append(Messages
.CustomXmlParserInputWizardPage_missingElementNameError
1694 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(child
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1695 if (childElementNode
!= null) {
1696 childElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1699 boolean duplicate
= false;
1700 for (int j
= i
+ 1; j
< inputElement
.childElements
.size(); j
++) {
1701 InputElement otherChild
= inputElement
.childElements
.get(j
);
1702 if (otherChild
.elementName
.equals(child
.elementName
)) {
1704 ElementNode otherChildElementNode
= null;
1705 if (selectedElement
!= null && selectedElement
.inputElement
.equals(otherChild
)) {
1706 otherChildElementNode
= selectedElement
;
1708 if (otherChildElementNode
!= null) {
1709 otherChildElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1714 errors
.append(Messages
.CustomXmlParserInputWizardPage_duplicateElementNameError
1715 + " (" + Messages
.CustomXmlParserInputWizardPage_attributePrompt
+ " " + getName(child
) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1716 if (childElementNode
!= null) {
1717 childElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1722 errors
.append(validateElement(child
));
1729 * Get the trace definition.
1731 * @return The trace definition
1733 public CustomXmlTraceDefinition
getDefinition() {
1738 * Get the raw text input.
1740 * @return The raw text input.
1742 public char[] getInputText() {
1743 return inputText
.getText().toCharArray();