| 1 | /*******************************************************************************\r |
| 2 | * Copyright (c) 2010 Ericsson\r |
| 3 | * \r |
| 4 | * All rights reserved. This program and the accompanying materials are\r |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which\r |
| 6 | * accompanies this distribution, and is available at\r |
| 7 | * http://www.eclipse.org/legal/epl-v10.html\r |
| 8 | * \r |
| 9 | * Contributors:\r |
| 10 | * Patrick Tasse - Initial API and implementation\r |
| 11 | *******************************************************************************/\r |
| 12 | \r |
| 13 | package org.eclipse.linuxtools.tmf.ui.parsers;\r |
| 14 | \r |
| 15 | import java.util.ArrayList;\r |
| 16 | import java.util.LinkedHashMap;\r |
| 17 | import java.util.List;\r |
| 18 | import java.util.Map;\r |
| 19 | \r |
| 20 | import org.eclipse.core.resources.IResource;\r |
| 21 | import org.eclipse.core.runtime.CoreException;\r |
| 22 | import org.eclipse.core.runtime.IConfigurationElement;\r |
| 23 | import org.eclipse.core.runtime.IExtensionRegistry;\r |
| 24 | import org.eclipse.core.runtime.Platform;\r |
| 25 | import org.eclipse.core.runtime.QualifiedName;\r |
| 26 | import org.eclipse.core.runtime.content.IContentType;\r |
| 27 | import org.eclipse.linuxtools.tmf.trace.ITmfTrace;\r |
| 28 | import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;\r |
| 29 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;\r |
| 30 | import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsTable;\r |
| 31 | import org.eclipse.swt.widgets.Composite;\r |
| 32 | \r |
| 33 | public class ParserProviderManager {\r |
| 34 | \r |
| 35 | public static final QualifiedName PARSER_PROPERTY = new QualifiedName(TmfUiPlugin.PLUGIN_ID, "PARSER"); //$NON-NLS-1$\r |
| 36 | \r |
| 37 | private static List<IParserProvider> fParserProviders = new ArrayList<IParserProvider>();\r |
| 38 | \r |
| 39 | public static void init() {\r |
| 40 | IExtensionRegistry reg = Platform.getExtensionRegistry();\r |
| 41 | IConfigurationElement[] extensions = reg.getConfigurationElementsFor("org.eclipse.linuxtools.tmf.ui.parserProviders"); //$NON-NLS-1$\r |
| 42 | for (int i = 0; i < extensions.length; i++) {\r |
| 43 | IConfigurationElement element = extensions[i];\r |
| 44 | try {\r |
| 45 | IParserProvider parserProvider = (IParserProvider) element.createExecutableExtension("class"); //$NON-NLS-1$\r |
| 46 | addParserProvider(parserProvider);\r |
| 47 | } catch (CoreException e) {\r |
| 48 | e.printStackTrace();\r |
| 49 | }\r |
| 50 | }\r |
| 51 | }\r |
| 52 | \r |
| 53 | public static void addParserProvider(IParserProvider parserProvider) {\r |
| 54 | fParserProviders.add(parserProvider);\r |
| 55 | }\r |
| 56 | \r |
| 57 | public static void removeParserProvider(IParserProvider parserProvider) {\r |
| 58 | fParserProviders.remove(parserProvider);\r |
| 59 | }\r |
| 60 | \r |
| 61 | public static ITmfTrace getTrace(IResource resource) {\r |
| 62 | if (resource == null) {\r |
| 63 | return null;\r |
| 64 | }\r |
| 65 | try {\r |
| 66 | String parser = resource.getPersistentProperty(PARSER_PROPERTY);\r |
| 67 | if (parser != null) {\r |
| 68 | for (IParserProvider parserProvider : fParserProviders) {\r |
| 69 | if (parserProvider != null) {\r |
| 70 | ITmfTrace trace = parserProvider.getTraceForParser(parser, resource);\r |
| 71 | if (trace != null) {\r |
| 72 | return trace;\r |
| 73 | }\r |
| 74 | }\r |
| 75 | }\r |
| 76 | }\r |
| 77 | IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(resource.getName());\r |
| 78 | if (contentType != null) {\r |
| 79 | for (IParserProvider parserProvider : fParserProviders) {\r |
| 80 | if (parserProvider != null) {\r |
| 81 | ITmfTrace trace = parserProvider.getTraceForContentType(contentType.getId(), resource);\r |
| 82 | if (trace != null) {\r |
| 83 | resource.setPersistentProperty(PARSER_PROPERTY, trace.getClass().getCanonicalName());\r |
| 84 | return trace;\r |
| 85 | }\r |
| 86 | }\r |
| 87 | }\r |
| 88 | }\r |
| 89 | } catch (CoreException e) {\r |
| 90 | e.printStackTrace();\r |
| 91 | }\r |
| 92 | return null;\r |
| 93 | }\r |
| 94 | \r |
| 95 | public static String getEditorId(IResource resource) {\r |
| 96 | if (resource == null) {\r |
| 97 | return null;\r |
| 98 | }\r |
| 99 | try {\r |
| 100 | String parser = resource.getPersistentProperty(PARSER_PROPERTY);\r |
| 101 | if (parser != null) {\r |
| 102 | for (IParserProvider parserProvider : fParserProviders) {\r |
| 103 | if (parserProvider != null) {\r |
| 104 | String editorId = parserProvider.getEditorIdForParser(parser);\r |
| 105 | if (editorId != null) {\r |
| 106 | return editorId;\r |
| 107 | }\r |
| 108 | }\r |
| 109 | }\r |
| 110 | }\r |
| 111 | return TmfEventsEditor.ID;\r |
| 112 | } catch (CoreException e) {\r |
| 113 | e.printStackTrace();\r |
| 114 | }\r |
| 115 | return null;\r |
| 116 | }\r |
| 117 | \r |
| 118 | public static Map<String, Map<String, String>> getParserMap() {\r |
| 119 | Map<String, Map<String, String>> parserMap = new LinkedHashMap<String, Map<String, String>>();\r |
| 120 | for (IParserProvider parserProvider : fParserProviders) {\r |
| 121 | parserMap.put(parserProvider.getCategory(), parserProvider.getParserMap());\r |
| 122 | }\r |
| 123 | return parserMap;\r |
| 124 | }\r |
| 125 | \r |
| 126 | public static Map<String, String> getEventTypeMapForParser(String parser) {\r |
| 127 | for (IParserProvider parserProvider : fParserProviders) {\r |
| 128 | Map<String, String> map = parserProvider.getEventTypeMapForParser(parser);\r |
| 129 | if (map != null) {\r |
| 130 | return map;\r |
| 131 | }\r |
| 132 | }\r |
| 133 | return new LinkedHashMap<String, String>(0);\r |
| 134 | }\r |
| 135 | \r |
| 136 | public static String[] getFieldLabelsForEventType(String eventType) {\r |
| 137 | for (IParserProvider parserProvider : fParserProviders) {\r |
| 138 | String[] fieldLabels = parserProvider.getFieldLabelsForEventType(eventType);\r |
| 139 | if (fieldLabels != null) {\r |
| 140 | return fieldLabels;\r |
| 141 | }\r |
| 142 | }\r |
| 143 | return new String[0];\r |
| 144 | }\r |
| 145 | \r |
| 146 | public static TmfEventsTable getEventsTable(ITmfTrace trace, Composite parent, int cacheSize) {\r |
| 147 | for (IParserProvider parserProvider : fParserProviders) {\r |
| 148 | if (parserProvider != null) {\r |
| 149 | TmfEventsTable eventsTable = parserProvider.getEventsTable(trace, parent, cacheSize);\r |
| 150 | if (eventsTable != null) {\r |
| 151 | return eventsTable;\r |
| 152 | }\r |
| 153 | }\r |
| 154 | }\r |
| 155 | return null;\r |
| 156 | }\r |
| 157 | \r |
| 158 | }\r |