2010-11-18 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[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"; //$NON-NLS-1$
77 private static final String SIMPLE_DATE_FORMAT_URL = "http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#skip-navbar_top"; //$NON-NLS-1$
78 private static final Image elementImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/element_icon.gif"); //$NON-NLS-1$
79 private static final Image addImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/add_button.gif"); //$NON-NLS-1$
80 private static final Image addNextImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/addnext_button.gif"); //$NON-NLS-1$
81 private static final Image addChildImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/addchild_button.gif"); //$NON-NLS-1$
82 private static final Image addManyImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/addmany_button.gif"); //$NON-NLS-1$
83 private static final Image deleteImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/delete_button.gif"); //$NON-NLS-1$
84 private static final Image moveUpImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/moveup_button.gif"); //$NON-NLS-1$
85 private static final Image moveDownImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/movedown_button.gif"); //$NON-NLS-1$
86 private static final Image helpImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/help_button.gif"); //$NON-NLS-1$
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"); //$NON-NLS-1$
128 if (definition == null) {
129 setTitle(Messages.CustomXmlParserInputWizardPage_titleNew);
130 defaultDescription = Messages.CustomXmlParserInputWizardPage_descriptionNew;
131 } else {
132 setTitle(Messages.CustomXmlParserInputWizardPage_titleEdit);
133 defaultDescription = Messages.CustomXmlParserInputWizardPage_descriptionEdit;
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 @Override
144 public void createControl(Composite parent) {
145 container = new Composite(parent, SWT.NULL);
146 container.setLayout(new GridLayout());
147
148 updateListener = new UpdateListener();
149
150 Composite headerComposite = new Composite(container, SWT.FILL);
151 GridLayout headerLayout = new GridLayout(5, false);
152 headerLayout.marginHeight = 0;
153 headerLayout.marginWidth = 0;
154 headerComposite.setLayout(headerLayout);
155 headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
156
157 Label logtypeLabel = new Label(headerComposite, SWT.NULL);
158 logtypeLabel.setText(Messages.CustomXmlParserInputWizardPage_logType);
159
160 logtypeText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
161 logtypeText.setLayoutData(new GridData(120, SWT.DEFAULT));
162 logtypeText.addModifyListener(updateListener);
163
164 Label timeStampFormatLabel = new Label(headerComposite, SWT.NULL);
165 timeStampFormatLabel.setText(Messages.CustomXmlParserInputWizardPage_timestampFormat);
166
167 timeStampOutputFormatText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);
168 timeStampOutputFormatText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
169 timeStampOutputFormatText.setText(DEFAULT_TIMESTAMP_FORMAT);
170 timeStampOutputFormatText.addModifyListener(updateListener);
171
172 Button dateFormatHelpButton = new Button(headerComposite, SWT.PUSH);
173 dateFormatHelpButton.setImage(helpImage);
174 dateFormatHelpButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_dateFormatHelp);
175 dateFormatHelpButton.addSelectionListener(new SelectionAdapter() {
176 @Override
177 public void widgetSelected(SelectionEvent e) {
178 openHelpShell(SIMPLE_DATE_FORMAT_URL);
179 }
180 });
181
182 Label timeStampPreviewLabel = new Label(headerComposite, SWT.NULL);
183 timeStampPreviewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 3, 1));
184 timeStampPreviewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview);
185
186 timeStampPreviewText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
187 timeStampPreviewText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
188 timeStampPreviewText.setText("*no time stamp element or attribute*"); //$NON-NLS-1$
189
190 createButtonBar();
191
192 SashForm vSash = new SashForm(container, SWT.VERTICAL);
193 vSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
194 vSash.setBackground(vSash.getDisplay().getSystemColor(SWT.COLOR_GRAY));
195
196 SashForm hSash = new SashForm(vSash, SWT.HORIZONTAL);
197 hSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
198
199 treeScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL | SWT.H_SCROLL);
200 treeScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
201 treeContainer = new Composite(treeScrolledComposite, SWT.NONE);
202 treeContainer.setLayout(new FillLayout());
203 treeScrolledComposite.setContent(treeContainer);
204 treeScrolledComposite.setExpandHorizontal(true);
205 treeScrolledComposite.setExpandVertical(true);
206
207 treeViewer = new TreeViewer(treeContainer, SWT.SINGLE | SWT.BORDER);
208 treeViewer.setContentProvider(new InputElementTreeNodeContentProvider());
209 treeViewer.setLabelProvider(new InputElementTreeLabelProvider());
210 treeViewer.addSelectionChangedListener(new InputElementTreeSelectionChangedListener());
211 treeContainer.layout();
212
213 treeScrolledComposite.setMinSize(treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
214
215 elementScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL);
216 elementScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
217 elementContainer = new Composite(elementScrolledComposite, SWT.NONE);
218 GridLayout gl = new GridLayout();
219 gl.marginHeight = 1;
220 gl.marginWidth = 0;
221 elementContainer.setLayout(gl);
222 elementScrolledComposite.setContent(elementContainer);
223 elementScrolledComposite.setExpandHorizontal(true);
224 elementScrolledComposite.setExpandVertical(true);
225
226 if (definition == null) {
227 definition = new CustomXmlTraceDefinition();
228 }
229 loadDefinition(definition);
230 treeViewer.expandAll();
231 elementContainer.layout();
232
233 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);
234
235 hSash.setWeights(new int[] {1, 2});
236
237 if (definition.rootInputElement == null) {
238 removeButton.setEnabled(false);
239 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentElement);
240 addNextButton.setEnabled(false);
241 moveUpButton.setEnabled(false);
242 moveDownButton.setEnabled(false);
243 } else { // root is selected
244 addNextButton.setEnabled(false);
245 }
246
247 Composite sashBottom = new Composite(vSash, SWT.NONE);
248 GridLayout sashBottomLayout = new GridLayout(2, false);
249 sashBottomLayout.marginHeight = 0;
250 sashBottomLayout.marginWidth = 0;
251 sashBottom.setLayout(sashBottomLayout);
252
253 Label previewLabel = new Label(sashBottom, SWT.NULL);
254 previewLabel.setText(Messages.CustomXmlParserInputWizardPage_previewInput);
255
256 errorText = new Text(sashBottom, SWT.SINGLE | SWT.READ_ONLY);
257 errorText.setBackground(COLOR_WIDGET_BACKGROUND);
258 errorText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
259 errorText.setVisible(false);
260
261 inputText = new StyledText(sashBottom, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
262 if (fixedFont == null) {
263 if (System.getProperty("os.name").contains("Windows")) { //$NON-NLS-1$ //$NON-NLS-2$
264 fixedFont = new Font(Display.getCurrent(), new FontData("Courier New", 10, SWT.NORMAL)); //$NON-NLS-1$
265 } else {
266 fixedFont = new Font(Display.getCurrent(), new FontData("Monospace", 10, SWT.NORMAL)); //$NON-NLS-1$
267 }
268 }
269 inputText.setFont(fixedFont);
270 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
271 gd.heightHint = inputText.computeSize(SWT.DEFAULT, inputText.getLineHeight() * 4).y;
272 gd.widthHint = 800;
273 inputText.setLayoutData(gd);
274 inputText.setText(getSelectionText());
275 inputText.addModifyListener(new ModifyListener(){
276 @Override
277 public void modifyText(ModifyEvent e) {
278 parseXmlInput(inputText.getText());
279 }});
280 inputText.addModifyListener(updateListener);
281
282 vSash.setWeights(new int[] {hSash.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sashBottom.computeSize(SWT.DEFAULT, SWT.DEFAULT).y});
283
284 setControl(container);
285 }
286
287 private void createButtonBar() {
288 Composite buttonBar = new Composite(container, SWT.NONE);
289 GridLayout buttonBarLayout = new GridLayout(6, false);
290 buttonBarLayout.marginHeight = 0;
291 buttonBarLayout.marginWidth = 0;
292 buttonBar.setLayout(buttonBarLayout);
293
294 removeButton = new Button(buttonBar, SWT.PUSH);
295 removeButton.setImage(deleteImage);
296 removeButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_removeElement);
297 removeButton.addSelectionListener(new SelectionAdapter() {
298 @Override
299 public void widgetSelected(SelectionEvent e) {
300 if (treeViewer.getSelection().isEmpty() || selectedElement == null) return;
301 removeElement();
302 InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
303 if (inputElement == definition.rootInputElement) {
304 definition.rootInputElement = null;
305 } else {
306 inputElement.parentElement.childElements.remove(inputElement);
307 }
308 treeViewer.refresh();
309 validate();
310 updatePreviews();
311 removeButton.setEnabled(false);
312 if (definition.rootInputElement == null) {
313 addChildButton.setEnabled(true);
314 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentEleemnt);
315 } else {
316 addChildButton.setEnabled(false);
317 }
318 addNextButton.setEnabled(false);
319 moveUpButton.setEnabled(false);
320 moveDownButton.setEnabled(false);
321 }
322 });
323
324 addChildButton = new Button(buttonBar, SWT.PUSH);
325 addChildButton.setImage(addChildImage);
326 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addChildElement);
327 addChildButton.addSelectionListener(new SelectionAdapter() {
328 @Override
329 public void widgetSelected(SelectionEvent e) {
330 InputElement inputElement = new InputElement("", false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$
331 if (definition.rootInputElement == null) {
332 definition.rootInputElement = inputElement;
333 inputElement.elementName = getChildNameSuggestion(null);
334 } else if (treeViewer.getSelection().isEmpty()) {
335 return;
336 } else {
337 InputElement parentInputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
338 parentInputElement.addChild(inputElement);
339 inputElement.elementName = getChildNameSuggestion(parentInputElement);
340 }
341 treeViewer.refresh();
342 treeViewer.setSelection(new StructuredSelection(inputElement), true);
343 }
344 });
345
346 addNextButton = new Button(buttonBar, SWT.PUSH);
347 addNextButton.setImage(addNextImage);
348 addNextButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addNextElement);
349 addNextButton.addSelectionListener(new SelectionAdapter() {
350 @Override
351 public void widgetSelected(SelectionEvent e) {
352 InputElement inputElement = new InputElement("", false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$
353 if (definition.rootInputElement == null) {
354 definition.rootInputElement = inputElement;
355 inputElement.elementName = getChildNameSuggestion(null);
356 } else if (treeViewer.getSelection().isEmpty()) {
357 return;
358 } else {
359 InputElement previousInputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
360 if (previousInputElement == definition.rootInputElement) {
361 return;
362 } else {
363 previousInputElement.addNext(inputElement);
364 inputElement.elementName = getChildNameSuggestion(inputElement.parentElement);
365 }
366 }
367 treeViewer.refresh();
368 treeViewer.setSelection(new StructuredSelection(inputElement), true);
369 }
370 });
371
372 feelingLuckyButton = new Button(buttonBar, SWT.PUSH);
373 feelingLuckyButton.setImage(addManyImage);
374 feelingLuckyButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_feelingLucky);
375 feelingLuckyButton.addSelectionListener(new SelectionAdapter() {
376 @Override
377 public void widgetSelected(SelectionEvent e) {
378 InputElement inputElement = null;
379 if (definition.rootInputElement == null) {
380 if (getChildNameSuggestion(null).length() != 0) {
381 inputElement = new InputElement(getChildNameSuggestion(null), false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null); //$NON-NLS-1$
382 definition.rootInputElement = inputElement;
383 feelingLucky(inputElement);
384 }
385 } else if (treeViewer.getSelection().isEmpty()) {
386 return;
387 } else {
388 inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
389 feelingLucky(inputElement);
390 }
391 treeViewer.refresh();
392 treeViewer.setSelection(new StructuredSelection(inputElement), true);
393 treeViewer.expandToLevel(inputElement, TreeViewer.ALL_LEVELS);
394 }
395 });
396
397 moveUpButton = new Button(buttonBar, SWT.PUSH);
398 moveUpButton.setImage(moveUpImage);
399 moveUpButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_moveUp);
400 moveUpButton.addSelectionListener(new SelectionAdapter() {
401 @Override
402 public void widgetSelected(SelectionEvent e) {
403 if (treeViewer.getSelection().isEmpty()) return;
404 InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
405 if (inputElement == definition.rootInputElement) {
406 return;
407 } else {
408 inputElement.moveUp();
409 }
410 treeViewer.refresh();
411 validate();
412 updatePreviews();
413 }
414 });
415
416 moveDownButton = new Button(buttonBar, SWT.PUSH);
417 moveDownButton.setImage(moveDownImage);
418 moveDownButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_moveDown);
419 moveDownButton.addSelectionListener(new SelectionAdapter() {
420 @Override
421 public void widgetSelected(SelectionEvent e) {
422 if (treeViewer.getSelection().isEmpty()) return;
423 InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
424 if (inputElement == definition.rootInputElement) {
425 return;
426 } else {
427 inputElement.moveDown();
428 }
429 treeViewer.refresh();
430 validate();
431 updatePreviews();
432 }
433 });
434 }
435
436 private void feelingLucky(InputElement inputElement) {
437 while (true) {
438 String attributeName = getAttributeNameSuggestion(inputElement);
439 if (attributeName.length() == 0) {
440 break;
441 } else {
442 InputAttribute attribute = new InputAttribute(attributeName, attributeName, 0, ""); //$NON-NLS-1$
443 inputElement.addAttribute(attribute);
444 }
445 }
446 while (true) {
447 String childName = getChildNameSuggestion(inputElement);
448 if (childName.length() == 0) {
449 break;
450 } else {
451 InputElement childElement = new InputElement(childName, false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null); //$NON-NLS-1$
452 inputElement.addChild(childElement);
453 feelingLucky(childElement);
454 }
455 }
456 }
457
458 private class InputElementTreeNodeContentProvider implements ITreeContentProvider {
459
460 @Override
461 public Object[] getElements(Object inputElement) {
462 CustomXmlTraceDefinition def = (CustomXmlTraceDefinition) inputElement;
463 if (def.rootInputElement != null) {
464 return new Object[] {def.rootInputElement};
465 } else {
466 return new Object[0];
467 }
468 }
469
470 @Override
471 public Object[] getChildren(Object parentElement) {
472 InputElement inputElement = (InputElement) parentElement;
473 if (inputElement.childElements == null) return new InputElement[0];
474 return inputElement.childElements.toArray();
475 }
476
477 @Override
478 public boolean hasChildren(Object element) {
479 InputElement inputElement = (InputElement) element;
480 return (inputElement.childElements != null && inputElement.childElements.size() > 0);
481 }
482
483 @Override
484 public void dispose() {
485 }
486
487 @Override
488 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
489 }
490
491 @Override
492 public Object getParent(Object element) {
493 InputElement inputElement = (InputElement) element;
494 return inputElement.parentElement;
495 }
496 }
497
498 private class InputElementTreeLabelProvider extends ColumnLabelProvider {
499
500 @Override
501 public Image getImage(Object element) {
502 return elementImage;
503 }
504
505 @Override
506 public String getText(Object element) {
507 InputElement inputElement = (InputElement) element;
508 return (inputElement.elementName.trim().length() == 0) ? "?" : inputElement.elementName; //$NON-NLS-1$
509 }
510 }
511
512 private class InputElementTreeSelectionChangedListener implements ISelectionChangedListener {
513 @Override
514 public void selectionChanged(SelectionChangedEvent event) {
515 if (selectedElement != null) {
516 selectedElement.dispose();
517 }
518 if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
519 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
520 InputElement inputElement = (InputElement) selection.getFirstElement();
521 selectedElement = new ElementNode(elementContainer, inputElement);
522 elementContainer.layout();
523 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);
524 container.layout();
525 validate();
526 updatePreviews();
527 removeButton.setEnabled(true);
528 addChildButton.setEnabled(true);
529 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addChildElement);
530 if (definition.rootInputElement == inputElement) {
531 addNextButton.setEnabled(false);
532 } else {
533 addNextButton.setEnabled(true);
534 }
535 moveUpButton.setEnabled(true);
536 moveDownButton.setEnabled(true);
537 } else {
538 removeButton.setEnabled(false);
539 if (definition.rootInputElement == null) {
540 addChildButton.setEnabled(true);
541 addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentElement);
542 } else {
543 addChildButton.setEnabled(false);
544 }
545 addNextButton.setEnabled(false);
546 moveUpButton.setEnabled(false);
547 moveDownButton.setEnabled(false);
548 }
549 }
550 }
551
552 /* (non-Javadoc)
553 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
554 */
555 @Override
556 public void dispose() {
557 if (fixedFont != null) {
558 fixedFont.dispose();
559 fixedFont = null;
560 }
561 super.dispose();
562 }
563
564 private void loadDefinition(CustomXmlTraceDefinition def) {
565 logtypeText.setText(def.definitionName);
566 timeStampOutputFormatText.setText(def.timeStampOutputFormat);
567 treeViewer.setInput(def);
568
569 if (def.rootInputElement != null) {
570 treeViewer.setSelection(new StructuredSelection(def.rootInputElement));
571 }
572 }
573
574 private String getName(InputElement inputElement) {
575 String name = (inputElement.elementName.trim().length() == 0) ? "?" : inputElement.elementName.trim(); //$NON-NLS-1$
576 if (inputElement.parentElement == null) {
577 return name;
578 }
579 return getName(inputElement.parentElement) + " : " + name; //$NON-NLS-1$
580 }
581
582 private String getName(InputAttribute inputAttribute, InputElement inputElement) {
583 String name = (inputAttribute.attributeName.trim().length() == 0) ? "?" : inputAttribute.attributeName.trim(); //$NON-NLS-1$
584 return getName(inputElement) + " : " + name; //$NON-NLS-1$
585 }
586
587 /* (non-Javadoc)
588 * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
589 */
590 @Override
591 public void setVisible(boolean visible) {
592 if (visible) {
593 validate();
594 updatePreviews();
595 }
596 super.setVisible(visible);
597 }
598
599 public List<String> getInputNames() {
600 return getInputNames(definition.rootInputElement);
601 }
602
603 public List<String> getInputNames(InputElement inputElement) {
604 List<String> inputs = new ArrayList<String>();
605 if (inputElement.inputName != null && !inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {
606 String inputName = inputElement.inputName;
607 if (!inputs.contains(inputName)) {
608 inputs.add(inputName);
609 }
610 }
611 if (inputElement.attributes != null) {
612 for (InputAttribute attribute : inputElement.attributes) {
613 String inputName = attribute.inputName;
614 if (!inputs.contains(inputName)) {
615 inputs.add(inputName);
616 }
617 }
618 }
619 if (inputElement.childElements != null) {
620 for (InputElement childInputElement : inputElement.childElements) {
621 for (String inputName : getInputNames(childInputElement)) {
622 if (!inputs.contains(inputName)) {
623 inputs.add(inputName);
624 }
625 }
626 }
627 }
628 return inputs;
629 }
630
631 private void removeElement() {
632 selectedElement.dispose();
633 selectedElement = null;
634 elementContainer.layout();
635 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);
636 container.layout();
637 }
638
639 private String getSelectionText() {
640 InputStream inputStream = null;
641 if (this.selection instanceof IStructuredSelection) {
642 Object selection = ((IStructuredSelection)this.selection).getFirstElement();
643 if (selection instanceof IFile) {
644 IFile file = (IFile)selection;
645 try {
646 inputStream = file.getContents();
647 } catch (CoreException e) {
648 return ""; //$NON-NLS-1$
649 }
650 }
651 }
652 if (inputStream != null) {
653 try {
654 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
655 StringBuilder sb = new StringBuilder();
656 String line = null;
657 while ((line = reader.readLine()) != null) {
658 sb.append(line + "\n"); //$NON-NLS-1$
659 }
660 parseXmlInput(sb.toString());
661 return sb.toString();
662 } catch (IOException e) {
663 return ""; //$NON-NLS-1$
664 }
665 }
666 return ""; //$NON-NLS-1$
667 }
668
669 private void parseXmlInput(final String string) {
670 try {
671 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
672 DocumentBuilder db = dbf.newDocumentBuilder();
673
674 // The following allows xml parsing without access to the dtd
675 EntityResolver resolver = new EntityResolver () {
676 @Override
677 public InputSource resolveEntity (String publicId, String systemId) {
678 String empty = ""; //$NON-NLS-1$
679 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
680 return new InputSource(bais);
681 }
682 };
683 db.setEntityResolver(resolver);
684
685 // The following catches xml parsing exceptions
686 db.setErrorHandler(new ErrorHandler(){
687 @Override
688 public void error(SAXParseException saxparseexception) throws SAXException {}
689 @Override
690 public void warning(SAXParseException saxparseexception) throws SAXException {}
691 @Override
692 public void fatalError(SAXParseException saxparseexception) throws SAXException {
693 if (string.trim().length() != 0) {
694 errorText.setText(saxparseexception.getMessage());
695 errorText.setBackground(COLOR_LIGHT_RED);
696 errorText.setVisible(true);
697 }
698 throw saxparseexception;
699 }});
700
701 errorText.setVisible(false);
702 Document doc = null;
703 doc = db.parse(new ByteArrayInputStream(string.getBytes()));
704 documentElement = doc.getDocumentElement();
705 } catch (ParserConfigurationException e) {
706 e.printStackTrace();
707 documentElement = null;
708 } catch (SAXException e) {
709 documentElement = null;
710 } catch (IOException e) {
711 e.printStackTrace();
712 documentElement = null;
713 }
714 }
715
716 private void updatePreviews() {
717 updatePreviews(false);
718 }
719
720 private void updatePreviews(boolean updateAll) {
721 if (inputText == null) {
722 // early update during construction
723 return;
724 }
725 inputText.setStyleRanges(new StyleRange[] {});
726 if (selectedElement == null) {
727 return;
728 }
729
730 timeStampValue = null;
731 timeStampFormat = null;
732 logEntriesCount = 0;
733 logEntryFound = false;
734
735 selectedElement.updatePreview();
736
737 if (timeStampValue != null && timeStampFormat != null) {
738 try {
739 SimpleDateFormat dateFormat = new SimpleDateFormat(timeStampFormat);
740 Date date = dateFormat.parse(timeStampValue);
741 dateFormat = new SimpleDateFormat(timeStampOutputFormatText.getText().trim());
742 timeStampPreviewText.setText(dateFormat.format(date));
743 } catch (ParseException e) {
744 timeStampPreviewText.setText("*parse exception* [" + timeStampValue + "] <> [" + timeStampFormat + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
745 } catch (IllegalArgumentException e) {
746 timeStampPreviewText.setText("*parse exception* [Illegal Argument]"); //$NON-NLS-1$
747 }
748 } else {
749 timeStampPreviewText.setText("*no matching time stamp*"); //$NON-NLS-1$
750 }
751 }
752
753 private void openHelpShell(String url) {
754 if (helpBrowser != null && !helpBrowser.isDisposed()) {
755 helpBrowser.getShell().setActive();
756 if (!helpBrowser.getUrl().equals(url)) {
757 helpBrowser.setUrl(url);
758 }
759 return;
760 }
761 final Shell helpShell = new Shell(getShell(), SWT.SHELL_TRIM);
762 helpShell.setLayout(new FillLayout());
763 helpBrowser = new Browser(helpShell, SWT.NONE);
764 helpBrowser.addTitleListener(new TitleListener() {
765 @Override
766 public void changed(TitleEvent event) {
767 helpShell.setText(event.title);
768 }
769 });
770 helpBrowser.setBounds(0,0,600,400);
771 helpShell.pack();
772 helpShell.open();
773 helpBrowser.setUrl(url);
774 }
775
776 private class UpdateListener implements ModifyListener, SelectionListener {
777
778 @Override
779 public void modifyText(ModifyEvent e) {
780 validate();
781 updatePreviews();
782 }
783
784 @Override
785 public void widgetDefaultSelected(SelectionEvent e) {
786 validate();
787 updatePreviews();
788 }
789
790 @Override
791 public void widgetSelected(SelectionEvent e) {
792 validate();
793 updatePreviews();
794 }
795
796 }
797
798 private class ElementNode {
799 final InputElement inputElement;
800 final Group group;
801 ArrayList<Attribute> attributes = new ArrayList<Attribute>();
802 ArrayList<ElementNode> childElements = new ArrayList<ElementNode>();
803 Text elementNameText;
804 Composite tagComposite;
805 Combo tagCombo;
806 Label tagLabel;
807 Text tagText;
808 Combo actionCombo;
809 Label previewLabel;
810 Text previewText;
811 Button logEntryButton;
812 Label fillerLabel;
813 Composite addAttributeComposite;
814 Button addAttributeButton;
815 Label addAttributeLabel;
816
817 public ElementNode(Composite parent, InputElement inputElement) {
818 this.inputElement = inputElement;
819
820 group = new Group(parent, SWT.NONE);
821 GridLayout gl = new GridLayout(2, false);
822 gl.marginHeight = 0;
823 group.setLayout(gl);
824 group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
825 group.setText(getName(inputElement));
826
827 Label label = new Label(group, SWT.NULL);
828 label.setText(Messages.CustomXmlParserInputWizardPage_elementName);
829 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
830
831 elementNameText = new Text(group, SWT.BORDER | SWT.SINGLE);
832 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
833 gd.widthHint = 0;
834 elementNameText.setLayoutData(gd);
835 elementNameText.addModifyListener(new ModifyListener(){
836 @Override
837 public void modifyText(ModifyEvent e) {
838 ElementNode.this.inputElement.elementName = elementNameText.getText().trim();
839 group.setText(getName(ElementNode.this.inputElement));
840 }});
841 elementNameText.setText(inputElement.elementName);
842 elementNameText.addModifyListener(updateListener);
843
844 if (inputElement.parentElement != null) {
845 previewLabel = new Label(group, SWT.NULL);
846 previewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
847 previewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview);
848
849 previewText = new Text(group, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
850 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
851 gd.widthHint = 0;
852 previewText.setLayoutData(gd);
853 previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement);
854 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
855
856 logEntryButton = new Button(group, SWT.CHECK);
857 logEntryButton.setText(Messages.CustomXmlParserInputWizardPage_logEntry);
858 logEntryButton.setSelection(inputElement.logEntry);
859 logEntryButton.addSelectionListener(new SelectionListener(){
860 @Override
861 public void widgetDefaultSelected(SelectionEvent e) {}
862 @Override
863 public void widgetSelected(SelectionEvent e) {
864 InputElement parent = ElementNode.this.inputElement.parentElement;
865 while (parent != null) {
866 parent.logEntry = false;
867 parent = parent.parentElement;
868 }
869 }});
870 logEntryButton.addSelectionListener(updateListener);
871
872 tagComposite = new Composite(group, SWT.FILL);
873 GridLayout tagLayout = new GridLayout(4, false);
874 tagLayout.marginWidth = 0;
875 tagLayout.marginHeight = 0;
876 tagComposite.setLayout(tagLayout);
877 tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
878
879 tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
880 tagCombo.setItems(new String[] {CustomXmlTraceDefinition.TAG_IGNORE,
881 CustomXmlTraceDefinition.TAG_TIMESTAMP,
882 CustomXmlTraceDefinition.TAG_MESSAGE,
883 CustomXmlTraceDefinition.TAG_OTHER});
884 tagCombo.setVisibleItemCount(tagCombo.getItemCount());
885 tagCombo.addSelectionListener(new SelectionListener(){
886 @Override
887 public void widgetDefaultSelected(SelectionEvent e) {}
888 @Override
889 public void widgetSelected(SelectionEvent e) {
890 tagText.removeModifyListener(updateListener);
891 switch (tagCombo.getSelectionIndex()) {
892 case 0: //Ignore
893 tagLabel.setVisible(false);
894 tagText.setVisible(false);
895 actionCombo.setVisible(false);
896 break;
897 case 1: //Time Stamp
898 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
899 tagLabel.setVisible(true);
900 tagText.setVisible(true);
901 tagText.addModifyListener(updateListener);
902 actionCombo.setVisible(true);
903 break;
904 case 2: //Message
905 tagLabel.setVisible(false);
906 tagText.setVisible(false);
907 actionCombo.setVisible(true);
908 break;
909 case 3: //Other
910 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_tagName);
911 tagLabel.setVisible(true);
912 if (tagText.getText().trim().length() == 0) {
913 tagText.setText(elementNameText.getText().trim());
914 }
915 tagText.setVisible(true);
916 tagText.addModifyListener(updateListener);
917 actionCombo.setVisible(true);
918 break;
919 }
920 tagComposite.layout();
921 validate();
922 updatePreviews();
923 }});
924
925 tagLabel = new Label(tagComposite, SWT.NULL);
926 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
927
928 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
929 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
930 gd.widthHint = 0;
931 tagText.setLayoutData(gd);
932
933 actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
934 actionCombo.setItems(new String[] {Messages.CustomXmlParserInputWizardPage_set, Messages.CustomXmlParserInputWizardPage_append, Messages.CustomXmlParserInputWizardPage_appendWith});
935 actionCombo.select(inputElement.inputAction);
936 actionCombo.addSelectionListener(updateListener);
937
938 if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {
939 tagCombo.select(0);
940 tagLabel.setVisible(false);
941 tagText.setVisible(false);
942 actionCombo.setVisible(false);
943 } else if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
944 tagCombo.select(1);
945 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
946 tagText.setText(inputElement.inputFormat);
947 tagText.addModifyListener(updateListener);
948 } else if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_MESSAGE)) {
949 tagCombo.select(2);
950 tagLabel.setVisible(false);
951 tagText.setVisible(false);
952 } else {
953 tagCombo.select(3);
954 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_tagName);
955 tagText.setText(inputElement.inputName);
956 tagText.addModifyListener(updateListener);
957 }
958 }
959
960 if (inputElement.attributes != null) {
961 for (InputAttribute inputAttribute : inputElement.attributes) {
962 Attribute attribute = new Attribute(group, this, inputAttribute, attributes.size()+1);
963 attributes.add(attribute);
964 }
965 }
966
967 createAddButton();
968 }
969
970 private void updatePreview() {
971 Element element = getPreviewElement(inputElement);
972 if (inputElement.parentElement != null) { // no preview text for document element
973 previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement);
974 if (element != null) {
975 previewText.setText(CustomXmlTrace.parseElement(element, new StringBuffer()).toString());
976 if (logEntryButton.getSelection()) {
977 if (logEntryFound == false) {
978 logEntryFound = true;
979 logEntriesCount++;
980 } else {
981 logEntryButton.setSelection(false); // remove nested log entry
982 }
983 }
984 if (tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP) && logEntriesCount <= 1) {
985 String value = previewText.getText().trim();
986 if (value.length() != 0) {
987 if (actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_SET) {
988 timeStampValue = value;
989 timeStampFormat = tagText.getText().trim();
990 } else if (actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND) {
991 if (timeStampValue != null) {
992 timeStampValue += value;
993 timeStampFormat += tagText.getText().trim();
994 } else {
995 timeStampValue = value;
996 timeStampFormat = tagText.getText().trim();
997 }
998 } else if (actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
999 if (timeStampValue != null) {
1000 timeStampValue += " | " + value; //$NON-NLS-1$
1001 timeStampFormat += " | " + tagText.getText().trim(); //$NON-NLS-1$
1002 } else {
1003 timeStampValue = value;
1004 timeStampFormat = tagText.getText().trim();
1005 }
1006 }
1007 }
1008 }
1009 }
1010 }
1011 for (Attribute attribute : attributes) {
1012 if (element != null) {
1013 String value = element.getAttribute(attribute.attributeNameText.getText().trim());
1014 if (value.length() != 0) {
1015 attribute.previewText.setText(value);
1016 if (attribute.tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP) && logEntriesCount <= 1) {
1017 if (attribute.actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_SET) {
1018 timeStampValue = value;
1019 timeStampFormat = attribute.tagText.getText().trim();
1020 } else if (attribute.actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND) {
1021 if (timeStampValue != null) {
1022 timeStampValue += value;
1023 timeStampFormat += attribute.tagText.getText().trim();
1024 } else {
1025 timeStampValue = value;
1026 timeStampFormat = attribute.tagText.getText().trim();
1027 }
1028 } else if (attribute.actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
1029 if (timeStampValue != null) {
1030 timeStampValue += " | " + value; //$NON-NLS-1$
1031 timeStampFormat += " | " + attribute.tagText.getText().trim(); //$NON-NLS-1$
1032 } else {
1033 timeStampValue = value;
1034 timeStampFormat = attribute.tagText.getText().trim();
1035 }
1036 }
1037 }
1038 } else {
1039 attribute.previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingAttribute);
1040 }
1041 } else {
1042 attribute.previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement);
1043 }
1044 }
1045 for (ElementNode child : childElements) {
1046 child.updatePreview();
1047 }
1048 if (logEntryButton != null && logEntryButton.getSelection()) {
1049 logEntryFound = false;
1050 }
1051 }
1052
1053 private void createAddButton() {
1054 fillerLabel = new Label(group, SWT.NONE);
1055
1056 addAttributeComposite = new Composite(group, SWT.NONE);
1057 addAttributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1058 GridLayout addAttributeLayout = new GridLayout(2, false);
1059 addAttributeLayout.marginHeight = 0;
1060 addAttributeLayout.marginWidth = 0;
1061 addAttributeComposite.setLayout(addAttributeLayout);
1062
1063 addAttributeButton = new Button(addAttributeComposite, SWT.PUSH);
1064 addAttributeButton.setImage(addImage);
1065 addAttributeButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addAttribute);
1066 addAttributeButton.addSelectionListener(new SelectionAdapter() {
1067 @Override
1068 public void widgetSelected(SelectionEvent e) {
1069 removeAddButton();
1070 String attributeName = getAttributeNameSuggestion(inputElement);
1071 InputAttribute inputAttribute = new InputAttribute(attributeName, attributeName, 0, ""); //$NON-NLS-1$
1072 attributes.add(new Attribute(group, ElementNode.this, inputAttribute, attributes.size()+1));
1073 createAddButton();
1074 elementContainer.layout();
1075 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);
1076 group.getParent().layout();
1077 validate();
1078 updatePreviews();
1079 }
1080 });
1081
1082 addAttributeLabel = new Label(addAttributeComposite, SWT.NULL);
1083 addAttributeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1084 addAttributeLabel.setText(Messages.CustomXmlParserInputWizardPage_newAttibute);
1085 }
1086
1087 private void removeAddButton() {
1088 fillerLabel.dispose();
1089 addAttributeComposite.dispose();
1090 }
1091
1092 private void removeAttribute(int attributeNumber) {
1093 if (--attributeNumber < attributes.size()) {
1094 attributes.remove(attributeNumber).dispose();
1095 for (int i = attributeNumber; i < attributes.size(); i++) {
1096 attributes.get(i).setAttributeNumber(i+1);
1097 }
1098 elementContainer.layout();
1099 elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);
1100 group.getParent().layout();
1101 }
1102 }
1103
1104 private void dispose() {
1105 group.dispose();
1106 }
1107
1108 private void extractInputs() {
1109 inputElement.elementName = elementNameText.getText().trim();
1110 if (inputElement.parentElement != null) {
1111 inputElement.logEntry = logEntryButton.getSelection();
1112 if (tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_OTHER)) {
1113 inputElement.inputName = tagText.getText().trim();
1114 } else {
1115 inputElement.inputName = tagCombo.getText();
1116 if (tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
1117 inputElement.inputFormat = tagText.getText().trim();
1118 }
1119 }
1120 inputElement.inputAction = actionCombo.getSelectionIndex();
1121 }
1122 inputElement.attributes = new ArrayList<InputAttribute>(attributes.size());
1123 for (int i = 0; i < attributes.size(); i++) {
1124 Attribute attribute = attributes.get(i);
1125 InputAttribute inputAttribute = new InputAttribute();
1126 inputAttribute.attributeName = attribute.attributeNameText.getText().trim();
1127 if (attribute.tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_OTHER)) {
1128 inputAttribute.inputName = attribute.tagText.getText().trim();
1129 } else {
1130 inputAttribute.inputName = attribute.tagCombo.getText();
1131 if (attribute.tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
1132 inputAttribute.inputFormat = attribute.tagText.getText().trim();
1133 }
1134 }
1135 inputAttribute.inputAction = attribute.actionCombo.getSelectionIndex();
1136 inputElement.addAttribute(inputAttribute);
1137 }
1138 }
1139 }
1140
1141 private class Attribute {
1142 ElementNode element;
1143 int attributeNumber;
1144
1145 // children of parent (must be disposed)
1146 Composite labelComposite;
1147 Composite attributeComposite;
1148 Label filler;
1149 Composite tagComposite;
1150
1151 // children of labelComposite
1152 Label attributeLabel;
1153
1154 // children of attributeComposite
1155 Text attributeNameText;
1156 Text previewText;
1157
1158 // children of tagComposite
1159 Combo tagCombo;
1160 Label tagLabel;
1161 Text tagText;
1162 Combo actionCombo;
1163
1164 public Attribute(Composite parent, ElementNode element, InputAttribute inputAttribute, int attributeNumber) {
1165 this.element = element;
1166 this.attributeNumber = attributeNumber;
1167
1168 labelComposite = new Composite(parent, SWT.FILL);
1169 GridLayout labelLayout = new GridLayout(2, false);
1170 labelLayout.marginWidth = 0;
1171 labelLayout.marginHeight = 0;
1172 labelComposite.setLayout(labelLayout);
1173 labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1174
1175 Button deleteButton = new Button(labelComposite, SWT.PUSH);
1176 deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1177 deleteButton.setImage(deleteImage);
1178 deleteButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_removeAttribute);
1179 deleteButton.addSelectionListener(new SelectionAdapter() {
1180 @Override
1181 public void widgetSelected(SelectionEvent e) {
1182 Attribute.this.element.removeAttribute(Attribute.this.attributeNumber);
1183 validate();
1184 updatePreviews();
1185 }
1186 });
1187
1188 attributeLabel = new Label(labelComposite, SWT.NULL);
1189 attributeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1190 attributeLabel.setText(Messages.CustomXmlParserInputWizardPage_attibute);
1191
1192 attributeComposite = new Composite(parent, SWT.FILL);
1193 GridLayout attributeLayout = new GridLayout(4, false);
1194 attributeLayout.marginWidth = 0;
1195 attributeLayout.marginHeight = 0;
1196 attributeComposite.setLayout(attributeLayout);
1197 attributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1198
1199 Label nameLabel = new Label(attributeComposite, SWT.NONE);
1200 nameLabel.setText(Messages.CustomXmlParserInputWizardPage_name);
1201
1202 attributeNameText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE);
1203 attributeNameText.setLayoutData(new GridData(120, SWT.DEFAULT));
1204 attributeNameText.setText(inputAttribute.attributeName);
1205 attributeNameText.addModifyListener(updateListener);
1206
1207 Label previewLabel = new Label(attributeComposite, SWT.NONE);
1208 previewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview);
1209
1210 previewText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
1211 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1212 gd.widthHint = 0;
1213 previewText.setLayoutData(gd);
1214 previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatch);
1215 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
1216
1217 filler = new Label(parent, SWT.NULL);
1218
1219 tagComposite = new Composite(parent, SWT.FILL);
1220 GridLayout tagLayout = new GridLayout(4, false);
1221 tagLayout.marginWidth = 0;
1222 tagLayout.marginHeight = 0;
1223 tagComposite.setLayout(tagLayout);
1224 tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1225
1226 tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1227 tagCombo.setItems(new String[] {CustomXmlTraceDefinition.TAG_TIMESTAMP,
1228 CustomXmlTraceDefinition.TAG_MESSAGE,
1229 CustomXmlTraceDefinition.TAG_OTHER});
1230 tagCombo.select(2); //Other
1231 tagCombo.addSelectionListener(new SelectionListener(){
1232 @Override
1233 public void widgetDefaultSelected(SelectionEvent e) {}
1234 @Override
1235 public void widgetSelected(SelectionEvent e) {
1236 tagText.removeModifyListener(updateListener);
1237 switch (tagCombo.getSelectionIndex()) {
1238 case 0: //Time Stamp
1239 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
1240 tagLabel.setVisible(true);
1241 tagText.setVisible(true);
1242 tagText.addModifyListener(updateListener);
1243 break;
1244 case 1: //Message
1245 tagLabel.setVisible(false);
1246 tagText.setVisible(false);
1247 break;
1248 case 2: //Other
1249 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_tagName);
1250 tagLabel.setVisible(true);
1251 if (tagText.getText().trim().length() == 0) {
1252 tagText.setText(attributeNameText.getText().trim());
1253 }
1254 tagText.setVisible(true);
1255 tagText.addModifyListener(updateListener);
1256 break;
1257 }
1258 tagComposite.layout();
1259 validate();
1260 updatePreviews();
1261 }});
1262
1263 tagLabel = new Label(tagComposite, SWT.NULL);
1264 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1265
1266 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
1267 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1268 gd.widthHint = 0;
1269 tagText.setLayoutData(gd);
1270 tagText.setText(attributeNameText.getText());
1271
1272 actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1273 actionCombo.setItems(new String[] {Messages.CustomXmlParserInputWizardPage_set, Messages.CustomXmlParserInputWizardPage_append, Messages.CustomXmlParserInputWizardPage_appendWith});
1274 actionCombo.select(inputAttribute.inputAction);
1275 actionCombo.addSelectionListener(updateListener);
1276
1277 if (inputAttribute.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
1278 tagCombo.select(0);
1279 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format);
1280 tagText.setText(inputAttribute.inputFormat);
1281 tagText.addModifyListener(updateListener);
1282 } else if (inputAttribute.inputName.equals(CustomXmlTraceDefinition.TAG_MESSAGE)) {
1283 tagCombo.select(1);
1284 tagLabel.setVisible(false);
1285 tagText.setVisible(false);
1286 } else {
1287 tagCombo.select(2);
1288 tagLabel.setText(Messages.CustomXmlParserInputWizardPage_tagName);
1289 tagText.setText(inputAttribute.inputName);
1290 tagText.addModifyListener(updateListener);
1291 }
1292 }
1293
1294 private void dispose() {
1295 labelComposite.dispose();
1296 attributeComposite.dispose();
1297 filler.dispose();
1298 tagComposite.dispose();
1299 }
1300
1301 private void setAttributeNumber(int attributeNumber) {
1302 this.attributeNumber = attributeNumber;
1303 labelComposite.layout();
1304 }
1305 }
1306
1307 private Element getPreviewElement(InputElement inputElement) {
1308 Element element = documentElement;
1309 if (element != null) {
1310 if (!documentElement.getNodeName().equals(definition.rootInputElement.elementName)) {
1311 return null;
1312 }
1313 ArrayList<String> elementNames = new ArrayList<String>();
1314 while (inputElement != null) {
1315 elementNames.add(inputElement.elementName);
1316 inputElement = inputElement.parentElement;
1317 }
1318 for (int i = elementNames.size() - 1; --i >= 0;) {
1319 NodeList childList = element.getChildNodes();
1320 element = null;
1321 for (int j = 0; j < childList.getLength(); j++) {
1322 Node child = childList.item(j);
1323 if (child instanceof Element && child.getNodeName().equals(elementNames.get(i))) {
1324 element = (Element)child;
1325 break;
1326 }
1327 }
1328 if (element == null) {
1329 break;
1330 }
1331 }
1332 if (element != null) {
1333 return element;
1334 }
1335 }
1336 return null;
1337 }
1338
1339 private String getChildNameSuggestion(InputElement inputElement) {
1340 if (inputElement == null) {
1341 if (documentElement != null) {
1342 return documentElement.getNodeName();
1343 }
1344 } else {
1345 Element element = getPreviewElement(inputElement);
1346 if (element != null) {
1347 NodeList childNodes = element.getChildNodes();
1348 for (int i = 0; i < childNodes.getLength(); i++) {
1349 Node node = childNodes.item(i);
1350 if (node instanceof Element) {
1351 boolean unused = true;
1352 if (inputElement.childElements != null) {
1353 for (InputElement child : inputElement.childElements) {
1354 if (child.elementName.equals(node.getNodeName())) {
1355 unused = false;
1356 break;
1357 }
1358 }
1359 }
1360 if (unused) {
1361 return node.getNodeName();
1362 }
1363 }
1364 }
1365 }
1366 }
1367 return ""; //$NON-NLS-1$
1368 }
1369
1370 private String getAttributeNameSuggestion(InputElement inputElement) {
1371 Element element = getPreviewElement(inputElement);
1372 if (element != null) {
1373 NamedNodeMap attributeMap = element.getAttributes();
1374 for (int i = 0; i < attributeMap.getLength(); i++) {
1375 Node node = attributeMap.item(i);
1376 boolean unused = true;
1377 if (inputElement.attributes != null) {
1378 for (InputAttribute attribute : inputElement.attributes) {
1379 if (attribute.attributeName.equals(node.getNodeName())) {
1380 unused = false;
1381 break;
1382 }
1383 }
1384 }
1385 if (unused) {
1386 return node.getNodeName();
1387 }
1388 }
1389 }
1390 return ""; //$NON-NLS-1$
1391 }
1392
1393 private void validate() {
1394 definition.definitionName = logtypeText.getText().trim();
1395 definition.timeStampOutputFormat = timeStampOutputFormatText.getText().trim();
1396
1397 if (selectedElement != null) {
1398 selectedElement.extractInputs();
1399 treeViewer.refresh();
1400 }
1401
1402 StringBuffer errors = new StringBuffer();
1403
1404 if (definition.definitionName.length() == 0) {
1405 errors.append(Messages.CustomXmlParserInputWizardPage_emptyLogTypeError);
1406 logtypeText.setBackground(COLOR_LIGHT_RED);
1407 } else {
1408 logtypeText.setBackground(COLOR_TEXT_BACKGROUND);
1409 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
1410 if (definition.definitionName.equals(def.definitionName)) {
1411 if (editDefinitionName == null || ! editDefinitionName.equals(definition.definitionName)) {
1412 errors.append(Messages.CustomXmlParserInputWizardPage_duplicatelogTypeError);
1413 logtypeText.setBackground(COLOR_LIGHT_RED);
1414 break;
1415 }
1416 }
1417 }
1418 }
1419
1420 if (definition.rootInputElement == null) {
1421 errors.append(Messages.CustomXmlParserInputWizardPage_noDocumentError);
1422 }
1423
1424 if (definition.rootInputElement != null) {
1425 logEntryFound = false;
1426 logEntryNestedCount = 0;
1427 timeStampFound = false;
1428
1429 errors.append(validateElement(definition.rootInputElement));
1430
1431 if ((definition.rootInputElement.attributes != null && definition.rootInputElement.attributes.size() != 0) ||
1432 (definition.rootInputElement.childElements != null && definition.rootInputElement.childElements.size() != 0) || errors.length() == 0) {
1433 if (!logEntryFound) {
1434 errors.append(Messages.CustomXmlParserInputWizardPage_missingLogEntryError);
1435 }
1436
1437 if (timeStampFound) {
1438 if (timeStampOutputFormatText.getText().trim().length() == 0) {
1439 errors.append(Messages.CustomXmlParserInputWizardPage_missingTimestampFmtError);
1440 timeStampOutputFormatText.setBackground(COLOR_LIGHT_RED);
1441 } else {
1442 try {
1443 new SimpleDateFormat(timeStampOutputFormatText.getText().trim());
1444 timeStampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1445 } catch (IllegalArgumentException e) {
1446 errors.append(Messages.CustomXmlParserInputWizardPage_invalidTimestampFmtError);
1447 timeStampOutputFormatText.setBackground(COLOR_LIGHT_RED);
1448 }
1449 }
1450 } else {
1451 timeStampPreviewText.setText(Messages.CustomXmlParserInputWizardPage_notimestamporAttributeError);
1452 }
1453 }
1454 } else {
1455 timeStampPreviewText.setText(Messages.CustomXmlParserInputWizardPage_notimestamporAttributeError);
1456 }
1457
1458 if (errors.length() == 0) {
1459 setDescription(defaultDescription);
1460 setPageComplete(true);
1461 } else {
1462 setDescription(errors.toString());
1463 setPageComplete(false);
1464 }
1465 }
1466
1467 public StringBuffer validateElement(InputElement inputElement) {
1468 StringBuffer errors = new StringBuffer();
1469 ElementNode elementNode = null;
1470 if (selectedElement != null && selectedElement.inputElement.equals(inputElement)) elementNode = selectedElement;
1471 if (inputElement == definition.rootInputElement) {
1472 if (inputElement.elementName.length() == 0) {
1473 errors.append(Messages.CustomXmlParserInputWizardPage_missingDocumentElementError);
1474 if (elementNode != null) elementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1475 } else {
1476 if (elementNode != null) elementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);
1477 }
1478 }
1479 if (inputElement != definition.rootInputElement) {
1480 if (inputElement.logEntry) {
1481 logEntryFound = true;
1482 logEntryNestedCount++;
1483 }
1484 if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
1485 timeStampFound = true;
1486 if (inputElement.inputFormat.length() == 0) {
1487 errors.append(Messages.CustomXmlParserInputWizardPage_timestampFormatPrompt +
1488 " (" + Messages.CustomXmlParserInputWizardPage_timestampElementPrompt + " " + getName(inputElement) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1489 if (elementNode != null) elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1490 } else {
1491 try {
1492 new SimpleDateFormat(inputElement.inputFormat);
1493 if (elementNode != null) elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1494 } catch (IllegalArgumentException e) {
1495 errors.append(Messages.CustomXmlParserInputWizardPage_invalidTimestampFmtError +
1496 " (" + Messages.CustomXmlParserInputWizardPage_timestampElementPrompt + " " + getName(inputElement) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1497 if (elementNode != null) elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1498 }
1499 }
1500 } else if (inputElement.inputName.length() == 0) {
1501 errors.append(Messages.CustomXmlParserInputWizardPage_missingInputElementNameError);
1502 if (elementNode != null) elementNode.tagText.setBackground(COLOR_LIGHT_RED);
1503 } else {
1504 if (elementNode != null) elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1505 }
1506 }
1507 if (inputElement.attributes != null) {
1508 if (elementNode != null) {
1509 for (Attribute attribute : elementNode.attributes) {
1510 attribute.attributeNameText.setBackground(COLOR_TEXT_BACKGROUND);
1511 }
1512 }
1513 for (int i = 0; i < inputElement.attributes.size(); i++) {
1514 InputAttribute attribute = inputElement.attributes.get(i);
1515 boolean duplicate = false;
1516 for (int j = i + 1; j < inputElement.attributes.size(); j++) {
1517 InputAttribute otherAttribute = inputElement.attributes.get(j);
1518 if (otherAttribute.attributeName.equals(attribute.attributeName)) {
1519 duplicate = true;
1520 if (elementNode != null) {
1521 elementNode.attributes.get(j).attributeNameText.setBackground(COLOR_LIGHT_RED);
1522 }
1523 }
1524 }
1525 if (attribute.attributeName.length() == 0) {
1526 errors.append(Messages.CustomXmlParserInputWizardPage_missingAttribute +
1527 " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(inputElement) + ": ?). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1528 if (elementNode != null) elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED);
1529 } else if (duplicate) {
1530 errors.append(Messages.CustomXmlParserInputWizardPage_duplicateAttributeError +
1531 " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(attribute, inputElement) +"). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1532 if (elementNode != null) elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED);
1533 }
1534 if (attribute.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {
1535 timeStampFound = true;
1536 if (attribute.inputFormat.length() == 0) {
1537 errors.append(Messages.CustomXmlParserInputWizardPage_missingTimestampInFmtError +
1538 " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(attribute, inputElement) +"). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1539 if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1540 } else {
1541 try {
1542 new SimpleDateFormat(attribute.inputFormat);
1543 if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);
1544 } catch (IllegalArgumentException e) {
1545 errors.append(Messages.CustomXmlParserInputWizardPage_invalidTimestampInFmtError +
1546 " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(attribute, inputElement) +"). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1547 if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1548 }
1549 }
1550 } else if (attribute.inputName.length() == 0) {
1551 errors.append(Messages.CustomXmlParserInputWizardPage_missingDataGroupNameError +
1552 " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(attribute, inputElement) +"). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1553 if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);
1554 } else {
1555 if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);
1556 }
1557 }
1558 }
1559 if (inputElement.childElements != null) {
1560 for (InputElement child : inputElement.childElements) {
1561 ElementNode childElementNode = null;
1562 if (selectedElement != null && selectedElement.inputElement.equals(child)) childElementNode = selectedElement;
1563 if (childElementNode != null) childElementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);
1564 }
1565 for (int i = 0; i < inputElement.childElements.size(); i++) {
1566 InputElement child = inputElement.childElements.get(i);
1567 ElementNode childElementNode = null;
1568 if (selectedElement != null && selectedElement.inputElement.equals(child)) childElementNode = selectedElement;
1569 if (child.elementName.length() == 0) {
1570 errors.append(Messages.CustomXmlParserInputWizardPage_missingElementNameError +
1571 " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(child) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1572 if (childElementNode != null) childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1573 } else {
1574 boolean duplicate = false;
1575 for (int j = i + 1; j < inputElement.childElements.size(); j++) {
1576 InputElement otherChild = inputElement.childElements.get(j);
1577 if (otherChild.elementName.equals(child.elementName)) {
1578 duplicate = true;
1579 ElementNode otherChildElementNode = null;
1580 if (selectedElement != null && selectedElement.inputElement.equals(otherChild)) otherChildElementNode = selectedElement;
1581 if (otherChildElementNode != null) otherChildElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1582 }
1583 }
1584 if (duplicate) {
1585 errors.append(Messages.CustomXmlParserInputWizardPage_duplicateElementNameError +
1586 " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(child) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1587 if (childElementNode != null) childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);
1588 }
1589 }
1590
1591 errors.append(validateElement(child));
1592 }
1593 }
1594 if (inputElement.logEntry) {
1595 logEntryNestedCount--;
1596 }
1597 return errors;
1598 }
1599
1600 public CustomXmlTraceDefinition getDefinition() {
1601 return definition;
1602 }
1603
1604 public char[] getInputText() {
1605 return inputText.getText().toCharArray();
1606 }
1607 }
This page took 0.082082 seconds and 5 git commands to generate.