1 /******************************************************************************* 2 * Copyright (c) 2000, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.ui.internal.registry; 15 16 import java.util.ArrayList; 17 import java.util.List; 18 import java.util.StringTokenizer; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExtensionRegistry; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.content.IContentType; 23 import org.eclipse.ui.IEditorDescriptor; 24 import org.eclipse.ui.PlatformUI; 25 26 /** 27 * This class is used to read resource editor registry descriptors from the 28 * platform registry. 29 */ 30 public class EditorRegistryReader extends RegistryReader { 31 32 private EditorRegistry editorRegistry; 33 34 /** 35 * Get the editors that are defined in the registry and add them to the 36 * ResourceEditorRegistry 37 * 38 * Warning: The registry must be passed in because this method is called during 39 * the process of setting up the registry and at this time it has not been 40 * safely setup with the plugin. 41 */ addEditors(EditorRegistry registry)42 protected void addEditors(EditorRegistry registry) { 43 IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry(); 44 this.editorRegistry = registry; 45 readRegistry(extensionRegistry, PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_EDITOR); 46 } 47 48 /** 49 * Implementation of the abstract method that processes one configuration 50 * element. 51 */ 52 @Override readElement(IConfigurationElement element)53 protected boolean readElement(IConfigurationElement element) { 54 if (element.getName().equals(IWorkbenchRegistryConstants.TAG_EDITOR)) { 55 return readEditorElement(element); 56 } 57 if (element.getName().equals(IWorkbenchRegistryConstants.TAG_EDITOR_CONTENT_TYPTE_BINDING)) { 58 return readEditorContentTypeBinding(element); 59 } 60 return false; 61 } 62 63 /** 64 * @param element 65 * @return 66 */ readEditorContentTypeBinding(IConfigurationElement element)67 private boolean readEditorContentTypeBinding(IConfigurationElement element) { 68 IEditorDescriptor descriptor = null; 69 String editorId = element.getAttribute(IWorkbenchRegistryConstants.ATT_EDITOR_ID); 70 if (editorId == null) { 71 logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_EDITOR_ID); 72 } else { 73 descriptor = editorRegistry.findEditor(editorId); 74 if (descriptor == null) { 75 logError(element, "Unknown editor with id: " + editorId); //$NON-NLS-1$ 76 } 77 } 78 79 IContentType contentType = null; 80 String contentTypeId = element.getAttribute(IWorkbenchRegistryConstants.ATT_CONTENT_TYPE_ID); 81 if (contentTypeId == null) { 82 logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_CONTENT_TYPE_ID); 83 } else { 84 contentType = Platform.getContentTypeManager().getContentType(contentTypeId); 85 if (contentType == null) { 86 logError(element, "Unknown content-type with id: " + contentTypeId); //$NON-NLS-1$ 87 } 88 } 89 90 if (descriptor != null && contentType != null) { 91 editorRegistry.addContentTypeBindingFromPlugin(contentType, descriptor, false); 92 } 93 return true; 94 } 95 readEditorElement(IConfigurationElement element)96 private boolean readEditorElement(IConfigurationElement element) { 97 String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 98 if (id == null) { 99 logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_ID); 100 return true; 101 } 102 103 EditorDescriptor editor = new EditorDescriptor(id, element); 104 105 List<String> extensionsVector = new ArrayList<>(); 106 List<String> filenamesVector = new ArrayList<>(); 107 List<String> contentTypeVector = new ArrayList<>(); 108 boolean defaultEditor = false; 109 110 // Get editor name (required field). 111 if (element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null) { 112 logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_NAME); 113 return true; 114 } 115 116 // Get target extensions (optional field) 117 String extensionsString = element.getAttribute(IWorkbenchRegistryConstants.ATT_EXTENSIONS); 118 if (extensionsString != null) { 119 StringTokenizer tokenizer = new StringTokenizer(extensionsString, ",");//$NON-NLS-1$ 120 while (tokenizer.hasMoreTokens()) { 121 extensionsVector.add(tokenizer.nextToken().trim()); 122 } 123 } 124 String filenamesString = element.getAttribute(IWorkbenchRegistryConstants.ATT_FILENAMES); 125 if (filenamesString != null) { 126 StringTokenizer tokenizer = new StringTokenizer(filenamesString, ",");//$NON-NLS-1$ 127 while (tokenizer.hasMoreTokens()) { 128 filenamesVector.add(tokenizer.nextToken().trim()); 129 } 130 } 131 132 IConfigurationElement[] bindings = element.getChildren(IWorkbenchRegistryConstants.TAG_CONTENT_TYPE_BINDING); 133 for (IConfigurationElement binding : bindings) { 134 String contentTypeId = binding.getAttribute(IWorkbenchRegistryConstants.ATT_CONTENT_TYPE_ID); 135 if (contentTypeId == null) { 136 continue; 137 } 138 contentTypeVector.add(contentTypeId); 139 } 140 141 // Is this the default editor? 142 String def = element.getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULT); 143 if (def != null) { 144 defaultEditor = Boolean.parseBoolean(def); 145 } 146 147 // Add the editor to the manager. 148 editorRegistry.addEditorFromPlugin(editor, extensionsVector, filenamesVector, contentTypeVector, defaultEditor); 149 return true; 150 } 151 152 /** 153 * @param editorRegistry 154 * @param element 155 */ readElement(EditorRegistry editorRegistry, IConfigurationElement element)156 public void readElement(EditorRegistry editorRegistry, IConfigurationElement element) { 157 this.editorRegistry = editorRegistry; 158 readElement(element); 159 } 160 } 161