1 11 package org.eclipse.core.internal.content; 12 13 import java.util.*; 14 import org.eclipse.core.internal.runtime.RuntimeLog; 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.core.runtime.content.IContentDescription; 17 import org.eclipse.core.runtime.content.IContentType; 18 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 19 import org.eclipse.core.runtime.preferences.IPreferenceNodeVisitor; 20 import org.eclipse.osgi.util.NLS; 21 import org.osgi.service.prefs.BackingStoreException; 22 23 28 public class ContentTypeBuilder { 29 public static final String PT_CONTENTTYPES = "contentTypes"; private ContentTypeCatalog catalog; 31 32 private static String getUniqueId(String namespace, String baseTypeId) { 33 if (baseTypeId == null) 34 return null; 35 int separatorPosition = baseTypeId.lastIndexOf('.'); 36 if (separatorPosition == -1) 38 baseTypeId = namespace + '.' + baseTypeId; 39 return baseTypeId; 40 } 41 42 private static QualifiedName parseQualifiedName(String namespace, String value) { 43 if (value == null) 44 return null; 45 int separatorPosition = value.lastIndexOf('.'); 46 if (separatorPosition == -1) 48 return new QualifiedName(namespace, value); 49 if (separatorPosition == 0 || separatorPosition == value.length() - 1) 50 return null; 52 namespace = value.substring(0, separatorPosition); 53 String simpleValue = value.substring(separatorPosition + 1); 54 return new QualifiedName(namespace, simpleValue); 55 } 56 57 private static byte parsePriority(String priority) { 58 if (priority == null) 59 return ContentType.PRIORITY_NORMAL; 60 if (priority.equals("high")) return ContentType.PRIORITY_HIGH; 62 if (priority.equals("low")) return ContentType.PRIORITY_LOW; 64 if (!priority.equals("normal")) return ContentType.PRIORITY_NORMAL; 66 return ContentType.PRIORITY_NORMAL; 68 } 69 70 protected ContentTypeBuilder(ContentTypeCatalog catalog) { 71 this.catalog = catalog; 72 } 73 74 private void addFileAssociation(IConfigurationElement fileAssociationElement, ContentType target) { 75 String [] fileNames = Util.parseItems(fileAssociationElement.getAttributeAsIs("file-names")); for (int i = 0; i < fileNames.length; i++) 77 target.internalAddFileSpec(fileNames[i], IContentType.FILE_NAME_SPEC | ContentType.SPEC_PRE_DEFINED); 78 String [] fileExtensions = Util.parseItems(fileAssociationElement.getAttributeAsIs("file-extensions")); for (int i = 0; i < fileExtensions.length; i++) 80 target.internalAddFileSpec(fileExtensions[i], IContentType.FILE_EXTENSION_SPEC | ContentType.SPEC_PRE_DEFINED); 81 } 82 83 86 public void buildCatalog() { 87 IConfigurationElement[] allContentTypeCEs = getConfigurationElements(); 88 for (int i = 0; i < allContentTypeCEs.length; i++) 89 if (allContentTypeCEs[i].getName().equals("content-type")) registerContentType(allContentTypeCEs[i]); 91 for (int i = 0; i < allContentTypeCEs.length; i++) 92 if (allContentTypeCEs[i].getName().equals("file-association")) registerFileAssociation(allContentTypeCEs[i]); 94 applyPreferences(); 95 } 96 97 100 private void applyPreferences() { 101 try { 102 final ContentTypeCatalog localCatalog = catalog; 103 final IEclipsePreferences root = localCatalog.getManager().getPreferences(); 104 root.accept(new IPreferenceNodeVisitor() { 105 public boolean visit(IEclipsePreferences node) { 106 if (node == root) 107 return true; 108 ContentType contentType = localCatalog.internalGetContentType(node.name()); 109 if (contentType != null) 110 contentType.processPreferences(node); 111 return false; 113 } 114 }); 115 } catch (BackingStoreException bse) { 116 ContentType.log(ContentMessages.content_errorLoadingSettings, bse); 117 } 118 } 119 120 123 private ContentType createContentType(IConfigurationElement contentTypeCE) throws CoreException { 124 String namespace = contentTypeCE.getContributor().getName(); 125 String simpleId = contentTypeCE.getAttributeAsIs("id"); String name = contentTypeCE.getAttribute("name"); 128 if (simpleId == null) 129 missingMandatoryAttribute(ContentMessages.content_missingIdentifier, namespace); 130 String uniqueId; 131 if (simpleId.lastIndexOf('.') == -1) 132 uniqueId = namespace + '.' + simpleId; 133 else 134 uniqueId = simpleId; 135 if (name == null) 136 missingMandatoryAttribute(ContentMessages.content_missingName, uniqueId); 137 138 byte priority = parsePriority(contentTypeCE.getAttributeAsIs("priority")); String [] fileNames = Util.parseItems(contentTypeCE.getAttributeAsIs("file-names")); String [] fileExtensions = Util.parseItems(contentTypeCE.getAttributeAsIs("file-extensions")); String baseTypeId = getUniqueId(namespace, contentTypeCE.getAttributeAsIs("base-type")); String aliasTargetTypeId = getUniqueId(namespace, contentTypeCE.getAttributeAsIs("alias-for")); IConfigurationElement[] propertyCEs = null; 144 Map defaultProperties = null; 145 if ((propertyCEs = contentTypeCE.getChildren("property")).length > 0) { defaultProperties = new HashMap(); 147 for (int i = 0; i < propertyCEs.length; i++) { 148 String defaultValue = propertyCEs[i].getAttributeAsIs("default"); if (defaultValue == null) 150 defaultValue = ""; String propertyKey = propertyCEs[i].getAttributeAsIs("name"); QualifiedName qualifiedKey = parseQualifiedName(namespace, propertyKey); 154 if (qualifiedKey == null) { 155 if (ContentTypeManager.DEBUGGING) { 156 String message = NLS.bind(ContentMessages.content_invalidProperty, propertyKey, getUniqueId(namespace, simpleId)); 157 ContentType.log(message, null); 158 } 159 continue; 160 } 161 defaultProperties.put(qualifiedKey, defaultValue); 162 } 163 } 164 String defaultCharset = contentTypeCE.getAttributeAsIs("default-charset"); if (defaultCharset != null) 166 if (defaultProperties == null) 167 defaultProperties = Collections.singletonMap(IContentDescription.CHARSET, defaultCharset); 168 else if (!defaultProperties.containsKey(IContentDescription.CHARSET)) 169 defaultProperties.put(IContentDescription.CHARSET, defaultCharset); 170 return ContentType.createContentType(catalog, uniqueId, name, priority, fileExtensions, fileNames, baseTypeId, aliasTargetTypeId, defaultProperties, contentTypeCE); 171 } 172 173 private final static IConfigurationElement[] emptyConfArray = new IConfigurationElement[0]; 175 176 182 protected IConfigurationElement[] getConfigurationElements() { 183 IExtensionRegistry registry = RegistryFactory.getRegistry(); 184 if (registry == null) 185 return emptyConfArray; 186 IConfigurationElement[] oldConfigElements = emptyConfArray; 187 IConfigurationElement[] newConfigElements = emptyConfArray; 188 IExtensionPoint oldPoint = registry.getExtensionPoint(IContentConstants.RUNTIME_NAME, PT_CONTENTTYPES); 190 if (oldPoint != null) 191 oldConfigElements = oldPoint.getConfigurationElements(); 192 IExtensionPoint newPoint = registry.getExtensionPoint(IContentConstants.CONTENT_NAME, PT_CONTENTTYPES); 194 if (newPoint != null) 195 newConfigElements = newPoint.getConfigurationElements(); 196 197 IConfigurationElement[] allContentTypeCEs = new IConfigurationElement[oldConfigElements.length + newConfigElements.length]; 198 System.arraycopy(oldConfigElements, 0, allContentTypeCEs, 0, oldConfigElements.length); 199 System.arraycopy(newConfigElements, 0, allContentTypeCEs, oldConfigElements.length, newConfigElements.length); 200 201 return allContentTypeCEs; 202 } 203 204 private void missingMandatoryAttribute(String messageKey, String argument) throws CoreException { 205 String message = NLS.bind(messageKey, argument); 206 throw new CoreException(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, null)); 207 } 208 209 private void registerContentType(IConfigurationElement contentTypeCE) { 210 try { 211 ContentType contentType = createContentType(contentTypeCE); 212 catalog.addContentType(contentType); 213 } catch (CoreException e) { 214 RuntimeLog.log(e.getStatus()); 216 } 217 } 218 219 222 private void registerFileAssociation(IConfigurationElement fileAssociationElement) { 223 String contentTypeId = getUniqueId(fileAssociationElement.getContributor().getName(), fileAssociationElement.getAttribute("content-type")); ContentType target = catalog.internalGetContentType(contentTypeId); 226 if (target == null) 227 return; 228 addFileAssociation(fileAssociationElement, target); 229 } 230 } 231 | Popular Tags |