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