2010-07-28 Francois Chouinard <fchouinard@gmail.com> Fix for Bug316349 + a bunch...
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / wizards / CustomXmlParserInputWizardPage.java
1 package org.eclipse.linuxtools.tmf.ui.wizards;
2
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;
13
14 import javax.xml.parsers.DocumentBuilder;
15 import javax.xml.parsers.DocumentBuilderFactory;
16 import javax.xml.parsers.ParserConfigurationException;
17
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;
73
74 public class CustomXmlParserInputWizardPage extends WizardPage {
75
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);
90
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;
117
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;
125
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";
131 } else {
132 setTitle("Edit Custom XML Parser");
133 defaultDescription = "Edit an existing custom parser for XML log files";
134 }
135 setDescription(defaultDescription);
136 this.selection = selection;
137 this.definition = definition;
138 if (definition != null) {
139 this.editDefinitionName = definition.definitionName;
140 }
141 }
142
143 public void createControl(Composite parent) {
144 container = new Composite(parent, SWT.NULL);
145 container.setLayout(new GridLayout());
146
147 updateListener = new UpdateListener();
148
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));
155
156 Label logtypeLabel = new Label(headerComposite, SWT.NULL);
157 logtypeLabel.setText("Log type:");
158
159 logtypeText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
160 logtypeText.setLayoutData(new GridData(120, SWT.DEFAULT));
161 logtypeText.addModifyListener(updateListener);
162
163 Label timeStampFormatLabel = new Label(headerComposite, SWT.NULL);
164 timeStampFormatLabel.setText("Time Stamp format:");
165
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);
170
171 Button dateFormatHelpButton = new Button(headerComposite, SWT.PUSH);
172 dateFormatHelpButton.setImage(helpImage);
173 dateFormatHelpButton.setToolTipText("Date Format Help");
174 dateFormatHelpButton.addSelectionListener(new SelectionAdapter() {
175 @Override
176 public void widgetSelected(SelectionEvent e) {
177 openHelpShell(SIMPLE_DATE_FORMAT_URL);
178 }
179 });
180
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:");
184
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*");
188
189 createButtonBar();
190
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));
194
195 SashForm hSash = new SashForm(vSash, SWT.HORIZONTAL);
196 hSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
197
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);
205
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();
211
212 treeScrolledComposite.setMinSize(treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
213
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();
218 gl.marginHeight = 1;
219 gl.marginWidth = 0;
220 elementContainer.setLayout(gl);
221 elementScrolledComposite.setContent(elementContainer);
222 elementScrolledComposite.setExpandHorizontal(true);
223 elementScrolledComposite.setExpandVertical(true);
224
225 if (definition == null) {
226 definition = new CustomXmlTraceDefinition();
227 }
228 loadDefinition(definition);
229 treeViewer.expandAll();
230 elementContainer.layout();
231
232 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);
233
234 hSash.setWeights(new int[] {1, 2});
235
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);
244 }
245
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);
251
252 Label previewLabel = new Label(sashBottom, SWT.NULL);
253 previewLabel.setText("Preview input");
254
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);
259
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));
264 } else {
265 fixedFont = new Font(Display.getCurrent(), new FontData("Monospace", 10, SWT.NORMAL));
266 }
267 }
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;
271 gd.widthHint = 800;
272 inputText.setLayoutData(gd);
273 inputText.setText(getSelectionText());
274 inputText.addModifyListener(new ModifyListener(){
275 public void modifyText(ModifyEvent e) {
276 parseXmlInput(inputText.getText());
277 }});
278 inputText.addModifyListener(updateListener);
279
280 vSash.setWeights(new int[] {hSash.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sashBottom.computeSize(SWT.DEFAULT, SWT.DEFAULT).y});
281
282 setControl(container);
283 }
284
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);
291
292 removeButton = new Button(buttonBar, SWT.PUSH);
293 removeButton.setImage(deleteImage);
294 removeButton.setToolTipText("Remove element");
295 removeButton.addSelectionListener(new SelectionAdapter() {
296 @Override
297 public void widgetSelected(SelectionEvent e) {
298 if (treeViewer.getSelection().isEmpty() || selectedElement == null) return;
299 removeElement();
300 InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
301 if (inputElement == definition.rootInputElement) {
302 definition.rootInputElement = null;
303 } else {
304 inputElement.parentElement.childElements.remove(inputElement);
305 }
306 treeViewer.refresh();
307 validate();
308 updatePreviews();
309 removeButton.setEnabled(false);
310 if (definition.rootInputElement == null) {
311 addChildButton.setEnabled(true);
312 addChildButton.setToolTipText("Add document element");
313 } else {
314 addChildButton.setEnabled(false);
315 }
316 addNextButton.setEnabled(false);
317 moveUpButton.setEnabled(false);
318 moveDownButton.setEnabled(false);
319 }
320 });
321
322 addChildButton = new Button(buttonBar, SWT.PUSH);
323 addChildButton.setImage(addChildImage);
324 addChildButton.setToolTipText("Add child element");
325 addChildButton.addSelectionListener(new SelectionAdapter() {
326 @Override
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()) {
333 return;
334 } else {
335 InputElement parentInputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
336 parentInputElement.addChild(inputElement);
337 inputElement.elementName = getChildNameSuggestion(parentInputElement);
338 }
339 treeViewer.refresh();
340 treeViewer.setSelection(new StructuredSelection(inputElement), true);
341 }
342 });
343
344 addNextButton = new Button(buttonBar, SWT.PUSH);
345 addNextButton.setImage(addNextImage);
346 addNextButton.setToolTipText("Add next element");
347 addNextButton.addSelectionListener(new SelectionAdapter() {
348 @Override
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()) {
355 return;
356 } else {
357 InputElement previousInputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
358 if (previousInputElement == definition.rootInputElement) {
359 return;
360 } else {
361 previousInputElement.addNext(inputElement);
362 inputElement.elementName = getChildNameSuggestion(inputElement.parentElement);
363 }
364 }
365 treeViewer.refresh();
366 treeViewer.setSelection(new StructuredSelection(inputElement), true);
367 }
368 });
369
370 feelingLuckyButton = new Button(buttonBar, SWT.PUSH);
371 feelingLuckyButton.setImage(addManyImage);
372 feelingLuckyButton.setToolTipText("Feeling lucky");
373 feelingLuckyButton.addSelectionListener(new SelectionAdapter() {
374 @Override
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);
382 }
383 } else if (treeViewer.getSelection().isEmpty()) {
384 return;
385 } else {
386 inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
387 feelingLucky(inputElement);
388 }
389 treeViewer.refresh();
390 treeViewer.setSelection(new StructuredSelection(inputElement), true);
391 treeViewer.expandToLevel(inputElement, TreeViewer.ALL_LEVELS);
392 }
393 });
394
395 moveUpButton = new Button(buttonBar, SWT.PUSH);
396 moveUpButton.setImage(moveUpImage);
397 moveUpButton.setToolTipText("Move up");
398 moveUpButton.addSelectionListener(new SelectionAdapter() {
399 @Override
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) {
404 return;
405 } else {
406 inputElement.moveUp();
407 }
408 treeViewer.refresh();
409 validate();
410 updatePreviews();
411 }
412 });
413
414 moveDownButton = new Button(buttonBar, SWT.PUSH);
415 moveDownButton.setImage(moveDownImage);
416 moveDownButton.setToolTipText("Move down");
417 moveDownButton.addSelectionListener(new SelectionAdapter() {
418 @Override
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) {
423 return;
424 } else {
425 inputElement.moveDown();
426 }
427 treeViewer.refresh();
428 validate();
429 updatePreviews();
430 }
431 });
432 }
433
434 private void feelingLucky(InputElement inputElement) {
435 while (true) {
436 String attributeName = getAttributeNameSuggestion(inputElement);
437 if (attributeName.length() == 0) {
438 break;
439 } else {
440 InputAttribute attribute = new InputAttribute(attributeName, attributeName, 0, "");
441 inputElement.addAttribute(attribute);
442 }
443 }
444 while (true) {
445 String childName = getChildNameSuggestion(inputElement);
446 if (childName.length() == 0) {
447 break;
448 } else {
449 InputElement childElement = new InputElement(childName, false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null);
450 inputElement.addChild(childElement);
451 feelingLucky(childElement);
452 }
453 }
454 }
455
456 private class InputElementTreeNodeContentProvider implements ITreeContentProvider {
457
458 public Object[] getElements(Object inputElement) {
459 CustomXmlTraceDefinition def = (CustomXmlTraceDefinition) inputElement;
460 if (def.rootInputElement != null) {
461 return new Object[] {def.rootInputElement};
462 } else {
463 return new Object[0];
464 }
465 }
466
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();
471 }
472
473 public boolean hasChildren(Object element) {
474 InputElement inputElement = (InputElement) element;
475 return (inputElement.childElements != null && inputElement.childElements.size() > 0);
476 }
477
478 public void dispose() {
479 }
480
481 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
482 }
483
484 public Object getParent(Object element) {
485 InputElement inputElement = (InputElement) element;
486 return inputElement.parentElement;
487 }
488 }
489
490 private class InputElementTreeLabelProvider extends ColumnLabelProvider {
491
492 @Override
493 public Image getImage(Object element) {
494 return elementImage;
495 }
496
497 @Override
498 public String getText(Object element) {
499 InputElement inputElement = (InputElement) element;
500 return (inputElement.elementName.trim().length() == 0) ? "?" : inputElement.elementName;
501 }
502 }
503
504 private class InputElementTreeSelectionChangedListener implements ISelectionChangedListener {
505 public void selectionChanged(SelectionChangedEvent event) {
506 if (selectedElement != null) {
507 selectedElement.dispose();
508 }
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);
515 container.layout();
516 validate();
517 updatePreviews();
518 removeButton.setEnabled(true);
519 addChildButton.setEnabled(true);
520 addChildButton.setToolTipText("Add child element");
521 if (definition.rootInputElement == inputElement) {
522 addNextButton.setEnabled(false);
523 } else {
524 addNextButton.setEnabled(true);
525 }
526 moveUpButton.setEnabled(true);
527 moveDownButton.setEnabled(true);
528 } else {
529 removeButton.setEnabled(false);
530 if (definition.rootInputElement == null) {
531 addChildButton.setEnabled(true);
532 addChildButton.setToolTipText("Add document element");
533 } else {
534 addChildButton.setEnabled(false);
535 }
536 addNextButton.setEnabled(false);
537 moveUpButton.setEnabled(false);
538 moveDownButton.setEnabled(false);
539 }
540 }
541 }
542
543 /* (non-Javadoc)
544 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
545 */
546 @Override
547 public void dispose() {
548 if (fixedFont != null) {
549 fixedFont.dispose();
550 fixedFont = null;
551 }
552 super.dispose();
553 }
554
555 private void loadDefinition(CustomXmlTraceDefinition def) {
556 logtypeText.setText(def.definitionName);
557 timeStampOutputFormatText.setText(def.timeStampOutputFormat);
558 treeViewer.setInput(def);
559
560 if (def.rootInputElement != null) {
561 treeViewer.setSelection(new StructuredSelection(def.rootInputElement));
562 }
563 }
564
565 private String getName(InputElement inputElement) {
566 String name = (inputElement.elementName.trim().length() == 0) ? "?" : inputElement.elementName.trim();
567 if (inputElement.parentElement == null) {
568 return name;
569 }
570 return getName(inputElement.parentElement) + " : " + name;
571 }
572
573 private String getName(InputAttribute inputAttribute, InputElement inputElement) {
574 String name = (inputAttribute.attributeName.trim().length() == 0) ? "?" : inputAttribute.attributeName.trim();
575 return getName(inputElement) + " : " + name;
576 }
577
578 /* (non-Javadoc)
579 * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
580 */
581 @Override
582 public void setVisible(boolean visible) {
583 if (visible) {
584 validate();
585 updatePreviews();
586 }
587 super.setVisible(visible);
588 }
589
590 public List<String> getInputNames() {
591 return getInputNames(definition.rootInputElement);
592 }
593
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);
600 }
601 }
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);
607 }
608 }
609 }
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);
615 }
616 }
617 }
618 }
619 return inputs;
620 }
621
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);
627 container.layout();
628 }
629
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;
636 try {
637 inputStream = file.getContents();
638 } catch (CoreException e) {
639 return "";
640 }
641 }
642 }
643 if (inputStream != null) {
644 try {
645 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
646 StringBuilder sb = new StringBuilder();
647 String line = null;
648 while ((line = reader.readLine()) != null) {
649 sb.append(line + "\n");
650 }
651 parseXmlInput(sb.toString());
652 return sb.toString();
653 } catch (IOException e) {
654 return "";
655 }
656 }
657 return "";
658 }
659
660 private void parseXmlInput(final String string) {
661 try {
662 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
663 DocumentBuilder db = dbf.newDocumentBuilder();
664
665 // The following allows xml parsing without access to the dtd
666 EntityResolver resolver = new EntityResolver () {
667 public InputSource resolveEntity (String publicId, String systemId) {
668 String empty = "";
669 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
670 return new InputSource(bais);
671 }
672 };
673 db.setEntityResolver(resolver);
674
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);
684 }
685 throw saxparseexception;
686 }});
687
688 errorText.setVisible(false);
689 Document doc = null;
690 doc = db.parse(new ByteArrayInputStream(string.getBytes()));
691 documentElement = doc.getDocumentElement();
692 } catch (ParserConfigurationException e) {
693 e.printStackTrace();
694 documentElement = null;
695 } catch (SAXException e) {
696 documentElement = null;
697 } catch (IOException e) {
698 e.printStackTrace();
699 documentElement = null;
700 }
701 }
702
703 private void updatePreviews() {
704 updatePreviews(false);
705 }
706
707 private void updatePreviews(boolean updateAll) {
708 if (inputText == null) {
709 // early update during construction
710 return;
711 }
712 inputText.setStyleRanges(new StyleRange[] {});
713 if (selectedElement == null) {
714 return;
715 }
716
717 timeStampValue = null;
718 timeStampFormat = null;
719 logEntriesCount = 0;
720 logEntryFound = false;
721
722 selectedElement.updatePreview();
723
724 if (timeStampValue != null && timeStampFormat != null) {
725 try {
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]");
734 }
735 } else {
736 timeStampPreviewText.setText("*no matching time stamp*");
737 }
738 }
739
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);
745 }
746 return;
747 }
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);
754 }
755 });
756 helpBrowser.setBounds(0,0,600,400);
757 helpShell.pack();
758 helpShell.open();
759 helpBrowser.setUrl(url);
760 }
761
762 private class UpdateListener implements ModifyListener, SelectionListener {
763
764 public void modifyText(ModifyEvent e) {
765 validate();
766 updatePreviews();
767 }
768
769 public void widgetDefaultSelected(SelectionEvent e) {
770 validate();
771 updatePreviews();
772 }
773
774 public void widgetSelected(SelectionEvent e) {
775 validate();
776 updatePreviews();
777 }
778
779 }
780
781 private class ElementNode {
782 final InputElement inputElement;
783 final Group group;
784 ArrayList<Attribute> attributes = new ArrayList<Attribute>();
785 ArrayList<ElementNode> childElements = new ArrayList<ElementNode>();
786 Text elementNameText;
787 Composite tagComposite;
788 Combo tagCombo;
789 Label tagLabel;
790 Text tagText;
791 Combo actionCombo;
792 Label previewLabel;
793 Text previewText;
794 Button logEntryButton;
795 Label fillerLabel;
796 Composite addAttributeComposite;
797 Button addAttributeButton;
798 Label addAttributeLabel;
799
800 public ElementNode(Composite parent, InputElement inputElement) {
801 this.inputElement = inputElement;
802
803 group = new Group(parent, SWT.NONE);
804 GridLayout gl = new GridLayout(2, false);
805 gl.marginHeight = 0;
806 group.setLayout(gl);
807 group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
808 group.setText(getName(inputElement));
809
810 Label label = new Label(group, SWT.NULL);
811 label.setText("Element name:");
812 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
813
814 elementNameText = new Text(group, SWT.BORDER | SWT.SINGLE);
815 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
816 gd.widthHint = 0;
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));
822 }});
823 elementNameText.setText(inputElement.elementName);
824 elementNameText.addModifyListener(updateListener);
825
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:");
830
831 previewText = new Text(group, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
832 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
833 gd.widthHint = 0;
834 previewText.setLayoutData(gd);
835 previewText.setText("*no matching element*");
836 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
837
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;
848 }
849 }});
850 logEntryButton.addSelectionListener(updateListener);
851
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));
858
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()) {
870 case 0: //Ignore
871 tagLabel.setVisible(false);
872 tagText.setVisible(false);
873 actionCombo.setVisible(false);
874 break;
875 case 1: //Time Stamp
876 tagLabel.setText("format:");
877 tagLabel.setVisible(true);
878 tagText.setVisible(true);
879 tagText.addModifyListener(updateListener);
880 actionCombo.setVisible(true);
881 break;
882 case 2: //Message
883 tagLabel.setVisible(false);
884 tagText.setVisible(false);
885 actionCombo.setVisible(true);
886 break;
887 case 3: //Other
888 tagLabel.setText("tag name:");
889 tagLabel.setVisible(true);
890 if (tagText.getText().trim().length() == 0) {
891 tagText.setText(elementNameText.getText().trim());
892 }
893 tagText.setVisible(true);
894 tagText.addModifyListener(updateListener);
895 actionCombo.setVisible(true);
896 break;
897 }
898 tagComposite.layout();
899 validate();
900 updatePreviews();
901 }});
902
903 tagLabel = new Label(tagComposite, SWT.NULL);
904 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
905
906 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
907 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
908 gd.widthHint = 0;
909 tagText.setLayoutData(gd);
910
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);
915
916 if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {
917 tagCombo.select(0);
918 tagLabel.setVisible(false);
919 tagText.setVisible(false);
920 actionCombo.setVisible(false);
921 } else if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
922 tagCombo.select(1);
923 tagLabel.setText("format:");
924 tagText.setText(inputElement.inputFormat);
925 tagText.addModifyListener(updateListener);
926 } else if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_MESSAGE)) {
927 tagCombo.select(2);
928 tagLabel.setVisible(false);
929 tagText.setVisible(false);
930 } else {
931 tagCombo.select(3);
932 tagLabel.setText("tag name:");
933 tagText.setText(inputElement.inputName);
934 tagText.addModifyListener(updateListener);
935 }
936 }
937
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);
942 }
943 }
944
945 createAddButton();
946 }
947
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;
957 logEntriesCount++;
958 } else {
959 logEntryButton.setSelection(false); // remove nested log entry
960 }
961 }
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();
972 } else {
973 timeStampValue = value;
974 timeStampFormat = tagText.getText().trim();
975 }
976 } else if (actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
977 if (timeStampValue != null) {
978 timeStampValue += " | " + value;
979 timeStampFormat += " | " + tagText.getText().trim();
980 } else {
981 timeStampValue = value;
982 timeStampFormat = tagText.getText().trim();
983 }
984 }
985 }
986 }
987 }
988 }
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();
1002 } else {
1003 timeStampValue = value;
1004 timeStampFormat = attribute.tagText.getText().trim();
1005 }
1006 } else if (attribute.actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
1007 if (timeStampValue != null) {
1008 timeStampValue += " | " + value;
1009 timeStampFormat += " | " + attribute.tagText.getText().trim();
1010 } else {
1011 timeStampValue = value;
1012 timeStampFormat = attribute.tagText.getText().trim();
1013 }
1014 }
1015 }
1016 } else {
1017 attribute.previewText.setText("*no matching attribute*");
1018 }
1019 } else {
1020 attribute.previewText.setText("*no matching element*");
1021 }
1022 }
1023 for (ElementNode child : childElements) {
1024 child.updatePreview();
1025 }
1026 if (logEntryButton != null && logEntryButton.getSelection()) {
1027 logEntryFound = false;
1028 }
1029 }
1030
1031 private void createAddButton() {
1032 fillerLabel = new Label(group, SWT.NONE);
1033
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);
1040
1041 addAttributeButton = new Button(addAttributeComposite, SWT.PUSH);
1042 addAttributeButton.setImage(addImage);
1043 addAttributeButton.setToolTipText("Add attribute");
1044 addAttributeButton.addSelectionListener(new SelectionAdapter() {
1045 @Override
1046 public void widgetSelected(SelectionEvent e) {
1047 removeAddButton();
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));
1051 createAddButton();
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();
1055 validate();
1056 updatePreviews();
1057 }
1058 });
1059
1060 addAttributeLabel = new Label(addAttributeComposite, SWT.NULL);
1061 addAttributeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1062 addAttributeLabel.setText("New attribute");
1063 }
1064
1065 private void removeAddButton() {
1066 fillerLabel.dispose();
1067 addAttributeComposite.dispose();
1068 }
1069
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);
1075 }
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();
1079 }
1080 }
1081
1082 private void dispose() {
1083 group.dispose();
1084 }
1085
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();
1092 } else {
1093 inputElement.inputName = tagCombo.getText();
1094 if (tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
1095 inputElement.inputFormat = tagText.getText().trim();
1096 }
1097 }
1098 inputElement.inputAction = actionCombo.getSelectionIndex();
1099 }
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();
1107 } else {
1108 inputAttribute.inputName = attribute.tagCombo.getText();
1109 if (attribute.tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
1110 inputAttribute.inputFormat = attribute.tagText.getText().trim();
1111 }
1112 }
1113 inputAttribute.inputAction = attribute.actionCombo.getSelectionIndex();
1114 inputElement.addAttribute(inputAttribute);
1115 }
1116 }
1117 }
1118
1119 private class Attribute {
1120 ElementNode element;
1121 int attributeNumber;
1122
1123 // children of parent (must be disposed)
1124 Composite labelComposite;
1125 Composite attributeComposite;
1126 Label filler;
1127 Composite tagComposite;
1128
1129 // children of labelComposite
1130 Label attributeLabel;
1131
1132 // children of attributeComposite
1133 Text attributeNameText;
1134 Text previewText;
1135
1136 // children of tagComposite
1137 Combo tagCombo;
1138 Label tagLabel;
1139 Text tagText;
1140 Combo actionCombo;
1141
1142 public Attribute(Composite parent, ElementNode element, InputAttribute inputAttribute, int attributeNumber) {
1143 this.element = element;
1144 this.attributeNumber = attributeNumber;
1145
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));
1152
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() {
1158 @Override
1159 public void widgetSelected(SelectionEvent e) {
1160 Attribute.this.element.removeAttribute(Attribute.this.attributeNumber);
1161 validate();
1162 updatePreviews();
1163 }
1164 });
1165
1166 attributeLabel = new Label(labelComposite, SWT.NULL);
1167 attributeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1168 attributeLabel.setText("Attribute");
1169
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));
1176
1177 Label nameLabel = new Label(attributeComposite, SWT.NONE);
1178 nameLabel.setText("name:");
1179
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);
1184
1185 Label previewLabel = new Label(attributeComposite, SWT.NONE);
1186 previewLabel.setText("Preview:");
1187
1188 previewText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
1189 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1190 gd.widthHint = 0;
1191 previewText.setLayoutData(gd);
1192 previewText.setText("*no match*");
1193 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
1194
1195 filler = new Label(parent, SWT.NULL);
1196
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));
1203
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);
1219 break;
1220 case 1: //Message
1221 tagLabel.setVisible(false);
1222 tagText.setVisible(false);
1223 break;
1224 case 2: //Other
1225 tagLabel.setText("tag name:");
1226 tagLabel.setVisible(true);
1227 if (tagText.getText().trim().length() == 0) {
1228 tagText.setText(attributeNameText.getText().trim());
1229 }
1230 tagText.setVisible(true);
1231 tagText.addModifyListener(updateListener);
1232 break;
1233 }
1234 tagComposite.layout();
1235 validate();
1236 updatePreviews();
1237 }});
1238
1239 tagLabel = new Label(tagComposite, SWT.NULL);
1240 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1241
1242 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
1243 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1244 gd.widthHint = 0;
1245 tagText.setLayoutData(gd);
1246 tagText.setText(attributeNameText.getText());
1247
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);
1252
1253 if (inputAttribute.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
1254 tagCombo.select(0);
1255 tagLabel.setText("format:");
1256 tagText.setText(inputAttribute.inputFormat);
1257 tagText.addModifyListener(updateListener);
1258 } else if (inputAttribute.inputName.equals(CustomXmlTraceDefinition.TAG_MESSAGE)) {
1259 tagCombo.select(1);
1260 tagLabel.setVisible(false);
1261 tagText.setVisible(false);
1262 } else {
1263 tagCombo.select(2);
1264 tagLabel.setText("tag name:");
1265 tagText.setText(inputAttribute.inputName);
1266 tagText.addModifyListener(updateListener);
1267 }
1268 }
1269
1270 private void dispose() {
1271 labelComposite.dispose();
1272 attributeComposite.dispose();
1273 filler.dispose();
1274 tagComposite.dispose();
1275 }
1276
1277 private void setAttributeNumber(int attributeNumber) {
1278 this.attributeNumber = attributeNumber;
1279 labelComposite.layout();
1280 }
1281 }
1282
1283 private Element getPreviewElement(InputElement inputElement) {
1284 Element element = documentElement;
1285 if (element != null) {
1286 if (!documentElement.getNodeName().equals(definition.rootInputElement.elementName)) {
1287 return null;
1288 }
1289 ArrayList<String> elementNames = new ArrayList<String>();
1290 while (inputElement != null) {
1291 elementNames.add(inputElement.elementName);
1292 inputElement = inputElement.parentElement;
1293 }
1294 for (int i = elementNames.size() - 1; --i >= 0;) {
1295 NodeList childList = element.getChildNodes();
1296 element = null;
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;
1301 break;
1302 }
1303 }
1304 if (element == null) {
1305 break;
1306 }
1307 }
1308 if (element != null) {
1309 return element;
1310 }
1311 }
1312 return null;
1313 }
1314
1315 private String getChildNameSuggestion(InputElement inputElement) {
1316 if (inputElement == null) {
1317 if (documentElement != null) {
1318 return documentElement.getNodeName();
1319 }
1320 } else {
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())) {
1331 unused = false;
1332 break;
1333 }
1334 }
1335 }
1336 if (unused) {
1337 return node.getNodeName();
1338 }
1339 }
1340 }
1341 }
1342 }
1343 return ("");
1344 }
1345
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())) {
1356 unused = false;
1357 break;
1358 }
1359 }
1360 }
1361 if (unused) {
1362 return node.getNodeName();
1363 }
1364 }
1365 }
1366 return ("");
1367 }
1368
1369 private void validate() {
1370 definition.definitionName = logtypeText.getText().trim();
1371 definition.timeStampOutputFormat = timeStampOutputFormatText.getText().trim();
1372
1373 if (selectedElement != null) {
1374 selectedElement.extractInputs();
1375 treeViewer.refresh();
1376 }
1377
1378 StringBuffer errors = new StringBuffer();
1379
1380 if (definition.definitionName.length() == 0) {
1381 errors.append("Enter a name for the new log type. ");
1382 logtypeText.setBackground(COLOR_LIGHT_RED);
1383 } else {
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);
1390 break;
1391 }
1392 }
1393 }
1394 }
1395
1396 if (definition.rootInputElement == null) {
1397 errors.append("Add a document element. ");
1398 }
1399
1400 if (definition.rootInputElement != null) {
1401 logEntryFound = false;
1402 logEntryNestedCount = 0;
1403 timeStampFound = false;
1404
1405 errors.append(validateElement(definition.rootInputElement));
1406
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. ");
1411 }
1412
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);
1417 } else {
1418 try {
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);
1424 }
1425 }
1426 } else {
1427 timeStampPreviewText.setText("*no time stamp element or attribute*");
1428 }
1429 }
1430 } else {
1431 timeStampPreviewText.setText("*no time stamp element or attribute*");
1432 }
1433
1434 if (errors.length() == 0) {
1435 setDescription(defaultDescription);
1436 setPageComplete(true);
1437 } else {
1438 setDescription(errors.toString());
1439 setPageComplete(false);
1440 }
1441 }
1442
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);
1451 } else {
1452 if (elementNode != null) elementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);
1453 }
1454 }
1455 if (inputElement != definition.rootInputElement) {
1456 if (inputElement.logEntry) {
1457 logEntryFound = true;
1458 logEntryNestedCount++;
1459 }
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);
1465 } else {
1466 try {
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);
1472 }
1473 }
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);
1477 } else {
1478 if (elementNode != null) elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1479 }
1480 }
1481 if (inputElement.attributes != null) {
1482 if (elementNode != null) {
1483 for (Attribute attribute : elementNode.attributes) {
1484 attribute.attributeNameText.setBackground(COLOR_TEXT_BACKGROUND);
1485 }
1486 }
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)) {
1493 duplicate = true;
1494 if (elementNode != null) {
1495 elementNode.attributes.get(j).attributeNameText.setBackground(COLOR_LIGHT_RED);
1496 }
1497 }
1498 }
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);
1505 }
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);
1511 } else {
1512 try {
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);
1518 }
1519 }
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);
1523 } else {
1524 if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);
1525 }
1526 }
1527 }
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);
1533 }
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);
1541 } else {
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)) {
1546 duplicate = true;
1547 ElementNode otherChildElementNode = null;
1548 if (selectedElement != null && selectedElement.inputElement.equals(otherChild)) otherChildElementNode = selectedElement;
1549 if (otherChildElementNode != null) otherChildElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1550 }
1551 }
1552 if (duplicate) {
1553 errors.append("Duplicate element names (Element " + getName(child) + "). ");
1554 if (childElementNode != null) childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1555 }
1556 }
1557
1558 errors.append(validateElement(child));
1559 }
1560 }
1561 if (inputElement.logEntry) {
1562 logEntryNestedCount--;
1563 }
1564 return errors;
1565 }
1566
1567 public CustomXmlTraceDefinition getDefinition() {
1568 return definition;
1569 }
1570
1571 public char[] getInputText() {
1572 return inputText.getText().toCharArray();
1573 }
1574 }
This page took 0.065938 seconds and 5 git commands to generate.