+package org.eclipse.linuxtools.tmf.ui.wizards;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.ByteArrayInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.text.ParseException;\r
+import java.text.SimpleDateFormat;\r
+import java.util.ArrayList;\r
+import java.util.Date;\r
+import java.util.List;\r
+\r
+import javax.xml.parsers.DocumentBuilder;\r
+import javax.xml.parsers.DocumentBuilderFactory;\r
+import javax.xml.parsers.ParserConfigurationException;\r
+\r
+import org.eclipse.core.resources.IFile;\r
+import org.eclipse.core.runtime.CoreException;\r
+import org.eclipse.jface.viewers.ColumnLabelProvider;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.ISelectionChangedListener;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.viewers.ITreeContentProvider;\r
+import org.eclipse.jface.viewers.SelectionChangedEvent;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.jface.viewers.TreeViewer;\r
+import org.eclipse.jface.viewers.Viewer;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;\r
+import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTrace;\r
+import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition;\r
+import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition.InputAttribute;\r
+import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition.InputElement;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.browser.Browser;\r
+import org.eclipse.swt.browser.TitleEvent;\r
+import org.eclipse.swt.browser.TitleListener;\r
+import org.eclipse.swt.custom.SashForm;\r
+import org.eclipse.swt.custom.ScrolledComposite;\r
+import org.eclipse.swt.custom.StyleRange;\r
+import org.eclipse.swt.custom.StyledText;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.events.SelectionListener;\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.graphics.Font;\r
+import org.eclipse.swt.graphics.FontData;\r
+import org.eclipse.swt.graphics.Image;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Combo;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Group;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.w3c.dom.Document;\r
+import org.w3c.dom.Element;\r
+import org.w3c.dom.NamedNodeMap;\r
+import org.w3c.dom.Node;\r
+import org.w3c.dom.NodeList;\r
+import org.xml.sax.EntityResolver;\r
+import org.xml.sax.ErrorHandler;\r
+import org.xml.sax.InputSource;\r
+import org.xml.sax.SAXException;\r
+import org.xml.sax.SAXParseException;\r
+\r
+public class CustomXmlParserInputWizardPage extends WizardPage {\r
+\r
+ private static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";\r
+ private static final String SIMPLE_DATE_FORMAT_URL = "http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#skip-navbar_top";\r
+ private static final Image elementImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/element_icon.gif");\r
+ private static final Image addImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/add_button.gif");\r
+ private static final Image addNextImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/addnext_button.gif");\r
+ private static final Image addChildImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/addchild_button.gif");\r
+ private static final Image addManyImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/addmany_button.gif");\r
+ private static final Image deleteImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/delete_button.gif");\r
+ private static final Image moveUpImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/moveup_button.gif");\r
+ private static final Image moveDownImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/movedown_button.gif");\r
+ private static final Image helpImage = TmfUiPlugin.getDefault().getImageFromPath("/icons/help_button.gif");\r
+ private static final Color COLOR_LIGHT_RED = new Color(Display.getDefault(), 255, 192, 192);\r
+ private static final Color COLOR_TEXT_BACKGROUND = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);\r
+ private static final Color COLOR_WIDGET_BACKGROUND = Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);\r
+\r
+ private ISelection selection;\r
+ private CustomXmlTraceDefinition definition;\r
+ private String editDefinitionName;\r
+ private String defaultDescription;\r
+ private ElementNode selectedElement;\r
+ private Composite container;\r
+ private Text logtypeText;\r
+ private Text timeStampOutputFormatText;\r
+ private Text timeStampPreviewText;\r
+ private Button removeButton;\r
+ private Button addChildButton;\r
+ private Button addNextButton;\r
+ private Button moveUpButton;\r
+ private Button moveDownButton;\r
+ private Button feelingLuckyButton;\r
+ private ScrolledComposite treeScrolledComposite;\r
+ private ScrolledComposite elementScrolledComposite;\r
+ private TreeViewer treeViewer;\r
+ private Composite treeContainer;\r
+ private Composite elementContainer;\r
+ private Text errorText;\r
+ private StyledText inputText;\r
+ private Font fixedFont;\r
+ private UpdateListener updateListener;\r
+ private Browser helpBrowser;\r
+ private Element documentElement;\r
+\r
+ // variables used recursively through element traversal\r
+ private String timeStampValue;\r
+ private String timeStampFormat;\r
+ private boolean timeStampFound;\r
+ private int logEntriesCount;\r
+ private boolean logEntryFound;\r
+ private int logEntryNestedCount;\r
+\r
+ protected CustomXmlParserInputWizardPage(ISelection selection, CustomXmlTraceDefinition definition) {\r
+ super("CustomXmlParserWizardPage");\r
+ if (definition == null) {\r
+ setTitle("New Custom XML Parser");\r
+ defaultDescription = "Create a new custom parser for XML log files";\r
+ } else {\r
+ setTitle("Edit Custom XML Parser");\r
+ defaultDescription = "Edit an existing custom parser for XML log files";\r
+ }\r
+ setDescription(defaultDescription);\r
+ this.selection = selection;\r
+ this.definition = definition;\r
+ if (definition != null) {\r
+ this.editDefinitionName = definition.definitionName;\r
+ }\r
+ }\r
+\r
+ public void createControl(Composite parent) {\r
+ container = new Composite(parent, SWT.NULL);\r
+ container.setLayout(new GridLayout());\r
+\r
+ updateListener = new UpdateListener();\r
+ \r
+ Composite headerComposite = new Composite(container, SWT.FILL);\r
+ GridLayout headerLayout = new GridLayout(5, false);\r
+ headerLayout.marginHeight = 0;\r
+ headerLayout.marginWidth = 0;\r
+ headerComposite.setLayout(headerLayout);\r
+ headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ \r
+ Label logtypeLabel = new Label(headerComposite, SWT.NULL);\r
+ logtypeLabel.setText("Log type:");\r
+ \r
+ logtypeText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);\r
+ logtypeText.setLayoutData(new GridData(120, SWT.DEFAULT));\r
+ logtypeText.addModifyListener(updateListener);\r
+ \r
+ Label timeStampFormatLabel = new Label(headerComposite, SWT.NULL);\r
+ timeStampFormatLabel.setText("Time Stamp format:");\r
+ \r
+ timeStampOutputFormatText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE);\r
+ timeStampOutputFormatText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ timeStampOutputFormatText.setText(DEFAULT_TIMESTAMP_FORMAT);\r
+ timeStampOutputFormatText.addModifyListener(updateListener);\r
+\r
+ Button dateFormatHelpButton = new Button(headerComposite, SWT.PUSH);\r
+ dateFormatHelpButton.setImage(helpImage);\r
+ dateFormatHelpButton.setToolTipText("Date Format Help");\r
+ dateFormatHelpButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ openHelpShell(SIMPLE_DATE_FORMAT_URL);\r
+ }\r
+ });\r
+ \r
+ Label timeStampPreviewLabel = new Label(headerComposite, SWT.NULL);\r
+ timeStampPreviewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 3, 1));\r
+ timeStampPreviewLabel.setText("Preview:");\r
+ \r
+ timeStampPreviewText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);\r
+ timeStampPreviewText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));\r
+ timeStampPreviewText.setText("*no time stamp element or attribute*");\r
+\r
+ createButtonBar();\r
+ \r
+ SashForm vSash = new SashForm(container, SWT.VERTICAL);\r
+ vSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
+ vSash.setBackground(vSash.getDisplay().getSystemColor(SWT.COLOR_GRAY));\r
+\r
+ SashForm hSash = new SashForm(vSash, SWT.HORIZONTAL);\r
+ hSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
+\r
+ treeScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL | SWT.H_SCROLL);\r
+ treeScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
+ treeContainer = new Composite(treeScrolledComposite, SWT.NONE);\r
+ treeContainer.setLayout(new FillLayout());\r
+ treeScrolledComposite.setContent(treeContainer);\r
+ treeScrolledComposite.setExpandHorizontal(true);\r
+ treeScrolledComposite.setExpandVertical(true);\r
+ \r
+ treeViewer = new TreeViewer(treeContainer, SWT.SINGLE | SWT.BORDER);\r
+ treeViewer.setContentProvider(new InputElementTreeNodeContentProvider());\r
+ treeViewer.setLabelProvider(new InputElementTreeLabelProvider());\r
+ treeViewer.addSelectionChangedListener(new InputElementTreeSelectionChangedListener());\r
+ treeContainer.layout();\r
+ \r
+ treeScrolledComposite.setMinSize(treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);\r
+ \r
+ elementScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL);\r
+ elementScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
+ elementContainer = new Composite(elementScrolledComposite, SWT.NONE);\r
+ GridLayout gl = new GridLayout();\r
+ gl.marginHeight = 1;\r
+ gl.marginWidth = 0;\r
+ elementContainer.setLayout(gl);\r
+ elementScrolledComposite.setContent(elementContainer);\r
+ elementScrolledComposite.setExpandHorizontal(true);\r
+ elementScrolledComposite.setExpandVertical(true);\r
+\r
+ if (definition == null) {\r
+ definition = new CustomXmlTraceDefinition();\r
+ }\r
+ loadDefinition(definition);\r
+ treeViewer.expandAll();\r
+ elementContainer.layout();\r
+ \r
+ elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);\r
+ \r
+ hSash.setWeights(new int[] {1, 2});\r
+ \r
+ if (definition.rootInputElement == null) {\r
+ removeButton.setEnabled(false);\r
+ addChildButton.setToolTipText("Add document element");\r
+ addNextButton.setEnabled(false);\r
+ moveUpButton.setEnabled(false);\r
+ moveDownButton.setEnabled(false);\r
+ } else { // root is selected\r
+ addNextButton.setEnabled(false);\r
+ }\r
+ \r
+ Composite sashBottom = new Composite(vSash, SWT.NONE);\r
+ GridLayout sashBottomLayout = new GridLayout(2, false);\r
+ sashBottomLayout.marginHeight = 0;\r
+ sashBottomLayout.marginWidth = 0;\r
+ sashBottom.setLayout(sashBottomLayout);\r
+\r
+ Label previewLabel = new Label(sashBottom, SWT.NULL);\r
+ previewLabel.setText("Preview input");\r
+\r
+ errorText = new Text(sashBottom, SWT.SINGLE | SWT.READ_ONLY);\r
+ errorText.setBackground(COLOR_WIDGET_BACKGROUND);\r
+ errorText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ errorText.setVisible(false);\r
+ \r
+ inputText = new StyledText(sashBottom, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);\r
+ if (fixedFont == null) {\r
+ if (System.getProperty("os.name").contains("Windows")) {\r
+ fixedFont = new Font(Display.getCurrent(), new FontData("Courier New", 10, SWT.NORMAL));\r
+ } else {\r
+ fixedFont = new Font(Display.getCurrent(), new FontData("Monospace", 10, SWT.NORMAL));\r
+ }\r
+ }\r
+ inputText.setFont(fixedFont);\r
+ GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);\r
+ gd.heightHint = inputText.computeSize(SWT.DEFAULT, inputText.getLineHeight() * 4).y;\r
+ gd.widthHint = 800;\r
+ inputText.setLayoutData(gd);\r
+ inputText.setText(getSelectionText());\r
+ inputText.addModifyListener(new ModifyListener(){\r
+ public void modifyText(ModifyEvent e) {\r
+ parseXmlInput(inputText.getText());\r
+ }});\r
+ inputText.addModifyListener(updateListener);\r
+\r
+ vSash.setWeights(new int[] {hSash.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sashBottom.computeSize(SWT.DEFAULT, SWT.DEFAULT).y});\r
+ \r
+ setControl(container);\r
+ }\r
+\r
+ private void createButtonBar() {\r
+ Composite buttonBar = new Composite(container, SWT.NONE);\r
+ GridLayout buttonBarLayout = new GridLayout(6, false);\r
+ buttonBarLayout.marginHeight = 0;\r
+ buttonBarLayout.marginWidth = 0;\r
+ buttonBar.setLayout(buttonBarLayout);\r
+ \r
+ removeButton = new Button(buttonBar, SWT.PUSH);\r
+ removeButton.setImage(deleteImage);\r
+ removeButton.setToolTipText("Remove element");\r
+ removeButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ if (treeViewer.getSelection().isEmpty() || selectedElement == null) return;\r
+ removeElement();\r
+ InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();\r
+ if (inputElement == definition.rootInputElement) {\r
+ definition.rootInputElement = null;\r
+ } else {\r
+ inputElement.parentElement.childElements.remove(inputElement);\r
+ }\r
+ treeViewer.refresh();\r
+ validate();\r
+ updatePreviews();\r
+ removeButton.setEnabled(false);\r
+ if (definition.rootInputElement == null) {\r
+ addChildButton.setEnabled(true);\r
+ addChildButton.setToolTipText("Add document element");\r
+ } else {\r
+ addChildButton.setEnabled(false);\r
+ }\r
+ addNextButton.setEnabled(false);\r
+ moveUpButton.setEnabled(false);\r
+ moveDownButton.setEnabled(false);\r
+ }\r
+ });\r
+ \r
+ addChildButton = new Button(buttonBar, SWT.PUSH);\r
+ addChildButton.setImage(addChildImage);\r
+ addChildButton.setToolTipText("Add child element");\r
+ addChildButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ InputElement inputElement = new InputElement("", false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null);\r
+ if (definition.rootInputElement == null) {\r
+ definition.rootInputElement = inputElement;\r
+ inputElement.elementName = getChildNameSuggestion(null);\r
+ } else if (treeViewer.getSelection().isEmpty()) {\r
+ return;\r
+ } else {\r
+ InputElement parentInputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();\r
+ parentInputElement.addChild(inputElement);\r
+ inputElement.elementName = getChildNameSuggestion(parentInputElement);\r
+ }\r
+ treeViewer.refresh();\r
+ treeViewer.setSelection(new StructuredSelection(inputElement), true);\r
+ }\r
+ });\r
+ \r
+ addNextButton = new Button(buttonBar, SWT.PUSH);\r
+ addNextButton.setImage(addNextImage);\r
+ addNextButton.setToolTipText("Add next element");\r
+ addNextButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ InputElement inputElement = new InputElement("", false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null);\r
+ if (definition.rootInputElement == null) {\r
+ definition.rootInputElement = inputElement;\r
+ inputElement.elementName = getChildNameSuggestion(null);\r
+ } else if (treeViewer.getSelection().isEmpty()) {\r
+ return;\r
+ } else {\r
+ InputElement previousInputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();\r
+ if (previousInputElement == definition.rootInputElement) {\r
+ return;\r
+ } else {\r
+ previousInputElement.addNext(inputElement);\r
+ inputElement.elementName = getChildNameSuggestion(inputElement.parentElement);\r
+ }\r
+ }\r
+ treeViewer.refresh();\r
+ treeViewer.setSelection(new StructuredSelection(inputElement), true);\r
+ }\r
+ });\r
+ \r
+ feelingLuckyButton = new Button(buttonBar, SWT.PUSH);\r
+ feelingLuckyButton.setImage(addManyImage);\r
+ feelingLuckyButton.setToolTipText("Feeling lucky");\r
+ feelingLuckyButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ InputElement inputElement = null;\r
+ if (definition.rootInputElement == null) {\r
+ if (getChildNameSuggestion(null).length() != 0) {\r
+ inputElement = new InputElement(getChildNameSuggestion(null), false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null);\r
+ definition.rootInputElement = inputElement;\r
+ feelingLucky(inputElement);\r
+ }\r
+ } else if (treeViewer.getSelection().isEmpty()) {\r
+ return;\r
+ } else {\r
+ inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();\r
+ feelingLucky(inputElement);\r
+ }\r
+ treeViewer.refresh();\r
+ treeViewer.setSelection(new StructuredSelection(inputElement), true);\r
+ treeViewer.expandToLevel(inputElement, TreeViewer.ALL_LEVELS);\r
+ }\r
+ });\r
+ \r
+ moveUpButton = new Button(buttonBar, SWT.PUSH);\r
+ moveUpButton.setImage(moveUpImage);\r
+ moveUpButton.setToolTipText("Move up");\r
+ moveUpButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ if (treeViewer.getSelection().isEmpty()) return;\r
+ InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();\r
+ if (inputElement == definition.rootInputElement) {\r
+ return;\r
+ } else {\r
+ inputElement.moveUp();\r
+ }\r
+ treeViewer.refresh();\r
+ validate();\r
+ updatePreviews();\r
+ }\r
+ });\r
+ \r
+ moveDownButton = new Button(buttonBar, SWT.PUSH);\r
+ moveDownButton.setImage(moveDownImage);\r
+ moveDownButton.setToolTipText("Move down");\r
+ moveDownButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ if (treeViewer.getSelection().isEmpty()) return;\r
+ InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();\r
+ if (inputElement == definition.rootInputElement) {\r
+ return;\r
+ } else {\r
+ inputElement.moveDown();\r
+ }\r
+ treeViewer.refresh();\r
+ validate();\r
+ updatePreviews();\r
+ }\r
+ });\r
+ }\r
+\r
+ private void feelingLucky(InputElement inputElement) {\r
+ while (true) {\r
+ String attributeName = getAttributeNameSuggestion(inputElement);\r
+ if (attributeName.length() == 0) {\r
+ break;\r
+ } else {\r
+ InputAttribute attribute = new InputAttribute(attributeName, attributeName, 0, "");\r
+ inputElement.addAttribute(attribute);\r
+ }\r
+ }\r
+ while (true) {\r
+ String childName = getChildNameSuggestion(inputElement);\r
+ if (childName.length() == 0) {\r
+ break;\r
+ } else {\r
+ InputElement childElement = new InputElement(childName, false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null);\r
+ inputElement.addChild(childElement);\r
+ feelingLucky(childElement);\r
+ }\r
+ }\r
+ }\r
+ \r
+ private class InputElementTreeNodeContentProvider implements ITreeContentProvider {\r
+\r
+ public Object[] getElements(Object inputElement) {\r
+ CustomXmlTraceDefinition def = (CustomXmlTraceDefinition) inputElement;\r
+ if (def.rootInputElement != null) {\r
+ return new Object[] {def.rootInputElement};\r
+ } else {\r
+ return new Object[0];\r
+ }\r
+ }\r
+\r
+ public Object[] getChildren(Object parentElement) {\r
+ InputElement inputElement = (InputElement) parentElement;\r
+ if (inputElement.childElements == null) return new InputElement[0];\r
+ return inputElement.childElements.toArray();\r
+ }\r
+\r
+ public boolean hasChildren(Object element) {\r
+ InputElement inputElement = (InputElement) element;\r
+ return (inputElement.childElements != null && inputElement.childElements.size() > 0);\r
+ }\r
+\r
+ public void dispose() {\r
+ }\r
+\r
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {\r
+ }\r
+\r
+ public Object getParent(Object element) {\r
+ InputElement inputElement = (InputElement) element;\r
+ return inputElement.parentElement;\r
+ }\r
+ }\r
+ \r
+ private class InputElementTreeLabelProvider extends ColumnLabelProvider {\r
+\r
+ @Override\r
+ public Image getImage(Object element) {\r
+ return elementImage;\r
+ }\r
+\r
+ @Override\r
+ public String getText(Object element) {\r
+ InputElement inputElement = (InputElement) element;\r
+ return (inputElement.elementName.trim().length() == 0) ? "?" : inputElement.elementName;\r
+ }\r
+ }\r
+\r
+ private class InputElementTreeSelectionChangedListener implements ISelectionChangedListener {\r
+ public void selectionChanged(SelectionChangedEvent event) {\r
+ if (selectedElement != null) {\r
+ selectedElement.dispose();\r
+ }\r
+ if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {\r
+ IStructuredSelection selection = (IStructuredSelection) event.getSelection();\r
+ InputElement inputElement = (InputElement) selection.getFirstElement();\r
+ selectedElement = new ElementNode(elementContainer, inputElement);\r
+ elementContainer.layout();\r
+ elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);\r
+ container.layout();\r
+ validate();\r
+ updatePreviews();\r
+ removeButton.setEnabled(true);\r
+ addChildButton.setEnabled(true);\r
+ addChildButton.setToolTipText("Add child element");\r
+ if (definition.rootInputElement == inputElement) {\r
+ addNextButton.setEnabled(false);\r
+ } else {\r
+ addNextButton.setEnabled(true);\r
+ }\r
+ moveUpButton.setEnabled(true);\r
+ moveDownButton.setEnabled(true);\r
+ } else {\r
+ removeButton.setEnabled(false);\r
+ if (definition.rootInputElement == null) {\r
+ addChildButton.setEnabled(true);\r
+ addChildButton.setToolTipText("Add document element");\r
+ } else {\r
+ addChildButton.setEnabled(false);\r
+ }\r
+ addNextButton.setEnabled(false);\r
+ moveUpButton.setEnabled(false);\r
+ moveDownButton.setEnabled(false);\r
+ }\r
+ }\r
+ }\r
+ \r
+ /* (non-Javadoc)\r
+ * @see org.eclipse.jface.dialogs.DialogPage#dispose()\r
+ */\r
+ @Override\r
+ public void dispose() {\r
+ if (fixedFont != null) {\r
+ fixedFont.dispose();\r
+ fixedFont = null;\r
+ }\r
+ super.dispose();\r
+ }\r
+\r
+ private void loadDefinition(CustomXmlTraceDefinition def) {\r
+ logtypeText.setText(def.definitionName);\r
+ timeStampOutputFormatText.setText(def.timeStampOutputFormat);\r
+ treeViewer.setInput(def);\r
+ \r
+ if (def.rootInputElement != null) {\r
+ treeViewer.setSelection(new StructuredSelection(def.rootInputElement));\r
+ }\r
+ }\r
+ \r
+ private String getName(InputElement inputElement) {\r
+ String name = (inputElement.elementName.trim().length() == 0) ? "?" : inputElement.elementName.trim();\r
+ if (inputElement.parentElement == null) {\r
+ return name;\r
+ }\r
+ return getName(inputElement.parentElement) + " : " + name;\r
+ }\r
+\r
+ private String getName(InputAttribute inputAttribute, InputElement inputElement) {\r
+ String name = (inputAttribute.attributeName.trim().length() == 0) ? "?" : inputAttribute.attributeName.trim();\r
+ return getName(inputElement) + " : " + name;\r
+ }\r
+\r
+ /* (non-Javadoc)\r
+ * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)\r
+ */\r
+ @Override\r
+ public void setVisible(boolean visible) {\r
+ if (visible) {\r
+ validate();\r
+ updatePreviews();\r
+ }\r
+ super.setVisible(visible);\r
+ }\r
+\r
+ public List<String> getInputNames() {\r
+ return getInputNames(definition.rootInputElement);\r
+ }\r
+ \r
+ public List<String> getInputNames(InputElement inputElement) {\r
+ List<String> inputs = new ArrayList<String>();\r
+ if (inputElement.inputName != null && !inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {\r
+ String inputName = inputElement.inputName;\r
+ if (!inputs.contains(inputName)) {\r
+ inputs.add(inputName);\r
+ }\r
+ }\r
+ if (inputElement.attributes != null) {\r
+ for (InputAttribute attribute : inputElement.attributes) {\r
+ String inputName = attribute.inputName;\r
+ if (!inputs.contains(inputName)) {\r
+ inputs.add(inputName);\r
+ }\r
+ }\r
+ }\r
+ if (inputElement.childElements != null) {\r
+ for (InputElement childInputElement : inputElement.childElements) {\r
+ for (String inputName : getInputNames(childInputElement)) {\r
+ if (!inputs.contains(inputName)) {\r
+ inputs.add(inputName);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return inputs;\r
+ }\r
+ \r
+ private void removeElement() {\r
+ selectedElement.dispose();\r
+ selectedElement = null;\r
+ elementContainer.layout();\r
+ elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);\r
+ container.layout();\r
+ }\r
+\r
+ private String getSelectionText() {\r
+ InputStream inputStream = null;\r
+ if (this.selection instanceof IStructuredSelection) {\r
+ Object selection = ((IStructuredSelection)this.selection).getFirstElement();\r
+ if (selection instanceof IFile) {\r
+ IFile file = (IFile)selection;\r
+ try {\r
+ inputStream = file.getContents();\r
+ } catch (CoreException e) {\r
+ return "";\r
+ }\r
+ }\r
+ }\r
+ if (inputStream != null) {\r
+ try {\r
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));\r
+ StringBuilder sb = new StringBuilder();\r
+ String line = null;\r
+ while ((line = reader.readLine()) != null) {\r
+ sb.append(line + "\n");\r
+ }\r
+ parseXmlInput(sb.toString());\r
+ return sb.toString();\r
+ } catch (IOException e) {\r
+ return "";\r
+ }\r
+ }\r
+ return "";\r
+ }\r
+ \r
+ private void parseXmlInput(final String string) {\r
+ try {\r
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r
+ DocumentBuilder db = dbf.newDocumentBuilder();\r
+\r
+ // The following allows xml parsing without access to the dtd\r
+ EntityResolver resolver = new EntityResolver () {\r
+ public InputSource resolveEntity (String publicId, String systemId) {\r
+ String empty = "";\r
+ ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r
+ return new InputSource(bais);\r
+ }\r
+ };\r
+ db.setEntityResolver(resolver);\r
+\r
+ // The following catches xml parsing exceptions\r
+ db.setErrorHandler(new ErrorHandler(){\r
+ public void error(SAXParseException saxparseexception) throws SAXException {}\r
+ public void warning(SAXParseException saxparseexception) throws SAXException {}\r
+ public void fatalError(SAXParseException saxparseexception) throws SAXException {\r
+ if (string.trim().length() != 0) {\r
+ errorText.setText(saxparseexception.getMessage());\r
+ errorText.setBackground(COLOR_LIGHT_RED);\r
+ errorText.setVisible(true);\r
+ }\r
+ throw saxparseexception;\r
+ }});\r
+ \r
+ errorText.setVisible(false);\r
+ Document doc = null;\r
+ doc = db.parse(new ByteArrayInputStream(string.getBytes()));\r
+ documentElement = doc.getDocumentElement();\r
+ } catch (ParserConfigurationException e) {\r
+ e.printStackTrace();\r
+ documentElement = null;\r
+ } catch (SAXException e) {\r
+ documentElement = null;\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ documentElement = null;\r
+ }\r
+ }\r
+\r
+ private void updatePreviews() {\r
+ updatePreviews(false);\r
+ }\r
+\r
+ private void updatePreviews(boolean updateAll) {\r
+ if (inputText == null) {\r
+ // early update during construction\r
+ return;\r
+ }\r
+ inputText.setStyleRanges(new StyleRange[] {});\r
+ if (selectedElement == null) {\r
+ return;\r
+ }\r
+ \r
+ timeStampValue = null;\r
+ timeStampFormat = null;\r
+ logEntriesCount = 0;\r
+ logEntryFound = false;\r
+ \r
+ selectedElement.updatePreview();\r
+\r
+ if (timeStampValue != null && timeStampFormat != null) {\r
+ try {\r
+ SimpleDateFormat dateFormat = new SimpleDateFormat(timeStampFormat);\r
+ Date date = dateFormat.parse(timeStampValue);\r
+ dateFormat = new SimpleDateFormat(timeStampOutputFormatText.getText().trim());\r
+ timeStampPreviewText.setText(dateFormat.format(date));\r
+ } catch (ParseException e) {\r
+ timeStampPreviewText.setText("*parse exception* [" + timeStampValue + "] <> [" + timeStampFormat + "]");\r
+ } catch (IllegalArgumentException e) {\r
+ timeStampPreviewText.setText("*parse exception* [Illegal Argument]");\r
+ }\r
+ } else {\r
+ timeStampPreviewText.setText("*no matching time stamp*");\r
+ }\r
+ }\r
+\r
+ private void openHelpShell(String url) {\r
+ if (helpBrowser != null && !helpBrowser.isDisposed()) {\r
+ helpBrowser.getShell().setActive();\r
+ if (!helpBrowser.getUrl().equals(url)) {\r
+ helpBrowser.setUrl(url);\r
+ }\r
+ return;\r
+ }\r
+ final Shell helpShell = new Shell(getShell(), SWT.SHELL_TRIM);\r
+ helpShell.setLayout(new FillLayout());\r
+ helpBrowser = new Browser(helpShell, SWT.NONE);\r
+ helpBrowser.addTitleListener(new TitleListener() {\r
+ public void changed(TitleEvent event) {\r
+ helpShell.setText(event.title);\r
+ }\r
+ });\r
+ helpBrowser.setBounds(0,0,600,400);\r
+ helpShell.pack();\r
+ helpShell.open();\r
+ helpBrowser.setUrl(url);\r
+ }\r
+\r
+ private class UpdateListener implements ModifyListener, SelectionListener {\r
+\r
+ public void modifyText(ModifyEvent e) {\r
+ validate();\r
+ updatePreviews();\r
+ }\r
+\r
+ public void widgetDefaultSelected(SelectionEvent e) {\r
+ validate();\r
+ updatePreviews();\r
+ }\r
+\r
+ public void widgetSelected(SelectionEvent e) {\r
+ validate();\r
+ updatePreviews();\r
+ }\r
+\r
+ }\r
+ \r
+ private class ElementNode {\r
+ final InputElement inputElement;\r
+ final Group group;\r
+ ArrayList<Attribute> attributes = new ArrayList<Attribute>();\r
+ ArrayList<ElementNode> childElements = new ArrayList<ElementNode>();\r
+ Text elementNameText;\r
+ Composite tagComposite;\r
+ Combo tagCombo;\r
+ Label tagLabel;\r
+ Text tagText;\r
+ Combo actionCombo;\r
+ Label previewLabel;\r
+ Text previewText;\r
+ Button logEntryButton;\r
+ Label fillerLabel;\r
+ Composite addAttributeComposite;\r
+ Button addAttributeButton;\r
+ Label addAttributeLabel;\r
+ \r
+ public ElementNode(Composite parent, InputElement inputElement) {\r
+ this.inputElement = inputElement;\r
+ \r
+ group = new Group(parent, SWT.NONE);\r
+ GridLayout gl = new GridLayout(2, false);\r
+ gl.marginHeight = 0;\r
+ group.setLayout(gl);\r
+ group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ group.setText(getName(inputElement));\r
+ \r
+ Label label = new Label(group, SWT.NULL);\r
+ label.setText("Element name:");\r
+ label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
+ \r
+ elementNameText = new Text(group, SWT.BORDER | SWT.SINGLE);\r
+ GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);\r
+ gd.widthHint = 0;\r
+ elementNameText.setLayoutData(gd);\r
+ elementNameText.addModifyListener(new ModifyListener(){\r
+ public void modifyText(ModifyEvent e) {\r
+ ElementNode.this.inputElement.elementName = elementNameText.getText().trim();\r
+ group.setText(getName(ElementNode.this.inputElement));\r
+ }});\r
+ elementNameText.setText(inputElement.elementName);\r
+ elementNameText.addModifyListener(updateListener);\r
+\r
+ if (inputElement.parentElement != null) {\r
+ previewLabel = new Label(group, SWT.NULL);\r
+ previewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
+ previewLabel.setText("Preview:");\r
+ \r
+ previewText = new Text(group, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);\r
+ gd = new GridData(SWT.FILL, SWT.CENTER, true, false);\r
+ gd.widthHint = 0;\r
+ previewText.setLayoutData(gd);\r
+ previewText.setText("*no matching element*");\r
+ previewText.setBackground(COLOR_WIDGET_BACKGROUND);\r
+ \r
+ logEntryButton = new Button(group, SWT.CHECK);\r
+ logEntryButton.setText("Log Entry");\r
+ logEntryButton.setSelection(inputElement.logEntry);\r
+ logEntryButton.addSelectionListener(new SelectionListener(){\r
+ public void widgetDefaultSelected(SelectionEvent e) {}\r
+ public void widgetSelected(SelectionEvent e) {\r
+ InputElement parent = ElementNode.this.inputElement.parentElement;\r
+ while (parent != null) {\r
+ parent.logEntry = false;\r
+ parent = parent.parentElement;\r
+ }\r
+ }});\r
+ logEntryButton.addSelectionListener(updateListener);\r
+ \r
+ tagComposite = new Composite(group, SWT.FILL);\r
+ GridLayout tagLayout = new GridLayout(4, false);\r
+ tagLayout.marginWidth = 0;\r
+ tagLayout.marginHeight = 0;\r
+ tagComposite.setLayout(tagLayout);\r
+ tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ \r
+ tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);\r
+ tagCombo.setItems(new String[] {CustomXmlTraceDefinition.TAG_IGNORE,\r
+ CustomXmlTraceDefinition.TAG_TIMESTAMP,\r
+ CustomXmlTraceDefinition.TAG_MESSAGE,\r
+ CustomXmlTraceDefinition.TAG_OTHER});\r
+ tagCombo.setVisibleItemCount(tagCombo.getItemCount());\r
+ tagCombo.addSelectionListener(new SelectionListener(){\r
+ public void widgetDefaultSelected(SelectionEvent e) {}\r
+ public void widgetSelected(SelectionEvent e) {\r
+ tagText.removeModifyListener(updateListener);\r
+ switch (tagCombo.getSelectionIndex()) {\r
+ case 0: //Ignore\r
+ tagLabel.setVisible(false);\r
+ tagText.setVisible(false);\r
+ actionCombo.setVisible(false);\r
+ break;\r
+ case 1: //Time Stamp\r
+ tagLabel.setText("format:");\r
+ tagLabel.setVisible(true);\r
+ tagText.setVisible(true);\r
+ tagText.addModifyListener(updateListener);\r
+ actionCombo.setVisible(true);\r
+ break;\r
+ case 2: //Message\r
+ tagLabel.setVisible(false);\r
+ tagText.setVisible(false);\r
+ actionCombo.setVisible(true);\r
+ break;\r
+ case 3: //Other\r
+ tagLabel.setText("tag name:");\r
+ tagLabel.setVisible(true);\r
+ if (tagText.getText().trim().length() == 0) {\r
+ tagText.setText(elementNameText.getText().trim());\r
+ }\r
+ tagText.setVisible(true);\r
+ tagText.addModifyListener(updateListener);\r
+ actionCombo.setVisible(true);\r
+ break;\r
+ }\r
+ tagComposite.layout();\r
+ validate();\r
+ updatePreviews();\r
+ }});\r
+ \r
+ tagLabel = new Label(tagComposite, SWT.NULL);\r
+ tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
+ \r
+ tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);\r
+ gd = new GridData(SWT.FILL, SWT.CENTER, true, false);\r
+ gd.widthHint = 0;\r
+ tagText.setLayoutData(gd);\r
+ \r
+ actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);\r
+ actionCombo.setItems(new String[] {"Set", "Append", "Append with |"});\r
+ actionCombo.select(inputElement.inputAction);\r
+ actionCombo.addSelectionListener(updateListener);\r
+ \r
+ if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {\r
+ tagCombo.select(0);\r
+ tagLabel.setVisible(false);\r
+ tagText.setVisible(false);\r
+ actionCombo.setVisible(false);\r
+ } else if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {\r
+ tagCombo.select(1);\r
+ tagLabel.setText("format:");\r
+ tagText.setText(inputElement.inputFormat);\r
+ tagText.addModifyListener(updateListener);\r
+ } else if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_MESSAGE)) {\r
+ tagCombo.select(2);\r
+ tagLabel.setVisible(false);\r
+ tagText.setVisible(false);\r
+ } else {\r
+ tagCombo.select(3);\r
+ tagLabel.setText("tag name:");\r
+ tagText.setText(inputElement.inputName);\r
+ tagText.addModifyListener(updateListener);\r
+ }\r
+ }\r
+\r
+ if (inputElement.attributes != null) {\r
+ for (InputAttribute inputAttribute : inputElement.attributes) {\r
+ Attribute attribute = new Attribute(group, this, inputAttribute, attributes.size()+1);\r
+ attributes.add(attribute);\r
+ }\r
+ }\r
+ \r
+ createAddButton();\r
+ }\r
+ \r
+ private void updatePreview() {\r
+ Element element = getPreviewElement(inputElement);\r
+ if (inputElement.parentElement != null) { // no preview text for document element\r
+ previewText.setText("*no matching element*");\r
+ if (element != null) {\r
+ previewText.setText(CustomXmlTrace.parseElement(element, new StringBuffer()).toString());\r
+ if (logEntryButton.getSelection()) {\r
+ if (logEntryFound == false) {\r
+ logEntryFound = true;\r
+ logEntriesCount++;\r
+ } else {\r
+ logEntryButton.setSelection(false); // remove nested log entry\r
+ }\r
+ }\r
+ if (tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP) && logEntriesCount <= 1) {\r
+ String value = previewText.getText().trim();\r
+ if (value.length() != 0) {\r
+ if (actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_SET) {\r
+ timeStampValue = value;\r
+ timeStampFormat = tagText.getText().trim();\r
+ } else if (actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND) {\r
+ if (timeStampValue != null) {\r
+ timeStampValue += value;\r
+ timeStampFormat += tagText.getText().trim();\r
+ } else {\r
+ timeStampValue = value;\r
+ timeStampFormat = tagText.getText().trim();\r
+ }\r
+ } else if (actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {\r
+ if (timeStampValue != null) {\r
+ timeStampValue += " | " + value;\r
+ timeStampFormat += " | " + tagText.getText().trim();\r
+ } else {\r
+ timeStampValue = value;\r
+ timeStampFormat = tagText.getText().trim();\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ for (Attribute attribute : attributes) {\r
+ if (element != null) {\r
+ String value = element.getAttribute(attribute.attributeNameText.getText().trim());\r
+ if (value.length() != 0) {\r
+ attribute.previewText.setText(value);\r
+ if (attribute.tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP) && logEntriesCount <= 1) {\r
+ if (attribute.actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_SET) {\r
+ timeStampValue = value;\r
+ timeStampFormat = attribute.tagText.getText().trim();\r
+ } else if (attribute.actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND) {\r
+ if (timeStampValue != null) {\r
+ timeStampValue += value;\r
+ timeStampFormat += attribute.tagText.getText().trim();\r
+ } else {\r
+ timeStampValue = value;\r
+ timeStampFormat = attribute.tagText.getText().trim();\r
+ }\r
+ } else if (attribute.actionCombo.getSelectionIndex() == CustomXmlTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {\r
+ if (timeStampValue != null) {\r
+ timeStampValue += " | " + value;\r
+ timeStampFormat += " | " + attribute.tagText.getText().trim();\r
+ } else {\r
+ timeStampValue = value;\r
+ timeStampFormat = attribute.tagText.getText().trim();\r
+ }\r
+ }\r
+ }\r
+ } else {\r
+ attribute.previewText.setText("*no matching attribute*");\r
+ }\r
+ } else {\r
+ attribute.previewText.setText("*no matching element*");\r
+ }\r
+ }\r
+ for (ElementNode child : childElements) {\r
+ child.updatePreview();\r
+ }\r
+ if (logEntryButton != null && logEntryButton.getSelection()) {\r
+ logEntryFound = false;\r
+ }\r
+ }\r
+\r
+ private void createAddButton() {\r
+ fillerLabel = new Label(group, SWT.NONE);\r
+ \r
+ addAttributeComposite = new Composite(group, SWT.NONE);\r
+ addAttributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ GridLayout addAttributeLayout = new GridLayout(2, false);\r
+ addAttributeLayout.marginHeight = 0;\r
+ addAttributeLayout.marginWidth = 0;\r
+ addAttributeComposite.setLayout(addAttributeLayout);\r
+ \r
+ addAttributeButton = new Button(addAttributeComposite, SWT.PUSH);\r
+ addAttributeButton.setImage(addImage);\r
+ addAttributeButton.setToolTipText("Add attribute");\r
+ addAttributeButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ removeAddButton();\r
+ String attributeName = getAttributeNameSuggestion(inputElement);\r
+ InputAttribute inputAttribute = new InputAttribute(attributeName, attributeName, 0, "");\r
+ attributes.add(new Attribute(group, ElementNode.this, inputAttribute, attributes.size()+1));\r
+ createAddButton();\r
+ elementContainer.layout();\r
+ elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);\r
+ group.getParent().layout();\r
+ validate();\r
+ updatePreviews();\r
+ }\r
+ });\r
+ \r
+ addAttributeLabel = new Label(addAttributeComposite, SWT.NULL);\r
+ addAttributeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ addAttributeLabel.setText("New attribute");\r
+ }\r
+\r
+ private void removeAddButton() {\r
+ fillerLabel.dispose();\r
+ addAttributeComposite.dispose();\r
+ }\r
+ \r
+ private void removeAttribute(int attributeNumber) {\r
+ if (--attributeNumber < attributes.size()) {\r
+ attributes.remove(attributeNumber).dispose();\r
+ for (int i = attributeNumber; i < attributes.size(); i++) {\r
+ attributes.get(i).setAttributeNumber(i+1);\r
+ }\r
+ elementContainer.layout();\r
+ elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);\r
+ group.getParent().layout();\r
+ }\r
+ }\r
+ \r
+ private void dispose() {\r
+ group.dispose();\r
+ }\r
+\r
+ private void extractInputs() {\r
+ inputElement.elementName = elementNameText.getText().trim();\r
+ if (inputElement.parentElement != null) {\r
+ inputElement.logEntry = logEntryButton.getSelection();\r
+ if (tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_OTHER)) {\r
+ inputElement.inputName = tagText.getText().trim();\r
+ } else {\r
+ inputElement.inputName = tagCombo.getText();\r
+ if (tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {\r
+ inputElement.inputFormat = tagText.getText().trim();\r
+ }\r
+ }\r
+ inputElement.inputAction = actionCombo.getSelectionIndex();\r
+ }\r
+ inputElement.attributes = new ArrayList<InputAttribute>(attributes.size());\r
+ for (int i = 0; i < attributes.size(); i++) {\r
+ Attribute attribute = attributes.get(i);\r
+ InputAttribute inputAttribute = new InputAttribute();\r
+ inputAttribute.attributeName = attribute.attributeNameText.getText().trim();\r
+ if (attribute.tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_OTHER)) {\r
+ inputAttribute.inputName = attribute.tagText.getText().trim();\r
+ } else {\r
+ inputAttribute.inputName = attribute.tagCombo.getText();\r
+ if (attribute.tagCombo.getText().equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {\r
+ inputAttribute.inputFormat = attribute.tagText.getText().trim();\r
+ }\r
+ }\r
+ inputAttribute.inputAction = attribute.actionCombo.getSelectionIndex();\r
+ inputElement.addAttribute(inputAttribute);\r
+ }\r
+ }\r
+ }\r
+\r
+ private class Attribute {\r
+ ElementNode element;\r
+ int attributeNumber;\r
+ \r
+ // children of parent (must be disposed)\r
+ Composite labelComposite;\r
+ Composite attributeComposite;\r
+ Label filler;\r
+ Composite tagComposite;\r
+\r
+ // children of labelComposite\r
+ Label attributeLabel;\r
+ \r
+ // children of attributeComposite\r
+ Text attributeNameText;\r
+ Text previewText;\r
+ \r
+ // children of tagComposite\r
+ Combo tagCombo;\r
+ Label tagLabel;\r
+ Text tagText;\r
+ Combo actionCombo;\r
+ \r
+ public Attribute(Composite parent, ElementNode element, InputAttribute inputAttribute, int attributeNumber) {\r
+ this.element = element;\r
+ this.attributeNumber = attributeNumber;\r
+ \r
+ labelComposite = new Composite(parent, SWT.FILL);\r
+ GridLayout labelLayout = new GridLayout(2, false);\r
+ labelLayout.marginWidth = 0;\r
+ labelLayout.marginHeight = 0;\r
+ labelComposite.setLayout(labelLayout);\r
+ labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
+ \r
+ Button deleteButton = new Button(labelComposite, SWT.PUSH);\r
+ deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
+ deleteButton.setImage(deleteImage);\r
+ deleteButton.setToolTipText("Remove attribute");\r
+ deleteButton.addSelectionListener(new SelectionAdapter() {\r
+ public void widgetSelected(SelectionEvent e) {\r
+ Attribute.this.element.removeAttribute(Attribute.this.attributeNumber);\r
+ validate();\r
+ updatePreviews();\r
+ }\r
+ });\r
+ \r
+ attributeLabel = new Label(labelComposite, SWT.NULL);\r
+ attributeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
+ attributeLabel.setText("Attribute");\r
+ \r
+ attributeComposite = new Composite(parent, SWT.FILL);\r
+ GridLayout attributeLayout = new GridLayout(4, false);\r
+ attributeLayout.marginWidth = 0;\r
+ attributeLayout.marginHeight = 0;\r
+ attributeComposite.setLayout(attributeLayout);\r
+ attributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+\r
+ Label nameLabel = new Label(attributeComposite, SWT.NONE);\r
+ nameLabel.setText("name:");\r
+ \r
+ attributeNameText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE);\r
+ attributeNameText.setLayoutData(new GridData(120, SWT.DEFAULT));\r
+ attributeNameText.setText(inputAttribute.attributeName);\r
+ attributeNameText.addModifyListener(updateListener);\r
+ \r
+ Label previewLabel = new Label(attributeComposite, SWT.NONE);\r
+ previewLabel.setText("Preview:");\r
+ \r
+ previewText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);\r
+ GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);\r
+ gd.widthHint = 0;\r
+ previewText.setLayoutData(gd);\r
+ previewText.setText("*no match*");\r
+ previewText.setBackground(COLOR_WIDGET_BACKGROUND);\r
+\r
+ filler = new Label(parent, SWT.NULL);\r
+ \r
+ tagComposite = new Composite(parent, SWT.FILL);\r
+ GridLayout tagLayout = new GridLayout(4, false);\r
+ tagLayout.marginWidth = 0;\r
+ tagLayout.marginHeight = 0;\r
+ tagComposite.setLayout(tagLayout);\r
+ tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
+ \r
+ tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);\r
+ tagCombo.setItems(new String[] {CustomXmlTraceDefinition.TAG_TIMESTAMP,\r
+ CustomXmlTraceDefinition.TAG_MESSAGE,\r
+ CustomXmlTraceDefinition.TAG_OTHER});\r
+ tagCombo.select(2); //Other\r
+ tagCombo.addSelectionListener(new SelectionListener(){\r
+ public void widgetDefaultSelected(SelectionEvent e) {}\r
+ public void widgetSelected(SelectionEvent e) {\r
+ tagText.removeModifyListener(updateListener);\r
+ switch (tagCombo.getSelectionIndex()) {\r
+ case 0: //Time Stamp\r
+ tagLabel.setText("format:");\r
+ tagLabel.setVisible(true);\r
+ tagText.setVisible(true);\r
+ tagText.addModifyListener(updateListener);\r
+ break;\r
+ case 1: //Message\r
+ tagLabel.setVisible(false);\r
+ tagText.setVisible(false);\r
+ break;\r
+ case 2: //Other\r
+ tagLabel.setText("tag name:");\r
+ tagLabel.setVisible(true);\r
+ if (tagText.getText().trim().length() == 0) {\r
+ tagText.setText(attributeNameText.getText().trim());\r
+ }\r
+ tagText.setVisible(true);\r
+ tagText.addModifyListener(updateListener);\r
+ break;\r
+ }\r
+ tagComposite.layout();\r
+ validate();\r
+ updatePreviews();\r
+ }});\r
+ \r
+ tagLabel = new Label(tagComposite, SWT.NULL);\r
+ tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
+ \r
+ tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);\r
+ gd = new GridData(SWT.FILL, SWT.CENTER, true, false);\r
+ gd.widthHint = 0;\r
+ tagText.setLayoutData(gd);\r
+ tagText.setText(attributeNameText.getText());\r
+ \r
+ actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);\r
+ actionCombo.setItems(new String[] {"Set", "Append", "Append with |"});\r
+ actionCombo.select(inputAttribute.inputAction);\r
+ actionCombo.addSelectionListener(updateListener);\r
+ \r
+ if (inputAttribute.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {\r
+ tagCombo.select(0);\r
+ tagLabel.setText("format:");\r
+ tagText.setText(inputAttribute.inputFormat);\r
+ tagText.addModifyListener(updateListener);\r
+ } else if (inputAttribute.inputName.equals(CustomXmlTraceDefinition.TAG_MESSAGE)) {\r
+ tagCombo.select(1);\r
+ tagLabel.setVisible(false);\r
+ tagText.setVisible(false);\r
+ } else {\r
+ tagCombo.select(2);\r
+ tagLabel.setText("tag name:");\r
+ tagText.setText(inputAttribute.inputName);\r
+ tagText.addModifyListener(updateListener);\r
+ }\r
+ }\r
+ \r
+ private void dispose() {\r
+ labelComposite.dispose();\r
+ attributeComposite.dispose();\r
+ filler.dispose();\r
+ tagComposite.dispose();\r
+ }\r
+ \r
+ private void setAttributeNumber(int attributeNumber) {\r
+ this.attributeNumber = attributeNumber;\r
+ labelComposite.layout();\r
+ }\r
+ }\r
+\r
+ private Element getPreviewElement(InputElement inputElement) {\r
+ Element element = documentElement;\r
+ if (element != null) {\r
+ if (!documentElement.getNodeName().equals(definition.rootInputElement.elementName)) {\r
+ return null;\r
+ }\r
+ ArrayList<String> elementNames = new ArrayList<String>();\r
+ while (inputElement != null) {\r
+ elementNames.add(inputElement.elementName);\r
+ inputElement = inputElement.parentElement;\r
+ }\r
+ for (int i = elementNames.size() - 1; --i >= 0;) {\r
+ NodeList childList = element.getChildNodes();\r
+ element = null;\r
+ for (int j = 0; j < childList.getLength(); j++) {\r
+ Node child = childList.item(j);\r
+ if (child instanceof Element && child.getNodeName().equals(elementNames.get(i))) {\r
+ element = (Element)child;\r
+ break;\r
+ }\r
+ }\r
+ if (element == null) {\r
+ break;\r
+ }\r
+ }\r
+ if (element != null) {\r
+ return element;\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ private String getChildNameSuggestion(InputElement inputElement) {\r
+ if (inputElement == null) {\r
+ if (documentElement != null) {\r
+ return documentElement.getNodeName();\r
+ }\r
+ } else {\r
+ Element element = getPreviewElement(inputElement);\r
+ if (element != null) {\r
+ NodeList childNodes = element.getChildNodes();\r
+ for (int i = 0; i < childNodes.getLength(); i++) {\r
+ Node node = childNodes.item(i);\r
+ if (node instanceof Element) {\r
+ boolean unused = true;\r
+ if (inputElement.childElements != null) {\r
+ for (InputElement child : inputElement.childElements) {\r
+ if (child.elementName.equals(node.getNodeName())) {\r
+ unused = false;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ if (unused) {\r
+ return node.getNodeName();\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return ("");\r
+ }\r
+ \r
+ private String getAttributeNameSuggestion(InputElement inputElement) {\r
+ Element element = getPreviewElement(inputElement);\r
+ if (element != null) {\r
+ NamedNodeMap attributeMap = element.getAttributes();\r
+ for (int i = 0; i < attributeMap.getLength(); i++) {\r
+ Node node = attributeMap.item(i);\r
+ boolean unused = true;\r
+ if (inputElement.attributes != null) {\r
+ for (InputAttribute attribute : inputElement.attributes) {\r
+ if (attribute.attributeName.equals(node.getNodeName())) {\r
+ unused = false;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ if (unused) {\r
+ return node.getNodeName();\r
+ }\r
+ }\r
+ }\r
+ return ("");\r
+ }\r
+\r
+ private void validate() {\r
+ definition.definitionName = logtypeText.getText().trim();\r
+ definition.timeStampOutputFormat = timeStampOutputFormatText.getText().trim();\r
+ \r
+ if (selectedElement != null) {\r
+ selectedElement.extractInputs();\r
+ treeViewer.refresh();\r
+ }\r
+ \r
+ StringBuffer errors = new StringBuffer();\r
+\r
+ if (definition.definitionName.length() == 0) {\r
+ errors.append("Enter a name for the new log type. ");\r
+ logtypeText.setBackground(COLOR_LIGHT_RED);\r
+ } else {\r
+ logtypeText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {\r
+ if (definition.definitionName.equals(def.definitionName)) {\r
+ if (editDefinitionName == null || ! editDefinitionName.equals(definition.definitionName)) {\r
+ errors.append("The log type name already exists. ");\r
+ logtypeText.setBackground(COLOR_LIGHT_RED);\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ if (definition.rootInputElement == null) {\r
+ errors.append("Add a document element. ");\r
+ }\r
+ \r
+ if (definition.rootInputElement != null) {\r
+ logEntryFound = false;\r
+ logEntryNestedCount = 0;\r
+ timeStampFound = false;\r
+ \r
+ errors.append(validateElement(definition.rootInputElement));\r
+\r
+ if ((definition.rootInputElement.attributes != null && definition.rootInputElement.attributes.size() != 0) ||\r
+ (definition.rootInputElement.childElements != null && definition.rootInputElement.childElements.size() != 0) || errors.length() == 0) {\r
+ if (!logEntryFound) {\r
+ errors.append("Identify a Log Entry element. ");\r
+ }\r
+\r
+ if (timeStampFound) {\r
+ if (timeStampOutputFormatText.getText().trim().length() == 0) {\r
+ errors.append("Enter the output format for the Time Stamp field. ");\r
+ timeStampOutputFormatText.setBackground(COLOR_LIGHT_RED);\r
+ } else {\r
+ try {\r
+ new SimpleDateFormat(timeStampOutputFormatText.getText().trim());\r
+ timeStampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ } catch (IllegalArgumentException e) {\r
+ errors.append("Enter a valid output format for the Time Stamp field. ");\r
+ timeStampOutputFormatText.setBackground(COLOR_LIGHT_RED);\r
+ }\r
+ }\r
+ } else {\r
+ timeStampPreviewText.setText("*no time stamp element or attribute*");\r
+ }\r
+ }\r
+ } else {\r
+ timeStampPreviewText.setText("*no time stamp element or attribute*");\r
+ }\r
+ \r
+ if (errors.length() == 0) {\r
+ setDescription(defaultDescription);\r
+ setPageComplete(true);\r
+ } else {\r
+ setDescription(errors.toString());\r
+ setPageComplete(false);\r
+ }\r
+ }\r
+ \r
+ public StringBuffer validateElement(InputElement inputElement) {\r
+ StringBuffer errors = new StringBuffer();\r
+ ElementNode elementNode = null;\r
+ if (selectedElement != null && selectedElement.inputElement.equals(inputElement)) elementNode = selectedElement;\r
+ if (inputElement == definition.rootInputElement) {\r
+ if (inputElement.elementName.length() == 0) {\r
+ errors.append("Enter a name for the document element. ");\r
+ if (elementNode != null) elementNode.elementNameText.setBackground(COLOR_LIGHT_RED);\r
+ } else {\r
+ if (elementNode != null) elementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ }\r
+ }\r
+ if (inputElement != definition.rootInputElement) {\r
+ if (inputElement.logEntry) {\r
+ logEntryFound = true;\r
+ logEntryNestedCount++;\r
+ }\r
+ if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {\r
+ timeStampFound = true;\r
+ if (inputElement.inputFormat.length() == 0) {\r
+ errors.append("Enter the input format for the Time Stamp (Element " + getName(inputElement) + "). ");\r
+ if (elementNode != null) elementNode.tagText.setBackground(COLOR_LIGHT_RED);\r
+ } else {\r
+ try {\r
+ new SimpleDateFormat(inputElement.inputFormat);\r
+ if (elementNode != null) elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ } catch (IllegalArgumentException e) {\r
+ errors.append("Enter a valid input format for the Time Stamp (Element " + getName(inputElement) + "). ");\r
+ if (elementNode != null) elementNode.tagText.setBackground(COLOR_LIGHT_RED);\r
+ }\r
+ }\r
+ } else if (inputElement.inputName.length() == 0) {\r
+ errors.append("Enter a name for the input . ");\r
+ if (elementNode != null) elementNode.tagText.setBackground(COLOR_LIGHT_RED);\r
+ } else {\r
+ if (elementNode != null) elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ }\r
+ }\r
+ if (inputElement.attributes != null) {\r
+ if (elementNode != null) {\r
+ for (Attribute attribute : elementNode.attributes) {\r
+ attribute.attributeNameText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ }\r
+ }\r
+ for (int i = 0; i < inputElement.attributes.size(); i++) {\r
+ InputAttribute attribute = inputElement.attributes.get(i);\r
+ boolean duplicate = false;\r
+ for (int j = i + 1; j < inputElement.attributes.size(); j++) {\r
+ InputAttribute otherAttribute = inputElement.attributes.get(j);\r
+ if (otherAttribute.attributeName.equals(attribute.attributeName)) {\r
+ duplicate = true;\r
+ if (elementNode != null) {\r
+ elementNode.attributes.get(j).attributeNameText.setBackground(COLOR_LIGHT_RED);\r
+ }\r
+ }\r
+ }\r
+ if (attribute.attributeName.length() == 0) {\r
+ errors.append("Enter a name for the attribute (Attribute " + getName(inputElement) + ": ?). ");\r
+ if (elementNode != null) elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED);\r
+ } else if (duplicate) {\r
+ errors.append("Duplicate attribute names (Attribute " + getName(attribute, inputElement) +"). ");\r
+ if (elementNode != null) elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED);\r
+ }\r
+ if (attribute.inputName.equals(CustomXmlTraceDefinition.TAG_TIMESTAMP)) {\r
+ timeStampFound = true;\r
+ if (attribute.inputFormat.length() == 0) {\r
+ errors.append("Enter the input format for the Time Stamp (Attribute " + getName(attribute, inputElement) +"). ");\r
+ if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);\r
+ } else {\r
+ try {\r
+ new SimpleDateFormat(attribute.inputFormat);\r
+ if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ } catch (IllegalArgumentException e) {\r
+ errors.append("Enter a valid input format for the Time Stamp (Attribute " + getName(attribute, inputElement) +"). ");\r
+ if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);\r
+ }\r
+ }\r
+ } else if (attribute.inputName.length() == 0) {\r
+ errors.append("Enter a name for the data group (Attribute " + getName(attribute, inputElement) +"). ");\r
+ if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED);\r
+ } else {\r
+ if (elementNode != null) elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ }\r
+ }\r
+ }\r
+ if (inputElement.childElements != null) {\r
+ for (InputElement child : inputElement.childElements) {\r
+ ElementNode childElementNode = null;\r
+ if (selectedElement != null && selectedElement.inputElement.equals(child)) childElementNode = selectedElement;\r
+ if (childElementNode != null) childElementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND);\r
+ }\r
+ for (int i = 0; i < inputElement.childElements.size(); i++) {\r
+ InputElement child = inputElement.childElements.get(i);\r
+ ElementNode childElementNode = null;\r
+ if (selectedElement != null && selectedElement.inputElement.equals(child)) childElementNode = selectedElement;\r
+ if (child.elementName.length() == 0) {\r
+ errors.append("Enter a name for the element (Element " + getName(child) + "). ");\r
+ if (childElementNode != null) childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);\r
+ } else {\r
+ boolean duplicate = false;\r
+ for (int j = i + 1; j < inputElement.childElements.size(); j++) {\r
+ InputElement otherChild = inputElement.childElements.get(j);\r
+ if (otherChild.elementName.equals(child.elementName)) {\r
+ duplicate = true;\r
+ ElementNode otherChildElementNode = null;\r
+ if (selectedElement != null && selectedElement.inputElement.equals(otherChild)) otherChildElementNode = selectedElement;\r
+ if (otherChildElementNode != null) otherChildElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);\r
+ }\r
+ }\r
+ if (duplicate) {\r
+ errors.append("Duplicate element names (Element " + getName(child) + "). ");\r
+ if (childElementNode != null) childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED);\r
+ }\r
+ }\r
+ \r
+ errors.append(validateElement(child));\r
+ }\r
+ }\r
+ if (inputElement.logEntry) {\r
+ logEntryNestedCount--;\r
+ }\r
+ return errors;\r
+ }\r
+ \r
+ public CustomXmlTraceDefinition getDefinition() {\r
+ return definition;\r
+ }\r
+\r
+ public char[] getInputText() {\r
+ return inputText.getText().toCharArray();\r
+ }\r
+}\r