1 package org
.eclipse
.linuxtools
.tmf
.ui
.wizards
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.ByteArrayInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.io
.InputStreamReader
;
8 import java
.text
.ParseException
;
9 import java
.text
.SimpleDateFormat
;
10 import java
.util
.ArrayList
;
11 import java
.util
.Date
;
12 import java
.util
.List
;
14 import javax
.xml
.parsers
.DocumentBuilder
;
15 import javax
.xml
.parsers
.DocumentBuilderFactory
;
16 import javax
.xml
.parsers
.ParserConfigurationException
;
18 import org
.eclipse
.core
.resources
.IFile
;
19 import org
.eclipse
.core
.runtime
.CoreException
;
20 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
21 import org
.eclipse
.jface
.viewers
.ISelection
;
22 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
23 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
24 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
25 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
26 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.TreeViewer
;
28 import org
.eclipse
.jface
.viewers
.Viewer
;
29 import org
.eclipse
.jface
.wizard
.WizardPage
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPlugin
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputAttribute
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputElement
;
35 import org
.eclipse
.swt
.SWT
;
36 import org
.eclipse
.swt
.browser
.Browser
;
37 import org
.eclipse
.swt
.browser
.TitleEvent
;
38 import org
.eclipse
.swt
.browser
.TitleListener
;
39 import org
.eclipse
.swt
.custom
.SashForm
;
40 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
41 import org
.eclipse
.swt
.custom
.StyleRange
;
42 import org
.eclipse
.swt
.custom
.StyledText
;
43 import org
.eclipse
.swt
.events
.ModifyEvent
;
44 import org
.eclipse
.swt
.events
.ModifyListener
;
45 import org
.eclipse
.swt
.events
.SelectionAdapter
;
46 import org
.eclipse
.swt
.events
.SelectionEvent
;
47 import org
.eclipse
.swt
.events
.SelectionListener
;
48 import org
.eclipse
.swt
.graphics
.Color
;
49 import org
.eclipse
.swt
.graphics
.Font
;
50 import org
.eclipse
.swt
.graphics
.FontData
;
51 import org
.eclipse
.swt
.graphics
.Image
;
52 import org
.eclipse
.swt
.layout
.FillLayout
;
53 import org
.eclipse
.swt
.layout
.GridData
;
54 import org
.eclipse
.swt
.layout
.GridLayout
;
55 import org
.eclipse
.swt
.widgets
.Button
;
56 import org
.eclipse
.swt
.widgets
.Combo
;
57 import org
.eclipse
.swt
.widgets
.Composite
;
58 import org
.eclipse
.swt
.widgets
.Display
;
59 import org
.eclipse
.swt
.widgets
.Group
;
60 import org
.eclipse
.swt
.widgets
.Label
;
61 import org
.eclipse
.swt
.widgets
.Shell
;
62 import org
.eclipse
.swt
.widgets
.Text
;
63 import org
.w3c
.dom
.Document
;
64 import org
.w3c
.dom
.Element
;
65 import org
.w3c
.dom
.NamedNodeMap
;
66 import org
.w3c
.dom
.Node
;
67 import org
.w3c
.dom
.NodeList
;
68 import org
.xml
.sax
.EntityResolver
;
69 import org
.xml
.sax
.ErrorHandler
;
70 import org
.xml
.sax
.InputSource
;
71 import org
.xml
.sax
.SAXException
;
72 import org
.xml
.sax
.SAXParseException
;
74 public class CustomXmlParserInputWizardPage
extends WizardPage
{
76 private static final String DEFAULT_TIMESTAMP_FORMAT
= "yyyy-MM-dd HH:mm:ss.SSS";
77 private static final String SIMPLE_DATE_FORMAT_URL
= "http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#skip-navbar_top";
78 private static final Image elementImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/element_icon.gif");
79 private static final Image addImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/add_button.gif");
80 private static final Image addNextImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/addnext_button.gif");
81 private static final Image addChildImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/addchild_button.gif");
82 private static final Image addManyImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/addmany_button.gif");
83 private static final Image deleteImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/delete_button.gif");
84 private static final Image moveUpImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/moveup_button.gif");
85 private static final Image moveDownImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/movedown_button.gif");
86 private static final Image helpImage
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/help_button.gif");
87 private static final Color COLOR_LIGHT_RED
= new Color(Display
.getDefault(), 255, 192, 192);
88 private static final Color COLOR_TEXT_BACKGROUND
= Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
);
89 private static final Color COLOR_WIDGET_BACKGROUND
= Display
.getCurrent().getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
);
91 private ISelection selection
;
92 private CustomXmlTraceDefinition definition
;
93 private String editDefinitionName
;
94 private String defaultDescription
;
95 private ElementNode selectedElement
;
96 private Composite container
;
97 private Text logtypeText
;
98 private Text timeStampOutputFormatText
;
99 private Text timeStampPreviewText
;
100 private Button removeButton
;
101 private Button addChildButton
;
102 private Button addNextButton
;
103 private Button moveUpButton
;
104 private Button moveDownButton
;
105 private Button feelingLuckyButton
;
106 private ScrolledComposite treeScrolledComposite
;
107 private ScrolledComposite elementScrolledComposite
;
108 private TreeViewer treeViewer
;
109 private Composite treeContainer
;
110 private Composite elementContainer
;
111 private Text errorText
;
112 private StyledText inputText
;
113 private Font fixedFont
;
114 private UpdateListener updateListener
;
115 private Browser helpBrowser
;
116 private Element documentElement
;
118 // variables used recursively through element traversal
119 private String timeStampValue
;
120 private String timeStampFormat
;
121 private boolean timeStampFound
;
122 private int logEntriesCount
;
123 private boolean logEntryFound
;
124 private int logEntryNestedCount
;
126 protected CustomXmlParserInputWizardPage(ISelection selection
, CustomXmlTraceDefinition definition
) {
127 super("CustomXmlParserWizardPage");
128 if (definition
== null) {
129 setTitle("New Custom XML Parser");
130 defaultDescription
= "Create a new custom parser for XML log files";
132 setTitle("Edit Custom XML Parser");
133 defaultDescription
= "Edit an existing custom parser for XML log files";
135 setDescription(defaultDescription
);
136 this.selection
= selection
;
137 this.definition
= definition
;
138 if (definition
!= null) {
139 this.editDefinitionName
= definition
.definitionName
;
143 public void createControl(Composite parent
) {
144 container
= new Composite(parent
, SWT
.NULL
);
145 container
.setLayout(new GridLayout());
147 updateListener
= new UpdateListener();
149 Composite headerComposite
= new Composite(container
, SWT
.FILL
);
150 GridLayout headerLayout
= new GridLayout(5, false);
151 headerLayout
.marginHeight
= 0;
152 headerLayout
.marginWidth
= 0;
153 headerComposite
.setLayout(headerLayout
);
154 headerComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
156 Label logtypeLabel
= new Label(headerComposite
, SWT
.NULL
);
157 logtypeLabel
.setText("Log type:");
159 logtypeText
= new Text(headerComposite
, SWT
.BORDER
| SWT
.SINGLE
);
160 logtypeText
.setLayoutData(new GridData(120, SWT
.DEFAULT
));
161 logtypeText
.addModifyListener(updateListener
);
163 Label timeStampFormatLabel
= new Label(headerComposite
, SWT
.NULL
);
164 timeStampFormatLabel
.setText("Time Stamp format:");
166 timeStampOutputFormatText
= new Text(headerComposite
, SWT
.BORDER
| SWT
.SINGLE
);
167 timeStampOutputFormatText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
168 timeStampOutputFormatText
.setText(DEFAULT_TIMESTAMP_FORMAT
);
169 timeStampOutputFormatText
.addModifyListener(updateListener
);
171 Button dateFormatHelpButton
= new Button(headerComposite
, SWT
.PUSH
);
172 dateFormatHelpButton
.setImage(helpImage
);
173 dateFormatHelpButton
.setToolTipText("Date Format Help");
174 dateFormatHelpButton
.addSelectionListener(new SelectionAdapter() {
176 public void widgetSelected(SelectionEvent e
) {
177 openHelpShell(SIMPLE_DATE_FORMAT_URL
);
181 Label timeStampPreviewLabel
= new Label(headerComposite
, SWT
.NULL
);
182 timeStampPreviewLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 3, 1));
183 timeStampPreviewLabel
.setText("Preview:");
185 timeStampPreviewText
= new Text(headerComposite
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.READ_ONLY
);
186 timeStampPreviewText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 1));
187 timeStampPreviewText
.setText("*no time stamp element or attribute*");
191 SashForm vSash
= new SashForm(container
, SWT
.VERTICAL
);
192 vSash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
193 vSash
.setBackground(vSash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
195 SashForm hSash
= new SashForm(vSash
, SWT
.HORIZONTAL
);
196 hSash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
198 treeScrolledComposite
= new ScrolledComposite(hSash
, SWT
.V_SCROLL
| SWT
.H_SCROLL
);
199 treeScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
200 treeContainer
= new Composite(treeScrolledComposite
, SWT
.NONE
);
201 treeContainer
.setLayout(new FillLayout());
202 treeScrolledComposite
.setContent(treeContainer
);
203 treeScrolledComposite
.setExpandHorizontal(true);
204 treeScrolledComposite
.setExpandVertical(true);
206 treeViewer
= new TreeViewer(treeContainer
, SWT
.SINGLE
| SWT
.BORDER
);
207 treeViewer
.setContentProvider(new InputElementTreeNodeContentProvider());
208 treeViewer
.setLabelProvider(new InputElementTreeLabelProvider());
209 treeViewer
.addSelectionChangedListener(new InputElementTreeSelectionChangedListener());
210 treeContainer
.layout();
212 treeScrolledComposite
.setMinSize(treeContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, treeContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
);
214 elementScrolledComposite
= new ScrolledComposite(hSash
, SWT
.V_SCROLL
);
215 elementScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
216 elementContainer
= new Composite(elementScrolledComposite
, SWT
.NONE
);
217 GridLayout gl
= new GridLayout();
220 elementContainer
.setLayout(gl
);
221 elementScrolledComposite
.setContent(elementContainer
);
222 elementScrolledComposite
.setExpandHorizontal(true);
223 elementScrolledComposite
.setExpandVertical(true);
225 if (definition
== null) {
226 definition
= new CustomXmlTraceDefinition();
228 loadDefinition(definition
);
229 treeViewer
.expandAll();
230 elementContainer
.layout();
232 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-1);
234 hSash
.setWeights(new int[] {1, 2});
236 if (definition
.rootInputElement
== null) {
237 removeButton
.setEnabled(false);
238 addChildButton
.setToolTipText("Add document element");
239 addNextButton
.setEnabled(false);
240 moveUpButton
.setEnabled(false);
241 moveDownButton
.setEnabled(false);
242 } else { // root is selected
243 addNextButton
.setEnabled(false);
246 Composite sashBottom
= new Composite(vSash
, SWT
.NONE
);
247 GridLayout sashBottomLayout
= new GridLayout(2, false);
248 sashBottomLayout
.marginHeight
= 0;
249 sashBottomLayout
.marginWidth
= 0;
250 sashBottom
.setLayout(sashBottomLayout
);
252 Label previewLabel
= new Label(sashBottom
, SWT
.NULL
);
253 previewLabel
.setText("Preview input");
255 errorText
= new Text(sashBottom
, SWT
.SINGLE
| SWT
.READ_ONLY
);
256 errorText
.setBackground(COLOR_WIDGET_BACKGROUND
);
257 errorText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
258 errorText
.setVisible(false);
260 inputText
= new StyledText(sashBottom
, SWT
.MULTI
| SWT
.V_SCROLL
| SWT
.H_SCROLL
);
261 if (fixedFont
== null) {
262 if (System
.getProperty("os.name").contains("Windows")) {
263 fixedFont
= new Font(Display
.getCurrent(), new FontData("Courier New", 10, SWT
.NORMAL
));
265 fixedFont
= new Font(Display
.getCurrent(), new FontData("Monospace", 10, SWT
.NORMAL
));
268 inputText
.setFont(fixedFont
);
269 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true, 2, 1);
270 gd
.heightHint
= inputText
.computeSize(SWT
.DEFAULT
, inputText
.getLineHeight() * 4).y
;
272 inputText
.setLayoutData(gd
);
273 inputText
.setText(getSelectionText());
274 inputText
.addModifyListener(new ModifyListener(){
275 public void modifyText(ModifyEvent e
) {
276 parseXmlInput(inputText
.getText());
278 inputText
.addModifyListener(updateListener
);
280 vSash
.setWeights(new int[] {hSash
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sashBottom
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
282 setControl(container
);
285 private void createButtonBar() {
286 Composite buttonBar
= new Composite(container
, SWT
.NONE
);
287 GridLayout buttonBarLayout
= new GridLayout(6, false);
288 buttonBarLayout
.marginHeight
= 0;
289 buttonBarLayout
.marginWidth
= 0;
290 buttonBar
.setLayout(buttonBarLayout
);
292 removeButton
= new Button(buttonBar
, SWT
.PUSH
);
293 removeButton
.setImage(deleteImage
);
294 removeButton
.setToolTipText("Remove element");
295 removeButton
.addSelectionListener(new SelectionAdapter() {
297 public void widgetSelected(SelectionEvent e
) {
298 if (treeViewer
.getSelection().isEmpty() || selectedElement
== null) return;
300 InputElement inputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
301 if (inputElement
== definition
.rootInputElement
) {
302 definition
.rootInputElement
= null;
304 inputElement
.parentElement
.childElements
.remove(inputElement
);
306 treeViewer
.refresh();
309 removeButton
.setEnabled(false);
310 if (definition
.rootInputElement
== null) {
311 addChildButton
.setEnabled(true);
312 addChildButton
.setToolTipText("Add document element");
314 addChildButton
.setEnabled(false);
316 addNextButton
.setEnabled(false);
317 moveUpButton
.setEnabled(false);
318 moveDownButton
.setEnabled(false);
322 addChildButton
= new Button(buttonBar
, SWT
.PUSH
);
323 addChildButton
.setImage(addChildImage
);
324 addChildButton
.setToolTipText("Add child element");
325 addChildButton
.addSelectionListener(new SelectionAdapter() {
327 public void widgetSelected(SelectionEvent e
) {
328 InputElement inputElement
= new InputElement("", false, CustomXmlTraceDefinition
.TAG_IGNORE
, 0, "", null);
329 if (definition
.rootInputElement
== null) {
330 definition
.rootInputElement
= inputElement
;
331 inputElement
.elementName
= getChildNameSuggestion(null);
332 } else if (treeViewer
.getSelection().isEmpty()) {
335 InputElement parentInputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
336 parentInputElement
.addChild(inputElement
);
337 inputElement
.elementName
= getChildNameSuggestion(parentInputElement
);
339 treeViewer
.refresh();
340 treeViewer
.setSelection(new StructuredSelection(inputElement
), true);
344 addNextButton
= new Button(buttonBar
, SWT
.PUSH
);
345 addNextButton
.setImage(addNextImage
);
346 addNextButton
.setToolTipText("Add next element");
347 addNextButton
.addSelectionListener(new SelectionAdapter() {
349 public void widgetSelected(SelectionEvent e
) {
350 InputElement inputElement
= new InputElement("", false, CustomXmlTraceDefinition
.TAG_IGNORE
, 0, "", null);
351 if (definition
.rootInputElement
== null) {
352 definition
.rootInputElement
= inputElement
;
353 inputElement
.elementName
= getChildNameSuggestion(null);
354 } else if (treeViewer
.getSelection().isEmpty()) {
357 InputElement previousInputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
358 if (previousInputElement
== definition
.rootInputElement
) {
361 previousInputElement
.addNext(inputElement
);
362 inputElement
.elementName
= getChildNameSuggestion(inputElement
.parentElement
);
365 treeViewer
.refresh();
366 treeViewer
.setSelection(new StructuredSelection(inputElement
), true);
370 feelingLuckyButton
= new Button(buttonBar
, SWT
.PUSH
);
371 feelingLuckyButton
.setImage(addManyImage
);
372 feelingLuckyButton
.setToolTipText("Feeling lucky");
373 feelingLuckyButton
.addSelectionListener(new SelectionAdapter() {
375 public void widgetSelected(SelectionEvent e
) {
376 InputElement inputElement
= null;
377 if (definition
.rootInputElement
== null) {
378 if (getChildNameSuggestion(null).length() != 0) {
379 inputElement
= new InputElement(getChildNameSuggestion(null), false, CustomXmlTraceDefinition
.TAG_IGNORE
, 0, "", null);
380 definition
.rootInputElement
= inputElement
;
381 feelingLucky(inputElement
);
383 } else if (treeViewer
.getSelection().isEmpty()) {
386 inputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
387 feelingLucky(inputElement
);
389 treeViewer
.refresh();
390 treeViewer
.setSelection(new StructuredSelection(inputElement
), true);
391 treeViewer
.expandToLevel(inputElement
, TreeViewer
.ALL_LEVELS
);
395 moveUpButton
= new Button(buttonBar
, SWT
.PUSH
);
396 moveUpButton
.setImage(moveUpImage
);
397 moveUpButton
.setToolTipText("Move up");
398 moveUpButton
.addSelectionListener(new SelectionAdapter() {
400 public void widgetSelected(SelectionEvent e
) {
401 if (treeViewer
.getSelection().isEmpty()) return;
402 InputElement inputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
403 if (inputElement
== definition
.rootInputElement
) {
406 inputElement
.moveUp();
408 treeViewer
.refresh();
414 moveDownButton
= new Button(buttonBar
, SWT
.PUSH
);
415 moveDownButton
.setImage(moveDownImage
);
416 moveDownButton
.setToolTipText("Move down");
417 moveDownButton
.addSelectionListener(new SelectionAdapter() {
419 public void widgetSelected(SelectionEvent e
) {
420 if (treeViewer
.getSelection().isEmpty()) return;
421 InputElement inputElement
= (InputElement
) ((IStructuredSelection
) treeViewer
.getSelection()).getFirstElement();
422 if (inputElement
== definition
.rootInputElement
) {
425 inputElement
.moveDown();
427 treeViewer
.refresh();
434 private void feelingLucky(InputElement inputElement
) {
436 String attributeName
= getAttributeNameSuggestion(inputElement
);
437 if (attributeName
.length() == 0) {
440 InputAttribute attribute
= new InputAttribute(attributeName
, attributeName
, 0, "");
441 inputElement
.addAttribute(attribute
);
445 String childName
= getChildNameSuggestion(inputElement
);
446 if (childName
.length() == 0) {
449 InputElement childElement
= new InputElement(childName
, false, CustomXmlTraceDefinition
.TAG_IGNORE
, 0, "", null);
450 inputElement
.addChild(childElement
);
451 feelingLucky(childElement
);
456 private class InputElementTreeNodeContentProvider
implements ITreeContentProvider
{
458 public Object
[] getElements(Object inputElement
) {
459 CustomXmlTraceDefinition def
= (CustomXmlTraceDefinition
) inputElement
;
460 if (def
.rootInputElement
!= null) {
461 return new Object
[] {def
.rootInputElement
};
463 return new Object
[0];
467 public Object
[] getChildren(Object parentElement
) {
468 InputElement inputElement
= (InputElement
) parentElement
;
469 if (inputElement
.childElements
== null) return new InputElement
[0];
470 return inputElement
.childElements
.toArray();
473 public boolean hasChildren(Object element
) {
474 InputElement inputElement
= (InputElement
) element
;
475 return (inputElement
.childElements
!= null && inputElement
.childElements
.size() > 0);
478 public void dispose() {
481 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
484 public Object
getParent(Object element
) {
485 InputElement inputElement
= (InputElement
) element
;
486 return inputElement
.parentElement
;
490 private class InputElementTreeLabelProvider
extends ColumnLabelProvider
{
493 public Image
getImage(Object element
) {
498 public String
getText(Object element
) {
499 InputElement inputElement
= (InputElement
) element
;
500 return (inputElement
.elementName
.trim().length() == 0) ?
"?" : inputElement
.elementName
;
504 private class InputElementTreeSelectionChangedListener
implements ISelectionChangedListener
{
505 public void selectionChanged(SelectionChangedEvent event
) {
506 if (selectedElement
!= null) {
507 selectedElement
.dispose();
509 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
510 IStructuredSelection selection
= (IStructuredSelection
) event
.getSelection();
511 InputElement inputElement
= (InputElement
) selection
.getFirstElement();
512 selectedElement
= new ElementNode(elementContainer
, inputElement
);
513 elementContainer
.layout();
514 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-1);
518 removeButton
.setEnabled(true);
519 addChildButton
.setEnabled(true);
520 addChildButton
.setToolTipText("Add child element");
521 if (definition
.rootInputElement
== inputElement
) {
522 addNextButton
.setEnabled(false);
524 addNextButton
.setEnabled(true);
526 moveUpButton
.setEnabled(true);
527 moveDownButton
.setEnabled(true);
529 removeButton
.setEnabled(false);
530 if (definition
.rootInputElement
== null) {
531 addChildButton
.setEnabled(true);
532 addChildButton
.setToolTipText("Add document element");
534 addChildButton
.setEnabled(false);
536 addNextButton
.setEnabled(false);
537 moveUpButton
.setEnabled(false);
538 moveDownButton
.setEnabled(false);
544 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
547 public void dispose() {
548 if (fixedFont
!= null) {
555 private void loadDefinition(CustomXmlTraceDefinition def
) {
556 logtypeText
.setText(def
.definitionName
);
557 timeStampOutputFormatText
.setText(def
.timeStampOutputFormat
);
558 treeViewer
.setInput(def
);
560 if (def
.rootInputElement
!= null) {
561 treeViewer
.setSelection(new StructuredSelection(def
.rootInputElement
));
565 private String
getName(InputElement inputElement
) {
566 String name
= (inputElement
.elementName
.trim().length() == 0) ?
"?" : inputElement
.elementName
.trim();
567 if (inputElement
.parentElement
== null) {
570 return getName(inputElement
.parentElement
) + " : " + name
;
573 private String
getName(InputAttribute inputAttribute
, InputElement inputElement
) {
574 String name
= (inputAttribute
.attributeName
.trim().length() == 0) ?
"?" : inputAttribute
.attributeName
.trim();
575 return getName(inputElement
) + " : " + name
;
579 * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
582 public void setVisible(boolean visible
) {
587 super.setVisible(visible
);
590 public List
<String
> getInputNames() {
591 return getInputNames(definition
.rootInputElement
);
594 public List
<String
> getInputNames(InputElement inputElement
) {
595 List
<String
> inputs
= new ArrayList
<String
>();
596 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
597 String inputName
= inputElement
.inputName
;
598 if (!inputs
.contains(inputName
)) {
599 inputs
.add(inputName
);
602 if (inputElement
.attributes
!= null) {
603 for (InputAttribute attribute
: inputElement
.attributes
) {
604 String inputName
= attribute
.inputName
;
605 if (!inputs
.contains(inputName
)) {
606 inputs
.add(inputName
);
610 if (inputElement
.childElements
!= null) {
611 for (InputElement childInputElement
: inputElement
.childElements
) {
612 for (String inputName
: getInputNames(childInputElement
)) {
613 if (!inputs
.contains(inputName
)) {
614 inputs
.add(inputName
);
622 private void removeElement() {
623 selectedElement
.dispose();
624 selectedElement
= null;
625 elementContainer
.layout();
626 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-1);
630 private String
getSelectionText() {
631 InputStream inputStream
= null;
632 if (this.selection
instanceof IStructuredSelection
) {
633 Object selection
= ((IStructuredSelection
)this.selection
).getFirstElement();
634 if (selection
instanceof IFile
) {
635 IFile file
= (IFile
)selection
;
637 inputStream
= file
.getContents();
638 } catch (CoreException e
) {
643 if (inputStream
!= null) {
645 BufferedReader reader
= new BufferedReader(new InputStreamReader(inputStream
));
646 StringBuilder sb
= new StringBuilder();
648 while ((line
= reader
.readLine()) != null) {
649 sb
.append(line
+ "\n");
651 parseXmlInput(sb
.toString());
652 return sb
.toString();
653 } catch (IOException e
) {
660 private void parseXmlInput(final String string
) {
662 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
663 DocumentBuilder db
= dbf
.newDocumentBuilder();
665 // The following allows xml parsing without access to the dtd
666 EntityResolver resolver
= new EntityResolver () {
667 public InputSource
resolveEntity (String publicId
, String systemId
) {
669 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
670 return new InputSource(bais
);
673 db
.setEntityResolver(resolver
);
675 // The following catches xml parsing exceptions
676 db
.setErrorHandler(new ErrorHandler(){
677 public void error(SAXParseException saxparseexception
) throws SAXException
{}
678 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
679 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
680 if (string
.trim().length() != 0) {
681 errorText
.setText(saxparseexception
.getMessage());
682 errorText
.setBackground(COLOR_LIGHT_RED
);
683 errorText
.setVisible(true);
685 throw saxparseexception
;
688 errorText
.setVisible(false);
690 doc
= db
.parse(new ByteArrayInputStream(string
.getBytes()));
691 documentElement
= doc
.getDocumentElement();
692 } catch (ParserConfigurationException e
) {
694 documentElement
= null;
695 } catch (SAXException e
) {
696 documentElement
= null;
697 } catch (IOException e
) {
699 documentElement
= null;
703 private void updatePreviews() {
704 updatePreviews(false);
707 private void updatePreviews(boolean updateAll
) {
708 if (inputText
== null) {
709 // early update during construction
712 inputText
.setStyleRanges(new StyleRange
[] {});
713 if (selectedElement
== null) {
717 timeStampValue
= null;
718 timeStampFormat
= null;
720 logEntryFound
= false;
722 selectedElement
.updatePreview();
724 if (timeStampValue
!= null && timeStampFormat
!= null) {
726 SimpleDateFormat dateFormat
= new SimpleDateFormat(timeStampFormat
);
727 Date date
= dateFormat
.parse(timeStampValue
);
728 dateFormat
= new SimpleDateFormat(timeStampOutputFormatText
.getText().trim());
729 timeStampPreviewText
.setText(dateFormat
.format(date
));
730 } catch (ParseException e
) {
731 timeStampPreviewText
.setText("*parse exception* [" + timeStampValue
+ "] <> [" + timeStampFormat
+ "]");
732 } catch (IllegalArgumentException e
) {
733 timeStampPreviewText
.setText("*parse exception* [Illegal Argument]");
736 timeStampPreviewText
.setText("*no matching time stamp*");
740 private void openHelpShell(String url
) {
741 if (helpBrowser
!= null && !helpBrowser
.isDisposed()) {
742 helpBrowser
.getShell().setActive();
743 if (!helpBrowser
.getUrl().equals(url
)) {
744 helpBrowser
.setUrl(url
);
748 final Shell helpShell
= new Shell(getShell(), SWT
.SHELL_TRIM
);
749 helpShell
.setLayout(new FillLayout());
750 helpBrowser
= new Browser(helpShell
, SWT
.NONE
);
751 helpBrowser
.addTitleListener(new TitleListener() {
752 public void changed(TitleEvent event
) {
753 helpShell
.setText(event
.title
);
756 helpBrowser
.setBounds(0,0,600,400);
759 helpBrowser
.setUrl(url
);
762 private class UpdateListener
implements ModifyListener
, SelectionListener
{
764 public void modifyText(ModifyEvent e
) {
769 public void widgetDefaultSelected(SelectionEvent e
) {
774 public void widgetSelected(SelectionEvent e
) {
781 private class ElementNode
{
782 final InputElement inputElement
;
784 ArrayList
<Attribute
> attributes
= new ArrayList
<Attribute
>();
785 ArrayList
<ElementNode
> childElements
= new ArrayList
<ElementNode
>();
786 Text elementNameText
;
787 Composite tagComposite
;
794 Button logEntryButton
;
796 Composite addAttributeComposite
;
797 Button addAttributeButton
;
798 Label addAttributeLabel
;
800 public ElementNode(Composite parent
, InputElement inputElement
) {
801 this.inputElement
= inputElement
;
803 group
= new Group(parent
, SWT
.NONE
);
804 GridLayout gl
= new GridLayout(2, false);
807 group
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
808 group
.setText(getName(inputElement
));
810 Label label
= new Label(group
, SWT
.NULL
);
811 label
.setText("Element name:");
812 label
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
814 elementNameText
= new Text(group
, SWT
.BORDER
| SWT
.SINGLE
);
815 GridData gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
817 elementNameText
.setLayoutData(gd
);
818 elementNameText
.addModifyListener(new ModifyListener(){
819 public void modifyText(ModifyEvent e
) {
820 ElementNode
.this.inputElement
.elementName
= elementNameText
.getText().trim();
821 group
.setText(getName(ElementNode
.this.inputElement
));
823 elementNameText
.setText(inputElement
.elementName
);
824 elementNameText
.addModifyListener(updateListener
);
826 if (inputElement
.parentElement
!= null) {
827 previewLabel
= new Label(group
, SWT
.NULL
);
828 previewLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
829 previewLabel
.setText("Preview:");
831 previewText
= new Text(group
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.READ_ONLY
);
832 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
834 previewText
.setLayoutData(gd
);
835 previewText
.setText("*no matching element*");
836 previewText
.setBackground(COLOR_WIDGET_BACKGROUND
);
838 logEntryButton
= new Button(group
, SWT
.CHECK
);
839 logEntryButton
.setText("Log Entry");
840 logEntryButton
.setSelection(inputElement
.logEntry
);
841 logEntryButton
.addSelectionListener(new SelectionListener(){
842 public void widgetDefaultSelected(SelectionEvent e
) {}
843 public void widgetSelected(SelectionEvent e
) {
844 InputElement parent
= ElementNode
.this.inputElement
.parentElement
;
845 while (parent
!= null) {
846 parent
.logEntry
= false;
847 parent
= parent
.parentElement
;
850 logEntryButton
.addSelectionListener(updateListener
);
852 tagComposite
= new Composite(group
, SWT
.FILL
);
853 GridLayout tagLayout
= new GridLayout(4, false);
854 tagLayout
.marginWidth
= 0;
855 tagLayout
.marginHeight
= 0;
856 tagComposite
.setLayout(tagLayout
);
857 tagComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
859 tagCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
860 tagCombo
.setItems(new String
[] {CustomXmlTraceDefinition
.TAG_IGNORE
,
861 CustomXmlTraceDefinition
.TAG_TIMESTAMP
,
862 CustomXmlTraceDefinition
.TAG_MESSAGE
,
863 CustomXmlTraceDefinition
.TAG_OTHER
});
864 tagCombo
.setVisibleItemCount(tagCombo
.getItemCount());
865 tagCombo
.addSelectionListener(new SelectionListener(){
866 public void widgetDefaultSelected(SelectionEvent e
) {}
867 public void widgetSelected(SelectionEvent e
) {
868 tagText
.removeModifyListener(updateListener
);
869 switch (tagCombo
.getSelectionIndex()) {
871 tagLabel
.setVisible(false);
872 tagText
.setVisible(false);
873 actionCombo
.setVisible(false);
876 tagLabel
.setText("format:");
877 tagLabel
.setVisible(true);
878 tagText
.setVisible(true);
879 tagText
.addModifyListener(updateListener
);
880 actionCombo
.setVisible(true);
883 tagLabel
.setVisible(false);
884 tagText
.setVisible(false);
885 actionCombo
.setVisible(true);
888 tagLabel
.setText("tag name:");
889 tagLabel
.setVisible(true);
890 if (tagText
.getText().trim().length() == 0) {
891 tagText
.setText(elementNameText
.getText().trim());
893 tagText
.setVisible(true);
894 tagText
.addModifyListener(updateListener
);
895 actionCombo
.setVisible(true);
898 tagComposite
.layout();
903 tagLabel
= new Label(tagComposite
, SWT
.NULL
);
904 tagLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
906 tagText
= new Text(tagComposite
, SWT
.BORDER
| SWT
.SINGLE
);
907 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
909 tagText
.setLayoutData(gd
);
911 actionCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
912 actionCombo
.setItems(new String
[] {"Set", "Append", "Append with |"});
913 actionCombo
.select(inputElement
.inputAction
);
914 actionCombo
.addSelectionListener(updateListener
);
916 if (inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
918 tagLabel
.setVisible(false);
919 tagText
.setVisible(false);
920 actionCombo
.setVisible(false);
921 } else if (inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_TIMESTAMP
)) {
923 tagLabel
.setText("format:");
924 tagText
.setText(inputElement
.inputFormat
);
925 tagText
.addModifyListener(updateListener
);
926 } else if (inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_MESSAGE
)) {
928 tagLabel
.setVisible(false);
929 tagText
.setVisible(false);
932 tagLabel
.setText("tag name:");
933 tagText
.setText(inputElement
.inputName
);
934 tagText
.addModifyListener(updateListener
);
938 if (inputElement
.attributes
!= null) {
939 for (InputAttribute inputAttribute
: inputElement
.attributes
) {
940 Attribute attribute
= new Attribute(group
, this, inputAttribute
, attributes
.size()+1);
941 attributes
.add(attribute
);
948 private void updatePreview() {
949 Element element
= getPreviewElement(inputElement
);
950 if (inputElement
.parentElement
!= null) { // no preview text for document element
951 previewText
.setText("*no matching element*");
952 if (element
!= null) {
953 previewText
.setText(CustomXmlTrace
.parseElement(element
, new StringBuffer()).toString());
954 if (logEntryButton
.getSelection()) {
955 if (logEntryFound
== false) {
956 logEntryFound
= true;
959 logEntryButton
.setSelection(false); // remove nested log entry
962 if (tagCombo
.getText().equals(CustomXmlTraceDefinition
.TAG_TIMESTAMP
) && logEntriesCount
<= 1) {
963 String value
= previewText
.getText().trim();
964 if (value
.length() != 0) {
965 if (actionCombo
.getSelectionIndex() == CustomXmlTraceDefinition
.ACTION_SET
) {
966 timeStampValue
= value
;
967 timeStampFormat
= tagText
.getText().trim();
968 } else if (actionCombo
.getSelectionIndex() == CustomXmlTraceDefinition
.ACTION_APPEND
) {
969 if (timeStampValue
!= null) {
970 timeStampValue
+= value
;
971 timeStampFormat
+= tagText
.getText().trim();
973 timeStampValue
= value
;
974 timeStampFormat
= tagText
.getText().trim();
976 } else if (actionCombo
.getSelectionIndex() == CustomXmlTraceDefinition
.ACTION_APPEND_WITH_SEPARATOR
) {
977 if (timeStampValue
!= null) {
978 timeStampValue
+= " | " + value
;
979 timeStampFormat
+= " | " + tagText
.getText().trim();
981 timeStampValue
= value
;
982 timeStampFormat
= tagText
.getText().trim();
989 for (Attribute attribute
: attributes
) {
990 if (element
!= null) {
991 String value
= element
.getAttribute(attribute
.attributeNameText
.getText().trim());
992 if (value
.length() != 0) {
993 attribute
.previewText
.setText(value
);
994 if (attribute
.tagCombo
.getText().equals(CustomXmlTraceDefinition
.TAG_TIMESTAMP
) && logEntriesCount
<= 1) {
995 if (attribute
.actionCombo
.getSelectionIndex() == CustomXmlTraceDefinition
.ACTION_SET
) {
996 timeStampValue
= value
;
997 timeStampFormat
= attribute
.tagText
.getText().trim();
998 } else if (attribute
.actionCombo
.getSelectionIndex() == CustomXmlTraceDefinition
.ACTION_APPEND
) {
999 if (timeStampValue
!= null) {
1000 timeStampValue
+= value
;
1001 timeStampFormat
+= attribute
.tagText
.getText().trim();
1003 timeStampValue
= value
;
1004 timeStampFormat
= attribute
.tagText
.getText().trim();
1006 } else if (attribute
.actionCombo
.getSelectionIndex() == CustomXmlTraceDefinition
.ACTION_APPEND_WITH_SEPARATOR
) {
1007 if (timeStampValue
!= null) {
1008 timeStampValue
+= " | " + value
;
1009 timeStampFormat
+= " | " + attribute
.tagText
.getText().trim();
1011 timeStampValue
= value
;
1012 timeStampFormat
= attribute
.tagText
.getText().trim();
1017 attribute
.previewText
.setText("*no matching attribute*");
1020 attribute
.previewText
.setText("*no matching element*");
1023 for (ElementNode child
: childElements
) {
1024 child
.updatePreview();
1026 if (logEntryButton
!= null && logEntryButton
.getSelection()) {
1027 logEntryFound
= false;
1031 private void createAddButton() {
1032 fillerLabel
= new Label(group
, SWT
.NONE
);
1034 addAttributeComposite
= new Composite(group
, SWT
.NONE
);
1035 addAttributeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1036 GridLayout addAttributeLayout
= new GridLayout(2, false);
1037 addAttributeLayout
.marginHeight
= 0;
1038 addAttributeLayout
.marginWidth
= 0;
1039 addAttributeComposite
.setLayout(addAttributeLayout
);
1041 addAttributeButton
= new Button(addAttributeComposite
, SWT
.PUSH
);
1042 addAttributeButton
.setImage(addImage
);
1043 addAttributeButton
.setToolTipText("Add attribute");
1044 addAttributeButton
.addSelectionListener(new SelectionAdapter() {
1046 public void widgetSelected(SelectionEvent e
) {
1048 String attributeName
= getAttributeNameSuggestion(inputElement
);
1049 InputAttribute inputAttribute
= new InputAttribute(attributeName
, attributeName
, 0, "");
1050 attributes
.add(new Attribute(group
, ElementNode
.this, inputAttribute
, attributes
.size()+1));
1052 elementContainer
.layout();
1053 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-1);
1054 group
.getParent().layout();
1060 addAttributeLabel
= new Label(addAttributeComposite
, SWT
.NULL
);
1061 addAttributeLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1062 addAttributeLabel
.setText("New attribute");
1065 private void removeAddButton() {
1066 fillerLabel
.dispose();
1067 addAttributeComposite
.dispose();
1070 private void removeAttribute(int attributeNumber
) {
1071 if (--attributeNumber
< attributes
.size()) {
1072 attributes
.remove(attributeNumber
).dispose();
1073 for (int i
= attributeNumber
; i
< attributes
.size(); i
++) {
1074 attributes
.get(i
).setAttributeNumber(i
+1);
1076 elementContainer
.layout();
1077 elementScrolledComposite
.setMinSize(elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, elementContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-1);
1078 group
.getParent().layout();
1082 private void dispose() {
1086 private void extractInputs() {
1087 inputElement
.elementName
= elementNameText
.getText().trim();
1088 if (inputElement
.parentElement
!= null) {
1089 inputElement
.logEntry
= logEntryButton
.getSelection();
1090 if (tagCombo
.getText().equals(CustomXmlTraceDefinition
.TAG_OTHER
)) {
1091 inputElement
.inputName
= tagText
.getText().trim();
1093 inputElement
.inputName
= tagCombo
.getText();
1094 if (tagCombo
.getText().equals(CustomXmlTraceDefinition
.TAG_TIMESTAMP
)) {
1095 inputElement
.inputFormat
= tagText
.getText().trim();
1098 inputElement
.inputAction
= actionCombo
.getSelectionIndex();
1100 inputElement
.attributes
= new ArrayList
<InputAttribute
>(attributes
.size());
1101 for (int i
= 0; i
< attributes
.size(); i
++) {
1102 Attribute attribute
= attributes
.get(i
);
1103 InputAttribute inputAttribute
= new InputAttribute();
1104 inputAttribute
.attributeName
= attribute
.attributeNameText
.getText().trim();
1105 if (attribute
.tagCombo
.getText().equals(CustomXmlTraceDefinition
.TAG_OTHER
)) {
1106 inputAttribute
.inputName
= attribute
.tagText
.getText().trim();
1108 inputAttribute
.inputName
= attribute
.tagCombo
.getText();
1109 if (attribute
.tagCombo
.getText().equals(CustomXmlTraceDefinition
.TAG_TIMESTAMP
)) {
1110 inputAttribute
.inputFormat
= attribute
.tagText
.getText().trim();
1113 inputAttribute
.inputAction
= attribute
.actionCombo
.getSelectionIndex();
1114 inputElement
.addAttribute(inputAttribute
);
1119 private class Attribute
{
1120 ElementNode element
;
1121 int attributeNumber
;
1123 // children of parent (must be disposed)
1124 Composite labelComposite
;
1125 Composite attributeComposite
;
1127 Composite tagComposite
;
1129 // children of labelComposite
1130 Label attributeLabel
;
1132 // children of attributeComposite
1133 Text attributeNameText
;
1136 // children of tagComposite
1142 public Attribute(Composite parent
, ElementNode element
, InputAttribute inputAttribute
, int attributeNumber
) {
1143 this.element
= element
;
1144 this.attributeNumber
= attributeNumber
;
1146 labelComposite
= new Composite(parent
, SWT
.FILL
);
1147 GridLayout labelLayout
= new GridLayout(2, false);
1148 labelLayout
.marginWidth
= 0;
1149 labelLayout
.marginHeight
= 0;
1150 labelComposite
.setLayout(labelLayout
);
1151 labelComposite
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1153 Button deleteButton
= new Button(labelComposite
, SWT
.PUSH
);
1154 deleteButton
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1155 deleteButton
.setImage(deleteImage
);
1156 deleteButton
.setToolTipText("Remove attribute");
1157 deleteButton
.addSelectionListener(new SelectionAdapter() {
1159 public void widgetSelected(SelectionEvent e
) {
1160 Attribute
.this.element
.removeAttribute(Attribute
.this.attributeNumber
);
1166 attributeLabel
= new Label(labelComposite
, SWT
.NULL
);
1167 attributeLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1168 attributeLabel
.setText("Attribute");
1170 attributeComposite
= new Composite(parent
, SWT
.FILL
);
1171 GridLayout attributeLayout
= new GridLayout(4, false);
1172 attributeLayout
.marginWidth
= 0;
1173 attributeLayout
.marginHeight
= 0;
1174 attributeComposite
.setLayout(attributeLayout
);
1175 attributeComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1177 Label nameLabel
= new Label(attributeComposite
, SWT
.NONE
);
1178 nameLabel
.setText("name:");
1180 attributeNameText
= new Text(attributeComposite
, SWT
.BORDER
| SWT
.SINGLE
);
1181 attributeNameText
.setLayoutData(new GridData(120, SWT
.DEFAULT
));
1182 attributeNameText
.setText(inputAttribute
.attributeName
);
1183 attributeNameText
.addModifyListener(updateListener
);
1185 Label previewLabel
= new Label(attributeComposite
, SWT
.NONE
);
1186 previewLabel
.setText("Preview:");
1188 previewText
= new Text(attributeComposite
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.READ_ONLY
);
1189 GridData gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
1191 previewText
.setLayoutData(gd
);
1192 previewText
.setText("*no match*");
1193 previewText
.setBackground(COLOR_WIDGET_BACKGROUND
);
1195 filler
= new Label(parent
, SWT
.NULL
);
1197 tagComposite
= new Composite(parent
, SWT
.FILL
);
1198 GridLayout tagLayout
= new GridLayout(4, false);
1199 tagLayout
.marginWidth
= 0;
1200 tagLayout
.marginHeight
= 0;
1201 tagComposite
.setLayout(tagLayout
);
1202 tagComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
1204 tagCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
1205 tagCombo
.setItems(new String
[] {CustomXmlTraceDefinition
.TAG_TIMESTAMP
,
1206 CustomXmlTraceDefinition
.TAG_MESSAGE
,
1207 CustomXmlTraceDefinition
.TAG_OTHER
});
1208 tagCombo
.select(2); //Other
1209 tagCombo
.addSelectionListener(new SelectionListener(){
1210 public void widgetDefaultSelected(SelectionEvent e
) {}
1211 public void widgetSelected(SelectionEvent e
) {
1212 tagText
.removeModifyListener(updateListener
);
1213 switch (tagCombo
.getSelectionIndex()) {
1214 case 0: //Time Stamp
1215 tagLabel
.setText("format:");
1216 tagLabel
.setVisible(true);
1217 tagText
.setVisible(true);
1218 tagText
.addModifyListener(updateListener
);
1221 tagLabel
.setVisible(false);
1222 tagText
.setVisible(false);
1225 tagLabel
.setText("tag name:");
1226 tagLabel
.setVisible(true);
1227 if (tagText
.getText().trim().length() == 0) {
1228 tagText
.setText(attributeNameText
.getText().trim());
1230 tagText
.setVisible(true);
1231 tagText
.addModifyListener(updateListener
);
1234 tagComposite
.layout();
1239 tagLabel
= new Label(tagComposite
, SWT
.NULL
);
1240 tagLabel
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
1242 tagText
= new Text(tagComposite
, SWT
.BORDER
| SWT
.SINGLE
);
1243 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
1245 tagText
.setLayoutData(gd
);
1246 tagText
.setText(attributeNameText
.getText());
1248 actionCombo
= new Combo(tagComposite
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
1249 actionCombo
.setItems(new String
[] {"Set", "Append", "Append with |"});
1250 actionCombo
.select(inputAttribute
.inputAction
);
1251 actionCombo
.addSelectionListener(updateListener
);
1253 if (inputAttribute
.inputName
.equals(CustomXmlTraceDefinition
.TAG_TIMESTAMP
)) {
1255 tagLabel
.setText("format:");
1256 tagText
.setText(inputAttribute
.inputFormat
);
1257 tagText
.addModifyListener(updateListener
);
1258 } else if (inputAttribute
.inputName
.equals(CustomXmlTraceDefinition
.TAG_MESSAGE
)) {
1260 tagLabel
.setVisible(false);
1261 tagText
.setVisible(false);
1264 tagLabel
.setText("tag name:");
1265 tagText
.setText(inputAttribute
.inputName
);
1266 tagText
.addModifyListener(updateListener
);
1270 private void dispose() {
1271 labelComposite
.dispose();
1272 attributeComposite
.dispose();
1274 tagComposite
.dispose();
1277 private void setAttributeNumber(int attributeNumber
) {
1278 this.attributeNumber
= attributeNumber
;
1279 labelComposite
.layout();
1283 private Element
getPreviewElement(InputElement inputElement
) {
1284 Element element
= documentElement
;
1285 if (element
!= null) {
1286 if (!documentElement
.getNodeName().equals(definition
.rootInputElement
.elementName
)) {
1289 ArrayList
<String
> elementNames
= new ArrayList
<String
>();
1290 while (inputElement
!= null) {
1291 elementNames
.add(inputElement
.elementName
);
1292 inputElement
= inputElement
.parentElement
;
1294 for (int i
= elementNames
.size() - 1; --i
>= 0;) {
1295 NodeList childList
= element
.getChildNodes();
1297 for (int j
= 0; j
< childList
.getLength(); j
++) {
1298 Node child
= childList
.item(j
);
1299 if (child
instanceof Element
&& child
.getNodeName().equals(elementNames
.get(i
))) {
1300 element
= (Element
)child
;
1304 if (element
== null) {
1308 if (element
!= null) {
1315 private String
getChildNameSuggestion(InputElement inputElement
) {
1316 if (inputElement
== null) {
1317 if (documentElement
!= null) {
1318 return documentElement
.getNodeName();
1321 Element element
= getPreviewElement(inputElement
);
1322 if (element
!= null) {
1323 NodeList childNodes
= element
.getChildNodes();
1324 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
1325 Node node
= childNodes
.item(i
);
1326 if (node
instanceof Element
) {
1327 boolean unused
= true;
1328 if (inputElement
.childElements
!= null) {
1329 for (InputElement child
: inputElement
.childElements
) {
1330 if (child
.elementName
.equals(node
.getNodeName())) {
1337 return node
.getNodeName();
1346 private String
getAttributeNameSuggestion(InputElement inputElement
) {
1347 Element element
= getPreviewElement(inputElement
);
1348 if (element
!= null) {
1349 NamedNodeMap attributeMap
= element
.getAttributes();
1350 for (int i
= 0; i
< attributeMap
.getLength(); i
++) {
1351 Node node
= attributeMap
.item(i
);
1352 boolean unused
= true;
1353 if (inputElement
.attributes
!= null) {
1354 for (InputAttribute attribute
: inputElement
.attributes
) {
1355 if (attribute
.attributeName
.equals(node
.getNodeName())) {
1362 return node
.getNodeName();
1369 private void validate() {
1370 definition
.definitionName
= logtypeText
.getText().trim();
1371 definition
.timeStampOutputFormat
= timeStampOutputFormatText
.getText().trim();
1373 if (selectedElement
!= null) {
1374 selectedElement
.extractInputs();
1375 treeViewer
.refresh();
1378 StringBuffer errors
= new StringBuffer();
1380 if (definition
.definitionName
.length() == 0) {
1381 errors
.append("Enter a name for the new log type. ");
1382 logtypeText
.setBackground(COLOR_LIGHT_RED
);
1384 logtypeText
.setBackground(COLOR_TEXT_BACKGROUND
);
1385 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
1386 if (definition
.definitionName
.equals(def
.definitionName
)) {
1387 if (editDefinitionName
== null || ! editDefinitionName
.equals(definition
.definitionName
)) {
1388 errors
.append("The log type name already exists. ");
1389 logtypeText
.setBackground(COLOR_LIGHT_RED
);
1396 if (definition
.rootInputElement
== null) {
1397 errors
.append("Add a document element. ");
1400 if (definition
.rootInputElement
!= null) {
1401 logEntryFound
= false;
1402 logEntryNestedCount
= 0;
1403 timeStampFound
= false;
1405 errors
.append(validateElement(definition
.rootInputElement
));
1407 if ((definition
.rootInputElement
.attributes
!= null && definition
.rootInputElement
.attributes
.size() != 0) ||
1408 (definition
.rootInputElement
.childElements
!= null && definition
.rootInputElement
.childElements
.size() != 0) || errors
.length() == 0) {
1409 if (!logEntryFound
) {
1410 errors
.append("Identify a Log Entry element. ");
1413 if (timeStampFound
) {
1414 if (timeStampOutputFormatText
.getText().trim().length() == 0) {
1415 errors
.append("Enter the output format for the Time Stamp field. ");
1416 timeStampOutputFormatText
.setBackground(COLOR_LIGHT_RED
);
1419 new SimpleDateFormat(timeStampOutputFormatText
.getText().trim());
1420 timeStampOutputFormatText
.setBackground(COLOR_TEXT_BACKGROUND
);
1421 } catch (IllegalArgumentException e
) {
1422 errors
.append("Enter a valid output format for the Time Stamp field. ");
1423 timeStampOutputFormatText
.setBackground(COLOR_LIGHT_RED
);
1427 timeStampPreviewText
.setText("*no time stamp element or attribute*");
1431 timeStampPreviewText
.setText("*no time stamp element or attribute*");
1434 if (errors
.length() == 0) {
1435 setDescription(defaultDescription
);
1436 setPageComplete(true);
1438 setDescription(errors
.toString());
1439 setPageComplete(false);
1443 public StringBuffer
validateElement(InputElement inputElement
) {
1444 StringBuffer errors
= new StringBuffer();
1445 ElementNode elementNode
= null;
1446 if (selectedElement
!= null && selectedElement
.inputElement
.equals(inputElement
)) elementNode
= selectedElement
;
1447 if (inputElement
== definition
.rootInputElement
) {
1448 if (inputElement
.elementName
.length() == 0) {
1449 errors
.append("Enter a name for the document element. ");
1450 if (elementNode
!= null) elementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1452 if (elementNode
!= null) elementNode
.elementNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1455 if (inputElement
!= definition
.rootInputElement
) {
1456 if (inputElement
.logEntry
) {
1457 logEntryFound
= true;
1458 logEntryNestedCount
++;
1460 if (inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_TIMESTAMP
)) {
1461 timeStampFound
= true;
1462 if (inputElement
.inputFormat
.length() == 0) {
1463 errors
.append("Enter the input format for the Time Stamp (Element " + getName(inputElement
) + "). ");
1464 if (elementNode
!= null) elementNode
.tagText
.setBackground(COLOR_LIGHT_RED
);
1467 new SimpleDateFormat(inputElement
.inputFormat
);
1468 if (elementNode
!= null) elementNode
.tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1469 } catch (IllegalArgumentException e
) {
1470 errors
.append("Enter a valid input format for the Time Stamp (Element " + getName(inputElement
) + "). ");
1471 if (elementNode
!= null) elementNode
.tagText
.setBackground(COLOR_LIGHT_RED
);
1474 } else if (inputElement
.inputName
.length() == 0) {
1475 errors
.append("Enter a name for the input . ");
1476 if (elementNode
!= null) elementNode
.tagText
.setBackground(COLOR_LIGHT_RED
);
1478 if (elementNode
!= null) elementNode
.tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1481 if (inputElement
.attributes
!= null) {
1482 if (elementNode
!= null) {
1483 for (Attribute attribute
: elementNode
.attributes
) {
1484 attribute
.attributeNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1487 for (int i
= 0; i
< inputElement
.attributes
.size(); i
++) {
1488 InputAttribute attribute
= inputElement
.attributes
.get(i
);
1489 boolean duplicate
= false;
1490 for (int j
= i
+ 1; j
< inputElement
.attributes
.size(); j
++) {
1491 InputAttribute otherAttribute
= inputElement
.attributes
.get(j
);
1492 if (otherAttribute
.attributeName
.equals(attribute
.attributeName
)) {
1494 if (elementNode
!= null) {
1495 elementNode
.attributes
.get(j
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1499 if (attribute
.attributeName
.length() == 0) {
1500 errors
.append("Enter a name for the attribute (Attribute " + getName(inputElement
) + ": ?). ");
1501 if (elementNode
!= null) elementNode
.attributes
.get(i
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1502 } else if (duplicate
) {
1503 errors
.append("Duplicate attribute names (Attribute " + getName(attribute
, inputElement
) +"). ");
1504 if (elementNode
!= null) elementNode
.attributes
.get(i
).attributeNameText
.setBackground(COLOR_LIGHT_RED
);
1506 if (attribute
.inputName
.equals(CustomXmlTraceDefinition
.TAG_TIMESTAMP
)) {
1507 timeStampFound
= true;
1508 if (attribute
.inputFormat
.length() == 0) {
1509 errors
.append("Enter the input format for the Time Stamp (Attribute " + getName(attribute
, inputElement
) +"). ");
1510 if (elementNode
!= null) elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_LIGHT_RED
);
1513 new SimpleDateFormat(attribute
.inputFormat
);
1514 if (elementNode
!= null) elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1515 } catch (IllegalArgumentException e
) {
1516 errors
.append("Enter a valid input format for the Time Stamp (Attribute " + getName(attribute
, inputElement
) +"). ");
1517 if (elementNode
!= null) elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_LIGHT_RED
);
1520 } else if (attribute
.inputName
.length() == 0) {
1521 errors
.append("Enter a name for the data group (Attribute " + getName(attribute
, inputElement
) +"). ");
1522 if (elementNode
!= null) elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_LIGHT_RED
);
1524 if (elementNode
!= null) elementNode
.attributes
.get(i
).tagText
.setBackground(COLOR_TEXT_BACKGROUND
);
1528 if (inputElement
.childElements
!= null) {
1529 for (InputElement child
: inputElement
.childElements
) {
1530 ElementNode childElementNode
= null;
1531 if (selectedElement
!= null && selectedElement
.inputElement
.equals(child
)) childElementNode
= selectedElement
;
1532 if (childElementNode
!= null) childElementNode
.elementNameText
.setBackground(COLOR_TEXT_BACKGROUND
);
1534 for (int i
= 0; i
< inputElement
.childElements
.size(); i
++) {
1535 InputElement child
= inputElement
.childElements
.get(i
);
1536 ElementNode childElementNode
= null;
1537 if (selectedElement
!= null && selectedElement
.inputElement
.equals(child
)) childElementNode
= selectedElement
;
1538 if (child
.elementName
.length() == 0) {
1539 errors
.append("Enter a name for the element (Element " + getName(child
) + "). ");
1540 if (childElementNode
!= null) childElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1542 boolean duplicate
= false;
1543 for (int j
= i
+ 1; j
< inputElement
.childElements
.size(); j
++) {
1544 InputElement otherChild
= inputElement
.childElements
.get(j
);
1545 if (otherChild
.elementName
.equals(child
.elementName
)) {
1547 ElementNode otherChildElementNode
= null;
1548 if (selectedElement
!= null && selectedElement
.inputElement
.equals(otherChild
)) otherChildElementNode
= selectedElement
;
1549 if (otherChildElementNode
!= null) otherChildElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1553 errors
.append("Duplicate element names (Element " + getName(child
) + "). ");
1554 if (childElementNode
!= null) childElementNode
.elementNameText
.setBackground(COLOR_LIGHT_RED
);
1558 errors
.append(validateElement(child
));
1561 if (inputElement
.logEntry
) {
1562 logEntryNestedCount
--;
1567 public CustomXmlTraceDefinition
getDefinition() {
1571 public char[] getInputText() {
1572 return inputText
.getText().toCharArray();