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